From 0b185abc509e164e6092ea6f58aacbd674145e66 Mon Sep 17 00:00:00 2001 From: William Stein Date: Fri, 15 Mar 2024 14:55:58 +0000 Subject: [PATCH 1/2] this should fix a crash warning that has been popping up in the hub-websocket logs --- src/packages/hub/proxy/handle-upgrade.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/packages/hub/proxy/handle-upgrade.ts b/src/packages/hub/proxy/handle-upgrade.ts index 555f7ab1bf..47e219ac66 100644 --- a/src/packages/hub/proxy/handle-upgrade.ts +++ b/src/packages/hub/proxy/handle-upgrade.ts @@ -23,6 +23,10 @@ export default function init( const re = new RegExp(proxy_regexp); async function handleProxyUpgradeRequest(req, socket, head): Promise { + socket.on("error", (err) => { + // server will crash sometimes without this: + logger.debug("WARNING -- websocket socket error", err); + }); const dbg = (...args) => { logger.silly(req.url, ...args); }; From a23d2f11e283578418db0709ae661449635bf364 Mon Sep 17 00:00:00 2001 From: William Stein Date: Sat, 16 Mar 2024 20:07:38 +0000 Subject: [PATCH 2/2] compute server: run https proxy on startup --- src/packages/server/compute/cloud/install.ts | 29 +++++++++++++++++++ .../server/compute/cloud/startup-script.ts | 4 +++ .../util/db-schema/compute-servers.ts | 12 ++++++++ 3 files changed, 45 insertions(+) diff --git a/src/packages/server/compute/cloud/install.ts b/src/packages/server/compute/cloud/install.ts index bd426a04a9..0aea618c40 100644 --- a/src/packages/server/compute/cloud/install.ts +++ b/src/packages/server/compute/cloud/install.ts @@ -107,6 +107,35 @@ set -v `; } +// if installHttpsProxy runs, it has to be after installNode +// TODO: A problem with this is for some reason it fails to run below, +// it won't be reported. Also, maybe this should be in some sort +// of official systemd daemon. +// Todo: maybe also nail down the version or tag better. I suspect +// the proxy will get very stable soon. +// This could also just merge with the @cocalc/compute-server package. +export function installHttpsProxy({ + IMAGES, + image, +}: { + IMAGES: Images; + image: string; +}) { + const proxy = IMAGES[image]?.proxy; + if (proxy === false) { + return ""; + } + + return ` +set +v +NVM_DIR=/cocalc/nvm source /cocalc/nvm/nvm.sh +set -v + +DEBUG=proxy:* PROXY_CONFIG=/cocalc/conf/proxy.json PROXY_AUTH_TOKEN_FILE=/cocalc/conf/auth_token npx -y @cocalc/compute-server-proxy@latest > /var/log/cocalc-proxy.log 2> /var/log/cocalc-proxy.err & + +`; +} + export function installMicroK8s({ image, IMAGES, diff --git a/src/packages/server/compute/cloud/startup-script.ts b/src/packages/server/compute/cloud/startup-script.ts index cf60566f98..14752ee1a1 100644 --- a/src/packages/server/compute/cloud/startup-script.ts +++ b/src/packages/server/compute/cloud/startup-script.ts @@ -4,6 +4,7 @@ import { installDocker, installNode, installCoCalc, + installHttpsProxy, installConf, installMicroK8s, installUser, @@ -122,6 +123,9 @@ if [ $? -ne 0 ]; then exit 1 fi +setState install install-proxy '' 60 55 +${installHttpsProxy({ IMAGES, image })} + setState install install-user '' 60 60 ${doInstallUser ? installUser() : ""} if [ $? -ne 0 ]; then diff --git a/src/packages/util/db-schema/compute-servers.ts b/src/packages/util/db-schema/compute-servers.ts index 68e578d4d9..9b80a44144 100644 --- a/src/packages/util/db-schema/compute-servers.ts +++ b/src/packages/util/db-schema/compute-servers.ts @@ -63,6 +63,18 @@ export interface Image { disabled?: boolean; // priority -- optional integer used for sorting options to display to user. The bigger the higher. priority?: number; + // proxy: if false, do NOT run https proxy server on host VM + // if nothing given, runs proxy server with no default config (so does nothing) + // if given, is array of proxy config. + proxy?: + | false + | { + path: string; + target: string; + ws?: boolean; + options?: any; + wsOptions?: any; + }[]; } export type Images = { [name: string]: Image };