From 7083e8e29f232765927d6751a29bc502ed1bd367 Mon Sep 17 00:00:00 2001 From: Moepchi Date: Thu, 10 Sep 2026 19:46:02 +0000 Subject: [PATCH] gateway: serve HTTPS when TLS_CERT and TLS_KEY are set Not a nicety. getDisplayMedia, getUserMedia and crypto.randomUUID are secure-context-only, so a LAN instance reached over plain http could neither share a screen nor use the microphone. Chrome additionally hides host ICE candidates behind mDNS names until a media permission is granted, which keeps a peer-to-peer stream from connecting at all - so the publishing direction was effectively unreachable from anywhere but localhost. The frontend already derives ws:// or wss:// from the page's own scheme, so nothing else needs configuring. Verified from the LAN address with a self-signed cert: isSecureContext true, both media APIs present. gateway/certs/ is gitignored - a private key never belongs in the repo. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU --- .gitignore | 3 +++ gateway/src/index.ts | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 54b76a2..41f0697 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,6 @@ docker-compose.override.yml .claude/ CLAUDE.md .serena/ + +# TLS material for local HTTPS - a private key never belongs in the repo. +gateway/certs/ diff --git a/gateway/src/index.ts b/gateway/src/index.ts index 18512af..f648041 100644 --- a/gateway/src/index.ts +++ b/gateway/src/index.ts @@ -1,4 +1,6 @@ import { createServer, type IncomingMessage, type ServerResponse } from "node:http"; +import { createServer as createSecureServer } from "node:https"; +import { readFileSync } from "node:fs"; import { readFile, appendFile, rename, stat } from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; @@ -249,7 +251,26 @@ const DEV_HINT = ` { res.end(DEV_HINT); } })(); -}); +}; + +// The frontend derives ws:// or wss:// from the page's own scheme, so the +// WebSocket follows this choice without any further configuration. +const server = tlsOptions + ? createSecureServer(tlsOptions, requestHandler) + : createServer(requestHandler); const wss = new WebSocketServer({ server, path: "/ws" }); @@ -319,7 +346,10 @@ const heartbeatTimer = setInterval(() => { wss.on("close", () => clearInterval(heartbeatTimer)); server.listen(PORT, () => { - console.log(`WebSpeak3 gateway listening on http://localhost:${PORT} (WebSocket at /ws)`); + const scheme = tlsOptions ? "https" : "http"; + console.log( + `WebSpeak3 gateway listening on ${scheme}://localhost:${PORT} (WebSocket at /ws)` + ); if (SERVE_STATIC) { console.log(`Serving static UI from ${WEB_DIST}`); console.log(`Dev tip: use http://localhost:5173/ for live UI; rebuild web/dist after UI changes if you open :${PORT}`);