@@ -12,27 +12,20 @@ import (
12
12
)
13
13
14
14
// 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",
18
17
// 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
+ //
21
19
// Relative to httputil.NewSingleHostReverseProxy with some additions.
22
20
func ProxyHandler (target * url.URL ) * httputil.ReverseProxy {
23
21
targetQuery := target .RawQuery
24
22
director := func (req * http.Request ) {
25
23
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
-
33
24
req .URL .Host = target .Host
34
25
req .Host = target .Host
35
26
27
+ req .URL .Path = path .Join (target .Path , req .URL .Path )
28
+
36
29
if targetQuery == "" || req .URL .RawQuery == "" {
37
30
req .URL .RawQuery = targetQuery + req .URL .RawQuery
38
31
} else {
@@ -56,6 +49,51 @@ func ProxyHandler(target *url.URL) *httputil.ReverseProxy {
56
49
return p
57
50
}
58
51
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
+
59
97
// NewProxy returns a new host (server supervisor) which
60
98
// proxies all requests to the target.
61
99
// It uses the httputil.NewSingleHostReverseProxy.
@@ -74,6 +112,24 @@ func NewProxy(hostAddr string, target *url.URL) *Supervisor {
74
112
return proxy
75
113
}
76
114
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
+
77
133
// NewRedirection returns a new host (server supervisor) which
78
134
// redirects all requests to the target.
79
135
// Usage:
0 commit comments