From 03e718023603cc06b75a08faa1b5988d50551caa Mon Sep 17 00:00:00 2001 From: nsfisis Date: Tue, 2 Apr 2024 01:12:12 +0900 Subject: modify location header the origin server responds --- server.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/server.go b/server.go index 7c7c6a6..261b6bf 100644 --- a/server.go +++ b/server.go @@ -64,6 +64,33 @@ func newMultipleReverseProxyServer(ps []ProxyConfig) (*multipleReverseProxyServe r.SetURL(targetUrl) r.SetXForwarded() }, + ModifyResponse: func(r *http.Response) error { + if r.StatusCode < 300 || 400 <= r.StatusCode { + return nil + } + + // If the response is redirect and has location header, rewrite it. + location := r.Header.Get("Location") + if location == "" { + return nil + } + locationUrl, err := url.Parse(location) + if err != nil { + return nil + } + if !locationUrl.IsAbs() { + return nil + } + if locationUrl.Hostname() != targetUrl.Hostname() { + return nil + } + locationUrl.Host = p.From.Host + if locationUrl.Scheme == "http" { + locationUrl.Scheme = "https" + } + r.Header.Set("Location", locationUrl.String()) + return nil + }, } if p.BasicAuth != nil { credentialFileContent, err := os.ReadFile(p.BasicAuth.CredentialFile) -- cgit v1.2.3-70-g09d2