entry.ts 3.6 KB
Newer Older
A
Asher 已提交
1
import { logger } from "@coder/logger"
A
Asher 已提交
2
import { Args, optionDescriptions, parse } from "./cli"
A
Asher 已提交
3 4 5 6
import { ApiHttpProvider } from "./app/api"
import { MainHttpProvider } from "./app/app"
import { LoginHttpProvider } from "./app/login"
import { VscodeHttpProvider } from "./app/vscode"
A
Asher 已提交
7 8 9 10
import { AuthType, HttpServer } from "./http"
import { generateCertificate, generatePassword, hash, open } from "./util"
import { ipcMain, wrap } from "./wrapper"

11
const main = async (args: Args): Promise<void> => {
A
Asher 已提交
12 13 14
  // For any future forking bypass nbin and drop straight to Node.
  process.env.NBIN_BYPASS = "true"

A
Asher 已提交
15 16 17
  const auth = args.auth || AuthType.Password
  const originalPassword = auth === AuthType.Password && (process.env.PASSWORD || (await generatePassword()))

A
Asher 已提交
18
  let commit: string | undefined
A
Asher 已提交
19 20 21 22 23 24
  try {
    commit = require("../../package.json").commit
  } catch (error) {
    logger.warn(error.message)
  }

A
Asher 已提交
25 26
  // Spawn the main HTTP server.
  const options = {
A
Asher 已提交
27
    auth,
A
Asher 已提交
28
    cert: args.cert ? args.cert.value : undefined,
A
Asher 已提交
29
    certKey: args["cert-key"],
A
Asher 已提交
30
    commit: commit || "development",
A
Asher 已提交
31
    host: args.host || (args.auth === AuthType.Password && typeof args.cert !== "undefined" ? "0.0.0.0" : "localhost"),
A
Asher 已提交
32
    password: originalPassword ? hash(originalPassword) : undefined,
A
Asher 已提交
33
    port: typeof args.port !== "undefined" ? args.port : 8080,
A
Asher 已提交
34 35
    socket: args.socket,
  }
A
Asher 已提交
36
  if (!options.cert && args.cert) {
A
Asher 已提交
37 38 39 40 41
    const { cert, certKey } = await generateCertificate()
    options.cert = cert
    options.certKey = certKey
  }

A
Asher 已提交
42
  const httpServer = new HttpServer(options)
A
Asher 已提交
43 44 45 46
  const api = httpServer.registerHttpProvider("/api", ApiHttpProvider, httpServer)
  httpServer.registerHttpProvider("/vscode", VscodeHttpProvider, args)
  httpServer.registerHttpProvider("/login", LoginHttpProvider)
  httpServer.registerHttpProvider("/", MainHttpProvider, api)
A
Asher 已提交
47

48
  ipcMain().onDispose(() => httpServer.dispose())
A
Asher 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

  const serverAddress = await httpServer.listen()
  logger.info(`Server listening on ${serverAddress}`)

  if (auth === AuthType.Password && !process.env.PASSWORD) {
    logger.info(`  - Password is ${originalPassword}`)
    logger.info("    - To use your own password, set the PASSWORD environment variable")
    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 已提交
67
      typeof args.cert === "string"
A
Asher 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        ? `  - Using provided certificate${args["cert-key"] ? " and key" : ""} for HTTPS`
        : `  - Using generated certificate and key for HTTPS`
    )
  } else {
    logger.info("  - Not serving HTTPS")
  }

  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)
    logger.info(`  - Opened ${openAddress}`)
  }
}

A
Asher 已提交
83 84 85 86 87 88 89 90 91 92 93
const args = parse(process.argv.slice(2))
if (args.help) {
  console.log("code-server", require("../../package.json").version)
  console.log("")
  console.log(`Usage: code-server [options] [path]`)
  console.log("")
  console.log("Options")
  optionDescriptions().forEach((description) => {
    console.log("", description)
  })
} else if (args.version) {
94
  const version = require("../../package.json").version
95
  if (args.json) {
96 97 98 99 100 101 102 103 104
    console.log({
      codeServer: version,
      vscode: require("../../lib/vscode/package.json").version,
    })
  } else {
    console.log(version)
  }
  process.exit(0)
} else {
105
  wrap(() => main(args))
106
}