@@ -6,6 +6,7 @@ import { Router as WsRouter } from "../wsRouter"
6
6
7
7
export const router = Router ( )
8
8
9
+
9
10
/**
10
11
* Return the port if the request should be proxied. Anything that ends in a
11
12
* proxy domain and has a *single* subdomain should be proxied. Anything else
@@ -21,14 +22,50 @@ const maybeProxy = (req: Request): string | undefined => {
21
22
const domain = idx !== - 1 ? host . substring ( 0 , idx ) : host
22
23
const parts = domain . split ( "." )
23
24
24
- // There must be an exact match.
25
+ // There must be an exact match for proxy-domain
25
26
const port = parts . shift ( )
26
27
const proxyDomain = parts . join ( "." )
27
- if ( ! port || ! req . args [ "proxy-domain" ] . includes ( proxyDomain ) ) {
28
- return undefined
28
+ if ( port && req . args [ "proxy-domain" ] . includes ( proxyDomain ) ) {
29
+ return port
30
+ }
31
+
32
+ // check based on VSCODE_PROXY_URI
33
+ const proxyTemplate = process . env . VSCODE_PROXY_URI
34
+ if ( proxyTemplate ) {
35
+ return matchVsCodeProxyUriAndExtractPort ( proxyTemplate , domain )
36
+ }
37
+
38
+ return undefined
39
+ }
40
+
41
+
42
+ let regex : RegExp | undefined = undefined ;
43
+ const matchVsCodeProxyUriAndExtractPort = ( matchString : string , domain : string ) : string | undefined => {
44
+ // init regex on first use
45
+ if ( ! regex ) {
46
+ // Escape dot characters in the match string
47
+ let escapedMatchString = matchString . replace ( / \. / g, "\\." ) ;
48
+
49
+ // Replace {{port}} with a regex group to capture the port
50
+ let regexString = escapedMatchString . replace ( "{{port}}" , "(\\d+)" ) ;
51
+
52
+ // remove http:// and https:// from matchString as protocol cannot be determined based on the Host header
53
+ regexString = regexString . replace ( "https://" , "" ) . replace ( "http://" , "" ) ;
54
+
55
+ // Replace {{host}} with .* to allow any host match (so rely on DNS record here)
56
+ regexString = regexString . replace ( "{{host}}" , ".*" ) ;
57
+
58
+ regex = new RegExp ( "^" + regexString + "$" ) ;
59
+ }
60
+
61
+ // Test the domain against the regex
62
+ let match = domain . match ( regex ) ;
63
+
64
+ if ( match ) {
65
+ return match [ 1 ] ; // match[1] contains the port
29
66
}
30
67
31
- return port
68
+ return undefined ;
32
69
}
33
70
34
71
router . all ( "*" , async ( req , res , next ) => {
0 commit comments