initial user-agent driven interstitial implementation (#715)

This commit is contained in:
Michael Quigley 2024-07-31 13:10:04 -04:00
parent ed09ab2c25
commit 1f08ca43ea
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 33 additions and 13 deletions

View File

@ -16,11 +16,16 @@ type Config struct {
Identity string Identity string
Address string Address string
HostMatch string HostMatch string
Interstitial bool Interstitial *InterstitialConfig
Oauth *OauthConfig Oauth *OauthConfig
Tls *endpoints.TlsConfig Tls *endpoints.TlsConfig
} }
type InterstitialConfig struct {
Enabled bool
UserAgentPrefixes []string
}
type OauthConfig struct { type OauthConfig struct {
BindAddress string BindAddress string
RedirectUrl string RedirectUrl string
@ -46,9 +51,8 @@ type OauthProviderConfig struct {
func DefaultConfig() *Config { func DefaultConfig() *Config {
return &Config{ return &Config{
Identity: "public", Identity: "public",
Address: "0.0.0.0:8080", Address: "0.0.0.0:8080",
Interstitial: false,
} }
} }

View File

@ -158,15 +158,31 @@ func shareHandler(handler http.Handler, pcfg *Config, key []byte, ctx ziti.Conte
if shrToken != "" { if shrToken != "" {
if svc, found := endpoints.GetRefreshedService(shrToken, ctx); found { if svc, found := endpoints.GetRefreshedService(shrToken, ctx); found {
if cfg, found := svc.Config[sdk.ZrokProxyConfig]; found { if cfg, found := svc.Config[sdk.ZrokProxyConfig]; found {
if pcfg.Interstitial { if pcfg.Interstitial != nil && pcfg.Interstitial.Enabled {
if v, istlFound := cfg["interstitial"]; istlFound { sendInterstitial := true
if istlEnabled, ok := v.(bool); ok && istlEnabled { if len(pcfg.Interstitial.UserAgentPrefixes) > 0 {
skip := r.Header.Get("skip_zrok_interstitial") ua := r.Header.Get("User-Agent")
_, zrokOkErr := r.Cookie("zrok_interstitial") matched := false
if skip == "" && zrokOkErr != nil { for _, prefix := range pcfg.Interstitial.UserAgentPrefixes {
logrus.Debugf("forcing interstitial for '%v'", r.URL) if strings.HasPrefix(ua, prefix) {
interstitialUi.WriteInterstitialAnnounce(w) matched = true
return break
}
}
if !matched {
sendInterstitial = false
}
}
if sendInterstitial {
if v, istlFound := cfg["interstitial"]; istlFound {
if istlEnabled, ok := v.(bool); ok && istlEnabled {
skip := r.Header.Get("skip_zrok_interstitial")
_, zrokOkErr := r.Cookie("zrok_interstitial")
if skip == "" && zrokOkErr != nil {
logrus.Debugf("forcing interstitial for '%v'", r.URL)
interstitialUi.WriteInterstitialAnnounce(w)
return
}
} }
} }
} }