From 518ca2ceb275706a0b6a4a8b6954837fae587c0a Mon Sep 17 00:00:00 2001 From: fatedier Date: Mon, 19 Feb 2024 21:04:29 +0800 Subject: [PATCH] ReverseProxy: use Rewrite to replace Director (#4005) * display go version in make * ReverseProxy: use Rewrite to replace Director --- Makefile | 2 +- pkg/plugin/client/http2https.go | 3 ++- pkg/plugin/client/https2http.go | 3 ++- pkg/plugin/client/https2https.go | 3 ++- pkg/util/vhost/http.go | 4 +++- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index b9dfb085..603aeabd 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -export PATH := $(GOPATH)/bin:$(PATH) +export PATH := $(PATH):`go env GOPATH`/bin export GO111MODULE=on LDFLAGS := -s -w diff --git a/pkg/plugin/client/http2https.go b/pkg/plugin/client/http2https.go index ac54551a..7cd3cd42 100644 --- a/pkg/plugin/client/http2https.go +++ b/pkg/plugin/client/http2https.go @@ -53,7 +53,8 @@ func NewHTTP2HTTPSPlugin(options v1.ClientPluginOptions) (Plugin, error) { } rp := &httputil.ReverseProxy{ - Director: func(req *http.Request) { + Rewrite: func(r *httputil.ProxyRequest) { + req := r.Out req.URL.Scheme = "https" req.URL.Host = p.opts.LocalAddr if p.opts.HostHeaderRewrite != "" { diff --git a/pkg/plugin/client/https2http.go b/pkg/plugin/client/https2http.go index ba66bfae..389ac849 100644 --- a/pkg/plugin/client/https2http.go +++ b/pkg/plugin/client/https2http.go @@ -50,7 +50,8 @@ func NewHTTPS2HTTPPlugin(options v1.ClientPluginOptions) (Plugin, error) { } rp := &httputil.ReverseProxy{ - Director: func(req *http.Request) { + Rewrite: func(r *httputil.ProxyRequest) { + req := r.Out req.URL.Scheme = "http" req.URL.Host = p.opts.LocalAddr if p.opts.HostHeaderRewrite != "" { diff --git a/pkg/plugin/client/https2https.go b/pkg/plugin/client/https2https.go index a79ea3b1..da3302b8 100644 --- a/pkg/plugin/client/https2https.go +++ b/pkg/plugin/client/https2https.go @@ -55,7 +55,8 @@ func NewHTTPS2HTTPSPlugin(options v1.ClientPluginOptions) (Plugin, error) { } rp := &httputil.ReverseProxy{ - Director: func(req *http.Request) { + Rewrite: func(r *httputil.ProxyRequest) { + req := r.Out req.URL.Scheme = "https" req.URL.Host = p.opts.LocalAddr if p.opts.HostHeaderRewrite != "" { diff --git a/pkg/util/vhost/http.go b/pkg/util/vhost/http.go index 72ab4775..7aa67c98 100644 --- a/pkg/util/vhost/http.go +++ b/pkg/util/vhost/http.go @@ -58,7 +58,9 @@ func NewHTTPReverseProxy(option HTTPReverseProxyOptions, vhostRouter *Routers) * } proxy := &httputil.ReverseProxy{ // Modify incoming requests by route policies. - Director: func(req *http.Request) { + Rewrite: func(r *httputil.ProxyRequest) { + r.SetXForwarded() + req := r.Out req.URL.Scheme = "http" reqRouteInfo := req.Context().Value(RouteInfoKey).(*RequestRouteInfo) oldHost, _ := httppkg.CanonicalHost(reqRouteInfo.Host)