Files
plugins/packages/agent-console/daemon/src/config.ts
T
qingnian108 270b228a96 agent-console 1.2.0: Preview tab, chat links, file chips, font size/width settings, live magpie theme (#582)
agent-console 1.2.0 by qingnian108 (#582): Preview tab, chat links, file chips, font size/width settings, live magpie theme.

Reviewed and run at c459773107caa20f7063d12b2133c230f91c066c (mote, macOS, Node 22, sandbox HOME):
- npm ci --workspaces=false; npm run check ok; npm test: 48 files, 339 passed, 17 skipped; npm run verify:build 5/5 OK; bun scripts/check.mjs agent-console ok.
- Only test/v1/ws.test.ts changed since the last reviewed head 66d1070 (the new "preview.open returns host from previewHost()" test, through registerMethods).
- Broke by hand: methods.ts `return { ...r, host: previewHost() }` -> `return r;` turns that test red (1 of 5); restored, green. Changing previewHost's body alone stays green, as the test compares to previewHost(); fix582.test.ts covers its values.
- Earlier reviews' findings (per-listener key host, nonce/canary, gateway port + identity check, mentions path, Darwin major from the daemon) were checked in the earlier rounds. The proxy listens on 127.0.0.1 only and answers only the keyed Host.
- Not run: preview-attack.mjs browser half at this head, Windows box.
2026-11-04 11:22:25 +08:00

72 lines
3.1 KiB
TypeScript

// Arguments and environment: port, dataDir, token, version, idleExitMinutes
import { randomBytes } from "node:crypto";
import { readFileSync } from "node:fs";
import { homedir } from "node:os";
import { join, resolve, dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { daemonBuildStamp } from "./util/buildStamp.js";
export type Config = {
port: number;
dataDir: string;
token: string;
version: string;
idleExitMinutes: number;
buildStamp: string;
parentPid?: number;
/** The plugin gate's port (index.mjs passes --gate-port); the preview refuses it */
gatePort?: number;
};
function pkgVersion(): string {
try {
const here = dirname(fileURLToPath(import.meta.url));
// dist/main.js → ../../package.json; src/main.ts also ends up in dist via esbuild
const pkg = JSON.parse(readFileSync(join(here, "..", "..", "package.json"), "utf8"));
return String(pkg.version || "0.0.0");
} catch {
return "0.0.0";
}
}
// --data <dir> sets the data root (equivalent to setting XDG_CONFIG_HOME and then joining magpie/agent-console?
// EXEC §5.2: --data <dir> (data root, equivalent to setting XDG_CONFIG_HOME; default $XDG_CONFIG_HOME
// or ~/.config, then joined with magpie/agent-console). i.e. the --data value has XDG_CONFIG_HOME semantics.
export function parseConfig(argv: string[]): Config {
let port: number | null = null;
let data: string | null = null;
let token: string | null = null;
let parentPid: number | undefined;
let gatePort: number | undefined;
for (let i = 0; i < argv.length; i++) {
const a = argv[i];
if (a === "--port" && argv[i + 1]) port = Number(argv[++i]);
else if (a.startsWith("--port=")) port = Number(a.slice(7));
else if (a === "--data" && argv[i + 1]) data = argv[++i];
else if (a.startsWith("--data=")) data = a.slice(7);
else if (a === "--token" && argv[i + 1]) token = argv[++i];
else if (a.startsWith("--token=")) token = a.slice(8);
else if (a === "--parent-pid" && argv[i + 1]) parentPid = Number(argv[++i]);
else if (a.startsWith("--parent-pid=")) parentPid = Number(a.slice(13));
else if (a === "--gate-port" && argv[i + 1]) gatePort = Number(argv[++i]);
else if (a.startsWith("--gate-port=")) gatePort = Number(a.slice(12));
}
if (port == null || !Number.isInteger(port) || port < 0 || port > 65535) {
const envPort = Number(process.env.AGENT_CONSOLE_PORT || "");
port = Number.isInteger(envPort) && envPort >= 0 && envPort <= 65535 ? envPort : 3488;
}
const xdg = data ?? process.env.XDG_CONFIG_HOME ?? join(homedir(), ".config");
const dataDir = resolve(join(xdg, "magpie", "agent-console"));
const idleEnv = Number(process.env.AGENT_CONSOLE_IDLE_EXIT_MIN || "");
return {
port,
dataDir,
token: token ?? process.env.AGENT_CONSOLE_TOKEN ?? randomBytes(16).toString("hex"),
version: pkgVersion(),
idleExitMinutes: Number.isFinite(idleEnv) && idleEnv > 0 ? idleEnv : 60,
buildStamp: daemonBuildStamp(),
parentPid: Number.isInteger(parentPid) ? parentPid : undefined,
gatePort: Number.isInteger(gatePort) && gatePort! > 0 && gatePort! <= 65535 ? gatePort : undefined,
};
}