code-server/src/node/entry.ts

106 lines
3.3 KiB
TypeScript
Raw Normal View History

2020-02-05 02:27:46 +07:00
import { logger } from "@coder/logger"
import { ApiHttpProvider } from "./api/server"
import { MainHttpProvider } from "./app/server"
import { AuthType, HttpServer } from "./http"
import { generateCertificate, generatePassword, hash, open } from "./util"
import { VscodeHttpProvider } from "./vscode/server"
import { ipcMain, wrap } from "./wrapper"
export interface Args {
auth?: AuthType
"base-path"?: string
cert?: string
"cert-key"?: string
format?: string
host?: string
open?: boolean
port?: string
socket?: string
_?: string[]
}
const main = async (args: Args = {}): Promise<void> => {
2020-02-05 05:55:27 +07:00
const auth = args.auth || AuthType.Password
const originalPassword = auth === AuthType.Password && (process.env.PASSWORD || (await generatePassword()))
2020-02-05 07:16:45 +07:00
let commit = "development"
try {
commit = require("../../package.json").commit
} catch (error) {
logger.warn(error.message)
}
2020-02-05 02:27:46 +07:00
// Spawn the main HTTP server.
const options = {
2020-02-05 07:16:45 +07:00
auth,
2020-02-05 02:27:46 +07:00
basePath: args["base-path"],
cert: args.cert,
certKey: args["cert-key"],
2020-02-05 07:16:45 +07:00
commit,
2020-02-05 02:27:46 +07:00
host: args.host || (args.auth === AuthType.Password && typeof args.cert !== "undefined" ? "0.0.0.0" : "localhost"),
2020-02-05 07:16:45 +07:00
password: originalPassword ? hash(originalPassword) : undefined,
2020-02-05 02:27:46 +07:00
port: typeof args.port !== "undefined" ? parseInt(args.port, 10) : 8080,
socket: args.socket,
}
if (!options.cert && typeof options.cert !== "undefined") {
const { cert, certKey } = await generateCertificate()
options.cert = cert
options.certKey = certKey
}
2020-02-05 07:16:45 +07:00
const httpServer = new HttpServer(options)
2020-02-05 05:55:27 +07:00
httpServer.registerHttpProvider("/", MainHttpProvider)
httpServer.registerHttpProvider("/api", ApiHttpProvider, httpServer)
httpServer.registerHttpProvider("/vscode-embed", VscodeHttpProvider, [])
2020-02-05 02:27:46 +07:00
ipcMain().onDispose(() => httpServer.dispose())
2020-02-05 02:27:46 +07:00
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(
args.cert
? ` - 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}`)
}
}
// TODO: Implement CLI parser.
if (process.argv.includes("--version")) {
const version = require("../../package.json").version
if (process.argv.includes("--json")) {
console.log({
codeServer: version,
vscode: require("../../lib/vscode/package.json").version,
})
} else {
console.log(version)
}
process.exit(0)
} else {
wrap(main)
}