2019-01-30 22:40:01 +01:00
|
|
|
import { field, logger } from "@coder/logger";
|
2019-03-12 17:12:50 +01:00
|
|
|
import { mkdirP } from "@coder/protocol";
|
2019-01-19 00:08:44 +01:00
|
|
|
import { ServerMessage, SharedProcessActiveMessage } from "@coder/protocol/src/proto";
|
2019-01-15 19:36:09 +01:00
|
|
|
import { Command, flags } from "@oclif/command";
|
2019-02-22 22:56:29 +01:00
|
|
|
import { fork, ForkOptions, ChildProcess } from "child_process";
|
2019-03-07 01:25:44 +01:00
|
|
|
import { randomFillSync } from "crypto";
|
2019-01-15 19:36:09 +01:00
|
|
|
import * as fs from "fs";
|
2019-03-12 17:12:50 +01:00
|
|
|
import * as os from "os";
|
2019-01-15 19:36:09 +01:00
|
|
|
import * as path from "path";
|
2019-01-19 00:08:44 +01:00
|
|
|
import * as WebSocket from "ws";
|
2019-01-18 22:46:40 +01:00
|
|
|
import { createApp } from "./server";
|
2019-02-22 22:56:29 +01:00
|
|
|
import { requireModule, requireFork, forkModule } from "./vscode/bootstrapFork";
|
2019-01-22 19:27:59 +01:00
|
|
|
import { SharedProcess, SharedProcessState } from "./vscode/sharedProcess";
|
2019-02-06 01:08:48 +01:00
|
|
|
import { setup as setupNativeModules } from "./modules";
|
|
|
|
import { fillFs } from "./fill";
|
2019-03-09 18:11:30 +01:00
|
|
|
import { isCli, serveStatic, buildDir, dataHome, cacheHome } from "./constants";
|
2019-03-07 18:23:54 +01:00
|
|
|
import opn = require("opn");
|
2019-01-15 19:36:09 +01:00
|
|
|
|
|
|
|
export class Entry extends Command {
|
|
|
|
public static description = "Start your own self-hosted browser-accessible VS Code";
|
|
|
|
public static flags = {
|
|
|
|
cert: flags.string(),
|
|
|
|
"cert-key": flags.string(),
|
|
|
|
"data-dir": flags.string({ char: "d" }),
|
|
|
|
help: flags.help(),
|
|
|
|
host: flags.string({ char: "h", default: "0.0.0.0" }),
|
|
|
|
open: flags.boolean({ char: "o", description: "Open in browser on startup" }),
|
2019-03-01 23:55:31 +01:00
|
|
|
port: flags.integer({ char: "p", default: 8443, description: "Port to bind on" }),
|
2019-01-15 19:36:09 +01:00
|
|
|
version: flags.version({ char: "v" }),
|
2019-02-26 23:03:42 +01:00
|
|
|
"no-auth": flags.boolean({ default: false }),
|
2019-03-04 08:45:35 +01:00
|
|
|
"allow-http": flags.boolean({ default: false }),
|
2019-03-06 09:08:43 +01:00
|
|
|
password: flags.string(),
|
2019-01-18 22:46:40 +01:00
|
|
|
|
|
|
|
// Dev flags
|
|
|
|
"bootstrap-fork": flags.string({ hidden: true }),
|
2019-02-21 18:55:42 +01:00
|
|
|
"fork": flags.string({ hidden: true }),
|
|
|
|
|
2019-02-19 17:17:03 +01:00
|
|
|
args: flags.string({ hidden: true }),
|
2019-01-15 19:36:09 +01:00
|
|
|
};
|
|
|
|
public static args = [{
|
|
|
|
name: "workdir",
|
|
|
|
description: "Specify working dir",
|
2019-01-18 22:46:40 +01:00
|
|
|
default: (): string => process.cwd(),
|
2019-01-15 19:36:09 +01:00
|
|
|
}];
|
|
|
|
|
|
|
|
public async run(): Promise<void> {
|
2019-02-07 18:47:00 +01:00
|
|
|
if (isCli) {
|
2019-02-05 18:15:20 +01:00
|
|
|
fillFs();
|
|
|
|
}
|
|
|
|
|
2019-01-15 19:36:09 +01:00
|
|
|
const { args, flags } = this.parse(Entry);
|
2019-03-09 18:11:30 +01:00
|
|
|
const dataDir = path.resolve(flags["data-dir"] || path.join(dataHome, "code-server"));
|
2019-03-07 18:24:25 +01:00
|
|
|
const workingDir = path.resolve(args["workdir"]);
|
2019-01-15 19:36:09 +01:00
|
|
|
|
2019-03-12 17:12:50 +01:00
|
|
|
if (!fs.existsSync(dataDir)) {
|
|
|
|
const oldDataDir = path.resolve(path.join(os.homedir(), ".code-server"));
|
|
|
|
if (fs.existsSync(oldDataDir)) {
|
|
|
|
fs.renameSync(oldDataDir, dataDir);
|
|
|
|
logger.info(`Moved data directory from ${oldDataDir} to ${dataDir}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
mkdirP(cacheHome),
|
|
|
|
mkdirP(dataDir),
|
|
|
|
mkdirP(workingDir),
|
|
|
|
]);
|
|
|
|
|
2019-02-22 02:32:08 +01:00
|
|
|
setupNativeModules(dataDir);
|
2019-03-07 18:24:25 +01:00
|
|
|
const builtInExtensionsDir = path.resolve(buildDir || path.join(__dirname, ".."), "build/extensions");
|
2019-01-18 22:46:40 +01:00
|
|
|
if (flags["bootstrap-fork"]) {
|
|
|
|
const modulePath = flags["bootstrap-fork"];
|
|
|
|
if (!modulePath) {
|
|
|
|
logger.error("No module path specified to fork!");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2019-02-19 17:17:03 +01:00
|
|
|
((flags.args ? JSON.parse(flags.args) : []) as string[]).forEach((arg, i) => {
|
|
|
|
// [0] contains the binary running the script (`node` for example) and
|
|
|
|
// [1] contains the script name, so the arguments come after that.
|
|
|
|
process.argv[i + 2] = arg;
|
|
|
|
});
|
|
|
|
|
2019-02-22 02:32:08 +01:00
|
|
|
return requireModule(modulePath, dataDir, builtInExtensionsDir);
|
2019-01-18 22:46:40 +01:00
|
|
|
}
|
|
|
|
|
2019-02-21 18:55:42 +01:00
|
|
|
if (flags["fork"]) {
|
|
|
|
const modulePath = flags["fork"];
|
|
|
|
|
|
|
|
return requireFork(modulePath, JSON.parse(flags.args!), builtInExtensionsDir);
|
|
|
|
}
|
|
|
|
|
2019-03-09 18:11:30 +01:00
|
|
|
const logDir = path.join(cacheHome, "code-server/logs", new Date().toISOString().replace(/[-:.TZ]/g, ""));
|
2019-01-28 18:14:06 +01:00
|
|
|
process.env.VSCODE_LOGS = logDir;
|
|
|
|
|
2019-03-07 18:24:25 +01:00
|
|
|
const certPath = flags.cert ? path.resolve(flags.cert) : undefined;
|
|
|
|
const certKeyPath = flags["cert-key"] ? path.resolve(flags["cert-key"]) : undefined;
|
2019-02-21 18:55:42 +01:00
|
|
|
|
|
|
|
if (certPath && !certKeyPath) {
|
|
|
|
logger.error("'--cert-key' flag is required when specifying a certificate!");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!certPath && certKeyPath) {
|
|
|
|
logger.error("'--cert' flag is required when specifying certificate key!");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
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}`);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
certKeyData = fs.readFileSync(certKeyPath);
|
|
|
|
} catch (ex) {
|
|
|
|
logger.error(`Failed to read certificate key: ${ex.message}`);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 01:15:52 +01:00
|
|
|
logger.info(`\u001B[1mcode-server ${process.env.VERSION ? `v${process.env.VERSION}` : "development"}`);
|
2019-01-15 19:36:09 +01:00
|
|
|
// TODO: fill in appropriate doc url
|
2019-02-28 21:04:19 +01:00
|
|
|
logger.info("Additional documentation: http://github.com/codercom/code-server");
|
2019-01-28 18:14:06 +01:00
|
|
|
logger.info("Initializing", field("data-dir", dataDir), field("working-dir", workingDir), field("log-dir", logDir));
|
2019-02-05 18:15:20 +01:00
|
|
|
const sharedProcess = new SharedProcess(dataDir, builtInExtensionsDir);
|
2019-01-22 19:44:03 +01:00
|
|
|
const sendSharedProcessReady = (socket: WebSocket): void => {
|
2019-01-19 00:08:44 +01:00
|
|
|
const active = new SharedProcessActiveMessage();
|
|
|
|
active.setSocketPath(sharedProcess.socketPath);
|
2019-01-28 18:14:06 +01:00
|
|
|
active.setLogPath(logDir);
|
2019-01-19 00:08:44 +01:00
|
|
|
const serverMessage = new ServerMessage();
|
|
|
|
serverMessage.setSharedProcessActive(active);
|
|
|
|
socket.send(serverMessage.serializeBinary());
|
|
|
|
};
|
|
|
|
sharedProcess.onState((event) => {
|
2019-02-07 01:11:31 +01:00
|
|
|
if (event.state === SharedProcessState.Ready) {
|
2019-01-19 00:08:44 +01:00
|
|
|
app.wss.clients.forEach((c) => sendSharedProcessReady(c));
|
|
|
|
}
|
2019-01-18 22:46:40 +01:00
|
|
|
});
|
2019-01-15 19:36:09 +01:00
|
|
|
|
2019-03-07 18:24:25 +01:00
|
|
|
let password = flags.password;
|
2019-03-06 09:08:43 +01:00
|
|
|
if (!password) {
|
2019-03-07 18:24:25 +01:00
|
|
|
// Generate a random password with a length of 24.
|
2019-03-07 01:25:44 +01:00
|
|
|
const buffer = Buffer.alloc(12);
|
|
|
|
randomFillSync(buffer);
|
|
|
|
password = buffer.toString("hex");
|
2019-03-04 18:46:34 +01:00
|
|
|
}
|
|
|
|
|
2019-02-21 18:55:42 +01:00
|
|
|
const hasCustomHttps = certData && certKeyData;
|
2019-02-26 23:03:42 +01:00
|
|
|
const app = await createApp({
|
2019-03-04 08:45:35 +01:00
|
|
|
allowHttp: flags["allow-http"],
|
2019-02-26 23:03:42 +01:00
|
|
|
bypassAuth: flags["no-auth"],
|
|
|
|
registerMiddleware: (app): void => {
|
|
|
|
app.use((req, res, next) => {
|
|
|
|
res.on("finish", () => {
|
|
|
|
logger.trace(`\u001B[1m${req.method} ${res.statusCode} \u001B[0m${req.url}`, field("host", req.hostname), field("ip", req.ip));
|
|
|
|
});
|
|
|
|
|
|
|
|
next();
|
2019-01-15 19:36:09 +01:00
|
|
|
});
|
2019-02-26 23:03:42 +01:00
|
|
|
// 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) {
|
2019-03-07 18:24:25 +01:00
|
|
|
const webpackConfig = require(path.resolve(__dirname, "..", "..", "web", "webpack.config.js"));
|
2019-02-26 23:03:42 +01:00
|
|
|
const compiler = require("webpack")(webpackConfig);
|
|
|
|
app.use(require("webpack-dev-middleware")(compiler, {
|
|
|
|
logger,
|
|
|
|
publicPath: webpackConfig.output.publicPath,
|
|
|
|
stats: webpackConfig.stats,
|
|
|
|
}));
|
|
|
|
app.use(require("webpack-hot-middleware")(compiler));
|
2019-02-22 22:56:29 +01:00
|
|
|
}
|
|
|
|
},
|
2019-02-26 23:03:42 +01:00
|
|
|
serverOptions: {
|
|
|
|
builtInExtensionsDirectory: builtInExtensionsDir,
|
|
|
|
dataDirectory: dataDir,
|
|
|
|
workingDirectory: workingDir,
|
2019-03-12 17:12:50 +01:00
|
|
|
cacheDirectory: cacheHome,
|
2019-02-26 23:03:42 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fork(modulePath, args, options);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
password,
|
|
|
|
httpsOptions: hasCustomHttps ? {
|
|
|
|
key: certKeyData,
|
|
|
|
cert: certData,
|
|
|
|
} : undefined,
|
|
|
|
});
|
2019-01-15 19:36:09 +01:00
|
|
|
|
2019-01-18 22:46:40 +01:00
|
|
|
logger.info("Starting webserver...", field("host", flags.host), field("port", flags.port));
|
2019-01-15 19:36:09 +01:00
|
|
|
app.server.listen(flags.port, flags.host);
|
|
|
|
let clientId = 1;
|
|
|
|
app.wss.on("connection", (ws, req) => {
|
|
|
|
const id = clientId++;
|
2019-01-18 22:46:40 +01:00
|
|
|
|
2019-01-22 18:48:01 +01:00
|
|
|
if (sharedProcess.state === SharedProcessState.Ready) {
|
|
|
|
sendSharedProcessReady(ws);
|
|
|
|
}
|
2019-01-19 00:08:44 +01:00
|
|
|
|
|
|
|
logger.info(`WebSocket opened \u001B[0m${req.url}`, field("client", id), field("ip", req.socket.remoteAddress));
|
2019-01-15 19:36:09 +01:00
|
|
|
|
|
|
|
ws.on("close", (code) => {
|
|
|
|
logger.info(`WebSocket closed \u001B[0m${req.url}`, field("client", id), field("code", code));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!flags["cert-key"] && !flags.cert) {
|
|
|
|
logger.warn("No certificate specified. \u001B[1mThis could be insecure.");
|
|
|
|
// TODO: fill in appropriate doc url
|
|
|
|
logger.warn("Documentation on securing your setup: https://coder.com/docs");
|
|
|
|
}
|
|
|
|
|
2019-02-26 23:03:42 +01:00
|
|
|
if (!flags["no-auth"]) {
|
|
|
|
logger.info(" ");
|
|
|
|
logger.info(`Password:\u001B[1m ${password}`);
|
|
|
|
} else {
|
|
|
|
logger.warn("Launched without authentication.");
|
|
|
|
}
|
2019-03-07 18:23:54 +01:00
|
|
|
|
|
|
|
const url = `http://localhost:${flags.port}/`;
|
2019-01-15 19:36:09 +01:00
|
|
|
logger.info(" ");
|
|
|
|
logger.info("Started (click the link below to open):");
|
2019-03-07 18:23:54 +01:00
|
|
|
logger.info(url);
|
2019-01-15 19:36:09 +01:00
|
|
|
logger.info(" ");
|
2019-03-07 18:23:54 +01:00
|
|
|
|
2019-03-11 23:50:35 +01:00
|
|
|
if (flags.open) {
|
2019-03-07 18:23:54 +01:00
|
|
|
try {
|
|
|
|
await opn(url);
|
|
|
|
} catch (e) {
|
|
|
|
logger.warn("Url couldn't be opened automatically.", field("url", url), field("exception", e));
|
|
|
|
}
|
|
|
|
}
|
2019-01-15 19:36:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Entry.run(undefined, {
|
2019-02-07 18:47:00 +01:00
|
|
|
root: buildDir || __dirname,
|
2019-03-11 23:38:11 +01:00
|
|
|
version: process.env.VERSION || "development",
|
2019-01-15 19:36:09 +01:00
|
|
|
//@ts-ignore
|
|
|
|
}).catch(require("@oclif/errors/handle"));
|