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
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@crosscode/contracts",
"version": "1.14.0",
"version": "1.15.0",
"description": "The shared JSON formats used by every Cross Code product: zod schemas, JSON Schema, TypeScript types and sample payloads",
"license": "UNLICENSED",
"type": "commonjs",
+314
View File
@@ -1675,6 +1675,320 @@
"args"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"schema": {
"type": "string",
"const": "crosscode.agent-job/1"
},
"id": {
"type": "string",
"pattern": "^job_[A-Za-z0-9_-]{8,64}$"
},
"serverId": {
"type": "string",
"pattern": "^srv_[A-Za-z0-9_-]{8,64}$"
},
"config": {
"anyOf": [
{
"type": "object",
"properties": {
"alg": {
"type": "string",
"const": "x25519-hkdf-sha256-aes-256-gcm"
},
"epk": {
"type": "string",
"pattern": "^[A-Za-z0-9_-]{43}$"
},
"iv": {
"type": "string",
"pattern": "^[A-Za-z0-9_-]{16}$"
},
"ciphertext": {
"type": "string",
"maxLength": 200000,
"pattern": "^[A-Za-z0-9_-]+$"
}
},
"required": [
"alg",
"epk",
"iv",
"ciphertext"
],
"additionalProperties": false
},
{
"type": "null"
}
]
},
"notBefore": {
"type": "string",
"format": "date-time",
"pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z))$"
},
"expiresAt": {
"type": "string",
"format": "date-time",
"pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z))$"
},
"signature": {
"type": "string",
"pattern": "^[A-Za-z0-9_-]{86}$"
},
"type": {
"type": "string",
"const": "frp-read"
},
"args": {
"type": "object",
"properties": {
"path": {
"type": "string",
"maxLength": 255,
"allOf": [
{
"pattern": "^\\/(?!.*\\/\\.\\.?(\\/|$))[A-Za-z0-9._/-]+$"
},
{
"pattern": "\\/frp[A-Za-z0-9._-]*\\.(ini|toml)$"
}
]
}
},
"additionalProperties": false
}
},
"required": [
"schema",
"id",
"serverId",
"notBefore",
"expiresAt",
"signature",
"type",
"args"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"schema": {
"type": "string",
"const": "crosscode.agent-job/1"
},
"id": {
"type": "string",
"pattern": "^job_[A-Za-z0-9_-]{8,64}$"
},
"serverId": {
"type": "string",
"pattern": "^srv_[A-Za-z0-9_-]{8,64}$"
},
"config": {
"anyOf": [
{
"type": "object",
"properties": {
"alg": {
"type": "string",
"const": "x25519-hkdf-sha256-aes-256-gcm"
},
"epk": {
"type": "string",
"pattern": "^[A-Za-z0-9_-]{43}$"
},
"iv": {
"type": "string",
"pattern": "^[A-Za-z0-9_-]{16}$"
},
"ciphertext": {
"type": "string",
"maxLength": 200000,
"pattern": "^[A-Za-z0-9_-]+$"
}
},
"required": [
"alg",
"epk",
"iv",
"ciphertext"
],
"additionalProperties": false
},
{
"type": "null"
}
]
},
"notBefore": {
"type": "string",
"format": "date-time",
"pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z))$"
},
"expiresAt": {
"type": "string",
"format": "date-time",
"pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z))$"
},
"signature": {
"type": "string",
"pattern": "^[A-Za-z0-9_-]{86}$"
},
"type": {
"type": "string",
"const": "frp-write"
},
"args": {
"type": "object",
"properties": {
"path": {
"type": "string",
"maxLength": 255,
"allOf": [
{
"pattern": "^\\/(?!.*\\/\\.\\.?(\\/|$))[A-Za-z0-9._/-]+$"
},
{
"pattern": "\\/frp[A-Za-z0-9._-]*\\.(ini|toml)$"
}
]
},
"content": {
"type": "string",
"minLength": 1,
"maxLength": 64000
},
"restart": {
"type": "boolean"
}
},
"required": [
"path",
"content",
"restart"
],
"additionalProperties": false
}
},
"required": [
"schema",
"id",
"serverId",
"notBefore",
"expiresAt",
"signature",
"type",
"args"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"schema": {
"type": "string",
"const": "crosscode.agent-job/1"
},
"id": {
"type": "string",
"pattern": "^job_[A-Za-z0-9_-]{8,64}$"
},
"serverId": {
"type": "string",
"pattern": "^srv_[A-Za-z0-9_-]{8,64}$"
},
"config": {
"anyOf": [
{
"type": "object",
"properties": {
"alg": {
"type": "string",
"const": "x25519-hkdf-sha256-aes-256-gcm"
},
"epk": {
"type": "string",
"pattern": "^[A-Za-z0-9_-]{43}$"
},
"iv": {
"type": "string",
"pattern": "^[A-Za-z0-9_-]{16}$"
},
"ciphertext": {
"type": "string",
"maxLength": 200000,
"pattern": "^[A-Za-z0-9_-]+$"
}
},
"required": [
"alg",
"epk",
"iv",
"ciphertext"
],
"additionalProperties": false
},
{
"type": "null"
}
]
},
"notBefore": {
"type": "string",
"format": "date-time",
"pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z))$"
},
"expiresAt": {
"type": "string",
"format": "date-time",
"pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z))$"
},
"signature": {
"type": "string",
"pattern": "^[A-Za-z0-9_-]{86}$"
},
"type": {
"type": "string",
"const": "probe-ports"
},
"args": {
"type": "object",
"properties": {
"ports": {
"minItems": 1,
"maxItems": 50,
"type": "array",
"items": {
"type": "integer",
"minimum": 1024,
"maximum": 65535
}
}
},
"required": [
"ports"
],
"additionalProperties": false
}
},
"required": [
"schema",
"id",
"serverId",
"notBefore",
"expiresAt",
"signature",
"type",
"args"
],
"additionalProperties": false
}
]
}
@@ -31,6 +31,9 @@
"uninstall",
"purge",
"update-agent",
"frp-read",
"frp-write",
"probe-ports",
"check-ports",
"status"
]
@@ -280,6 +280,17 @@
"type": "null"
}
]
},
"frpConfigPath": {
"anyOf": [
{
"type": "string",
"maxLength": 255
},
{
"type": "null"
}
]
}
},
"required": [
+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[] = [];
+35
View File
@@ -240,3 +240,38 @@ describe("env templates", () => {
expect(agent.invalidValues({ TRUST_PROXY: "loopback" })).toEqual({});
});
});
describe("frp config and free ports (1.15.0)", () => {
const read = (a: Record<string, unknown>) => agent.jobArgsSchemas["frp-read"].safeParse(a).success;
const write = (a: Record<string, unknown>) => agent.jobArgsSchemas["frp-write"].safeParse({ path: "/etc/frp/frps.toml", content: "bindPort = 7000\n", restart: true, ...a }).success;
it("read the config the agent finds, or one at a path", () => {
expect(read({})).toBe(true);
expect(read({ path: "/etc/frp/frps.toml" })).toBe(true);
expect(read({ path: "/usr/local/frp/frpc.ini" })).toBe(true);
for (const path of ["/etc/shadow", "etc/frp/frps.ini", "/etc/frp/../shadow/frps.ini", "/etc/frp/frps.ini; rm -rf /", "/etc/frp/frps.service", "/etc/frp/server.toml"]) expect(read({ path })).toBe(false);
expect(read({ path: "/etc/frp/frps.ini", extra: 1 })).toBe(false);
});
it("write one, with content and whether to restart", () => {
expect(write({})).toBe(true);
expect(write({ content: "" })).toBe(false);
expect(write({ content: "x".repeat(agent.FRP_CONFIG_MAX + 1) })).toBe(false);
expect(write({ path: "/etc/passwd" })).toBe(false);
expect(write({ restart: "yes" })).toBe(false);
});
it("probe ports", () => {
expect(agent.jobArgsSchemas["probe-ports"].safeParse({ ports: [20000, 20001] }).success).toBe(true);
expect(agent.jobArgsSchemas["probe-ports"].safeParse({ ports: [] }).success).toBe(false);
expect(agent.jobArgsSchemas["probe-ports"].safeParse({ ports: [80] }).success).toBe(false);
});
it("the central agent runs all three", () => {
expect(agent.CENTRAL_JOB_TYPES).toEqual(expect.arrayContaining(["frp-read", "frp-write", "probe-ports"]));
expect(agent.JOB_TIME_LIMIT_MINUTES["frp-write"]).toBeGreaterThan(0);
});
it("results", () => {
expect(agent.frpReadResultSchema.safeParse({
service: "frps", path: "/etc/frp/frps.toml", source: "systemd", found: true, format: "toml", content: `auth.token = "${agent.FRP_HIDDEN}"\n`, hidden: ["auth.token"],
proxyBindAddr: "127.0.0.1", allowPorts: "20000-29999", serviceRunning: true, readAt: "2026-11-21T10:00:00.000Z",
}).success).toBe(true);
expect(agent.probePortsResultSchema.safeParse({ ports: [{ port: 20000, free: false, reason: "in use" }], configPath: null }).success).toBe(true);
});
});