From 7658ed71898c7daca2661c7d15ffe03c8608c865 Mon Sep 17 00:00:00 2001 From: Igor Soarez Date: Fri, 20 Nov 2026 16:24:18 +0200 Subject: broker: local mode, a permanent bridge for pi sessions on this machine Upstream's bridge lives in whichever pi session binds 127.0.0.1:17318 first, so every reload or quit of that session hands the port to a race between all the others. With many sessions reloading at once, clients saw the Chrome extension as not polling for a minute or more. PI_CHROME_BROKER_LOCAL=1 runs the broker as that bridge, permanently: it accepts what upstream's bridge accepts (/command from local processes on loopback, no token, uploads not confined to a staging directory) and refuses a peer listener. pi sessions need no configuration; they find the port taken and forward to it. When a session holds the port, the broker retries every 25 ms and takes it as soon as that session lets go. --- broker/broker.mjs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/broker/broker.mjs b/broker/broker.mjs index 36fa302..fc5dfdc 100644 --- a/broker/broker.mjs +++ b/broker/broker.mjs @@ -34,6 +34,13 @@ const PKG_PATH = resolve(dirname(fileURLToPath(import.meta.url)), "..", "package const PORT = Number(process.env.PI_CHROME_BROKER_PORT ?? "17318"); const EXT_HOST = process.env.PI_CHROME_BROKER_EXT_HOST ?? "127.0.0.1"; const PEER_HOST = process.env.PI_CHROME_BROKER_PEER_HOST?.trim() || undefined; +// Local mode: the broker serves only pi sessions on this machine, as a permanent stand-in for the +// bridge upstream runs inside whichever pi session started first. It accepts what that bridge +// accepts: /command from local processes on loopback, with no token, and uploads from anywhere +// the user can read. pi sessions need no configuration; they find the port taken and forward to +// it as they would to another session. Never with a peer listener: that needs the token. +const LOCAL = /^(1|true|yes|on)$/i.test(process.env.PI_CHROME_BROKER_LOCAL?.trim() ?? ""); +if (LOCAL && PEER_HOST) throw new Error("PI_CHROME_BROKER_LOCAL serves this machine only; unset PI_CHROME_BROKER_PEER_HOST"); const DEFAULT_TIMEOUT_MS = 30_000; const MAX_TIMEOUT_MS = 10 * 60_000; @@ -67,7 +74,7 @@ function loadToken() { return token; } -const TOKEN_DIGEST = createHash("sha256").update(loadToken()).digest(); +const TOKEN_DIGEST = LOCAL ? undefined : createHash("sha256").update(loadToken()).digest(); // Compare digests rather than the raw strings: timingSafeEqual needs equal lengths, and the // length of a guess must not leak either. @@ -222,7 +229,7 @@ async function handleCommand(request, response) { sendJson(response, 403, { ok: false, error: "Chrome commands are accepted only from local Pi processes" }); return; } - if (!hasValidToken(request)) { + if (!LOCAL && !hasValidToken(request)) { sendJson(response, 401, { ok: false, error: "pi-chrome broker rejected the bearer token (check PI_CHROME_TOKEN on both ends)" }); return; } @@ -238,7 +245,7 @@ async function handleCommand(request, response) { return; } try { - assertUploadsStaged(body.action, body.params, process.env.PI_CHROME_UPLOAD_DIR?.trim() || undefined); + if (!LOCAL) assertUploadsStaged(body.action, body.params, process.env.PI_CHROME_UPLOAD_DIR?.trim() || undefined); } catch (error) { log("warn", "command", { action: body.action, peer: request.socket.remoteAddress, ok: false, refused: "upload outside the staging directory" }); sendJson(response, 403, { ok: false, error: error.message }); @@ -368,13 +375,24 @@ function listen(listener, host) { sendJson(response, 500, { ok: false, error: error.message }); }); }); + let waitingForPort = false; server.on("error", (error) => { + // Local mode: a pi session holds the port (it ran the bridge before this broker started). Keep + // trying, so the broker takes the port the moment that session reloads or quits, before + // another session can. + if (LOCAL && error.code === "EADDRINUSE") { + if (!waitingForPort) log("info", "port taken, waiting for it", { listener, host, port: PORT }); + waitingForPort = true; + setTimeout(() => server.listen(PORT, host), 25); + return; + } // Most likely the private address is not up yet. Exit and let the supervisor retry, rather than // run half a broker that the other hosts cannot reach. log("error", "listen failed", { listener, host, port: PORT, error: error.message }); process.exit(1); }); - server.listen(PORT, host, () => log("info", "listening", { listener, host, port: PORT, version: readVersion() })); + server.on("listening", () => log("info", "listening", { listener, host, port: PORT, version: readVersion(), local: LOCAL })); + server.listen(PORT, host); return server; } -- cgit v1.2.3