|
1 | 1 | import { logger } from "@coder/logger"
|
| 2 | +import * as crypto from "crypto" |
2 | 3 | import * as express from "express"
|
| 4 | +import { promises as fs } from "fs" |
3 | 5 | import * as http from "http"
|
4 | 6 | import * as net from "net"
|
5 | 7 | import * as path from "path"
|
@@ -32,6 +34,7 @@ export class CodeServerRouteWrapper {
|
32 | 34 | private _wsRouterWrapper = WsRouter()
|
33 | 35 | private _socketProxyProvider = new SocketProxyProvider()
|
34 | 36 | public router = express.Router()
|
| 37 | + private mintKeyPromise: Promise<Buffer> | undefined |
35 | 38 |
|
36 | 39 | public get wsRouter() {
|
37 | 40 | return this._wsRouterWrapper.router
|
@@ -66,6 +69,33 @@ export class CodeServerRouteWrapper {
|
66 | 69 | )
|
67 | 70 | }
|
68 | 71 |
|
| 72 | + private mintKey: express.Handler = async (req, res, next) => { |
| 73 | + if (!this.mintKeyPromise) { |
| 74 | + this.mintKeyPromise = new Promise(async (resolve) => { |
| 75 | + const keyPath = path.join(req.args["user-data-dir"], "serve-web-key-half") |
| 76 | + logger.debug(`Reading server web key half from ${keyPath}`) |
| 77 | + try { |
| 78 | + resolve(await fs.readFile(keyPath)) |
| 79 | + return |
| 80 | + } catch (error: any) { |
| 81 | + if (error.code !== "ENOENT") { |
| 82 | + logError(logger, `read ${keyPath}`, error) |
| 83 | + } |
| 84 | + } |
| 85 | + // VS Code wants 256 bits. |
| 86 | + const key = crypto.randomBytes(32) |
| 87 | + try { |
| 88 | + await fs.writeFile(keyPath, key) |
| 89 | + } catch (error: any) { |
| 90 | + logError(logger, `write ${keyPath}`, error) |
| 91 | + } |
| 92 | + resolve(key) |
| 93 | + }) |
| 94 | + } |
| 95 | + const key = await this.mintKeyPromise |
| 96 | + res.end(key) |
| 97 | + } |
| 98 | + |
69 | 99 | private $root: express.Handler = async (req, res, next) => {
|
70 | 100 | const isAuthenticated = await authenticated(req)
|
71 | 101 | const NO_FOLDER_OR_WORKSPACE_QUERY = !req.query.folder && !req.query.workspace
|
@@ -173,6 +203,7 @@ export class CodeServerRouteWrapper {
|
173 | 203 | constructor() {
|
174 | 204 | this.router.get("/", this.ensureCodeServerLoaded, this.$root)
|
175 | 205 | this.router.get("/manifest.json", this.manifest)
|
| 206 | + this.router.post("/mint-key", this.mintKey) |
176 | 207 | this.router.all("*", ensureAuthenticated, this.ensureCodeServerLoaded, this.$proxyRequest)
|
177 | 208 | this._wsRouterWrapper.ws("*", ensureOrigin, ensureAuthenticated, this.ensureCodeServerLoaded, this.$proxyWebsocket)
|
178 | 209 | }
|
|
0 commit comments