Archived
1
0

Add proxy-domain flag

This will be used for proxying ports.
This commit is contained in:
Asher 2020-03-23 12:08:50 -05:00
parent 37299abcc9
commit 13534fa0c0
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
4 changed files with 37 additions and 0 deletions

View File

@ -39,6 +39,7 @@ export interface Args extends VsArgs {
readonly "install-extension"?: string[] readonly "install-extension"?: string[]
readonly "show-versions"?: boolean readonly "show-versions"?: boolean
readonly "uninstall-extension"?: string[] readonly "uninstall-extension"?: string[]
readonly "proxy-domain"?: string[]
readonly locale?: string readonly locale?: string
readonly _: string[] readonly _: string[]
} }
@ -111,6 +112,7 @@ const options: Options<Required<Args>> = {
"install-extension": { type: "string[]", description: "Install or update a VS Code extension by id or vsix." }, "install-extension": { type: "string[]", description: "Install or update a VS Code extension by id or vsix." },
"uninstall-extension": { type: "string[]", description: "Uninstall a VS Code extension by id." }, "uninstall-extension": { type: "string[]", description: "Uninstall a VS Code extension by id." },
"show-versions": { type: "boolean", description: "Show VS Code extension versions." }, "show-versions": { type: "boolean", description: "Show VS Code extension versions." },
"proxy-domain": { type: "string[]", description: "Domain used for proxying ports." },
locale: { type: "string" }, locale: { type: "string" },
log: { type: LogLevel }, log: { type: LogLevel },

View File

@ -35,6 +35,14 @@ const main = async (args: Args): Promise<void> => {
const auth = args.auth || AuthType.Password const auth = args.auth || AuthType.Password
const originalPassword = auth === AuthType.Password && (process.env.PASSWORD || (await generatePassword())) const originalPassword = auth === AuthType.Password && (process.env.PASSWORD || (await generatePassword()))
/**
* Domains can be in the form `coder.com` or `*.coder.com`. Either way,
* `[number].coder.com` will be proxied to `number`.
*/
const normalizeProxyDomains = (domains?: string[]): string[] => {
return domains ? domains.map((d) => d.replace(/^\*\./, "")).filter((d, i) => domains.indexOf(d) === i) : []
}
// Spawn the main HTTP server. // Spawn the main HTTP server.
const options: HttpServerOptions = { const options: HttpServerOptions = {
auth, auth,
@ -42,6 +50,7 @@ const main = async (args: Args): Promise<void> => {
host: args.host || (args.auth === AuthType.Password && typeof args.cert !== "undefined" ? "0.0.0.0" : "localhost"), host: args.host || (args.auth === AuthType.Password && typeof args.cert !== "undefined" ? "0.0.0.0" : "localhost"),
password: originalPassword ? hash(originalPassword) : undefined, password: originalPassword ? hash(originalPassword) : undefined,
port: typeof args.port !== "undefined" ? args.port : process.env.PORT ? parseInt(process.env.PORT, 10) : 8080, port: typeof args.port !== "undefined" ? args.port : process.env.PORT ? parseInt(process.env.PORT, 10) : 8080,
proxyDomains: normalizeProxyDomains(args["proxy-domain"]),
socket: args.socket, socket: args.socket,
...(args.cert && !args.cert.value ...(args.cert && !args.cert.value
? await generateCertificate() ? await generateCertificate()
@ -91,6 +100,15 @@ const main = async (args: Args): Promise<void> => {
logger.info(" - Not serving HTTPS") logger.info(" - Not serving HTTPS")
} }
if (options.proxyDomains && options.proxyDomains.length === 1) {
logger.info(` - Proxying *.${options.proxyDomains[0]}`)
} else if (options.proxyDomains && options.proxyDomains.length > 1) {
logger.info(" - Proxying the following domains:")
options.proxyDomains.forEach((domain) => {
logger.info(` - *.${domain}`)
})
}
logger.info(`Automatic updates are ${update.enabled ? "enabled" : "disabled"}`) logger.info(`Automatic updates are ${update.enabled ? "enabled" : "disabled"}`)
let sshHostKey = args["ssh-host-key"] let sshHostKey = args["ssh-host-key"]

View File

@ -99,6 +99,7 @@ export interface HttpServerOptions {
readonly commit?: string readonly commit?: string
readonly host?: string readonly host?: string
readonly password?: string readonly password?: string
readonly proxyDomains?: string[]
readonly port?: number readonly port?: number
readonly socket?: string readonly socket?: string
} }

View File

@ -117,6 +117,7 @@ describe("cli", () => {
assert.throws(() => parse(["--auth=", "--log=debug"]), /--auth requires a value/) assert.throws(() => parse(["--auth=", "--log=debug"]), /--auth requires a value/)
assert.throws(() => parse(["--auth", "--log"]), /--auth requires a value/) assert.throws(() => parse(["--auth", "--log"]), /--auth requires a value/)
assert.throws(() => parse(["--auth", "--invalid"]), /--auth requires a value/) assert.throws(() => parse(["--auth", "--invalid"]), /--auth requires a value/)
assert.throws(() => parse(["--ssh-host-key"]), /--ssh-host-key requires a value/)
}) })
it("should error if value is invalid", () => { it("should error if value is invalid", () => {
@ -160,4 +161,19 @@ describe("cli", () => {
auth: "none", auth: "none",
}) })
}) })
it("should support repeatable flags", () => {
assert.deepEqual(parse(["--proxy-domain", "*.coder.com"]), {
_: [],
"extensions-dir": path.join(xdgLocalDir, "extensions"),
"user-data-dir": xdgLocalDir,
"proxy-domain": ["*.coder.com"],
})
assert.deepEqual(parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "test.com"]), {
_: [],
"extensions-dir": path.join(xdgLocalDir, "extensions"),
"user-data-dir": xdgLocalDir,
"proxy-domain": ["*.coder.com", "test.com"],
})
})
}) })