Skip to content

use VSCODE_PROXY_URI for domainProxy, allow {{host}} replacement #6225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions patches/proxy-uri.diff
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Index: code-server/lib/vscode/src/vs/code/browser/workbench/workbench.ts

interface ICredential {
service: string;
@@ -511,6 +512,38 @@ function doCreateUri(path: string, query
@@ -511,6 +512,42 @@ function doCreateUri(path: string, query
} : undefined,
workspaceProvider: WorkspaceProvider.create(config),
urlCallbackProvider: new LocalStorageURLCallbackProvider(config.callbackRoute),
Expand All @@ -125,7 +125,11 @@ Index: code-server/lib/vscode/src/vs/code/browser/workbench/workbench.ts
+
+ if (localhostMatch && resolvedUri.authority !== location.host) {
+ if (config.productConfiguration && config.productConfiguration.proxyEndpointTemplate) {
+ resolvedUri = URI.parse(new URL(config.productConfiguration.proxyEndpointTemplate.replace('{{port}}', localhostMatch.port.toString()), window.location.href).toString())
+ const renderedTemplate = config.productConfiguration.proxyEndpointTemplate
+ .replace('{{port}}', localhostMatch.port.toString())
+ .replace('{{host}}', window.location.hostname)
+
+ resolvedUri = URI.parse(new URL(renderedTemplate, window.location.href).toString())
+ } else {
+ throw new Error(`Failed to resolve external URI: ${uri.toString()}. Could not determine base url because productConfiguration missing.`)
+ }
Expand Down
45 changes: 41 additions & 4 deletions src/node/routes/domainProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Router as WsRouter } from "../wsRouter"

export const router = Router()


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

// There must be an exact match.
// There must be an exact match for proxy-domain
const port = parts.shift()
const proxyDomain = parts.join(".")
if (!port || !req.args["proxy-domain"].includes(proxyDomain)) {
return undefined
if (port && req.args["proxy-domain"].includes(proxyDomain)) {
return port
}

// check based on VSCODE_PROXY_URI
const proxyTemplate = process.env.VSCODE_PROXY_URI
if(proxyTemplate) {
return matchVsCodeProxyUriAndExtractPort(proxyTemplate, domain)
}

return undefined
}


let regex : RegExp | undefined = undefined;
const matchVsCodeProxyUriAndExtractPort = (matchString: string, domain: string): string | undefined => {
// init regex on first use
if(!regex) {
// Escape dot characters in the match string
let escapedMatchString = matchString.replace(/\./g, "\\.");

// Replace {{port}} with a regex group to capture the port
let regexString = escapedMatchString.replace("{{port}}", "(\\d+)");

// remove http:// and https:// from matchString as protocol cannot be determined based on the Host header
regexString = regexString.replace("https://", "").replace("http://", "");

// Replace {{host}} with .* to allow any host match (so rely on DNS record here)
regexString = regexString.replace("{{host}}", ".*");

regex = new RegExp("^" + regexString + "$");
}

// Test the domain against the regex
let match = domain.match(regex);

if (match) {
return match[1]; // match[1] contains the port
}

return port
return undefined;
}

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