From 877b3214125c88ec1db8720be3b582e38da36d4b Mon Sep 17 00:00:00 2001 From: becivells <732903873@qq.com> Date: Mon, 11 Oct 2021 16:13:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20location=20=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E9=94=99=E8=AF=AF=E5=AF=BC=E8=87=B4=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=20302=20=E8=B7=B3=E8=BD=AC=E5=A4=B1=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/options/parse.go | 4 ++-- internal/reverse/response.go | 5 +++-- internal/reverse/reverse.go | 14 +++++++++----- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/internal/options/parse.go b/internal/options/parse.go index 26c8974..43ebde5 100644 --- a/internal/options/parse.go +++ b/internal/options/parse.go @@ -160,7 +160,7 @@ func ParseOptions() *Options { } //https://www.cnblogs.com/feiquan/p/11429065.html os.Chmod(options.Proxy.PluginDir, 0755) //nolint: - // plugin demo.yaml + // plugin pam.yaml demo := `Name: demo Version: 0.0.1 Description: this is a description @@ -178,7 +178,7 @@ Rule: Response: # 替换的响应内容 Body: Append: # 追加字符串` - pluginDemo := options.Proxy.PluginDir + "/" + "demo.yaml" + pluginDemo := options.Proxy.PluginDir + "/" + "pam.yaml" err = ioutil.WriteFile(pluginDemo, []byte(demo), 0755) //nolint: if err != nil { log.Fatal("%s", err.Error()) diff --git a/internal/reverse/response.go b/internal/reverse/response.go index e31241a..26ce516 100644 --- a/internal/reverse/response.go +++ b/internal/reverse/response.go @@ -169,10 +169,11 @@ func (reverse *Reverse) modifyLocationHeader(shost string, response *http.Respon log.Trace("Location: %s", location.String()) target := reverse.AllowSite[shost] targetHost, _ := url.Parse(target) - locationHost, err := utils.Parse(location.String()) + locationHost, err := url.Parse(location.String()) if err != nil { return err } + log.Trace("targetHost.Host:%s, locationHost.Host: %s", targetHost.Host, locationHost.Host) if targetHost.Host == locationHost.Host { location.Scheme = "" @@ -181,7 +182,7 @@ func (reverse *Reverse) modifyLocationHeader(shost string, response *http.Respon log.Trace("url: %s,Location: %s", response.Request.URL, location.String()) } if location.String() == "" { - log.Trace("url: %s,Location is empty", response.Request.URL) + log.Trace("url: %s, Location is empty", response.Request.URL) return nil } response.Header.Set("Location", location.String()) diff --git a/internal/reverse/reverse.go b/internal/reverse/reverse.go index 4a96332..c93c5e7 100644 --- a/internal/reverse/reverse.go +++ b/internal/reverse/reverse.go @@ -77,12 +77,16 @@ func (reverse *Reverse) ServeHTTP(w http.ResponseWriter, r *http.Request) { if utils.EleInArray(r.Method, rp.Request.Method) { log.Info("[plugin:%s.Replace.%s] Method match:%s", rules.Name, rule.URL, rp.Request.Method) //处理响应数据 - if rp.Response.Location != "" { - log.Info("[plugin: %s.Location]: %s", rules.Name, rp.Response.Location) - w.Header().Set("Location", rp.Response.Location) - w.WriteHeader(302) - return + + if rp.Response != nil { + if rp.Response.Location != "" { + log.Info("[plugin: %s.Location]: %s", rules.Name, rp.Response.Location) + w.Header().Set("Location", rp.Response.Location) + w.WriteHeader(302) + return + } } + } } }