serve.mjs: an https port in use leaves http serving (PB-7)

Neither listener had an error handler, so a second serve.mjs died on the
fixed https port 6808 though its http port was free. An https listen
error now warns and the server goes on http-only, redirecting LAN clients
only once https listens; an http listen error says which port and exits 1.
performance.md's second-site-root recipe passes HTTPS=0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Xda8d7rfJjkFSmKVQtcBGh
This commit is contained in:
Konstantin Pereiaslov 2026-11-14 15:19:31 -06:00
parent cc2c4ff154
commit 28d19e9e85
2 changed files with 23 additions and 12 deletions

View file

@ -430,7 +430,7 @@ always revert with `git apply -R`.
- **Board configs:** `site/dist/boards.tar` is shared by every dist and
repacked from `bsp/` on every ninja-fast deploy. A dist that needs
another BSP must be served from a second site root
(`WEB_SITE_DIR=<root> PORT=8081 node serve.mjs`, then
(`WEB_SITE_DIR=<root> PORT=8081 HTTPS=0 node serve.mjs`, then
`PORT_B=8081 tools/perf/swab.sh ...`).
**Normal absolute values on a quiet host** (free-run, bounds checks,

View file

@ -310,24 +310,35 @@ const proxyGprs = (req, socket, head) => {
// Any other Upgrade (h2c) is ignored, as HTTP/1.1 allows: the request is
// answered as a plain one.
let httpsUp = false; // the redirect needs an https listener to go to
const httpServer = http.createServer({ shouldUpgradeCallback: isWebSocket }, (req, res) => {
if (cert && redirectNonLoopbackToHttps(req, res)) return;
if (httpsUp && redirectNonLoopbackToHttps(req, res)) return;
serve(req, res);
});
httpServer.on("upgrade", proxyGprs);
httpServer.listen(port, "0.0.0.0");
httpServer.on("error", (e) => {
console.error(`serve.mjs: cannot serve http on port ${port}: ${e.code === "EADDRINUSE" ? "the port is in use" : e.message}`);
process.exit(1);
});
httpServer.listen(port, "0.0.0.0", () =>
console.log(`qemu-pmb887x web: http://127.0.0.1:${port} (serving ${root})`));
// https is for other devices only: a port that is taken (another serve.mjs
// runs) leaves this one serving http
if (cert) {
const httpsServer = https.createServer(
{ key: cert.key, cert: cert.cert, shouldUpgradeCallback: isWebSocket }, serve);
httpsServer.on("upgrade", proxyGprs);
httpsServer.listen(httpsPort, "0.0.0.0");
httpsServer.on("error", (e) => {
console.warn(`https disabled, serving http only: port ${httpsPort}: ` +
`${e.code === "EADDRINUSE" ? "in use (HTTPS_PORT picks another, HTTPS=0 turns https off)" : e.message}`);
});
httpsServer.listen(httpsPort, "0.0.0.0", () => {
httpsUp = true;
const urls = lanIPs().map((ip) => `https://${ip}:${httpsPort}`).join(" ");
console.log(
` from other devices (phone/LAN): ${urls || `https://<lan-ip>:${httpsPort}`}` +
` — self-signed cert, accept the browser warning once`,
);
});
}
const urls = lanIPs().map((ip) => `https://${ip}:${httpsPort}`).join(" ");
console.log(`qemu-pmb887x web: http://127.0.0.1:${port} (serving ${root})`);
if (cert)
console.log(
` from other devices (phone/LAN): ${urls || `https://<lan-ip>:${httpsPort}`}` +
` — self-signed cert, accept the browser warning once`,
);