From df3089f3ad4b8231407e365819ef88aac42fcd76 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Wed, 7 Oct 2020 15:54:41 -0400 Subject: [PATCH] coder-cloud: Use consolidated bind command --- src/node/cli.ts | 4 ++-- src/node/coder-cloud.ts | 44 +++++------------------------------------ src/node/entry.ts | 7 ++----- 3 files changed, 9 insertions(+), 46 deletions(-) diff --git a/src/node/cli.ts b/src/node/cli.ts index deedf830..ed188f28 100644 --- a/src/node/cli.ts +++ b/src/node/cli.ts @@ -48,7 +48,7 @@ export interface Args extends VsArgs { readonly "reuse-window"?: boolean readonly "new-window"?: boolean - readonly "coder-bind"?: string + readonly "coder-bind"?: OptionalString } interface Option { @@ -160,7 +160,7 @@ const options: Options> = { verbose: { type: "boolean", short: "vvv", description: "Enable verbose logging." }, "coder-bind": { - type: "string", + type: OptionalString, description: ` Securely bind code-server via Coder Cloud with the passed name. You'll get a URL like https://myname.coder-cloud.com at which you can easily access your code-server instance. diff --git a/src/node/coder-cloud.ts b/src/node/coder-cloud.ts index a3c3c590..b57cf36d 100644 --- a/src/node/coder-cloud.ts +++ b/src/node/coder-cloud.ts @@ -5,8 +5,8 @@ import split2 from "split2" const coderCloudAgent = path.resolve(__dirname, "../../lib/coder-cloud-agent") -export async function coderCloudBind(serverName: string): Promise { - const agent = spawn(coderCloudAgent, ["link", serverName], { +function runAgent(...args: string[]): Promise { + const agent = spawn(coderCloudAgent, args, { stdio: ["inherit", "inherit", "pipe"], }) @@ -30,43 +30,9 @@ export async function coderCloudBind(serverName: string): Promise { }) } -export function coderCloudProxy(addr: string) { +export function coderCloudBind(csAddr: string, serverName = ""): Promise { // addr needs to be in host:port format. // So we trim the protocol. - addr = addr.replace(/^https?:\/\//, "") - - const _proxy = async () => { - const agent = spawn(coderCloudAgent, ["proxy", "--code-server-addr", addr], { - stdio: ["inherit", "inherit", "pipe"], - }) - - agent.stderr.pipe(split2()).on("data", (line) => { - line = line.replace(/^[0-9-]+ [0-9:]+ [^ ]+\t/, "") - logger.info(line) - }) - - return new Promise((res, rej) => { - agent.on("error", rej) - - agent.on("close", (code) => { - if (code !== 0) { - rej({ - message: `coder cloud agent exited with ${code}`, - }) - return - } - res() - }) - }) - } - - const proxy = async () => { - try { - await _proxy() - } catch (err) { - logger.error(err.message) - } - setTimeout(proxy, 3000) - } - proxy() + csAddr = csAddr.replace(/^https?:\/\//, "") + return runAgent("bind", `--code-server-addr=${csAddr}`, serverName) } diff --git a/src/node/entry.ts b/src/node/entry.ts index 16118a1e..a39db7ef 100644 --- a/src/node/entry.ts +++ b/src/node/entry.ts @@ -12,7 +12,7 @@ import { StaticHttpProvider } from "./app/static" import { UpdateHttpProvider } from "./app/update" import { VscodeHttpProvider } from "./app/vscode" import { Args, bindAddrFromAllSources, optionDescriptions, parse, readConfigFile, setDefaults } from "./cli" -import { coderCloudBind, coderCloudProxy } from "./coder-cloud" +import { coderCloudBind } from "./coder-cloud" import { AuthType, HttpServer, HttpServerOptions } from "./http" import { loadPlugins } from "./plugin" import { generateCertificate, hash, humanPath, open } from "./util" @@ -143,11 +143,8 @@ const main = async (args: Args, cliArgs: Args, configArgs: Args): Promise } if (args["coder-bind"]) { - try { - logger.info(`binding code-server to the cloud with name ${args["coder-bind"]}`) - await coderCloudBind(args["coder-bind"]) - coderCloudProxy(serverAddress!) + await coderCloudBind(serverAddress!, args["coder-bind"].value) } catch (err) { logger.error(err.message) ipcMain().exit(1)