- The agent finds frps/frpc's config (Hub path, systemd -c, usual places) and reads INI and TOML. - New jobs frp-read, frp-write (secrets kept on the server, verify, backup, restart, roll back) and probe-ports (which ports are free here), on central and church servers. - Stats report frpConfigPath; crosscode gets FRPC_INI on church servers. - Website plan versions move up to contracts 1.16.0 and Hub 2.12.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
168 lines
7.5 KiB
TypeScript
168 lines
7.5 KiB
TypeScript
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import { agent } from "@crosscode/contracts";
|
|
import {
|
|
configFromCommand, findConfig, forgetFound, hideSecrets, parseExecStart, parseSettings, probePorts, readConfig, restoreSecrets, writeConfig,
|
|
} from "../src/frp";
|
|
import { tempState } from "./helpers";
|
|
|
|
const H = agent.FRP_HIDDEN;
|
|
|
|
const TOML = `bindPort = 7000
|
|
proxyBindAddr = "127.0.0.1"
|
|
auth.token = "s3cret-token" # the shared token
|
|
allowPorts = [
|
|
{ start = 20000, end = 29999 },
|
|
{ single = 3001 },
|
|
]
|
|
|
|
[webServer]
|
|
port = 7500
|
|
password = 'dash-pass'
|
|
`;
|
|
|
|
const FRPC_INI = `[common]
|
|
server_addr = 203.0.113.10
|
|
server_port = 7000
|
|
token = abc123
|
|
|
|
[hope-events-web]
|
|
type = tcp
|
|
local_ip = 127.0.0.1
|
|
local_port = 20010
|
|
remote_port = 20010
|
|
`;
|
|
|
|
const noSystemd = async () => ({ ok: false, out: "", err: "", missing: true });
|
|
|
|
describe("finding frp's config", () => {
|
|
it("reads -c from the systemd unit", () => {
|
|
expect(configFromCommand("/usr/local/bin/frps -c /usr/local/frp/frps.toml")).toBe("/usr/local/frp/frps.toml");
|
|
expect(configFromCommand("/usr/bin/frps --config=/etc/frps/frps.ini")).toBe("/etc/frps/frps.ini");
|
|
expect(configFromCommand("/usr/bin/frps")).toBeNull();
|
|
expect(parseExecStart("{ path=/usr/local/bin/frps ; argv[]=/usr/local/bin/frps -c /opt/frp/frps.toml ; ignore_errors=no ; start_time=[n/a] }"))
|
|
.toEqual({ config: "/opt/frp/frps.toml", bin: "/usr/local/bin/frps" });
|
|
});
|
|
|
|
it("the path from the Hub wins, and is remembered", async () => {
|
|
const dir = tempState();
|
|
const file = path.join(dir, "frps.toml");
|
|
fs.writeFileSync(file, TOML);
|
|
forgetFound();
|
|
const exec = async () => ({ ok: true, out: "active\n", err: "", missing: false });
|
|
const r = await readConfig("central", { path: file }, { exec });
|
|
expect(r).toMatchObject({ found: true, source: "hub", path: file, format: "toml", proxyBindAddr: "127.0.0.1", allowPorts: "20000-29999,3001", serviceRunning: true });
|
|
forgetFound();
|
|
expect((await findConfig("central", { fresh: true })).path).toBe(file);
|
|
});
|
|
|
|
it("a path that isn't there is reported, not remembered", async () => {
|
|
const dir = tempState();
|
|
forgetFound();
|
|
const r = await readConfig("central", { path: path.join(dir, "nope", "frps.ini") }, { exec: noSystemd });
|
|
expect(r).toMatchObject({ found: false, content: null });
|
|
expect(fs.existsSync(path.join(dir, "frp.json"))).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("frp settings, INI and TOML", () => {
|
|
it("TOML", () => {
|
|
expect(parseSettings(TOML, "toml")).toMatchObject({ proxyBindAddr: "127.0.0.1", allowPorts: "20000-29999,3001" });
|
|
expect(parseSettings(TOML, "toml").usedPorts).toEqual(expect.arrayContaining([7000, 7500]));
|
|
});
|
|
it("INI: frpc's tunnels use their ports", () => {
|
|
expect(parseSettings(FRPC_INI, "ini").usedPorts).toEqual([20010]);
|
|
});
|
|
});
|
|
|
|
describe("secrets never leave the server", () => {
|
|
it("are hidden in TOML and INI, keeping the quotes", () => {
|
|
const t = hideSecrets(TOML, "toml");
|
|
expect(t.content).toContain(`auth.token = "${H}" # the shared token`);
|
|
expect(t.content).toContain(`password = '${H}'`);
|
|
expect(t.content).not.toMatch(/s3cret|dash-pass/);
|
|
expect(t.hidden).toEqual(["auth.token", "webServer.password"]);
|
|
const i = hideSecrets(FRPC_INI, "ini");
|
|
expect(i.content).toContain(`token = ${H}`);
|
|
expect(i.content).not.toContain("abc123");
|
|
});
|
|
it("in inline tables too", () => {
|
|
expect(hideSecrets(`auth = { method = "token", token = "zzz" }\n`, "toml").content).not.toContain("zzz");
|
|
});
|
|
it("are put back on write, and can't be changed from the Hub", () => {
|
|
const shown = hideSecrets(TOML, "toml").content.replace("bindPort = 7000", "bindPort = 7001");
|
|
const back = restoreSecrets(shown, TOML, "toml");
|
|
expect(back.ok && back.content).toBe(TOML.replace("bindPort = 7000", "bindPort = 7001"));
|
|
expect(restoreSecrets(shown.replace(`"${H}"`, '"new"'), TOML, "toml")).toMatchObject({ ok: false, message: expect.stringMatching(/auth.token is a secret/) });
|
|
expect(restoreSecrets(`${shown}\n[extra]\ntoken = "${H}"\n`, TOML, "toml")).toMatchObject({ ok: false });
|
|
});
|
|
});
|
|
|
|
describe("writing the config", () => {
|
|
const setup = () => {
|
|
const dir = tempState();
|
|
const file = path.join(dir, "frps.toml");
|
|
fs.writeFileSync(file, TOML);
|
|
fs.writeFileSync(path.join(dir, "frp.json"), JSON.stringify({ path: file }));
|
|
forgetFound();
|
|
return { dir, file };
|
|
};
|
|
const shown = () => hideSecrets(TOML, "toml").content;
|
|
|
|
it("verify, backup, replace, restart", async () => {
|
|
const { dir, file } = setup();
|
|
const calls: string[] = [];
|
|
const exec = async (cmd: string, args: string[]) => { calls.push(`${cmd} ${args.join(" ")}`); return { ok: true, out: "active", err: "", missing: false }; };
|
|
const r = await writeConfig("central", { path: file, content: shown().replace("bindPort = 7000", "bindPort = 7001"), restart: true }, { exec, wait: async () => {} });
|
|
expect(r.status).toBe("succeeded");
|
|
expect(fs.readFileSync(file, "utf8")).toContain("bindPort = 7001");
|
|
expect(fs.readFileSync(file, "utf8")).toContain("s3cret-token");
|
|
expect(fs.readdirSync(path.join(dir, "backups"))).toHaveLength(1);
|
|
expect(calls.some((c) => / verify -c /.test(c))).toBe(true);
|
|
expect(calls).toContain("systemctl restart frps");
|
|
expect(JSON.stringify(r.result)).not.toContain("s3cret");
|
|
});
|
|
|
|
it("verify refuses: nothing changes", async () => {
|
|
const { file } = setup();
|
|
const exec = async (cmd: string) => ({ ok: cmd === "systemctl", out: "", err: "bad key", missing: false });
|
|
const r = await writeConfig("central", { path: file, content: "nonsense = [", restart: true }, { exec, wait: async () => {} });
|
|
expect(r.status).toBe("failed");
|
|
expect(fs.readFileSync(file, "utf8")).toBe(TOML);
|
|
});
|
|
|
|
it("frps doesn't come back: the old file is put back", async () => {
|
|
const { file } = setup();
|
|
let restarts = 0;
|
|
const exec = async (_cmd: string, args: string[]) => {
|
|
if (args[0] === "restart") restarts++;
|
|
if (args[0] === "is-active") return { ok: true, out: restarts >= 2 ? "active" : "failed", err: "", missing: false };
|
|
return { ok: true, out: "", err: "", missing: false };
|
|
};
|
|
const r = await writeConfig("central", { path: file, content: shown().replace("bindPort = 7000", "bindPort = 7001"), restart: true }, { exec, wait: async () => {} });
|
|
expect(r.status).toBe("rolled-back");
|
|
expect(fs.readFileSync(file, "utf8")).toBe(TOML);
|
|
});
|
|
|
|
it("only the file the agent uses", async () => {
|
|
const { dir } = setup();
|
|
const r = await writeConfig("central", { path: path.join(dir, "frpc.toml"), content: "x = 1", restart: false }, { exec: noSystemd });
|
|
expect(r.status).toBe("refused");
|
|
});
|
|
});
|
|
|
|
describe("free ports before an install", () => {
|
|
it("central: not listening and inside allow_ports", () => {
|
|
const listening = new Map([[20001, ["0.0.0.0"]]]);
|
|
const r = probePorts([20000, 20001, 30000], "central", parseSettings(TOML, "toml"), listening, "/etc/frp/frps.toml");
|
|
expect(r.ports.map((p) => [p.port, p.free])).toEqual([[20000, true], [20001, false], [30000, false]]);
|
|
});
|
|
it("church: not listening and not in frpc's tunnels", () => {
|
|
const r = probePorts([20010, 20011], "church", parseSettings(FRPC_INI, "ini"), new Map(), "/etc/frp/frpc.ini");
|
|
expect(r.ports.map((p) => [p.port, p.free])).toEqual([[20010, false], [20011, true]]);
|
|
});
|
|
it("with no config found, listening is all it can check", () => {
|
|
expect(probePorts([20010], "central", null, new Map(), null).ports[0]!.free).toBe(true);
|
|
});
|
|
});
|