Skip to content

Commit c17038f

Browse files
committed
Add proxy-domain flag
This will be used for proxying ports.
1 parent a68b5b7 commit c17038f

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

src/node/cli.ts

Lines changed: 2 additions & 0 deletions
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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,22 @@ const main = async (args: Args): Promise<void> => {
3535
const auth = args.auth || AuthType.Password
3636
const originalPassword = auth === AuthType.Password && (process.env.PASSWORD || (await generatePassword()))
3737

38+
/**
39+
* Domains can be in the form `coder.com` or `*.coder.com`. Either way,
40+
* `[number].coder.com` will be proxied to `number`.
41+
*/
42+
const normalizeProxyDomains = (domains?: string[]): string[] => {
43+
return domains ? domains.map((d) => d.replace(/^\*\./, "")).filter((d, i) => domains.indexOf(d) === i) : []
44+
}
45+
3846
// Spawn the main HTTP server.
3947
const options: HttpServerOptions = {
4048
auth,
4149
commit,
4250
host: args.host || (args.auth === AuthType.Password && typeof args.cert !== "undefined" ? "0.0.0.0" : "localhost"),
4351
password: originalPassword ? hash(originalPassword) : undefined,
4452
port: typeof args.port !== "undefined" ? args.port : process.env.PORT ? parseInt(process.env.PORT, 10) : 8080,
53+
proxyDomains: normalizeProxyDomains(args["proxy-domain"]),
4554
socket: args.socket,
4655
...(args.cert && !args.cert.value
4756
? await generateCertificate()
@@ -91,6 +100,15 @@ const main = async (args: Args): Promise<void> => {
91100
logger.info(" - Not serving HTTPS")
92101
}
93102

103+
if (options.proxyDomains && options.proxyDomains.length === 1) {
104+
logger.info(` - Proxying *.${options.proxyDomains[0]}`)
105+
} else if (options.proxyDomains && options.proxyDomains.length > 1) {
106+
logger.info(" - Proxying the following domains:")
107+
options.proxyDomains.forEach((domain) => {
108+
logger.info(` - *.${domain}`)
109+
})
110+
}
111+
94112
logger.info(`Automatic updates are ${update.enabled ? "enabled" : "disabled"}`)
95113

96114
let sshHostKey = args["ssh-host-key"]

src/node/http.ts

Lines changed: 1 addition & 0 deletions
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

Lines changed: 16 additions & 0 deletions
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)