2020-02-04 20:27:46 +01:00
|
|
|
import { field, logger } from "@coder/logger"
|
|
|
|
import * as cp from "child_process"
|
|
|
|
import * as crypto from "crypto"
|
2020-04-16 18:50:53 +02:00
|
|
|
import * as fs from "fs-extra"
|
2020-02-04 20:27:46 +01:00
|
|
|
import * as http from "http"
|
|
|
|
import * as net from "net"
|
|
|
|
import * as path from "path"
|
|
|
|
import {
|
|
|
|
CodeServerMessage,
|
2020-03-13 22:44:56 +01:00
|
|
|
Options,
|
2020-02-06 19:29:19 +01:00
|
|
|
StartPath,
|
2020-02-04 20:27:46 +01:00
|
|
|
VscodeMessage,
|
|
|
|
VscodeOptions,
|
|
|
|
WorkbenchOptions,
|
|
|
|
} from "../../../lib/vscode/src/vs/server/ipc"
|
2020-02-06 00:44:32 +01:00
|
|
|
import { HttpCode, HttpError } from "../../common/http"
|
2020-07-30 19:14:31 +02:00
|
|
|
import { arrayify, generateUuid } from "../../common/util"
|
2020-02-06 19:29:19 +01:00
|
|
|
import { Args } from "../cli"
|
2020-02-06 00:30:09 +01:00
|
|
|
import { HttpProvider, HttpProviderOptions, HttpResponse, Route } from "../http"
|
2020-02-14 23:41:42 +01:00
|
|
|
import { settings } from "../settings"
|
2020-05-18 19:57:50 +02:00
|
|
|
import { pathToFsPath } from "../util"
|
2020-02-06 19:29:19 +01:00
|
|
|
|
2020-02-04 20:27:46 +01:00
|
|
|
export class VscodeHttpProvider extends HttpProvider {
|
|
|
|
private readonly serverRootPath: string
|
|
|
|
private readonly vsRootPath: string
|
|
|
|
private _vscode?: Promise<cp.ChildProcess>
|
|
|
|
|
2020-02-06 19:29:19 +01:00
|
|
|
public constructor(options: HttpProviderOptions, private readonly args: Args) {
|
2020-02-04 20:27:46 +01:00
|
|
|
super(options)
|
|
|
|
this.vsRootPath = path.resolve(this.rootPath, "lib/vscode")
|
|
|
|
this.serverRootPath = path.join(this.vsRootPath, "out/vs/server")
|
|
|
|
}
|
|
|
|
|
2020-02-27 19:04:23 +01:00
|
|
|
public get running(): boolean {
|
|
|
|
return !!this._vscode
|
|
|
|
}
|
|
|
|
|
|
|
|
public async dispose(): Promise<void> {
|
|
|
|
if (this._vscode) {
|
|
|
|
const vscode = await this._vscode
|
|
|
|
vscode.removeAllListeners()
|
|
|
|
this._vscode = undefined
|
|
|
|
vscode.kill()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:27:46 +01:00
|
|
|
private async initialize(options: VscodeOptions): Promise<WorkbenchOptions> {
|
|
|
|
const id = generateUuid()
|
|
|
|
const vscode = await this.fork()
|
|
|
|
|
2020-02-18 23:51:55 +01:00
|
|
|
logger.debug("setting up vs code...")
|
2020-02-04 20:27:46 +01:00
|
|
|
return new Promise<WorkbenchOptions>((resolve, reject) => {
|
|
|
|
vscode.once("message", (message: VscodeMessage) => {
|
2020-02-18 23:51:55 +01:00
|
|
|
logger.debug("got message from vs code", field("message", message))
|
2020-02-04 20:27:46 +01:00
|
|
|
return message.type === "options" && message.id === id
|
|
|
|
? resolve(message.options)
|
|
|
|
: reject(new Error("Unexpected response during initialization"))
|
|
|
|
})
|
|
|
|
vscode.once("error", reject)
|
|
|
|
vscode.once("exit", (code) => reject(new Error(`VS Code exited unexpectedly with code ${code}`)))
|
|
|
|
this.send({ type: "init", id, options }, vscode)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private fork(): Promise<cp.ChildProcess> {
|
|
|
|
if (!this._vscode) {
|
2020-02-18 23:51:55 +01:00
|
|
|
logger.debug("forking vs code...")
|
2020-02-04 20:27:46 +01:00
|
|
|
const vscode = cp.fork(path.join(this.serverRootPath, "fork"))
|
|
|
|
vscode.on("error", (error) => {
|
|
|
|
logger.error(error.message)
|
|
|
|
this._vscode = undefined
|
|
|
|
})
|
|
|
|
vscode.on("exit", (code) => {
|
|
|
|
logger.error(`VS Code exited unexpectedly with code ${code}`)
|
|
|
|
this._vscode = undefined
|
|
|
|
})
|
|
|
|
|
|
|
|
this._vscode = new Promise((resolve, reject) => {
|
|
|
|
vscode.once("message", (message: VscodeMessage) => {
|
2020-02-18 23:51:55 +01:00
|
|
|
logger.debug("got message from vs code", field("message", message))
|
2020-02-04 20:27:46 +01:00
|
|
|
return message.type === "ready"
|
|
|
|
? resolve(vscode)
|
|
|
|
: reject(new Error("Unexpected response waiting for ready response"))
|
|
|
|
})
|
|
|
|
vscode.once("error", reject)
|
|
|
|
vscode.once("exit", (code) => reject(new Error(`VS Code exited unexpectedly with code ${code}`)))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._vscode
|
|
|
|
}
|
|
|
|
|
2020-03-11 22:00:39 +01:00
|
|
|
public async handleWebSocket(route: Route, request: http.IncomingMessage, socket: net.Socket): Promise<void> {
|
2020-02-04 20:27:46 +01:00
|
|
|
if (!this.authenticated(request)) {
|
|
|
|
throw new Error("not authenticated")
|
|
|
|
}
|
|
|
|
|
|
|
|
// VS Code expects a raw socket. It will handle all the web socket frames.
|
|
|
|
// We just need to handle the initial upgrade.
|
|
|
|
// This magic value is specified by the websocket spec.
|
|
|
|
const magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
|
|
|
|
const reply = crypto
|
|
|
|
.createHash("sha1")
|
|
|
|
.update(request.headers["sec-websocket-key"] + magic)
|
|
|
|
.digest("base64")
|
|
|
|
socket.write(
|
|
|
|
[
|
|
|
|
"HTTP/1.1 101 Switching Protocols",
|
|
|
|
"Upgrade: websocket",
|
|
|
|
"Connection: Upgrade",
|
|
|
|
`Sec-WebSocket-Accept: ${reply}`,
|
2020-02-15 01:46:00 +01:00
|
|
|
].join("\r\n") + "\r\n\r\n",
|
2020-02-04 20:27:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const vscode = await this._vscode
|
2020-02-06 00:30:09 +01:00
|
|
|
this.send({ type: "socket", query: route.query }, vscode, socket)
|
2020-02-04 20:27:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private send(message: CodeServerMessage, vscode?: cp.ChildProcess, socket?: net.Socket): void {
|
|
|
|
if (!vscode || vscode.killed) {
|
|
|
|
throw new Error("vscode is not running")
|
|
|
|
}
|
|
|
|
vscode.send(message, socket)
|
|
|
|
}
|
|
|
|
|
2020-03-02 19:43:02 +01:00
|
|
|
public async handleRequest(route: Route, request: http.IncomingMessage): Promise<HttpResponse> {
|
2020-02-13 23:38:05 +01:00
|
|
|
this.ensureMethod(request)
|
|
|
|
|
2020-02-06 00:30:09 +01:00
|
|
|
switch (route.base) {
|
2020-02-04 20:27:46 +01:00
|
|
|
case "/":
|
2020-04-01 18:28:09 +02:00
|
|
|
if (!this.isRoot(route)) {
|
2020-02-06 00:44:32 +01:00
|
|
|
throw new HttpError("Not found", HttpCode.NotFound)
|
2020-02-13 23:38:05 +01:00
|
|
|
} else if (!this.authenticated(request)) {
|
2020-07-22 21:53:15 +02:00
|
|
|
return { redirect: "/login", query: { to: route.providerBase } }
|
2020-02-06 00:44:32 +01:00
|
|
|
}
|
2020-02-05 21:21:59 +01:00
|
|
|
try {
|
2020-02-06 00:30:09 +01:00
|
|
|
return await this.getRoot(request, route)
|
2020-02-05 21:21:59 +01:00
|
|
|
} catch (error) {
|
2020-02-26 21:57:07 +01:00
|
|
|
const message = `<div>VS Code failed to load.</div> ${
|
|
|
|
this.isDev
|
2020-02-27 19:04:23 +01:00
|
|
|
? `<div>It might not have finished compiling.</div>` +
|
|
|
|
`Check for <code>Finished <span class="success">compilation</span></code> in the output.`
|
2020-02-26 21:57:07 +01:00
|
|
|
: ""
|
2020-02-13 23:38:05 +01:00
|
|
|
} <br><br>${error}`
|
2020-02-26 21:57:07 +01:00
|
|
|
return this.getErrorRoot(route, "VS Code failed to load", "500", message)
|
2020-02-05 21:21:59 +01:00
|
|
|
}
|
2020-02-13 23:38:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.ensureAuthenticated(request)
|
|
|
|
|
|
|
|
switch (route.base) {
|
2020-02-04 20:27:46 +01:00
|
|
|
case "/resource":
|
|
|
|
case "/vscode-remote-resource":
|
2020-02-06 00:30:09 +01:00
|
|
|
if (typeof route.query.path === "string") {
|
2020-05-18 19:57:50 +02:00
|
|
|
return this.getResource(pathToFsPath(route.query.path))
|
2020-02-04 20:27:46 +01:00
|
|
|
}
|
|
|
|
break
|
|
|
|
case "/webview":
|
2020-02-06 00:30:09 +01:00
|
|
|
if (/^\/vscode-resource/.test(route.requestPath)) {
|
|
|
|
return this.getResource(route.requestPath.replace(/^\/vscode-resource(\/file)?/, ""))
|
2020-02-04 20:27:46 +01:00
|
|
|
}
|
2020-02-06 00:30:09 +01:00
|
|
|
return this.getResource(this.vsRootPath, "out/vs/workbench/contrib/webview/browser/pre", route.requestPath)
|
2020-02-04 20:27:46 +01:00
|
|
|
}
|
2020-03-02 19:43:02 +01:00
|
|
|
|
|
|
|
throw new HttpError("Not found", HttpCode.NotFound)
|
2020-02-04 20:27:46 +01:00
|
|
|
}
|
|
|
|
|
2020-02-06 00:30:09 +01:00
|
|
|
private async getRoot(request: http.IncomingMessage, route: Route): Promise<HttpResponse> {
|
2020-02-13 19:04:22 +01:00
|
|
|
const remoteAuthority = request.headers.host as string
|
2020-02-14 23:41:42 +01:00
|
|
|
const { lastVisited } = await settings.read()
|
2020-04-07 02:06:12 +02:00
|
|
|
const startPath = await this.getFirstPath([
|
|
|
|
{ url: route.query.workspace, workspace: true },
|
|
|
|
{ url: route.query.folder, workspace: false },
|
|
|
|
this.args._ && this.args._.length > 0 ? { url: path.resolve(this.args._[this.args._.length - 1]) } : undefined,
|
|
|
|
lastVisited,
|
|
|
|
])
|
2020-02-04 20:27:46 +01:00
|
|
|
const [response, options] = await Promise.all([
|
2020-02-13 23:38:05 +01:00
|
|
|
await this.getUtf8Resource(this.rootPath, "src/browser/pages/vscode.html"),
|
2020-02-04 20:27:46 +01:00
|
|
|
this.initialize({
|
|
|
|
args: this.args,
|
2020-02-13 19:04:22 +01:00
|
|
|
remoteAuthority,
|
2020-02-06 19:29:19 +01:00
|
|
|
startPath,
|
2020-02-04 20:27:46 +01:00
|
|
|
}),
|
|
|
|
])
|
|
|
|
|
2020-07-31 05:39:37 +02:00
|
|
|
settings.write({
|
|
|
|
lastVisited: startPath || lastVisited, // If startpath is undefined, then fallback to lastVisited
|
|
|
|
query: route.query,
|
2020-07-30 07:37:41 +02:00
|
|
|
})
|
2020-02-04 20:27:46 +01:00
|
|
|
|
2020-02-13 23:38:05 +01:00
|
|
|
if (!this.isDev) {
|
|
|
|
response.content = response.content.replace(/<!-- PROD_ONLY/g, "").replace(/END_PROD_ONLY -->/g, "")
|
|
|
|
}
|
|
|
|
|
2020-05-15 03:30:34 +02:00
|
|
|
options.productConfiguration.codeServerVersion = require("../../../package.json").version
|
|
|
|
|
2020-02-27 21:56:14 +01:00
|
|
|
response.content = response.content
|
|
|
|
.replace(`"{{REMOTE_USER_DATA_URI}}"`, `'${JSON.stringify(options.remoteUserDataUri)}'`)
|
|
|
|
.replace(`"{{PRODUCT_CONFIGURATION}}"`, `'${JSON.stringify(options.productConfiguration)}'`)
|
|
|
|
.replace(`"{{WORKBENCH_WEB_CONFIGURATION}}"`, `'${JSON.stringify(options.workbenchWebConfiguration)}'`)
|
|
|
|
.replace(`"{{NLS_CONFIGURATION}}"`, `'${JSON.stringify(options.nlsConfiguration)}'`)
|
2020-03-13 22:44:56 +01:00
|
|
|
return this.replaceTemplates<Options>(route, response, {
|
|
|
|
disableTelemetry: !!this.args["disable-telemetry"],
|
|
|
|
})
|
2020-02-04 20:27:46 +01:00
|
|
|
}
|
2020-02-05 21:21:59 +01:00
|
|
|
|
2020-02-06 19:29:19 +01:00
|
|
|
/**
|
2020-04-07 02:06:12 +02:00
|
|
|
* Choose the first non-empty path.
|
2020-02-06 19:29:19 +01:00
|
|
|
*/
|
2020-04-07 02:06:12 +02:00
|
|
|
private async getFirstPath(
|
2020-02-13 19:04:22 +01:00
|
|
|
startPaths: Array<{ url?: string | string[]; workspace?: boolean } | undefined>,
|
2020-02-06 19:29:19 +01:00
|
|
|
): Promise<StartPath | undefined> {
|
2020-04-16 18:50:53 +02:00
|
|
|
const isFile = async (path: string): Promise<boolean> => {
|
|
|
|
try {
|
|
|
|
const stat = await fs.stat(path)
|
|
|
|
return stat.isFile()
|
|
|
|
} catch (error) {
|
|
|
|
logger.warn(error.message)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 19:29:19 +01:00
|
|
|
for (let i = 0; i < startPaths.length; ++i) {
|
|
|
|
const startPath = startPaths[i]
|
2020-07-30 19:14:31 +02:00
|
|
|
const url = arrayify(startPath && startPath.url).find((p) => !!p)
|
2020-04-07 02:06:12 +02:00
|
|
|
if (startPath && url) {
|
|
|
|
return {
|
|
|
|
url,
|
2020-04-16 18:50:53 +02:00
|
|
|
// The only time `workspace` is undefined is for the command-line
|
|
|
|
// argument, in which case it's a path (not a URL) so we can stat it
|
|
|
|
// without having to parse it.
|
|
|
|
workspace: typeof startPath.workspace !== "undefined" ? startPath.workspace : await isFile(url),
|
2020-02-06 19:29:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undefined
|
|
|
|
}
|
2020-02-04 20:27:46 +01:00
|
|
|
}
|