Skip to content

Commit 9eb7d93

Browse files
committed
add ProxyHandlerRemote and NewProxyRemote
1 parent 93fa23b commit 9eb7d93

File tree

2 files changed

+70
-14
lines changed

2 files changed

+70
-14
lines changed

core/host/proxy.go

+68-12
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,20 @@ import (
1212
)
1313

1414
// ProxyHandler returns a new ReverseProxy that rewrites
15-
// URLs to the scheme, host, and base path provided in target.
16-
// Case 1: req.Host == target.Host
17-
// If the target's path is "/base" and the incoming request was for "/dir",
15+
// URLs to the scheme, host, and base path provided in target. If the
16+
// target's path is "/base" and the incoming request was for "/dir",
1817
// the target request will be for /base/dir.
19-
// Case 2: req.Host != target.Host
20-
// the target request will be forwarded to the target's url
18+
//
2119
// Relative to httputil.NewSingleHostReverseProxy with some additions.
2220
func ProxyHandler(target *url.URL) *httputil.ReverseProxy {
2321
targetQuery := target.RawQuery
2422
director := func(req *http.Request) {
2523
req.URL.Scheme = target.Scheme
26-
27-
if req.Host != target.Host {
28-
req.URL.Path = target.Path
29-
} else {
30-
req.URL.Path = path.Join(target.Path, req.URL.Path)
31-
}
32-
3324
req.URL.Host = target.Host
3425
req.Host = target.Host
3526

27+
req.URL.Path = path.Join(target.Path, req.URL.Path)
28+
3629
if targetQuery == "" || req.URL.RawQuery == "" {
3730
req.URL.RawQuery = targetQuery + req.URL.RawQuery
3831
} else {
@@ -56,6 +49,51 @@ func ProxyHandler(target *url.URL) *httputil.ReverseProxy {
5649
return p
5750
}
5851

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+
5997
// NewProxy returns a new host (server supervisor) which
6098
// proxies all requests to the target.
6199
// It uses the httputil.NewSingleHostReverseProxy.
@@ -74,6 +112,24 @@ func NewProxy(hostAddr string, target *url.URL) *Supervisor {
74112
return proxy
75113
}
76114

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+
77133
// NewRedirection returns a new host (server supervisor) which
78134
// redirects all requests to the target.
79135
// Usage:

core/host/proxy_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@ func TestProxy(t *testing.T) {
6464

6565
e := httptest.NewInsecure(t, httptest.URL("http://"+listener.Addr().String()))
6666
e.GET("/").Expect().Status(iris.StatusOK).Body().Equal(expectedIndex)
67-
e.GET("/about").Expect().Status(iris.StatusOK).Body().Equal(expectedIndex)
68-
e.GET("/notfound").Expect().Status(iris.StatusOK).Body().Equal(expectedIndex)
67+
e.GET("/about").Expect().Status(iris.StatusOK).Body().Equal(expectedAbout)
68+
e.GET("/notfound").Expect().Status(iris.StatusNotFound).Body().Equal(unexpectedRoute)
6969
}

0 commit comments

Comments
 (0)