2019-01-16 01:36:09 +07:00
|
|
|
import * as os from "os";
|
2019-01-19 06:08:44 +07:00
|
|
|
import * as cp from "child_process";
|
2019-01-19 04:46:40 +07:00
|
|
|
import * as path from "path";
|
|
|
|
import { mkdir } from "fs";
|
|
|
|
import { promisify } from "util";
|
2019-01-13 02:44:29 +07:00
|
|
|
import { TextDecoder } from "text-encoding";
|
2019-01-19 04:46:40 +07:00
|
|
|
import { logger, field } from "@coder/logger";
|
2019-01-19 06:08:44 +07:00
|
|
|
import { ClientMessage, WorkingInitMessage, ServerMessage, NewSessionMessage } from "../proto";
|
2019-01-12 02:33:44 +07:00
|
|
|
import { evaluate } from "./evaluate";
|
|
|
|
import { ReadWriteConnection } from "../common/connection";
|
2019-01-19 04:46:40 +07:00
|
|
|
import { Process, handleNewSession, handleNewConnection } from "./command";
|
|
|
|
import * as net from "net";
|
2019-01-12 02:33:44 +07:00
|
|
|
|
2019-01-16 01:36:09 +07:00
|
|
|
export interface ServerOptions {
|
|
|
|
readonly workingDirectory: string;
|
|
|
|
readonly dataDirectory: string;
|
2019-01-19 06:08:44 +07:00
|
|
|
|
|
|
|
forkProvider?(message: NewSessionMessage): cp.ChildProcess;
|
2019-01-16 01:36:09 +07:00
|
|
|
}
|
|
|
|
|
2019-01-12 02:33:44 +07:00
|
|
|
export class Server {
|
|
|
|
|
2019-01-19 04:46:40 +07:00
|
|
|
private readonly sessions: Map<number, Process> = new Map();
|
|
|
|
private readonly connections: Map<number, net.Socket> = new Map();
|
2019-01-13 02:44:29 +07:00
|
|
|
|
2019-01-12 02:33:44 +07:00
|
|
|
public constructor(
|
|
|
|
private readonly connection: ReadWriteConnection,
|
2019-01-19 06:08:44 +07:00
|
|
|
private readonly options?: ServerOptions,
|
2019-01-12 02:33:44 +07:00
|
|
|
) {
|
|
|
|
connection.onMessage((data) => {
|
|
|
|
try {
|
|
|
|
this.handleMessage(ClientMessage.deserializeBinary(data));
|
|
|
|
} catch (ex) {
|
|
|
|
logger.error("Failed to handle client message", field("length", data.byteLength), field("exception", ex));
|
|
|
|
}
|
|
|
|
});
|
2019-01-16 01:36:09 +07:00
|
|
|
|
|
|
|
if (!options) {
|
|
|
|
logger.warn("No server options provided. InitMessage will not be sent.");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-19 04:46:40 +07:00
|
|
|
// Ensure the data directory exists.
|
|
|
|
const mkdirP = async (path: string): Promise<void> => {
|
|
|
|
const split = path.replace(/^\/*|\/*$/g, "").split("/");
|
|
|
|
let dir = "";
|
|
|
|
while (split.length > 0) {
|
|
|
|
dir += "/" + split.shift();
|
|
|
|
try {
|
|
|
|
await promisify(mkdir)(dir);
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code !== "EEXIST") {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Promise.all([ mkdirP(path.join(options.dataDirectory, "User", "workspaceStorage")) ]).then(() => {
|
|
|
|
logger.info("Created data directory");
|
|
|
|
}).catch((error) => {
|
|
|
|
logger.error(error.message, field("error", error));
|
|
|
|
});
|
|
|
|
|
|
|
|
const initMsg = new WorkingInitMessage();
|
2019-01-16 01:36:09 +07:00
|
|
|
initMsg.setDataDirectory(options.dataDirectory);
|
|
|
|
initMsg.setWorkingDirectory(options.workingDirectory);
|
|
|
|
initMsg.setHomeDirectory(os.homedir());
|
|
|
|
initMsg.setTmpDirectory(os.tmpdir());
|
|
|
|
const platform = os.platform();
|
2019-01-19 04:46:40 +07:00
|
|
|
let operatingSystem: WorkingInitMessage.OperatingSystem;
|
2019-01-16 01:36:09 +07:00
|
|
|
switch (platform) {
|
|
|
|
case "win32":
|
2019-01-19 04:46:40 +07:00
|
|
|
operatingSystem = WorkingInitMessage.OperatingSystem.WINDOWS;
|
2019-01-16 01:36:09 +07:00
|
|
|
break;
|
|
|
|
case "linux":
|
2019-01-19 04:46:40 +07:00
|
|
|
operatingSystem = WorkingInitMessage.OperatingSystem.LINUX;
|
2019-01-16 01:36:09 +07:00
|
|
|
break;
|
|
|
|
case "darwin":
|
2019-01-19 04:46:40 +07:00
|
|
|
operatingSystem = WorkingInitMessage.OperatingSystem.MAC;
|
2019-01-16 01:36:09 +07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error(`unrecognized platform "${platform}"`);
|
|
|
|
}
|
|
|
|
initMsg.setOperatingSystem(operatingSystem);
|
|
|
|
const srvMsg = new ServerMessage();
|
|
|
|
srvMsg.setInit(initMsg);
|
|
|
|
connection.send(srvMsg.serializeBinary());
|
2019-01-12 02:33:44 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
private handleMessage(message: ClientMessage): void {
|
|
|
|
if (message.hasNewEval()) {
|
|
|
|
evaluate(this.connection, message.getNewEval()!);
|
2019-01-13 02:44:29 +07:00
|
|
|
} else if (message.hasNewSession()) {
|
2019-01-19 06:08:44 +07:00
|
|
|
const session = handleNewSession(this.connection, message.getNewSession()!, this.options, () => {
|
2019-01-19 04:46:40 +07:00
|
|
|
this.sessions.delete(message.getNewSession()!.getId());
|
2019-01-13 02:44:29 +07:00
|
|
|
});
|
|
|
|
this.sessions.set(message.getNewSession()!.getId(), session);
|
|
|
|
} else if (message.hasCloseSessionInput()) {
|
|
|
|
const s = this.getSession(message.getCloseSessionInput()!.getId());
|
|
|
|
if (!s || !s.stdin) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
s.stdin.end();
|
|
|
|
} else if (message.hasResizeSessionTty()) {
|
|
|
|
const s = this.getSession(message.getResizeSessionTty()!.getId());
|
|
|
|
if (!s || !s.resize) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const tty = message.getResizeSessionTty()!.getTtyDimensions()!;
|
|
|
|
s.resize(tty.getWidth(), tty.getHeight());
|
|
|
|
} else if (message.hasShutdownSession()) {
|
|
|
|
const s = this.getSession(message.getShutdownSession()!.getId());
|
|
|
|
if (!s) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
s.kill(message.getShutdownSession()!.getSignal());
|
|
|
|
} else if (message.hasWriteToSession()) {
|
|
|
|
const s = this.getSession(message.getWriteToSession()!.getId());
|
|
|
|
if (!s) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
s.write(new TextDecoder().decode(message.getWriteToSession()!.getData_asU8()));
|
2019-01-19 04:46:40 +07:00
|
|
|
} else if (message.hasNewConnection()) {
|
|
|
|
const socket = handleNewConnection(this.connection, message.getNewConnection()!, () => {
|
|
|
|
this.connections.delete(message.getNewConnection()!.getId());
|
|
|
|
});
|
|
|
|
this.connections.set(message.getNewConnection()!.getId(), socket);
|
|
|
|
} else if (message.hasConnectionOutput()) {
|
|
|
|
const c = this.getConnection(message.getConnectionOutput()!.getId());
|
|
|
|
if (!c) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
c.write(Buffer.from(message.getConnectionOutput()!.getData_asU8()));
|
|
|
|
} else if (message.hasConnectionClose()) {
|
|
|
|
const c = this.getConnection(message.getConnectionClose()!.getId());
|
|
|
|
if (!c) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
c.end();
|
2019-01-12 02:33:44 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 04:46:40 +07:00
|
|
|
private getConnection(id: number): net.Socket | undefined {
|
|
|
|
return this.connections.get(id);
|
|
|
|
}
|
|
|
|
|
2019-01-13 02:44:29 +07:00
|
|
|
private getSession(id: number): Process | undefined {
|
|
|
|
return this.sessions.get(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|