entry.ts 6.1 KB
Newer Older
A
Asher 已提交
1 2 3 4
import { field, logger } from "@coder/logger"
import * as cp from "child_process"
import * as path from "path"
import { CliMessage } from "../../lib/vscode/src/vs/server/ipc"
A
Asher 已提交
5
import { ApiHttpProvider } from "./app/api"
A
Asher 已提交
6
import { DashboardHttpProvider } from "./app/dashboard"
A
Asher 已提交
7
import { LoginHttpProvider } from "./app/login"
A
Asher 已提交
8
import { StaticHttpProvider } from "./app/static"
A
Asher 已提交
9
import { UpdateHttpProvider } from "./app/update"
A
Asher 已提交
10
import { VscodeHttpProvider } from "./app/vscode"
A
Asher 已提交
11
import { Args, optionDescriptions, parse } from "./cli"
A
Asher 已提交
12
import { AuthType, HttpServer, HttpServerOptions } from "./http"
W
Will O'Beirne 已提交
13 14
import { SshProvider } from "./ssh/server"
import { generateCertificate, generatePassword, generateSshHostKey, hash, open } from "./util"
A
Asher 已提交
15 16
import { ipcMain, wrap } from "./wrapper"

17 18 19 20 21 22 23
process.on("uncaughtException", (error) => {
  logger.error(`Uncaught exception: ${error.message}`)
  if (typeof error.stack !== "undefined") {
    logger.error(error.stack)
  }
})

A
Asher 已提交
24 25 26 27 28 29 30 31 32 33
let pkg: { version?: string; commit?: string } = {}
try {
  pkg = require("../../package.json")
} catch (error) {
  logger.warn(error.message)
}

const version = pkg.version || "development"
const commit = pkg.commit || "development"

34
const main = async (args: Args): Promise<void> => {
A
Asher 已提交
35 36 37
  const auth = args.auth || AuthType.Password
  const originalPassword = auth === AuthType.Password && (process.env.PASSWORD || (await generatePassword()))

A
Asher 已提交
38
  // Spawn the main HTTP server.
A
Asher 已提交
39
  const options: HttpServerOptions = {
A
Asher 已提交
40
    auth,
A
Asher 已提交
41
    commit,
A
Asher 已提交
42
    host: args.host || (args.auth === AuthType.Password && typeof args.cert !== "undefined" ? "0.0.0.0" : "localhost"),
A
Asher 已提交
43
    password: originalPassword ? hash(originalPassword) : undefined,
A
Asher 已提交
44
    port: typeof args.port !== "undefined" ? args.port : process.env.PORT ? parseInt(process.env.PORT, 10) : 8080,
A
Asher 已提交
45
    socket: args.socket,
A
Asher 已提交
46 47 48 49 50 51
    ...(args.cert && !args.cert.value
      ? await generateCertificate()
      : {
          cert: args.cert && args.cert.value,
          certKey: args["cert-key"],
        }),
A
Asher 已提交
52
  }
A
Asher 已提交
53

A
Asher 已提交
54
  if (options.cert && !options.certKey) {
A
Asher 已提交
55
    throw new Error("--cert-key is missing")
A
Asher 已提交
56
  }
A
Asher 已提交
57

A
Asher 已提交
58
  const httpServer = new HttpServer(options)
A
Asher 已提交
59
  const vscode = httpServer.registerHttpProvider("/", VscodeHttpProvider, args)
60
  const api = httpServer.registerHttpProvider("/api", ApiHttpProvider, httpServer, vscode, args["user-data-dir"])
A
Asher 已提交
61
  const update = httpServer.registerHttpProvider("/update", UpdateHttpProvider, !args["disable-updates"])
A
Asher 已提交
62
  httpServer.registerHttpProvider("/login", LoginHttpProvider)
A
Asher 已提交
63 64
  httpServer.registerHttpProvider("/static", StaticHttpProvider)
  httpServer.registerHttpProvider("/dashboard", DashboardHttpProvider, api, update)
A
Asher 已提交
65

66
  ipcMain().onDispose(() => httpServer.dispose())
A
Asher 已提交
67

A
Asher 已提交
68
  logger.info(`code-server ${version} ${commit}`)
A
Asher 已提交
69
  const serverAddress = await httpServer.listen()
70
  logger.info(`HTTP server listening on ${serverAddress}`)
A
Asher 已提交
71 72 73

  if (auth === AuthType.Password && !process.env.PASSWORD) {
    logger.info(`  - Password is ${originalPassword}`)
A
Asher 已提交
74
    logger.info("    - To use your own password set the PASSWORD environment variable")
A
Asher 已提交
75 76 77 78 79 80 81 82 83 84 85
    if (!args.auth) {
      logger.info("    - To disable use `--auth none`")
    }
  } else if (auth === AuthType.Password) {
    logger.info("  - Using custom password for authentication")
  } else {
    logger.info("  - No authentication")
  }

  if (httpServer.protocol === "https") {
    logger.info(
A
Asher 已提交
86
      args.cert && args.cert.value
A
Asher 已提交
87
        ? `  - Using provided certificate and key for HTTPS`
A
Anmol Sethi 已提交
88
        : `  - Using generated certificate and key for HTTPS`,
A
Asher 已提交
89 90 91 92 93
    )
  } else {
    logger.info("  - Not serving HTTPS")
  }

94
  logger.info(`Automatic updates are ${update.enabled ? "enabled" : "disabled"}`)
A
Asher 已提交
95

A
Asher 已提交
96 97 98 99 100 101 102 103 104
  let sshHostKey = args["ssh-host-key"]
  if (!args["disable-ssh"] && !sshHostKey) {
    try {
      sshHostKey = await generateSshHostKey()
    } catch (error) {
      logger.error("Unable to start SSH server", field("error", error.message))
    }
  }

A
Asher 已提交
105
  let sshPort: number | undefined
A
Asher 已提交
106 107
  if (!args["disable-ssh"] && sshHostKey) {
    const sshProvider = httpServer.registerHttpProvider("/ssh", SshProvider, sshHostKey)
A
Asher 已提交
108 109 110 111 112 113 114
    try {
      sshPort = await sshProvider.listen()
    } catch (error) {
      logger.warn(`SSH server: ${error.message}`)
    }
  }

115 116
  if (typeof sshPort !== "undefined") {
    logger.info(`SSH server listening on localhost:${sshPort}`)
A
Asher 已提交
117
    logger.info("  - To disable use `--disable-ssh`")
W
Will O'Beirne 已提交
118
  } else {
119
    logger.info("SSH server disabled")
W
Will O'Beirne 已提交
120 121
  }

A
Asher 已提交
122 123 124 125
  if (serverAddress && !options.socket && args.open) {
    // The web socket doesn't seem to work if browsing with 0.0.0.0.
    const openAddress = serverAddress.replace(/:\/\/0.0.0.0/, "://localhost")
    await open(openAddress).catch(console.error)
126
    logger.info(`Opened ${openAddress}`)
A
Asher 已提交
127 128 129
  }
}

130 131 132 133 134 135 136 137 138 139
const tryParse = (): Args => {
  try {
    return parse(process.argv.slice(2))
  } catch (error) {
    console.error(error.message)
    process.exit(1)
  }
}

const args = tryParse()
A
Asher 已提交
140
if (args.help) {
A
Asher 已提交
141
  console.log("code-server", version, commit)
A
Asher 已提交
142 143 144 145 146 147 148 149
  console.log("")
  console.log(`Usage: code-server [options] [path]`)
  console.log("")
  console.log("Options")
  optionDescriptions().forEach((description) => {
    console.log("", description)
  })
} else if (args.version) {
150
  if (args.json) {
151 152
    console.log({
      codeServer: version,
A
Asher 已提交
153
      commit,
154 155 156
      vscode: require("../../lib/vscode/package.json").version,
    })
  } else {
A
Asher 已提交
157
    console.log(version, commit)
158 159
  }
  process.exit(0)
A
Asher 已提交
160
} else if (args["list-extensions"] || args["install-extension"] || args["uninstall-extension"]) {
A
Asher 已提交
161
  logger.debug("forking vs code cli...")
A
Asher 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  const vscode = cp.fork(path.resolve(__dirname, "../../lib/vscode/out/vs/server/fork"), [], {
    env: {
      ...process.env,
      CODE_SERVER_PARENT_PID: process.pid.toString(),
    },
  })
  vscode.once("message", (message) => {
    logger.debug("Got message from VS Code", field("message", message))
    if (message.type !== "ready") {
      logger.error("Unexpected response waiting for ready response")
      process.exit(1)
    }
    const send: CliMessage = { type: "cli", args }
    vscode.send(send)
  })
  vscode.once("error", (error) => {
    logger.error(error.message)
    process.exit(1)
  })
  vscode.on("exit", (code) => process.exit(code || 0))
182
} else {
183
  wrap(() => main(args))
184
}