diff options
Diffstat (limited to 'workhorse/internal/proxy/proxy.go')
-rw-r--r-- | workhorse/internal/proxy/proxy.go | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/workhorse/internal/proxy/proxy.go b/workhorse/internal/proxy/proxy.go index be161c833a9..06e2c65a6a8 100644 --- a/workhorse/internal/proxy/proxy.go +++ b/workhorse/internal/proxy/proxy.go @@ -19,6 +19,7 @@ type Proxy struct { reverseProxy *httputil.ReverseProxy AllowResponseBuffering bool customHeaders map[string]string + forceTargetHostHeader bool } func WithCustomHeaders(customHeaders map[string]string) func(*Proxy) { @@ -27,6 +28,12 @@ func WithCustomHeaders(customHeaders map[string]string) func(*Proxy) { } } +func WithForcedTargetHostHeader() func(*Proxy) { + return func(proxy *Proxy) { + proxy.forceTargetHostHeader = true + } +} + func NewProxy(myURL *url.URL, version string, roundTripper http.RoundTripper, options ...func(*Proxy)) *Proxy { p := Proxy{Version: version, AllowResponseBuffering: true, customHeaders: make(map[string]string)} @@ -43,6 +50,25 @@ func NewProxy(myURL *url.URL, version string, roundTripper http.RoundTripper, op option(&p) } + if p.forceTargetHostHeader { + // because of https://github.com/golang/go/issues/28168, the + // upstream won't receive the expected Host header unless this + // is forced in the Director func here + previousDirector := p.reverseProxy.Director + p.reverseProxy.Director = func(request *http.Request) { + previousDirector(request) + + // send original host along for the upstream + // to know it's being proxied under a different Host + // (for redirects and other stuff that depends on this) + request.Header.Set("X-Forwarded-Host", request.Host) + request.Header.Set("Forwarded", fmt.Sprintf("host=%s", request.Host)) + + // override the Host with the target + request.Host = request.URL.Host + } + } + return &p } |