mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-03 18:02:49 +08:00
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
This commit is contained in:
1 parent
15e215d892
commit
7083e8e29f
2 files changed
+36
-3
No files matched your search
@@ -10,3 +10,6 @@ docker-compose.override.yml
|
|||||||
.claude/
|
.claude/
|
||||||
CLAUDE.md
|
CLAUDE.md
|
||||||
.serena/
|
.serena/
|
||||||
|
|
||||||
|
# TLS material for local HTTPS - a private key never belongs in the repo.
|
||||||
|
gateway/certs/
|
||||||
+33
-3
@@ -1,4 +1,6 @@
|
|||||||
import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
|
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 { readFile, appendFile, rename, stat } from "node:fs/promises";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { fileURLToPath } from "node:url";
|
import { fileURLToPath } from "node:url";
|
||||||
@@ -249,7 +251,26 @@ const DEV_HINT = `<!doctype html><html><body style="font:14px system-ui;padding:
|
|||||||
to serve the UI from this port again.</p>
|
to serve the UI from this port again.</p>
|
||||||
</body></html>`;
|
</body></html>`;
|
||||||
|
|
||||||
const server = createServer((req, res) => {
|
/**
|
||||||
|
* TLS material, when configured.
|
||||||
|
*
|
||||||
|
* Not a nicety: `getDisplayMedia`, `getUserMedia` and `crypto.randomUUID` are
|
||||||
|
* secure-context-only, so a LAN instance reached over plain http can neither
|
||||||
|
* share a screen nor use the microphone. Chrome also hides host ICE candidates
|
||||||
|
* behind mDNS names until a media permission is granted, which keeps a
|
||||||
|
* peer-to-peer stream from connecting at all.
|
||||||
|
*
|
||||||
|
* Set TLS_CERT and TLS_KEY to enable; a self-signed pair is enough, the
|
||||||
|
* browser only has to be told once to trust it.
|
||||||
|
*/
|
||||||
|
const TLS_CERT = process.env.TLS_CERT;
|
||||||
|
const TLS_KEY = process.env.TLS_KEY;
|
||||||
|
const tlsOptions =
|
||||||
|
TLS_CERT && TLS_KEY
|
||||||
|
? { cert: readFileSync(TLS_CERT), key: readFileSync(TLS_KEY) }
|
||||||
|
: null;
|
||||||
|
|
||||||
|
const requestHandler = (req: IncomingMessage, res: ServerResponse) => {
|
||||||
void (async () => {
|
void (async () => {
|
||||||
try {
|
try {
|
||||||
const requestUrl = new URL(req.url ?? "/", "http://localhost");
|
const requestUrl = new URL(req.url ?? "/", "http://localhost");
|
||||||
@@ -284,7 +305,13 @@ const server = createServer((req, res) => {
|
|||||||
res.end(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" });
|
const wss = new WebSocketServer({ server, path: "/ws" });
|
||||||
|
|
||||||
@@ -319,7 +346,10 @@ const heartbeatTimer = setInterval(() => {
|
|||||||
wss.on("close", () => clearInterval(heartbeatTimer));
|
wss.on("close", () => clearInterval(heartbeatTimer));
|
||||||
|
|
||||||
server.listen(PORT, () => {
|
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) {
|
if (SERVE_STATIC) {
|
||||||
console.log(`Serving static UI from ${WEB_DIST}`);
|
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}`);
|
console.log(`Dev tip: use http://localhost:5173/ for live UI; rebuild web/dist after UI changes if you open :${PORT}`);
|
||||||
|
|||||||
Reference in new issue
Block a user