Skip to content

Commit ab52615

Browse files
committed
Add proxy-domain flag
This will be used for proxying ports.
1 parent 0e0cfd1 commit ab52615

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

src/node/cli.ts

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export interface Args extends VsArgs {
3939
readonly "install-extension"?: string[]
4040
readonly "show-versions"?: boolean
4141
readonly "uninstall-extension"?: string[]
42+
readonly "proxy-domain"?: string[]
4243
readonly locale?: string
4344
readonly _: string[]
4445
}
@@ -111,6 +112,7 @@ const options: Options<Required<Args>> = {
111112
"install-extension": { type: "string[]", description: "Install or update a VS Code extension by id or vsix." },
112113
"uninstall-extension": { type: "string[]", description: "Uninstall a VS Code extension by id." },
113114
"show-versions": { type: "boolean", description: "Show VS Code extension versions." },
115+
"proxy-domain": { type: "string[]", description: "Domain used for proxying ports." },
114116

115117
locale: { type: "string" },
116118
log: { type: LogLevel },

src/node/entry.ts

+18
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,22 @@ const main = async (args: Args): Promise<void> => {
2525
logger.warn(error.message)
2626
}
2727

28+
/**
29+
* Domains can be in the form `coder.com` or `*.coder.com`. Either way,
30+
* `[number].coder.com` will be proxied to `number`.
31+
*/
32+
const normalizeProxyDomains = (domains?: string[]): string[] => {
33+
return domains ? domains.map((d) => d.replace(/^\*\./, "")).filter((d, i) => domains.indexOf(d) === i) : []
34+
}
35+
2836
// Spawn the main HTTP server.
2937
const options: HttpServerOptions = {
3038
auth,
3139
commit: commit || "development",
3240
host: args.host || (args.auth === AuthType.Password && typeof args.cert !== "undefined" ? "0.0.0.0" : "localhost"),
3341
password: originalPassword ? hash(originalPassword) : undefined,
3442
port: typeof args.port !== "undefined" ? args.port : process.env.PORT ? parseInt(process.env.PORT, 10) : 8080,
43+
proxyDomains: normalizeProxyDomains(args["proxy-domain"]),
3544
socket: args.socket,
3645
...(args.cert && !args.cert.value
3746
? await generateCertificate()
@@ -98,6 +107,15 @@ const main = async (args: Args): Promise<void> => {
98107
logger.info(" - Not serving HTTPS")
99108
}
100109

110+
if (options.proxyDomains && options.proxyDomains.length === 1) {
111+
logger.info(` - Proxying *.${options.proxyDomains[0]}`)
112+
} else if (options.proxyDomains && options.proxyDomains.length > 1) {
113+
logger.info(" - Proxying the following domains:")
114+
options.proxyDomains.forEach((domain) => {
115+
logger.info(` - *.${domain}`)
116+
})
117+
}
118+
101119
logger.info(`Automatic updates are ${update.enabled ? "enabled" : "disabled"}`)
102120

103121
if (sshPort) {

src/node/http.ts

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export interface HttpServerOptions {
9999
readonly commit?: string
100100
readonly host?: string
101101
readonly password?: string
102+
readonly proxyDomains?: string[]
102103
readonly port?: number
103104
readonly socket?: string
104105
}

test/cli.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ describe("cli", () => {
117117
assert.throws(() => parse(["--auth=", "--log=debug"]), /--auth requires a value/)
118118
assert.throws(() => parse(["--auth", "--log"]), /--auth requires a value/)
119119
assert.throws(() => parse(["--auth", "--invalid"]), /--auth requires a value/)
120+
assert.throws(() => parse(["--ssh-host-key"]), /--ssh-host-key requires a value/)
120121
})
121122

122123
it("should error if value is invalid", () => {
@@ -160,4 +161,19 @@ describe("cli", () => {
160161
auth: "none",
161162
})
162163
})
164+
165+
it("should support repeatable flags", () => {
166+
assert.deepEqual(parse(["--proxy-domain", "*.coder.com"]), {
167+
_: [],
168+
"extensions-dir": path.join(xdgLocalDir, "extensions"),
169+
"user-data-dir": xdgLocalDir,
170+
"proxy-domain": ["*.coder.com"],
171+
})
172+
assert.deepEqual(parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "test.com"]), {
173+
_: [],
174+
"extensions-dir": path.join(xdgLocalDir, "extensions"),
175+
"user-data-dir": xdgLocalDir,
176+
"proxy-domain": ["*.coder.com", "test.com"],
177+
})
178+
})
163179
})

0 commit comments

Comments
 (0)