Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: fail loudly when the Eidoverse bridge's port is already served
Adam Eivy17a2ff22f2cb
diff --git a/docs/PORTS.md b/docs/PORTS.mdindex 5e5158950b..e2ab45ef82 100644--- a/docs/PORTS.md+++ b/docs/PORTS.md@@ -118,18 +118,40 @@ PortOS automatically detects ports from env vars:## Guidelines for New Apps-1. **Check Available Ports**: Use PortOS apps list to see which ports are in use-2. **Pick a Contiguous Range**: Choose a starting port and allocate contiguously-3. **Define PORTS Object**: Always define ports in a top-level `PORTS` constant-4. **Avoid Common Ports**: Stay away from well-known ports (80, 443, 3000, 8080, etc.)+1. **Never bind a managed app inside `5553-5569`** — that whole band belongs to PortOS and its extensions, whether or not a given port currently shows a listener. Managed apps start at `5570`. See the warning below for why a collision here does not announce itself.+2. **Check Available Ports**: Use PortOS apps list to see which ports are in use+3. **Pick a Contiguous Range**: Choose a starting port and allocate contiguously+4. **Define PORTS Object**: Always define ports in a top-level `PORTS` constant+5. **Avoid Common Ports**: Stay away from well-known ports (80, 443, 3000, 8080, etc.)## Recommended Port Ranges| Range | Purpose ||-------|---------|| 5553-5561 | PortOS core services (includes the `:5553` loopback mirror and the `portos-db` Docker container on `:5561`) |-| 5562-5569 | Reserved for PortOS extensions (5563 hosts Eidoverse on demand; 5568 is the managed llama-server default) |-| 5570-5599 | User applications |+| 5562-5569 | Reserved for PortOS extensions. Assigned: 5562 whisper, 5563 Eidoverse bridge (on demand), 5568 llama-server. Unassigned but still reserved: 5564-5567, 5569 |+| 5570-5599 | User applications — **put managed apps here** |++> **A collision inside `5553-5569` is silent, not loud.** The natural assumption is+> that a second listener on a taken port fails with `EADDRINUSE`, so an accidental+> overlap would announce itself. It does not. A wildcard bind (`0.0.0.0`) does **not**+> collide with an existing address-specific bind (`127.0.0.1`, a Tailscale address) —+> macOS and the BSDs accept both, and the *specific* bind then wins every connection.+> Two processes each believe they own the port; one of them quietly receives nothing.+>+> This is not hypothetical. A managed app bound `127.0.0.1:5563` and the Tailscale+> address explicitly, while PortOS's Eidoverse bridge bound the wildcard. Both started+> without an error, PortOS logged `🌐 Eidoverse host listening`, and the Eidoverse page+> served the other app's admin UI. Nothing in either process reported a problem.+>+> The on-demand ports (5563, 5568) are the easiest to get wrong, because they are free+> at boot and only bind once a user opens the relevant page — so a port scan taken at+> install time shows them available. Treat the whole band as taken regardless.+>+> `server/services/eidoverseHost.js` now probes `127.0.0.1:<port>` before binding and+> fails with `EIDOVERSE_HOST_PORT_CONFLICT` (409) rather than serving into the void.+> That guard covers the Eidoverse bridge only — it is not a general defence, which is+> why the range rule above still matters.PostgreSQL in native mode listens on the system default `:5432`, outside these ranges. Two third-partylocal runtimes also sit outside them, each on its own upstream default so an unmodified install worksdiff --git a/server/services/eidoverseHost.js b/server/services/eidoverseHost.jsindex 82ec5859ed..ec8935d321 100644--- a/server/services/eidoverseHost.js+++ b/server/services/eidoverseHost.js@@ -8,6 +8,13 @@ import { ServerError } from '../lib/errorHandler.js';import { EIDOVERSE_PORT } from './eidoverse.js';const BAD_GATEWAY_BODY = 'Eidoverse Worlds is not running.';++// Where a conflicting listener would sit. A local squatter almost always claims+// loopback — Docker publishes to 127.0.0.1, dev servers bind localhost — and+// loopback is also the address this bridge's own traffic arrives on when the+// page is opened from the host itself.+const CONFLICT_PROBE_HOST = '127.0.0.1';+const CONFLICT_PROBE_TIMEOUT_MS = 300;const FORWARDED_HEADER_NAMES = new Set(['host', 'x-forwarded-host', 'x-forwarded-proto']);const targetAuthority = (host, port) => `${host}:${port}`;@@ -151,9 +158,36 @@ export function createEidoverseHost({});};- const start = () => {- if (server?.listening) return Promise.resolve(status());- if (startInFlight) return startInFlight;+ /**+ * Is someone already serving this port? A wildcard bind does NOT collide with+ * an existing address-specific bind — macOS/BSD accept both, and the specific+ * bind wins every connection — so `listen('0.0.0.0')` reports success while+ * this bridge receives nothing and logs that it is listening. `listen` cannot+ * surface that, so probe before binding and fail loudly instead.+ */+ const portIsClaimed = () => new Promise((resolve) => {+ // Port 0 asks the OS for a free ephemeral port, so there is nothing to+ // collide with — and it is not a connectable address to probe.+ if (!listenPort) return resolve(false);++ const probe = createConnection({ host: CONFLICT_PROBE_HOST, port: listenPort });+ const settle = (claimed) => {+ probe.destroy();+ resolve(claimed);+ };+ probe.once('connect', () => settle(true));+ probe.once('error', () => settle(false));+ probe.setTimeout(CONFLICT_PROBE_TIMEOUT_MS, () => settle(false));+ });++ const openListener = async () => {+ if (await portIsClaimed()) {+ throw new ServerError(+ `Port ${listenPort} is already served by another process, so the Eidoverse bridge would bind without ever receiving a request. `+ + `${listenPort} is reserved for PortOS — move that app to the user range (see docs/PORTS.md), then retry.`,+ { status: 409, code: 'EIDOVERSE_HOST_PORT_CONFLICT' },+ );+ }const created = createTailscaleServers(handleHttp, { certDir, httpMirror: false });server = created.server;@@ -161,7 +195,7 @@ export function createEidoverseHost({server.on('connection', trackSocket);server.on('upgrade', handleUpgrade);- const attempt = new Promise((resolve, reject) => {+ return new Promise((resolve, reject) => {const handleListenError = (error) => reject(error);server.once('error', handleListenError);server.listen(listenPort, listenHost, () => {@@ -172,8 +206,13 @@ export function createEidoverseHost({resolve(status());});});+ };++ const start = () => {+ if (server?.listening) return Promise.resolve(status());+ if (startInFlight) return startInFlight;- startInFlight = attempt+ startInFlight = openListener().catch((error) => {server?.close();server = null;diff --git a/server/services/eidoverseHost.test.js b/server/services/eidoverseHost.test.jsindex 7ac4a11f4d..21728f4484 100644--- a/server/services/eidoverseHost.test.js+++ b/server/services/eidoverseHost.test.js@@ -109,4 +109,29 @@ describe('Eidoverse host bridge', () => {await once(client, 'close');webSocketClients.pop();});++ // A managed app on PortOS's reserved :5563 bound 127.0.0.1 explicitly while+ // this bridge bound the wildcard. macOS/BSD allow both, and the specific bind+ // wins every connection — so the bridge logged "listening", served nothing,+ // and the Eidoverse page rendered the other app's UI instead of the world.+ it('refuses to start when another process already serves the port, rather than binding to silence', async () => {+ const squatter = createServer((_req, res) => {+ res.writeHead(200);+ res.end('a different app');+ });+ const contestedPort = await listen(squatter);++ const bridge = createEidoverseHost({+ targetPort: contestedPort,+ listenHost: '0.0.0.0',+ listenPort: contestedPort,+ certDir: null,+ });+ bridges.push(bridge);++ await expect(bridge.start()).rejects.toMatchObject({+ code: 'EIDOVERSE_HOST_PORT_CONFLICT',+ status: 409,+ });+ });});