Skip to content

Commit 5c63058

Browse files
committed
use VSCODE_PROXY_URI for domainProxy, allow {{host}} replacement
this enables support for VSCODE_PROXY_URIs like https://code-{{port}}.{{host}}
1 parent 74af05d commit 5c63058

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

patches/proxy-uri.diff

+6-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Index: code-server/lib/vscode/src/vs/code/browser/workbench/workbench.ts
113113

114114
interface ICredential {
115115
service: string;
116-
@@ -511,6 +512,38 @@ function doCreateUri(path: string, query
116+
@@ -511,6 +512,42 @@ function doCreateUri(path: string, query
117117
} : undefined,
118118
workspaceProvider: WorkspaceProvider.create(config),
119119
urlCallbackProvider: new LocalStorageURLCallbackProvider(config.callbackRoute),
@@ -125,7 +125,11 @@ Index: code-server/lib/vscode/src/vs/code/browser/workbench/workbench.ts
125125
+
126126
+ if (localhostMatch && resolvedUri.authority !== location.host) {
127127
+ if (config.productConfiguration && config.productConfiguration.proxyEndpointTemplate) {
128-
+ resolvedUri = URI.parse(new URL(config.productConfiguration.proxyEndpointTemplate.replace('{{port}}', localhostMatch.port.toString()), window.location.href).toString())
128+
+ const renderedTemplate = config.productConfiguration.proxyEndpointTemplate
129+
+ .replace('{{port}}', localhostMatch.port.toString())
130+
+ .replace('{{host}}', window.location.hostname)
131+
+
132+
+ resolvedUri = URI.parse(new URL(renderedTemplate, window.location.href).toString())
129133
+ } else {
130134
+ throw new Error(`Failed to resolve external URI: ${uri.toString()}. Could not determine base url because productConfiguration missing.`)
131135
+ }

src/node/routes/domainProxy.ts

+41-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Router as WsRouter } from "../wsRouter"
66

77
export const router = Router()
88

9+
910
/**
1011
* Return the port if the request should be proxied. Anything that ends in a
1112
* proxy domain and has a *single* subdomain should be proxied. Anything else
@@ -21,14 +22,50 @@ const maybeProxy = (req: Request): string | undefined => {
2122
const domain = idx !== -1 ? host.substring(0, idx) : host
2223
const parts = domain.split(".")
2324

24-
// There must be an exact match.
25+
// There must be an exact match for proxy-domain
2526
const port = parts.shift()
2627
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
2966
}
3067

31-
return port
68+
return undefined;
3269
}
3370

3471
router.all("*", async (req, res, next) => {

0 commit comments

Comments
 (0)