2019-06-28 06:34:33 +08:00
|
|
|
import * as fs from "fs";
|
|
|
|
import * as http from "http";
|
|
|
|
import * as net from "net";
|
|
|
|
import * as path from "path";
|
|
|
|
import * as util from "util";
|
|
|
|
import * as url from "url";
|
|
|
|
|
|
|
|
import { Emitter } from "vs/base/common/event";
|
2019-07-02 23:59:36 +08:00
|
|
|
import { sanitizeFilePath } from "vs/base/common/extpath";
|
2019-06-29 06:49:29 +08:00
|
|
|
import { getMediaMime } from "vs/base/common/mime";
|
|
|
|
import { extname } from "vs/base/common/path";
|
2019-07-02 23:34:03 +08:00
|
|
|
import { UriComponents, URI } from "vs/base/common/uri";
|
2019-06-29 06:37:23 +08:00
|
|
|
import { IPCServer, ClientConnectionEvent } from "vs/base/parts/ipc/common/ipc";
|
2019-07-03 04:26:40 +08:00
|
|
|
import { LogsDataCleaner } from "vs/code/electron-browser/sharedProcess/contrib/logsDataCleaner";
|
|
|
|
import { IEnvironmentService, ParsedArgs } from "vs/platform/environment/common/environment";
|
2019-06-29 06:37:23 +08:00
|
|
|
import { EnvironmentService } from "vs/platform/environment/node/environmentService";
|
|
|
|
import { InstantiationService } from "vs/platform/instantiation/common/instantiationService";
|
2019-07-03 04:26:40 +08:00
|
|
|
import { ServiceCollection } from "vs/platform/instantiation/common/serviceCollection";
|
2019-07-02 07:01:09 +08:00
|
|
|
import { getLogLevel, ILogService } from "vs/platform/log/common/log";
|
2019-06-29 06:37:23 +08:00
|
|
|
import { LogLevelSetterChannel } from "vs/platform/log/common/logIpc";
|
2019-07-02 03:18:30 +08:00
|
|
|
import { SpdLogService } from "vs/platform/log/node/spdlogService";
|
2019-07-02 03:00:11 +08:00
|
|
|
import { IProductConfiguration } from "vs/platform/product/common/product";
|
2019-07-03 05:55:54 +08:00
|
|
|
import product from "vs/platform/product/node/product";
|
2019-07-02 07:01:09 +08:00
|
|
|
import { ConnectionType, ConnectionTypeRequest } from "vs/platform/remote/common/remoteAgentConnection";
|
2019-06-29 06:37:23 +08:00
|
|
|
import { REMOTE_FILE_SYSTEM_CHANNEL_NAME } from "vs/platform/remote/common/remoteAgentFileSystemChannel";
|
2019-07-02 03:18:30 +08:00
|
|
|
import { RemoteExtensionLogFileName } from "vs/workbench/services/remote/common/remoteAgentService";
|
2019-07-02 03:00:11 +08:00
|
|
|
import { IWorkbenchConstructionOptions } from "vs/workbench/workbench.web.api";
|
2019-06-29 06:37:23 +08:00
|
|
|
|
2019-07-02 07:01:09 +08:00
|
|
|
import { Connection, ManagementConnection, ExtensionHostConnection } from "vs/server/connection";
|
2019-07-02 23:34:03 +08:00
|
|
|
import { ExtensionEnvironmentChannel, FileProviderChannel, getUriTransformer } from "vs/server/channel";
|
2019-07-02 07:01:09 +08:00
|
|
|
import { Protocol } from "vs/server/protocol";
|
2019-06-28 06:34:33 +08:00
|
|
|
|
2019-06-29 06:37:23 +08:00
|
|
|
export enum HttpCode {
|
2019-06-28 06:34:33 +08:00
|
|
|
Ok = 200,
|
|
|
|
NotFound = 404,
|
|
|
|
BadRequest = 400,
|
|
|
|
}
|
|
|
|
|
2019-07-02 03:00:11 +08:00
|
|
|
export interface Options {
|
|
|
|
WORKBENCH_WEB_CONGIGURATION: IWorkbenchConstructionOptions;
|
2019-07-02 23:34:03 +08:00
|
|
|
REMOTE_USER_DATA_URI: UriComponents | URI;
|
2019-07-02 03:00:11 +08:00
|
|
|
PRODUCT_CONFIGURATION: IProductConfiguration | null;
|
|
|
|
CONNECTION_AUTH_TOKEN: string;
|
|
|
|
}
|
|
|
|
|
2019-06-29 06:37:23 +08:00
|
|
|
export class HttpError extends Error {
|
2019-06-28 06:34:33 +08:00
|
|
|
public constructor(message: string, public readonly code: number) {
|
|
|
|
super(message);
|
|
|
|
// @ts-ignore
|
|
|
|
this.name = this.constructor.name;
|
|
|
|
Error.captureStackTrace(this, this.constructor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 05:15:41 +08:00
|
|
|
export abstract class Server {
|
|
|
|
// The underlying web server.
|
|
|
|
protected readonly server: http.Server;
|
2019-06-28 06:34:33 +08:00
|
|
|
|
|
|
|
public constructor() {
|
|
|
|
this.server = http.createServer(async (request, response): Promise<void> => {
|
|
|
|
try {
|
2019-07-03 05:15:41 +08:00
|
|
|
if (request.method !== "GET") {
|
|
|
|
throw new HttpError(
|
|
|
|
`Unsupported method ${request.method}`,
|
|
|
|
HttpCode.BadRequest,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const parsedUrl = url.parse(request.url || "", true);
|
|
|
|
const requestPath = parsedUrl.pathname || "/";
|
|
|
|
|
|
|
|
const [content, headers] = await this.handleRequest(request, parsedUrl, requestPath);
|
2019-06-28 06:34:33 +08:00
|
|
|
response.writeHead(HttpCode.Ok, {
|
|
|
|
"Cache-Control": "max-age=86400",
|
|
|
|
// TODO: ETag?
|
2019-06-29 06:49:29 +08:00
|
|
|
...headers,
|
2019-06-28 06:34:33 +08:00
|
|
|
});
|
|
|
|
response.end(content);
|
|
|
|
} catch (error) {
|
|
|
|
response.writeHead(typeof error.code === "number" ? error.code : 500);
|
|
|
|
response.end(error.message);
|
|
|
|
}
|
|
|
|
});
|
2019-07-03 05:15:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract handleRequest(
|
|
|
|
request: http.IncomingMessage,
|
|
|
|
parsedUrl: url.UrlWithParsedQuery,
|
|
|
|
requestPath: string,
|
|
|
|
): Promise<[string | Buffer, http.OutgoingHttpHeaders]>;
|
|
|
|
|
|
|
|
public listen(port: number): Promise<void> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.server.on("error", reject);
|
|
|
|
this.server.listen(port, resolve);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public get address(): string {
|
|
|
|
const address = this.server.address();
|
|
|
|
const endpoint = typeof address !== "string"
|
|
|
|
? ((address.address === "::" ? "localhost" : address.address) + ":" + address.port)
|
|
|
|
: address;
|
|
|
|
return `http://${endpoint}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class MainServer extends Server {
|
|
|
|
// Used to notify the IPC server that there is a new client.
|
|
|
|
public readonly _onDidClientConnect = new Emitter<ClientConnectionEvent>();
|
|
|
|
public readonly onDidClientConnect = this._onDidClientConnect.event;
|
|
|
|
|
|
|
|
private readonly rootPath = path.resolve(__dirname, "../../..");
|
|
|
|
|
|
|
|
// This is separate instead of just extending this class since we can't
|
|
|
|
// use properties in the super call. This manages channels.
|
|
|
|
private readonly ipc = new IPCServer(this.onDidClientConnect);
|
|
|
|
|
|
|
|
// Persistent connections. These can reconnect within a timeout.
|
|
|
|
private readonly connections = new Map<ConnectionType, Map<string, Connection>>();
|
|
|
|
|
|
|
|
private readonly services = new ServiceCollection();
|
|
|
|
|
2019-07-03 05:55:54 +08:00
|
|
|
public constructor(private readonly webviewServer: WebviewServer, args: ParsedArgs) {
|
2019-07-03 05:15:41 +08:00
|
|
|
super();
|
2019-06-28 06:34:33 +08:00
|
|
|
|
2019-07-02 07:01:09 +08:00
|
|
|
this.server.on("upgrade", async (request, socket) => {
|
|
|
|
const protocol = this.createProtocol(request, socket);
|
2019-06-29 06:37:23 +08:00
|
|
|
try {
|
2019-07-02 07:01:09 +08:00
|
|
|
await this.connect(await protocol.handshake(), protocol);
|
2019-06-29 06:37:23 +08:00
|
|
|
} catch (error) {
|
2019-07-02 07:01:09 +08:00
|
|
|
protocol.dispose(error);
|
2019-06-29 06:37:23 +08:00
|
|
|
}
|
2019-06-28 06:34:33 +08:00
|
|
|
});
|
|
|
|
|
2019-07-03 05:15:41 +08:00
|
|
|
const environmentService = new EnvironmentService(args, process.execPath);
|
|
|
|
this.services.set(IEnvironmentService, environmentService);
|
2019-06-29 06:37:23 +08:00
|
|
|
|
2019-07-03 05:15:41 +08:00
|
|
|
const logService = new SpdLogService(
|
2019-07-02 03:18:30 +08:00
|
|
|
RemoteExtensionLogFileName,
|
2019-07-03 05:15:41 +08:00
|
|
|
environmentService.logsPath,
|
|
|
|
getLogLevel(environmentService),
|
2019-07-02 03:18:30 +08:00
|
|
|
);
|
2019-07-03 05:15:41 +08:00
|
|
|
this.services.set(ILogService, logService);
|
2019-06-29 06:37:23 +08:00
|
|
|
|
2019-07-03 05:15:41 +08:00
|
|
|
this.ipc.registerChannel("loglevel", new LogLevelSetterChannel(logService));
|
|
|
|
|
|
|
|
const instantiationService = new InstantiationService(this.services);
|
2019-06-29 06:37:23 +08:00
|
|
|
instantiationService.invokeFunction(() => {
|
2019-07-03 04:26:40 +08:00
|
|
|
instantiationService.createInstance(LogsDataCleaner);
|
2019-06-29 06:37:23 +08:00
|
|
|
this.ipc.registerChannel(
|
|
|
|
REMOTE_FILE_SYSTEM_CHANNEL_NAME,
|
2019-07-03 05:15:41 +08:00
|
|
|
new FileProviderChannel(logService),
|
2019-06-29 06:37:23 +08:00
|
|
|
);
|
|
|
|
this.ipc.registerChannel(
|
|
|
|
"remoteextensionsenvironment",
|
2019-07-03 05:15:41 +08:00
|
|
|
new ExtensionEnvironmentChannel(environmentService, logService),
|
2019-06-29 06:37:23 +08:00
|
|
|
);
|
|
|
|
});
|
2019-07-03 05:15:41 +08:00
|
|
|
}
|
2019-06-28 06:34:33 +08:00
|
|
|
|
2019-07-03 05:15:41 +08:00
|
|
|
protected async handleRequest(
|
|
|
|
request: http.IncomingMessage,
|
|
|
|
parsedUrl: url.UrlWithParsedQuery,
|
|
|
|
requestPath: string,
|
|
|
|
): Promise<[string | Buffer, http.OutgoingHttpHeaders]> {
|
2019-06-28 06:34:33 +08:00
|
|
|
if (requestPath === "/") {
|
|
|
|
const htmlPath = path.join(
|
|
|
|
this.rootPath,
|
|
|
|
'out/vs/code/browser/workbench/workbench.html',
|
|
|
|
);
|
|
|
|
|
|
|
|
let html = await util.promisify(fs.readFile)(htmlPath, "utf8");
|
|
|
|
|
2019-07-02 23:34:03 +08:00
|
|
|
const remoteAuthority = request.headers.host as string;
|
|
|
|
const transformer = getUriTransformer(remoteAuthority);
|
|
|
|
|
2019-07-03 05:15:41 +08:00
|
|
|
const webviewEndpoint = this.webviewServer.address;
|
2019-07-03 04:26:40 +08:00
|
|
|
|
2019-07-02 23:59:36 +08:00
|
|
|
const cwd = process.env.VSCODE_CWD || process.cwd();
|
|
|
|
const workspacePath = parsedUrl.query.workspace as string | undefined;
|
|
|
|
const folderPath = !workspacePath ? parsedUrl.query.folder as string | undefined || cwd: undefined;
|
|
|
|
|
2019-07-02 03:00:11 +08:00
|
|
|
const options: Options = {
|
2019-06-28 06:34:33 +08:00
|
|
|
WORKBENCH_WEB_CONGIGURATION: {
|
2019-07-03 05:15:41 +08:00
|
|
|
workspaceUri: workspacePath
|
|
|
|
? transformer.transformOutgoing(URI.file(sanitizeFilePath(workspacePath, cwd)))
|
|
|
|
: undefined,
|
|
|
|
folderUri: folderPath
|
|
|
|
? transformer.transformOutgoing(URI.file(sanitizeFilePath(folderPath, cwd)))
|
|
|
|
: undefined,
|
2019-07-02 23:34:03 +08:00
|
|
|
remoteAuthority,
|
2019-07-03 04:26:40 +08:00
|
|
|
webviewEndpoint,
|
2019-06-28 06:34:33 +08:00
|
|
|
},
|
2019-07-03 05:15:41 +08:00
|
|
|
REMOTE_USER_DATA_URI: transformer.transformOutgoing(
|
|
|
|
(this.services.get(IEnvironmentService) as EnvironmentService).webUserDataHome,
|
|
|
|
),
|
2019-07-03 05:55:54 +08:00
|
|
|
PRODUCT_CONFIGURATION: product,
|
2019-07-02 03:00:11 +08:00
|
|
|
CONNECTION_AUTH_TOKEN: "",
|
2019-06-28 06:34:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Object.keys(options).forEach((key) => {
|
|
|
|
html = html.replace(`"{{${key}}}"`, `'${JSON.stringify(options[key])}'`);
|
|
|
|
});
|
|
|
|
|
2019-07-03 04:26:40 +08:00
|
|
|
html = html.replace('{{WEBVIEW_ENDPOINT}}', webviewEndpoint);
|
2019-06-28 06:34:33 +08:00
|
|
|
|
2019-06-29 06:49:29 +08:00
|
|
|
return [html, {
|
|
|
|
"Content-Type": "text/html",
|
|
|
|
}];
|
2019-06-28 06:34:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const content = await util.promisify(fs.readFile)(
|
|
|
|
path.join(this.rootPath, requestPath),
|
|
|
|
);
|
2019-06-29 06:49:29 +08:00
|
|
|
return [content, {
|
|
|
|
"Content-Type": getMediaMime(requestPath) || {
|
|
|
|
".css": "text/css",
|
|
|
|
".html": "text/html",
|
|
|
|
".js": "text/javascript",
|
|
|
|
".json": "application/json",
|
|
|
|
}[extname(requestPath)] || "text/plain",
|
|
|
|
}];
|
2019-06-28 06:34:33 +08:00
|
|
|
} catch (error) {
|
|
|
|
if (error.code === "ENOENT" || error.code === "EISDIR") {
|
|
|
|
throw new HttpError("Not found", HttpCode.NotFound);
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-02 07:01:09 +08:00
|
|
|
private createProtocol(request: http.IncomingMessage, socket: net.Socket): Protocol {
|
2019-06-28 06:34:33 +08:00
|
|
|
if (request.headers.upgrade !== "websocket") {
|
2019-06-29 06:37:23 +08:00
|
|
|
throw new Error("HTTP/1.1 400 Bad Request");
|
2019-06-28 06:34:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
reconnectionToken: "",
|
|
|
|
reconnection: false,
|
|
|
|
skipWebSocketFrames: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (request.url) {
|
|
|
|
const query = url.parse(request.url, true).query;
|
|
|
|
if (query.reconnectionToken) {
|
|
|
|
options.reconnectionToken = query.reconnectionToken as string;
|
|
|
|
}
|
|
|
|
if (query.reconnection === "true") {
|
|
|
|
options.reconnection = true;
|
|
|
|
}
|
|
|
|
if (query.skipWebSocketFrames === "true") {
|
|
|
|
options.skipWebSocketFrames = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-02 07:01:09 +08:00
|
|
|
return new Protocol(
|
|
|
|
request.headers["sec-websocket-key"] as string,
|
|
|
|
socket,
|
|
|
|
options,
|
|
|
|
);
|
2019-06-28 06:34:33 +08:00
|
|
|
}
|
|
|
|
|
2019-07-02 07:01:09 +08:00
|
|
|
private async connect(message: ConnectionTypeRequest, protocol: Protocol): Promise<void> {
|
|
|
|
switch (message.desiredConnectionType) {
|
|
|
|
case ConnectionType.ExtensionHost:
|
|
|
|
case ConnectionType.Management:
|
|
|
|
const debugPort = await this.getDebugPort();
|
|
|
|
const ok = message.desiredConnectionType === ConnectionType.ExtensionHost
|
|
|
|
? (debugPort ? { debugPort } : {})
|
|
|
|
: { type: "ok" };
|
|
|
|
|
|
|
|
if (!this.connections.has(message.desiredConnectionType)) {
|
|
|
|
this.connections.set(message.desiredConnectionType, new Map());
|
|
|
|
}
|
|
|
|
|
|
|
|
const connections = this.connections.get(message.desiredConnectionType)!;
|
|
|
|
const token = protocol.options.reconnectionToken;
|
|
|
|
|
|
|
|
if (protocol.options.reconnection && connections.has(token)) {
|
|
|
|
protocol.sendMessage(ok);
|
|
|
|
const buffer = protocol.readEntireBuffer();
|
|
|
|
protocol.dispose();
|
|
|
|
return connections.get(token)!.reconnect(protocol, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (protocol.options.reconnection || connections.has(token)) {
|
|
|
|
throw new Error(protocol.options.reconnection
|
|
|
|
? "Unrecognized reconnection token"
|
|
|
|
: "Duplicate reconnection token"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol.sendMessage(ok);
|
|
|
|
|
|
|
|
let connection: Connection;
|
|
|
|
if (message.desiredConnectionType === ConnectionType.Management) {
|
|
|
|
connection = new ManagementConnection(protocol);
|
|
|
|
this._onDidClientConnect.fire({
|
|
|
|
protocol,
|
|
|
|
onDidClientDisconnect: connection.onClose,
|
|
|
|
});
|
|
|
|
} else {
|
2019-07-03 05:15:41 +08:00
|
|
|
connection = new ExtensionHostConnection(
|
|
|
|
protocol, this.services.get(ILogService) as ILogService,
|
|
|
|
);
|
2019-07-02 07:01:09 +08:00
|
|
|
}
|
|
|
|
connections.set(protocol.options.reconnectionToken, connection);
|
|
|
|
connection.onClose(() => {
|
|
|
|
connections.delete(protocol.options.reconnectionToken);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case ConnectionType.Tunnel: return protocol.tunnel();
|
|
|
|
default: throw new Error("Unrecognized connection type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: implement.
|
|
|
|
*/
|
|
|
|
private async getDebugPort(): Promise<number | undefined> {
|
|
|
|
return undefined;
|
|
|
|
}
|
2019-06-28 06:34:33 +08:00
|
|
|
}
|
2019-07-03 05:15:41 +08:00
|
|
|
|
|
|
|
export class WebviewServer extends Server {
|
|
|
|
protected async handleRequest(): Promise<[string | Buffer, http.OutgoingHttpHeaders]> {
|
|
|
|
throw new Error("not implemented");
|
|
|
|
}
|
|
|
|
}
|