diff --git a/bin/agy-proxy.mjs b/bin/agy-proxy.mjs index d606ebc6a2..267ca8fd96 100755 --- a/bin/agy-proxy.mjs +++ b/bin/agy-proxy.mjs @@ -3,8 +3,9 @@ import http from 'node:http'; import { spawn } from 'node:child_process'; import crypto from 'node:crypto'; import fs from 'node:fs'; +import { parseProxyPort } from './proxy-port.mjs'; -const PORT = parseInt(process.env.AGY_PROXY_PORT || '18743', 10); +const PORT = parseProxyPort('AGY_PROXY_PORT', 18743); const HOST = process.env.AGY_PROXY_HOST || '127.0.0.1'; const MODELS = [ diff --git a/bin/antigravity-ls-proxy.mjs b/bin/antigravity-ls-proxy.mjs index d070886d72..d5f16c75c4 100755 --- a/bin/antigravity-ls-proxy.mjs +++ b/bin/antigravity-ls-proxy.mjs @@ -3,8 +3,9 @@ import http from 'node:http'; import { spawn, execSync } from 'node:child_process'; import crypto from 'node:crypto'; import fs from 'node:fs'; +import { parseProxyPort } from './proxy-port.mjs'; -const PORT = parseInt(process.env.ANTIGRAVITY_LS_PROXY_PORT || '18745', 10); +const PORT = parseProxyPort('ANTIGRAVITY_LS_PROXY_PORT', 18745); const HOST = process.env.ANTIGRAVITY_LS_PROXY_HOST || '127.0.0.1'; const DEFAULT_MANAGED_HTTP_PORT = 48999; diff --git a/bin/clef-proxy.mjs b/bin/clef-proxy.mjs index a68b9dd531..3a4ef8360a 100755 --- a/bin/clef-proxy.mjs +++ b/bin/clef-proxy.mjs @@ -3,8 +3,9 @@ import http from 'node:http'; import fs from 'node:fs'; import path from 'node:path'; import os from 'node:os'; +import { parseProxyPort } from './proxy-port.mjs'; -const PORT = parseInt(process.env.CLEF_PROXY_PORT || '18742', 10); +const PORT = parseProxyPort('CLEF_PROXY_PORT', 18742); const HOST = process.env.CLEF_PROXY_HOST || '127.0.0.1'; const readConfigFileSync = (filename, fallback = '') => { diff --git a/bin/codex-proxy.mjs b/bin/codex-proxy.mjs index df76b88c0b..c276413f34 100755 --- a/bin/codex-proxy.mjs +++ b/bin/codex-proxy.mjs @@ -3,8 +3,9 @@ import http from 'node:http'; import { spawn, execSync } from 'node:child_process'; import crypto from 'node:crypto'; import fs from 'node:fs'; +import { parseProxyPort } from './proxy-port.mjs'; -const PORT = parseInt(process.env.CODEX_PROXY_PORT || '18744', 10); +const PORT = parseProxyPort('CODEX_PROXY_PORT', 18744); const HOST = process.env.CODEX_PROXY_HOST || '127.0.0.1'; // Synchronize freshly refreshed ChatGPT OAuth tokens from Codex to OpenCode SQLite DB diff --git a/bin/design-code-proxy.mjs b/bin/design-code-proxy.mjs index 05f60a03df..177efac860 100755 --- a/bin/design-code-proxy.mjs +++ b/bin/design-code-proxy.mjs @@ -1,12 +1,13 @@ #!/usr/bin/env node import http from 'node:http'; import https from 'node:https'; +import { parseProxyPort } from './proxy-port.mjs'; -const PORT = parseInt(process.env.DESIGN_CODE_PROXY_PORT || '18746', 10); +const PORT = parseProxyPort('DESIGN_CODE_PROXY_PORT', 18746); const HOST = process.env.DESIGN_CODE_PROXY_HOST || '127.0.0.1'; -const ANTIGRAVITY_LS_PORT = parseInt(process.env.ANTIGRAVITY_LS_PROXY_PORT || '18745', 10); -const CODEX_PROXY_PORT = parseInt(process.env.CODEX_PROXY_PORT || '18744', 10); +const ANTIGRAVITY_LS_PORT = parseProxyPort('ANTIGRAVITY_LS_PROXY_PORT', 18745); +const CODEX_PROXY_PORT = parseProxyPort('CODEX_PROXY_PORT', 18744); const AMR_UPSTREAM_URL = process.env.AMR_UPSTREAM_URL || 'https://amr-link.open-design.ai/v1'; const DESIGN_SYSTEM_PROMPT = `You are an elite Code & Systems Design Architect. diff --git a/bin/proxy-port.mjs b/bin/proxy-port.mjs new file mode 100644 index 0000000000..f63b609b3e --- /dev/null +++ b/bin/proxy-port.mjs @@ -0,0 +1,33 @@ +// Strict parsing for the bin/* proxies' listen ports (#483). +// +// A bare parseInt is not a validator: 'abc' becomes NaN (an opaque +// ERR_SOCKET_BAD_PORT crash at listen time), '18744abc' and '18744.9' +// silently become 18744, and '0x4a38' becomes 0, which binds a random +// ephemeral port while every client keeps dialling the configured one. +// Every proxy parses its *_PROXY_PORT through here instead, so a bad +// value fails at startup with the env var and its value named. +// +// These scripts are launched by home-manager as +// `node ${../../bin}/.mjs` (the whole bin/ directory is copied to +// the store), so this sibling import resolves both from a checkout and +// from the store path. Do not inline-copy this helper into a proxy. + +export const parseProxyPortValue = (name, raw, fallback) => { + if (raw === undefined || raw === '') return fallback; + if (/^[0-9]+$/.test(raw)) { + const port = Number(raw); + if (port >= 1 && port <= 65535) return port; + } + throw new Error( + `${name}="${raw}" is not a valid port (expected an integer 1-65535)` + ); +}; + +export const parseProxyPort = (name, fallback) => { + try { + return parseProxyPortValue(name, process.env[name], fallback); + } catch (err) { + console.error(err.message); + process.exit(1); + } +}; diff --git a/bin/proxy-port.test.mjs b/bin/proxy-port.test.mjs new file mode 100644 index 0000000000..c4d847c2ab --- /dev/null +++ b/bin/proxy-port.test.mjs @@ -0,0 +1,160 @@ +// Tests for the shared proxy port parser (#483). +// Run: node --test bin/proxy-port.test.mjs +import assert from 'node:assert/strict'; +import { spawn, spawnSync } from 'node:child_process'; +import fs from 'node:fs'; +import net from 'node:net'; +import path from 'node:path'; +import test from 'node:test'; +import { fileURLToPath } from 'node:url'; +import { parseProxyPortValue } from './proxy-port.mjs'; + +const binDir = path.dirname(fileURLToPath(import.meta.url)); + +test('unset and empty values fall back to the default', () => { + assert.equal(parseProxyPortValue('X_PROXY_PORT', undefined, 18744), 18744); + assert.equal(parseProxyPortValue('X_PROXY_PORT', '', 18744), 18744); +}); + +test('valid decimal ports are accepted exactly', () => { + assert.equal(parseProxyPortValue('X_PROXY_PORT', '1', 18744), 1); + assert.equal(parseProxyPortValue('X_PROXY_PORT', '18744', 1), 18744); + assert.equal(parseProxyPortValue('X_PROXY_PORT', '65535', 1), 65535); +}); + +test('invalid values throw an error naming the env var and the value', () => { + const invalid = [ + 'abc', + '0x4a38', + '99999', + '65536', + '0', + '-1', + '18744.9', + '18744abc', + ' 18744', + '+18744' + ]; + for (const raw of invalid) { + assert.throws( + () => parseProxyPortValue('CODEX_PROXY_PORT', raw, 18744), + (err) => { + assert.match(err.message, /CODEX_PROXY_PORT/); + assert.ok( + err.message.includes(`"${raw}"`), + `message should quote the value: ${err.message}` + ); + assert.match(err.message, /integer 1-65535/); + return true; + }, + `expected ${JSON.stringify(raw)} to be rejected` + ); + } +}); + +const runClef = (value) => + spawnSync(process.execPath, [path.join(binDir, 'clef-proxy.mjs')], { + env: { ...process.env, CLEF_PROXY_PORT: value }, + encoding: 'utf8', + timeout: 10000 + }); + +test('clef-proxy exits at startup naming the var for abc / 0x… / 99999', () => { + for (const raw of ['abc', '0x4a38', '99999']) { + const result = runClef(raw); + assert.equal(result.status, 1, `value ${raw}: ${result.stderr}`); + assert.match(result.stderr, /CLEF_PROXY_PORT/); + assert.ok(result.stderr.includes(`"${raw}"`), result.stderr); + assert.doesNotMatch(result.stdout, /listening/); + } +}); + +test('clef-proxy binds exactly the requested valid port', async (t) => { + const probe = net.createServer(); + await new Promise((resolve) => probe.listen(0, '127.0.0.1', resolve)); + const port = probe.address().port; + await new Promise((resolve) => probe.close(resolve)); + + const child = spawn(process.execPath, [path.join(binDir, 'clef-proxy.mjs')], { + env: { ...process.env, CLEF_PROXY_PORT: String(port) }, + stdio: ['ignore', 'pipe', 'pipe'] + }); + t.after(() => child.kill()); + const listening = await new Promise((resolve, reject) => { + let out = ''; + const timer = setTimeout( + () => reject(new Error(`no listening line: ${out}`)), + 10000 + ); + child.stdout.on('data', (chunk) => { + out += chunk; + if (out.includes('listening')) { + clearTimeout(timer); + resolve(out); + } + }); + child.on('exit', () => { + clearTimeout(timer); + reject(new Error(`proxy exited early: ${out}`)); + }); + }); + assert.ok( + listening.includes(`http://127.0.0.1:${port}`), + `expected the requested port in: ${listening}` + ); + const res = await fetch(`http://127.0.0.1:${port}/health`); + assert.equal(res.status, 200); + assert.equal((await res.json()).port, port); +}); + +test('all seven parse sites in the five proxies use the shared helper', () => { + const files = [ + 'codex-proxy.mjs', + 'clef-proxy.mjs', + 'agy-proxy.mjs', + 'antigravity-ls-proxy.mjs', + 'design-code-proxy.mjs' + ]; + let sites = 0; + for (const file of files) { + const source = fs.readFileSync(path.join(binDir, file), 'utf8'); + assert.match( + source, + /import \{ parseProxyPort \} from '\.\/proxy-port\.mjs';/, + `${file} should import the shared helper` + ); + assert.doesNotMatch( + source, + /parseInt\(process\.env\.[A-Z_]*PROXY_PORT/, + `${file} still parses a proxy port with bare parseInt` + ); + sites += source.match(/parseProxyPort\('[A-Z_]+_PROXY_PORT', \d+\)/g).length; + } + assert.equal(sites, 7); +}); + +test('home-manager launches the proxies from the bin directory', () => { + // default.nix used to interpolate each script as a single store file + // (${../../bin/.mjs}); a sibling import like ./proxy-port.mjs + // cannot resolve from there. The directory form must stay. + const nix = fs.readFileSync( + path.join(binDir, '..', 'home', 'common', 'default.nix'), + 'utf8' + ); + for (const file of [ + 'codex-proxy.mjs', + 'clef-proxy.mjs', + 'agy-proxy.mjs', + 'antigravity-ls-proxy.mjs', + 'design-code-proxy.mjs' + ]) { + assert.ok( + nix.includes('${../../bin}/' + file), + `default.nix should launch ${file} via the bin directory` + ); + assert.ok( + !nix.includes('${../../bin/' + file + '}'), + `default.nix still launches ${file} as a single store file` + ); + } +}); diff --git a/home/common/default.nix b/home/common/default.nix index 6bda78902d..3a98398dfb 100644 --- a/home/common/default.nix +++ b/home/common/default.nix @@ -462,7 +462,7 @@ in Service = { ExecStart = toString ( pkgs.writeShellScript "clef-proxy-start" '' - exec ${pkgs.nodejs}/bin/node ${../../bin/clef-proxy.mjs} + exec ${pkgs.nodejs}/bin/node ${../../bin}/clef-proxy.mjs '' ); Restart = "always"; @@ -486,7 +486,7 @@ in ExecStart = toString ( pkgs.writeShellScript "agy-proxy-start" '' export PATH="$HOME/.gemini/bin:$HOME/.local/bin:$PATH" - exec ${pkgs.nodejs}/bin/node ${../../bin/agy-proxy.mjs} + exec ${pkgs.nodejs}/bin/node ${../../bin}/agy-proxy.mjs '' ); Restart = "no"; @@ -507,7 +507,7 @@ in ExecStart = toString ( pkgs.writeShellScript "codex-proxy-start" '' export PATH="/run/current-system/sw/bin:$HOME/.local/bin:$PATH" - exec ${pkgs.nodejs}/bin/node ${../../bin/codex-proxy.mjs} + exec ${pkgs.nodejs}/bin/node ${../../bin}/codex-proxy.mjs '' ); Restart = "always"; @@ -530,7 +530,7 @@ in ExecStart = toString ( pkgs.writeShellScript "antigravity-ls-proxy-start" '' export PATH="/run/current-system/sw/bin:$HOME/.gemini/bin:$HOME/.local/bin:$PATH" - exec ${pkgs.nodejs}/bin/node ${../../bin/antigravity-ls-proxy.mjs} + exec ${pkgs.nodejs}/bin/node ${../../bin}/antigravity-ls-proxy.mjs '' ); Restart = "always"; @@ -562,7 +562,7 @@ in ExecStart = toString ( pkgs.writeShellScript "design-code-proxy-start" '' export PATH="/run/current-system/sw/bin:$HOME/.local/bin:$PATH" - exec ${pkgs.nodejs}/bin/node ${../../bin/design-code-proxy.mjs} + exec ${pkgs.nodejs}/bin/node ${../../bin}/design-code-proxy.mjs '' ); Restart = "always"; @@ -1242,23 +1242,23 @@ in [ libnotify (pkgs.writeShellScriptBin "clef-proxy" '' - exec ${pkgs.nodejs}/bin/node ${../../bin/clef-proxy.mjs} "$@" + exec ${pkgs.nodejs}/bin/node ${../../bin}/clef-proxy.mjs "$@" '') (pkgs.writeShellScriptBin "agy-proxy" '' export PATH="$HOME/.gemini/bin:$HOME/.local/bin:$PATH" - exec ${pkgs.nodejs}/bin/node ${../../bin/agy-proxy.mjs} "$@" + exec ${pkgs.nodejs}/bin/node ${../../bin}/agy-proxy.mjs "$@" '') (pkgs.writeShellScriptBin "codex-proxy" '' export PATH="/run/current-system/sw/bin:$HOME/.local/bin:$PATH" - exec ${pkgs.nodejs}/bin/node ${../../bin/codex-proxy.mjs} "$@" + exec ${pkgs.nodejs}/bin/node ${../../bin}/codex-proxy.mjs "$@" '') (pkgs.writeShellScriptBin "antigravity-ls-proxy" '' export PATH="/run/current-system/sw/bin:$HOME/.gemini/bin:$HOME/.local/bin:$PATH" - exec ${pkgs.nodejs}/bin/node ${../../bin/antigravity-ls-proxy.mjs} "$@" + exec ${pkgs.nodejs}/bin/node ${../../bin}/antigravity-ls-proxy.mjs "$@" '') (pkgs.writeShellScriptBin "design-code-proxy" '' export PATH="/run/current-system/sw/bin:$HOME/.local/bin:$PATH" - exec ${pkgs.nodejs}/bin/node ${../../bin/design-code-proxy.mjs} "$@" + exec ${pkgs.nodejs}/bin/node ${../../bin}/design-code-proxy.mjs "$@" '') (pkgs.writeShellScriptBin "sync-models" '' export PATH="/run/current-system/sw/bin:$HOME/.gemini/bin:$HOME/.local/bin:$PATH"