@@ -49,6 +49,51 @@ func ProxyHandler(target *url.URL) *httputil.ReverseProxy {
49
49
return p
50
50
}
51
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
+
52
97
// NewProxy returns a new host (server supervisor) which
53
98
// proxies all requests to the target.
54
99
// It uses the httputil.NewSingleHostReverseProxy.
@@ -67,6 +112,24 @@ func NewProxy(hostAddr string, target *url.URL) *Supervisor {
67
112
return proxy
68
113
}
69
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
+
70
133
// NewRedirection returns a new host (server supervisor) which
71
134
// redirects all requests to the target.
72
135
// Usage:
0 commit comments