CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/hub/proxy/index.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { Application } from "express";
7
import getLogger from "../logger";
8
import initProxy from "./handle-request";
9
import initUpgrade from "./handle-upgrade";
10
import base_path from "@cocalc/backend/base-path";
11
import { ProjectControlFunction } from "@cocalc/server/projects/control";
12
13
const logger = getLogger("proxy");
14
15
interface Options {
16
app: Application;
17
httpServer; // got from express_app via httpServer = http.createServer(app).
18
projectControl: ProjectControlFunction; // controls projects (aka "compute server")
19
isPersonal: boolean; // if true, disables all access controls
20
listenersHack: boolean;
21
}
22
23
export default function init(opts: Options) {
24
const proxy_regexp = `^${
25
base_path.length <= 1 ? "" : base_path
26
}\/[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}\/*`;
27
logger.info("creating proxy server with proxy_regexp", proxy_regexp);
28
29
// tcp connections:
30
const handleProxy = initProxy(opts);
31
32
// websocket upgrades:
33
const handleUpgrade = initUpgrade(opts, proxy_regexp);
34
35
opts.app.all(proxy_regexp, handleProxy);
36
37
opts.httpServer.on("upgrade", handleUpgrade);
38
}
39
40