2019-01-31 04:40:01 +07:00
|
|
|
import { field, logger } from "@coder/logger";
|
2019-04-03 05:44:28 +07:00
|
|
|
import { ServerMessage, SharedProcessActive } from "@coder/protocol/src/proto";
|
2019-04-18 04:30:50 +07:00
|
|
|
import { ChildProcess, fork, ForkOptions } from "child_process";
|
2019-03-07 07:25:44 +07:00
|
|
|
import { randomFillSync } from "crypto";
|
2019-01-16 01:36:09 +07:00
|
|
|
import * as fs from "fs";
|
2019-03-19 23:53:05 +07:00
|
|
|
import * as fse from "fs-extra";
|
2019-03-12 23:12:50 +07:00
|
|
|
import * as os from "os";
|
2019-01-16 01:36:09 +07:00
|
|
|
import * as path from "path";
|
2019-01-19 06:08:44 +07:00
|
|
|
import * as WebSocket from "ws";
|
2019-03-19 23:53:05 +07:00
|
|
|
import { buildDir, cacheHome, dataHome, isCli, serveStatic } from "./constants";
|
2019-01-19 04:46:40 +07:00
|
|
|
import { createApp } from "./server";
|
2019-04-16 07:48:12 +07:00
|
|
|
import { forkModule, requireModule } from "./vscode/bootstrapFork";
|
2019-01-23 01:27:59 +07:00
|
|
|
import { SharedProcess, SharedProcessState } from "./vscode/sharedProcess";
|
2019-03-08 00:23:54 +07:00
|
|
|
import opn = require("opn");
|
2019-01-16 01:36:09 +07:00
|
|
|
|
2019-03-27 03:21:03 +07:00
|
|
|
import * as commander from "commander";
|
|
|
|
|
|
|
|
commander.version(process.env.VERSION || "development")
|
|
|
|
.name("code-server")
|
|
|
|
.description("Run VS Code on a remote server.")
|
2019-03-27 20:56:05 +07:00
|
|
|
.option("--cert <value>")
|
|
|
|
.option("--cert-key <value>")
|
2019-04-04 05:07:47 +07:00
|
|
|
.option("-e, --extensions-dir <dir>", "Set the root path for extensions.")
|
|
|
|
.option("-d --user-data-dir <dir>", " Specifies the directory that user data is kept in, useful when running as root.")
|
|
|
|
.option("--data-dir <value>", "DEPRECATED: Use '--user-data-dir' instead. Customize where user-data is stored.")
|
2019-03-27 03:21:03 +07:00
|
|
|
.option("-h, --host <value>", "Customize the hostname.", "0.0.0.0")
|
|
|
|
.option("-o, --open", "Open in the browser on startup.", false)
|
2019-04-18 04:30:39 +07:00
|
|
|
.option("-p, --port <number>", "Port to bind on.", parseInt(process.env.PORT, 10) || 8443)
|
2019-03-27 03:21:03 +07:00
|
|
|
.option("-N, --no-auth", "Start without requiring authentication.", undefined)
|
|
|
|
.option("-H, --allow-http", "Allow http connections.", false)
|
|
|
|
.option("-P, --password <value>", "Specify a password for authentication.")
|
2019-04-18 05:18:35 +07:00
|
|
|
.option("--disable-telemetry", "Disables ALL telemetry.", false)
|
2019-04-18 04:30:50 +07:00
|
|
|
.option("--install-extension <value>", "Install an extension by its ID.")
|
2019-03-27 03:21:03 +07:00
|
|
|
.option("--bootstrap-fork <name>", "Used for development. Never set.")
|
|
|
|
.option("--extra-args <args>", "Used for development. Never set.")
|
|
|
|
.arguments("Specify working directory.")
|
|
|
|
.parse(process.argv);
|
|
|
|
|
|
|
|
Error.stackTraceLimit = Infinity;
|
|
|
|
if (isCli) {
|
|
|
|
require("nbin").shimNativeFs(buildDir);
|
2019-04-16 07:48:12 +07:00
|
|
|
require("nbin").shimNativeFs("/node_modules");
|
2019-03-27 03:21:03 +07:00
|
|
|
}
|
2019-04-04 03:50:52 +07:00
|
|
|
// Makes strings or numbers bold in stdout
|
|
|
|
const bold = (text: string | number): string | number => {
|
|
|
|
return `\u001B[1m${text}\u001B[0m`;
|
|
|
|
};
|
2019-03-27 03:21:03 +07:00
|
|
|
|
|
|
|
(async (): Promise<void> => {
|
|
|
|
const args = commander.args;
|
|
|
|
const options = commander.opts() as {
|
|
|
|
noAuth: boolean;
|
|
|
|
readonly allowHttp: boolean;
|
|
|
|
readonly host: string;
|
|
|
|
readonly port: number;
|
2019-04-18 05:18:35 +07:00
|
|
|
readonly disableTelemetry: boolean;
|
2019-03-27 03:21:03 +07:00
|
|
|
|
2019-04-04 05:07:47 +07:00
|
|
|
readonly userDataDir?: string;
|
|
|
|
readonly extensionsDir?: string;
|
|
|
|
|
2019-03-27 03:21:03 +07:00
|
|
|
readonly dataDir?: string;
|
|
|
|
readonly password?: string;
|
|
|
|
readonly open?: boolean;
|
|
|
|
readonly cert?: string;
|
|
|
|
readonly certKey?: string;
|
|
|
|
|
2019-04-18 04:30:50 +07:00
|
|
|
readonly installExtension?: string;
|
|
|
|
|
2019-03-27 03:21:03 +07:00
|
|
|
readonly bootstrapFork?: string;
|
|
|
|
readonly extraArgs?: string;
|
2019-01-16 01:36:09 +07:00
|
|
|
};
|
|
|
|
|
2019-04-18 05:18:35 +07:00
|
|
|
if (options.disableTelemetry) {
|
|
|
|
process.env.DISABLE_TELEMETRY = "true";
|
|
|
|
}
|
|
|
|
|
2019-03-27 03:21:03 +07:00
|
|
|
// Commander has an exception for `--no` prefixes. Here we'll adjust that.
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
const noAuthValue = (commander as any).auth;
|
|
|
|
options.noAuth = !noAuthValue;
|
2019-03-27 01:01:25 +07:00
|
|
|
|
2019-04-04 05:07:47 +07:00
|
|
|
const dataDir = path.resolve(options.userDataDir || options.dataDir || path.join(dataHome, "code-server"));
|
|
|
|
const extensionsDir = options.extensionsDir ? path.resolve(options.extensionsDir) : path.resolve(dataDir, "extensions");
|
2019-03-27 03:21:03 +07:00
|
|
|
const workingDir = path.resolve(args[0] || process.cwd());
|
2019-04-16 07:48:12 +07:00
|
|
|
const dependenciesDir = path.join(os.tmpdir(), "code-server/dependencies");
|
2019-02-06 00:15:20 +07:00
|
|
|
|
2019-03-27 03:21:03 +07:00
|
|
|
if (!fs.existsSync(dataDir)) {
|
|
|
|
const oldDataDir = path.resolve(path.join(os.homedir(), ".code-server"));
|
|
|
|
if (fs.existsSync(oldDataDir)) {
|
|
|
|
await fse.move(oldDataDir, dataDir);
|
|
|
|
logger.info(`Moved data directory from ${oldDataDir} to ${dataDir}`);
|
|
|
|
}
|
|
|
|
}
|
2019-01-16 01:36:09 +07:00
|
|
|
|
2019-03-27 03:21:03 +07:00
|
|
|
await Promise.all([
|
|
|
|
fse.mkdirp(cacheHome),
|
|
|
|
fse.mkdirp(dataDir),
|
2019-04-04 05:07:47 +07:00
|
|
|
fse.mkdirp(extensionsDir),
|
2019-03-27 03:21:03 +07:00
|
|
|
fse.mkdirp(workingDir),
|
2019-04-16 07:48:12 +07:00
|
|
|
fse.mkdirp(dependenciesDir),
|
2019-03-27 03:21:03 +07:00
|
|
|
]);
|
|
|
|
|
2019-04-16 07:48:12 +07:00
|
|
|
const unpackExecutable = (binaryName: string): void => {
|
|
|
|
const memFile = path.join(isCli ? buildDir! : path.join(__dirname, ".."), "build/dependencies", binaryName);
|
|
|
|
const diskFile = path.join(dependenciesDir, binaryName);
|
|
|
|
if (!fse.existsSync(diskFile)) {
|
|
|
|
fse.writeFileSync(diskFile, fse.readFileSync(memFile));
|
|
|
|
}
|
|
|
|
fse.chmodSync(diskFile, "755");
|
|
|
|
};
|
|
|
|
|
|
|
|
unpackExecutable("rg");
|
|
|
|
// tslint:disable-next-line no-any
|
|
|
|
(<any>global).RIPGREP_LOCATION = path.join(dependenciesDir, "rg");
|
|
|
|
|
2019-03-27 03:21:03 +07:00
|
|
|
const builtInExtensionsDir = path.resolve(buildDir || path.join(__dirname, ".."), "build/extensions");
|
|
|
|
if (options.bootstrapFork) {
|
|
|
|
const modulePath = options.bootstrapFork;
|
|
|
|
if (!modulePath) {
|
|
|
|
logger.error("No module path specified to fork!");
|
|
|
|
process.exit(1);
|
2019-03-12 23:12:50 +07:00
|
|
|
}
|
|
|
|
|
2019-04-18 04:30:50 +07:00
|
|
|
process.argv = [
|
|
|
|
process.argv[0],
|
|
|
|
process.argv[1],
|
|
|
|
...(options.extraArgs ? JSON.parse(options.extraArgs) : []),
|
|
|
|
];
|
2019-01-19 04:46:40 +07:00
|
|
|
|
2019-04-16 07:48:12 +07:00
|
|
|
return requireModule(modulePath, builtInExtensionsDir);
|
2019-03-27 03:21:03 +07:00
|
|
|
}
|
2019-01-19 04:46:40 +07:00
|
|
|
|
2019-03-27 03:21:03 +07:00
|
|
|
const logDir = path.join(cacheHome, "code-server/logs", new Date().toISOString().replace(/[-:.TZ]/g, ""));
|
|
|
|
process.env.VSCODE_LOGS = logDir;
|
2019-02-22 00:55:42 +07:00
|
|
|
|
2019-03-27 03:21:03 +07:00
|
|
|
const certPath = options.cert ? path.resolve(options.cert) : undefined;
|
|
|
|
const certKeyPath = options.certKey ? path.resolve(options.certKey) : undefined;
|
2019-02-22 00:55:42 +07:00
|
|
|
|
2019-03-27 03:21:03 +07:00
|
|
|
if (certPath && !certKeyPath) {
|
|
|
|
logger.error("'--cert-key' flag is required when specifying a certificate!");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2019-01-29 00:14:06 +07:00
|
|
|
|
2019-03-27 03:21:03 +07:00
|
|
|
if (!certPath && certKeyPath) {
|
|
|
|
logger.error("'--cert' flag is required when specifying certificate key!");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2019-02-22 00:55:42 +07:00
|
|
|
|
2019-03-27 03:21:03 +07:00
|
|
|
let certData: Buffer | undefined;
|
|
|
|
let certKeyData: Buffer | undefined;
|
|
|
|
|
|
|
|
if (typeof certPath !== "undefined" && typeof certKeyPath !== "undefined") {
|
|
|
|
try {
|
|
|
|
certData = fs.readFileSync(certPath);
|
|
|
|
} catch (ex) {
|
|
|
|
logger.error(`Failed to read certificate: ${ex.message}`);
|
2019-02-22 00:55:42 +07:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2019-03-27 03:21:03 +07:00
|
|
|
try {
|
|
|
|
certKeyData = fs.readFileSync(certKeyPath);
|
|
|
|
} catch (ex) {
|
|
|
|
logger.error(`Failed to read certificate key: ${ex.message}`);
|
2019-02-22 00:55:42 +07:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2019-03-27 03:21:03 +07:00
|
|
|
}
|
2019-02-22 00:55:42 +07:00
|
|
|
|
2019-03-27 03:21:03 +07:00
|
|
|
logger.info(`\u001B[1mcode-server ${process.env.VERSION ? `v${process.env.VERSION}` : "development"}`);
|
2019-04-04 05:07:47 +07:00
|
|
|
|
|
|
|
if (options.dataDir) {
|
|
|
|
logger.warn('"--data-dir" is deprecated. Use "--user-data-dir" instead.');
|
|
|
|
}
|
|
|
|
|
2019-04-18 04:30:50 +07:00
|
|
|
if (options.installExtension) {
|
|
|
|
const fork = forkModule("vs/code/node/cli", [
|
|
|
|
"--user-data-dir", dataDir,
|
|
|
|
"--builtin-extensions-dir", builtInExtensionsDir,
|
|
|
|
"--extensions-dir", extensionsDir,
|
|
|
|
"--install-extension", options.installExtension,
|
|
|
|
], {
|
|
|
|
env: {
|
|
|
|
VSCODE_ALLOW_IO: "true",
|
|
|
|
VSCODE_LOGS: process.env.VSCODE_LOGS,
|
|
|
|
},
|
|
|
|
}, dataDir);
|
|
|
|
|
|
|
|
fork.stdout.on("data", (d: Buffer) => d.toString().split("\n").forEach((l) => logger.info(l)));
|
|
|
|
fork.stderr.on("data", (d: Buffer) => d.toString().split("\n").forEach((l) => logger.error(l)));
|
|
|
|
fork.on("exit", () => process.exit());
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-27 03:21:03 +07:00
|
|
|
// TODO: fill in appropriate doc url
|
|
|
|
logger.info("Additional documentation: http://github.com/codercom/code-server");
|
2019-04-04 05:07:47 +07:00
|
|
|
logger.info("Initializing", field("data-dir", dataDir), field("extensions-dir", extensionsDir), field("working-dir", workingDir), field("log-dir", logDir));
|
|
|
|
const sharedProcess = new SharedProcess(dataDir, extensionsDir, builtInExtensionsDir);
|
2019-03-27 03:21:03 +07:00
|
|
|
const sendSharedProcessReady = (socket: WebSocket): void => {
|
2019-04-03 05:44:28 +07:00
|
|
|
const active = new SharedProcessActive();
|
2019-03-27 03:21:03 +07:00
|
|
|
active.setSocketPath(sharedProcess.socketPath);
|
|
|
|
active.setLogPath(logDir);
|
|
|
|
const serverMessage = new ServerMessage();
|
|
|
|
serverMessage.setSharedProcessActive(active);
|
|
|
|
socket.send(serverMessage.serializeBinary());
|
|
|
|
};
|
|
|
|
sharedProcess.onState((event) => {
|
|
|
|
if (event.state === SharedProcessState.Ready) {
|
|
|
|
app.wss.clients.forEach((c) => sendSharedProcessReady(c));
|
2019-02-22 00:55:42 +07:00
|
|
|
}
|
2019-03-27 03:21:03 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
let password = options.password;
|
|
|
|
if (!password) {
|
|
|
|
// Generate a random password with a length of 24.
|
|
|
|
const buffer = Buffer.alloc(12);
|
|
|
|
randomFillSync(buffer);
|
|
|
|
password = buffer.toString("hex");
|
|
|
|
}
|
2019-02-22 00:55:42 +07:00
|
|
|
|
2019-03-27 03:21:03 +07:00
|
|
|
const hasCustomHttps = certData && certKeyData;
|
|
|
|
const app = await createApp({
|
|
|
|
allowHttp: options.allowHttp,
|
|
|
|
bypassAuth: options.noAuth,
|
|
|
|
registerMiddleware: (app): void => {
|
|
|
|
// If we're not running from the binary and we aren't serving the static
|
|
|
|
// pre-built version, use webpack to serve the web files.
|
|
|
|
if (!isCli && !serveStatic) {
|
|
|
|
const webpackConfig = require(path.resolve(__dirname, "..", "..", "web", "webpack.config.js"));
|
|
|
|
const compiler = require("webpack")(webpackConfig);
|
|
|
|
app.use(require("webpack-dev-middleware")(compiler, {
|
2019-04-12 02:55:06 +07:00
|
|
|
logger: {
|
|
|
|
trace: (m: string): void => logger.trace("webpack", field("message", m)),
|
|
|
|
debug: (m: string): void => logger.debug("webpack", field("message", m)),
|
|
|
|
info: (m: string): void => logger.info("webpack", field("message", m)),
|
|
|
|
warn: (m: string): void => logger.warn("webpack", field("message", m)),
|
|
|
|
error: (m: string): void => logger.error("webpack", field("message", m)),
|
|
|
|
},
|
2019-03-27 03:21:03 +07:00
|
|
|
publicPath: webpackConfig.output.publicPath,
|
|
|
|
stats: webpackConfig.stats,
|
|
|
|
}));
|
|
|
|
app.use(require("webpack-hot-middleware")(compiler));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
serverOptions: {
|
2019-04-04 05:07:47 +07:00
|
|
|
extensionsDirectory: extensionsDir,
|
2019-03-27 03:21:03 +07:00
|
|
|
builtInExtensionsDirectory: builtInExtensionsDir,
|
|
|
|
dataDirectory: dataDir,
|
|
|
|
workingDirectory: workingDir,
|
|
|
|
cacheDirectory: cacheHome,
|
|
|
|
fork: (modulePath: string, args?: string[], options?: ForkOptions): ChildProcess => {
|
|
|
|
if (options && options.env && options.env.AMD_ENTRYPOINT) {
|
|
|
|
return forkModule(options.env.AMD_ENTRYPOINT, args, options, dataDir);
|
|
|
|
}
|
2019-03-05 00:46:34 +07:00
|
|
|
|
2019-04-16 07:48:12 +07:00
|
|
|
return fork(modulePath, args, options);
|
2019-02-23 04:56:29 +07:00
|
|
|
},
|
2019-03-27 03:21:03 +07:00
|
|
|
},
|
|
|
|
password,
|
|
|
|
httpsOptions: hasCustomHttps ? {
|
|
|
|
key: certKeyData,
|
|
|
|
cert: certData,
|
|
|
|
} : undefined,
|
|
|
|
});
|
|
|
|
|
|
|
|
logger.info("Starting webserver...", field("host", options.host), field("port", options.port));
|
|
|
|
app.server.listen(options.port, options.host);
|
|
|
|
let clientId = 1;
|
|
|
|
app.wss.on("connection", (ws, req) => {
|
|
|
|
const id = clientId++;
|
|
|
|
|
|
|
|
if (sharedProcess.state === SharedProcessState.Ready) {
|
|
|
|
sendSharedProcessReady(ws);
|
|
|
|
}
|
2019-01-19 06:08:44 +07:00
|
|
|
|
2019-03-27 03:21:03 +07:00
|
|
|
logger.info(`WebSocket opened \u001B[0m${req.url}`, field("client", id), field("ip", req.socket.remoteAddress));
|
2019-01-16 01:36:09 +07:00
|
|
|
|
2019-03-27 03:21:03 +07:00
|
|
|
ws.on("close", (code) => {
|
|
|
|
logger.info(`WebSocket closed \u001B[0m${req.url}`, field("client", id), field("code", code));
|
2019-01-16 01:36:09 +07:00
|
|
|
});
|
2019-03-27 03:21:03 +07:00
|
|
|
});
|
2019-04-04 03:50:52 +07:00
|
|
|
app.wss.on("error", (err: NodeJS.ErrnoException) => {
|
|
|
|
if (err.code === "EADDRINUSE") {
|
|
|
|
logger.error(`Port ${bold(options.port)} is in use. Please free up port ${options.port} or specify a different port with the -p flag`);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
});
|
2019-03-27 03:21:03 +07:00
|
|
|
if (!options.certKey && !options.cert) {
|
|
|
|
logger.warn("No certificate specified. \u001B[1mThis could be insecure.");
|
|
|
|
// TODO: fill in appropriate doc url
|
2019-03-27 21:36:32 +07:00
|
|
|
logger.warn("Documentation on securing your setup: https://github.com/codercom/code-server/blob/master/doc/security/ssl.md");
|
2019-03-27 03:21:03 +07:00
|
|
|
}
|
2019-03-08 00:23:54 +07:00
|
|
|
|
2019-03-27 03:21:03 +07:00
|
|
|
if (!options.noAuth) {
|
2019-01-16 01:36:09 +07:00
|
|
|
logger.info(" ");
|
2019-03-27 03:21:03 +07:00
|
|
|
logger.info(`Password:\u001B[1m ${password}`);
|
|
|
|
} else {
|
|
|
|
logger.warn("Launched without authentication.");
|
|
|
|
}
|
2019-03-08 00:23:54 +07:00
|
|
|
|
2019-03-27 03:21:03 +07:00
|
|
|
const url = `http://localhost:${options.port}/`;
|
|
|
|
logger.info(" ");
|
|
|
|
logger.info("Started (click the link below to open):");
|
|
|
|
logger.info(url);
|
|
|
|
logger.info(" ");
|
|
|
|
|
|
|
|
if (options.open) {
|
|
|
|
try {
|
|
|
|
await opn(url);
|
|
|
|
} catch (e) {
|
|
|
|
logger.warn("Url couldn't be opened automatically.", field("url", url), field("exception", e));
|
2019-03-08 00:23:54 +07:00
|
|
|
}
|
2019-01-16 01:36:09 +07:00
|
|
|
}
|
2019-03-27 03:21:03 +07:00
|
|
|
})().catch((ex) => {
|
|
|
|
logger.error(ex);
|
|
|
|
});
|