Agent 1.4.0 + contracts 1.15.0: frp config anywhere, edited from the Hub; ports probed before installs

- 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>
This commit is contained in:
2026-11-21 11:07:50 +02:00
co-authored by Claude Opus 5.5
parent 4cc30c1445
commit 8542d19be0
23 changed files with 1183 additions and 84 deletions
+80 -3
View File
@@ -81,6 +81,25 @@ export const isSecretEnvName = (name: string): boolean => SECRET_ENV_NAME.test(n
/** At most this many changes in one `env-set` job. */
export const ENV_SET_MAX = 50;
/** The first agent that runs `frp-read`, `frp-write` and `probe-ports` jobs (an older one refuses the unknown types). */
export const FRP_JOBS_MIN_AGENT = "1.4.0";
/**
* Where frps (central server) or frpc (church server) keeps its config: an absolute path to a file
* whose name starts with `frp` and ends in `.ini` or `.toml` (`/etc/frp/frps.toml`).
*/
export const frpConfigPathSchema = serverPathSchema.regex(/\/frp[A-Za-z0-9._-]*\.(ini|toml)$/, "Expected the path of frps's or frpc's config: a file named frp….ini or frp….toml");
/** The longest frp config the Hub reads or writes. */
export const FRP_CONFIG_MAX = 64_000;
/**
* Secret values in an frp config (token, passwords) never leave the server: `frp-read` shows them
* as this, and `frp-write` puts the server's own value back where it finds it. A secret can't be
* changed from the Hub.
*/
export const FRP_HIDDEN = "***hidden***";
/** Each job type and the arguments it takes. Unknown keys are refused, not stripped. */
export const jobArgsSchemas = {
/** `crosscode install <church> --products … --frp --json` */
@@ -151,6 +170,24 @@ export const jobArgsSchemas = {
purge: z.strictObject({ church: shortNameSchema }),
/** The agent updates itself to that tag. */
"update-agent": z.strictObject({ version: agentTagSchema }),
/**
* frps's config (central server) or frpc's (church server), secrets hidden: an {@link frpReadResultSchema}.
* With `path`, the agent uses (and remembers) that file instead of the one it finds itself.
*/
"frp-read": z.strictObject({ path: frpConfigPathSchema.optional() }),
/**
* Replaces frps's or frpc's config: secrets put back from the file on the server, checked with
* `frps verify` / `frpc verify`, a backup kept, then (with `restart`) the service restarted. When
* it doesn't come back, the old file is put back (`rolled-back`). `path` must be the file the
* agent uses now (what the last `frp-read` showed).
*/
"frp-write": z.strictObject({ path: frpConfigPathSchema, content: z.string().min(1).max(FRP_CONFIG_MAX), restart: z.boolean() }),
/**
* Before an install: which of these ports are free here? On the central server: nothing listens
* on it and frps's allow_ports has it. On a church server: nothing listens on it and frpc's config
* doesn't use it. The result is a {@link probePortsResultSchema}.
*/
"probe-ports": z.strictObject({ ports: z.array(portSchema).min(1).max(50) }),
/** Central server only: are these ports allowed, open and bound to 127.0.0.1? */
"check-ports": z.strictObject({ ports: z.array(portSchema).min(1).max(50) }),
/** `crosscode status [<church>] --json` */
@@ -163,12 +200,13 @@ export const jobTypeSchema = z.enum(JOB_TYPES as [JobType, ...JobType[]]);
export type JobArgs<T extends JobType> = z.infer<(typeof jobArgsSchemas)[T]>;
/** The only jobs a central agent runs (§3). */
export const CENTRAL_JOB_TYPES: readonly JobType[] = ["check-ports", "status", "update-agent"];
export const CENTRAL_JOB_TYPES: readonly JobType[] = ["check-ports", "status", "update-agent", "frp-read", "frp-write", "probe-ports"];
/** Time limits per job type, in minutes (§3). A job past it is stopped and reported as failed. */
export const JOB_TIME_LIMIT_MINUTES: Record<JobType, number> = {
install: 30, "add-product": 30, deploy: 20, adopt: 30, "adopt-undo": 15, restart: 5, "seed-demo": 15, "env-read": 2, "env-set": 20, export: 20,
uninstall: 20, purge: 10, "update-agent": 15, "check-ports": 2, status: 3,
"frp-read": 2, "frp-write": 5, "probe-ports": 2,
};
/** Config sealed to the server's X25519 key (`sealConfig` in `@crosscode/contracts/signing`). */
@@ -213,6 +251,7 @@ function jobOf<T extends JobType>(type: T) {
export const agentJobSchema = z.discriminatedUnion("type", [
jobOf("install"), jobOf("add-product"), jobOf("deploy"), jobOf("adopt"), jobOf("adopt-undo"), jobOf("restart"), jobOf("seed-demo"),
jobOf("env-read"), jobOf("env-set"), jobOf("export"), jobOf("uninstall"), jobOf("purge"), jobOf("update-agent"), jobOf("check-ports"), jobOf("status"),
jobOf("frp-read"), jobOf("frp-write"), jobOf("probe-ports"),
]);
export type AgentJob = z.infer<typeof agentJobSchema>;
export type UnsignedAgentJob = Omit<AgentJob, "signature">;
@@ -280,9 +319,9 @@ export const processStatSchema = z.object({
});
export const centralStatSchema = z.object({
/** `proxy_bind_addr` in frps.ini, null when it isn't set (tunnels then open on every address). */
/** `proxy_bind_addr` (frps.ini) or `proxyBindAddr` (frps.toml), null when it isn't set (tunnels then open on every address). */
proxyBindAddr: z.string().max(100).nullable(),
/** `allow_ports` in frps.ini as written, e.g. `20000-29999`. */
/** `allow_ports` as written (`20000-29999`); frps.toml's `allowPorts` is written the same way. */
allowPorts: z.string().max(2000).nullable(),
frpsRunning: z.boolean().nullable(),
});
@@ -298,6 +337,8 @@ export const agentStatsSchema = z.object({
frpcRunning: z.boolean().nullable(),
processes: z.array(processStatSchema).max(200),
central: centralStatSchema.nullable().optional(),
/** Agent 1.4.0+: the frps (central) or frpc (church) config the agent uses, null when it found none. */
frpConfigPath: z.string().max(255).nullable().optional(),
});
export type AgentStats = z.infer<typeof agentStatsSchema>;
@@ -395,6 +436,42 @@ export const envReadResultSchema = z.object({
});
export type EnvReadResult = z.infer<typeof envReadResultSchema>;
// ---------------------------------------------------------------------------------------------
// frp config and free ports (contracts 1.15.0, agent 1.4.0)
// ---------------------------------------------------------------------------------------------
/** How the agent found the frp config: set from the Hub, the systemd unit's `-c`, or a usual place. */
export const FRP_CONFIG_SOURCES = ["hub", "systemd", "usual-place"] as const;
/** The `result` of an `frp-read` report (and of an `frp-write` that went through). */
export const frpReadResultSchema = z.object({
/** `frps` on the central server, `frpc` on a church server. */
service: z.enum(["frps", "frpc"]),
path: z.string().max(255).nullable(),
source: z.enum(FRP_CONFIG_SOURCES).nullable(),
found: z.boolean(),
format: z.enum(["ini", "toml"]).nullable(),
/** The file with each secret value replaced by {@link FRP_HIDDEN}. */
content: z.string().max(FRP_CONFIG_MAX).nullable(),
/** The names of the hidden values (`token`, `auth.token`, `webServer.password` …). */
hidden: z.array(z.string().max(100)).max(50),
/** frps only (null on a church server): what the Hub checks. */
proxyBindAddr: z.string().max(100).nullable(),
allowPorts: z.string().max(2000).nullable(),
serviceRunning: z.boolean().nullable(),
/** Where the agent looked, when it found nothing. */
looked: z.array(z.string().max(255)).max(20).optional(),
readAt: dateTimeSchema,
});
export type FrpReadResult = z.infer<typeof frpReadResultSchema>;
/** The `result` of a `probe-ports` report. */
export const probePortsResultSchema = z.object({
ports: z.array(z.object({ port: portSchema, free: z.boolean(), reason: z.string().max(200).nullable() })).max(50),
configPath: z.string().max(255).nullable(),
});
export type ProbePortsResult = z.infer<typeof probePortsResultSchema>;
/** The variables a layer must provide for a template, that it doesn't (the Hub's check before a job). */
export function missingRequired(template: EnvTemplate, layer: "server" | "defaults", values: Record<string, string | undefined>): string[] {
const missing: string[] = [];