Skip to content

Commit cf6a34a

Browse files
iQQBotroboquat
authored andcommitted
[ws-proxy] not use target host when serve workspace port route
1 parent c96adbe commit cf6a34a

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

components/ws-proxy/pkg/proxy/pass.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type proxyPassConfig struct {
2727
ResponseHandler []responseHandler
2828
ErrorHandler errorHandler
2929
Transport http.RoundTripper
30+
UseTargetHost bool
3031
}
3132

3233
func (ppc *proxyPassConfig) appendResponseHandler(handler responseHandler) {
@@ -77,12 +78,14 @@ func joinURLPath(a, b *url.URL) (path, rawpath string) {
7778
return a.Path + b.Path, apath + bpath
7879
}
7980

80-
func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
81+
func NewSingleHostReverseProxy(target *url.URL, useTargetHost bool) *httputil.ReverseProxy {
8182
targetQuery := target.RawQuery
8283
director := func(req *http.Request) {
8384
req.URL.Scheme = target.Scheme
8485
req.URL.Host = target.Host
85-
req.Host = target.Host
86+
if useTargetHost {
87+
req.Host = target.Host
88+
}
8689
req.URL.Path, req.URL.RawPath = joinURLPath(target, req.URL)
8790
if targetQuery == "" || req.URL.RawQuery == "" {
8891
req.URL.RawQuery = targetQuery + req.URL.RawQuery
@@ -130,7 +133,7 @@ func proxyPass(config *RouteHandlerConfig, infoProvider WorkspaceInfoProvider, r
130133

131134
// TODO(cw): we should cache the proxy for some time for each target URL
132135

133-
proxy := NewSingleHostReverseProxy(targetURL)
136+
proxy := NewSingleHostReverseProxy(targetURL, h.UseTargetHost)
134137
proxy.Transport = h.Transport
135138
proxy.ModifyResponse = func(resp *http.Response) error {
136139
url := resp.Request.URL
@@ -248,6 +251,12 @@ func withXFrameOptionsFilter() proxyPassOpt {
248251
}
249252
}
250253

254+
func withUseTargetHost() proxyPassOpt {
255+
return func(cfg *proxyPassConfig) {
256+
cfg.UseTargetHost = true
257+
}
258+
}
259+
251260
type workspaceTransport struct {
252261
transport http.RoundTripper
253262
}

components/ws-proxy/pkg/proxy/routes.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func (ir *ideRoutes) HandleSupervisorFrontendRoute(route *mux.Route) {
221221
return image
222222
},
223223
}
224-
}))
224+
}, withUseTargetHost()))
225225
}
226226

227227
func resolveSupervisorURL(cfg *Config, info *WorkspaceInfo, req *http.Request) (*url.URL, error) {
@@ -319,7 +319,7 @@ func (ir *ideRoutes) HandleRoot(route *mux.Route) {
319319
return image
320320
},
321321
}
322-
}, withHTTPErrorHandler(workspaceIDEPass)))
322+
}, withHTTPErrorHandler(workspaceIDEPass), withUseTargetHost()))
323323
}
324324

325325
const imagePathSeparator = "/__files__"

0 commit comments

Comments
 (0)