Skip to content

Commit e990b23

Browse files
authored
Merge pull request kataras#1703 from tuhao1020/master
update host.ProxyHandler to compatiable with different hosts in target url and request host
2 parents fe0f87d + 9eb7d93 commit e990b23

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

core/host/proxy.go

+63
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,51 @@ func ProxyHandler(target *url.URL) *httputil.ReverseProxy {
4949
return p
5050
}
5151

52+
// ProxyHandlerRemote returns a new ReverseProxy that rewrites
53+
// URLs to the scheme, host, and path provided in target.
54+
// Case 1: req.Host == target.Host
55+
// behavior same as ProxyHandler
56+
// Case 2: req.Host != target.Host
57+
// the target request will be forwarded to the target's url
58+
// insecureSkipVerify indicates enable ssl certificate verification or not
59+
func ProxyHandlerRemote(target *url.URL, insecureSkipVerify bool) *httputil.ReverseProxy {
60+
targetQuery := target.RawQuery
61+
director := func(req *http.Request) {
62+
req.URL.Scheme = target.Scheme
63+
64+
if req.Host != target.Host {
65+
req.URL.Path = target.Path
66+
} else {
67+
req.URL.Path = path.Join(target.Path, req.URL.Path)
68+
}
69+
70+
req.URL.Host = target.Host
71+
req.Host = target.Host
72+
73+
if targetQuery == "" || req.URL.RawQuery == "" {
74+
req.URL.RawQuery = targetQuery + req.URL.RawQuery
75+
} else {
76+
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
77+
}
78+
79+
if _, ok := req.Header["User-Agent"]; !ok {
80+
// explicitly disable User-Agent so it's not set to default value
81+
req.Header.Set("User-Agent", "")
82+
}
83+
}
84+
p := &httputil.ReverseProxy{Director: director}
85+
86+
if netutil.IsLoopbackHost(target.Host) {
87+
insecureSkipVerify = true
88+
}
89+
90+
transport := &http.Transport{
91+
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecureSkipVerify}, // lint:ignore
92+
}
93+
p.Transport = transport
94+
return p
95+
}
96+
5297
// NewProxy returns a new host (server supervisor) which
5398
// proxies all requests to the target.
5499
// It uses the httputil.NewSingleHostReverseProxy.
@@ -67,6 +112,24 @@ func NewProxy(hostAddr string, target *url.URL) *Supervisor {
67112
return proxy
68113
}
69114

115+
// NewProxyRemote returns a new host (server supervisor) which
116+
// proxies all requests to the target.
117+
// It uses the httputil.NewSingleHostReverseProxy.
118+
//
119+
// Usage:
120+
// target, _ := url.Parse("https://anotherdomain.com/abc")
121+
// proxy := NewProxyRemote("mydomain.com", target, false)
122+
// proxy.ListenAndServe() // use of `proxy.Shutdown` to close the proxy server.
123+
func NewProxyRemote(hostAddr string, target *url.URL, insecureSkipVerify bool) *Supervisor {
124+
proxyHandler := ProxyHandlerRemote(target, insecureSkipVerify)
125+
proxy := New(&http.Server{
126+
Addr: hostAddr,
127+
Handler: proxyHandler,
128+
})
129+
130+
return proxy
131+
}
132+
70133
// NewRedirection returns a new host (server supervisor) which
71134
// redirects all requests to the target.
72135
// Usage:

0 commit comments

Comments
 (0)