From c995988b70e4134dacfa98a8ae0af2b05c6cd7b6 Mon Sep 17 00:00:00 2001 From: Asher Date: Thu, 30 Mar 2023 12:01:49 -0800 Subject: [PATCH] Set proxy URI to domain proxy when possible (#6115) This will make the ports panel use it instead of the default path-based proxy. --- src/node/cli.ts | 3 +++ test/unit/node/cli.test.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/node/cli.ts b/src/node/cli.ts index 75e4ad200..18929faa0 100644 --- a/src/node/cli.ts +++ b/src/node/cli.ts @@ -571,6 +571,9 @@ export async function setDefaults(cliArgs: UserProvidedArgs, configArgs?: Config // Filter duplicate proxy domains and remove any leading `*.`. const proxyDomains = new Set((args["proxy-domain"] || []).map((d) => d.replace(/^\*\./, ""))) args["proxy-domain"] = Array.from(proxyDomains) + if (args["proxy-domain"].length > 0 && !process.env.VSCODE_PROXY_URI) { + process.env.VSCODE_PROXY_URI = `{{port}}.${args["proxy-domain"][0]}` + } if (typeof args._ === "undefined") { args._ = [] diff --git a/test/unit/node/cli.test.ts b/test/unit/node/cli.test.ts index 7e7fa29a4..8c4f19dbc 100644 --- a/test/unit/node/cli.test.ts +++ b/test/unit/node/cli.test.ts @@ -43,6 +43,7 @@ describe("parser", () => { delete process.env.PASSWORD delete process.env.CS_DISABLE_FILE_DOWNLOADS delete process.env.CS_DISABLE_GETTING_STARTED_OVERRIDE + delete process.env.VSCODE_PROXY_URI console.log = jest.fn() }) @@ -457,6 +458,31 @@ describe("parser", () => { port: 8082, }) }) + + it("should not set proxy uri", async () => { + await setDefaults(parse([])) + expect(process.env.VSCODE_PROXY_URI).toBeUndefined() + }) + + it("should set proxy uri", async () => { + await setDefaults(parse(["--proxy-domain", "coder.org"])) + expect(process.env.VSCODE_PROXY_URI).toEqual("{{port}}.coder.org") + }) + + it("should set proxy uri to first domain", async () => { + await setDefaults( + parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "coder.com", "--proxy-domain", "coder.org"]), + ) + expect(process.env.VSCODE_PROXY_URI).toEqual("{{port}}.coder.com") + }) + + it("should not override existing proxy uri", async () => { + process.env.VSCODE_PROXY_URI = "foo" + await setDefaults( + parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "coder.com", "--proxy-domain", "coder.org"]), + ) + expect(process.env.VSCODE_PROXY_URI).toEqual("foo") + }) }) describe("cli", () => {