// NAMED NETWORK EXITS (lane network-exits, 21 Nov 2026): proxies a fetch may // leave through, chosen per channel and kept, for the platforms and request // kinds the operator routes. Pure and client-safe: the types, the sanitizer, // the validator the writers refuse with, the sticky choice, the cookie-jar // classifier. State and the proxy client live in controller/networkExits.ts // and lib/networkProxy-server.ts; the credentials in // lib/networkCredentials-server.ts. // // WHAT IT IS FOR, AND WHAT IT IS NOT. It spreads logged-out listing load over // several addresses, and lets the archive list a site that does not answer the // home address. It is not a disguise: no fingerprint or user-agent spoofing is // added, and a login (a cookie jar, a browser's cookies, a token) never moves // between addresses. // // THE RULES, each enforced where its comment says: // - `direct` (no proxy) always exists and is reserved. // - A policy per platform queue, `network.platforms.

`; absent = direct. // THE TRIAL routes only TRIAL_SCOPE (rumble and twitch listings); the // writers refuse anything else with TRIAL_SCOPE_SENTENCE; a hand-edited // out-of-scope policy fails the trial platforms' listings (strict read). // - Sticky, never random: stickyExitFor hashes the channel slug over the // platform's enabled exits once; the choice is recorded and kept. // - A request that carries a login is pinned direct (requestJar). The trial // refuses every jar assignment. // - CREDENTIALS ARE NEVER A SETTING (review 1, finding 1). An exit is stored // as id, scheme, host, port and `credentials: true|false`; the user and // password live where the repo keeps every other secret — the environment, // else a mode-600 key file whose PATH is `network.credentialsFile` // (lib/networkCredentials-server.ts, the archive.org S3 keys' convention). // A proxy URL carrying credentials is refused at save. Every answer, log // line and record is built from the stored form, so there is nothing to // mask; a child's own output is scrubbed of the exact secret values. import type { FieldDocs } from "./fieldDocs"; import { YTDLP_OPTIONS } from "./ytdlpOptions"; import { YTDLP_OPTION_CLASSES, type YtdlpOptionClass } from "./ytdlpOptionClasses"; export const DIRECT_EXIT = "direct"; // The request kinds a policy may name. Only `listing` is routable in the // trial; the others are named so a refusal can say what was asked. export const NETWORK_REQUEST_KINDS = [ "listing", "download", "metadata", "subtitles", "clip", "live", "posts", ] as const; export type NetworkRequestKind = (typeof NETWORK_REQUEST_KINDS)[number]; export const NETWORK_POLICY_MODES = ["sticky"] as const; export type NetworkPolicyMode = (typeof NETWORK_POLICY_MODES)[number]; export const PROXY_SCHEMES = ["socks5h", "socks5", "http", "https"] as const; export type ProxyScheme = (typeof PROXY_SCHEMES)[number]; export type NetworkExit = { id: string; label: string; scheme: ProxyScheme; host: string; port: number; // Whether the proxy needs a user and password — kept in the key file. credentials: boolean; enabled: boolean; }; export type NetworkPlatformPolicy = { exits: string[]; mode: NetworkPolicyMode; kinds: NetworkRequestKind[]; }; export type NetworkSettings = { exits: NetworkExit[]; platforms: Record; jars: Record; echoUrl: string; credentialsFile: string; startupProbe: boolean; }; export const DEFAULT_NETWORK_CREDENTIALS_FILE = "~/.config/archilyzer/network-exits.env"; // THE TRIAL'S SCOPE — the one place it is widened, deliberately, later. export const TRIAL_SCOPE: Readonly> = Object.freeze({ rumble: Object.freeze(["listing"] as NetworkRequestKind[]), twitch: Object.freeze(["listing"] as NetworkRequestKind[]), }); export const TRIAL_SCOPE_SENTENCE = "the network-exit trial routes only the listing requests of rumble and twitch; every other " + "platform and request kind goes direct (TRIAL_SCOPE in common/lib/networkExits.ts)"; export const JAR_TRIAL_SENTENCE = "a request that carries a login (a cookie jar, a browser's cookies, a token) goes direct in the " + "network-exit trial — a login is never moved to another address"; export function isRoutable(platform: string, kind: NetworkRequestKind): boolean { return (TRIAL_SCOPE[platform] ?? []).includes(kind); } export const NETWORK_SETTINGS_FIELD_DOCS: FieldDocs = { exits: "NAMED NETWORK EXITS: proxies a fetch may leave through, `[{id, label, scheme, host, port, credentials, " + "enabled}]` (table below). The exit `direct` (no proxy) always exists and is reserved. Written by `pnpm ops " + "network` and /settings → Network exits, never by `pnpm ops settings`. It spreads logged-out listing load over " + "addresses and lets the archive list a site that does not answer the home address; it is not a disguise — no " + "fingerprint or user-agent spoofing is added, and a login never moves between addresses. Worked example, " + "Mullvad: while the machine is connected to the VPN, each WireGuard relay offers a SOCKS5 proxy at its own " + "hostname on port 1080 (`socks5h://nl-ams-wg-socks5-001.relays.mullvad.net:1080`), resolvable only inside the " + "tunnel — https://mullvad.net/en/help/socks5-proxy and " + "https://mullvad.net/en/help/different-entryexit-node-using-wireguard-and-socks5-proxy .", platforms: "Which exits a platform queue's requests may use, keyed by platform: `{exits: [ids], mode: \"sticky\", kinds: " + "[\"listing\"]}` (table below). Absent = every request goes direct, exactly as before. THE TRIAL routes only " + "the LISTING (flat-playlist) requests of `rumble` and `twitch`; any other platform or kind is refused when " + "saved; written by hand, it fails every rumble and twitch listing until fixed. Each channel's exit is chosen once, on its first listing, by a stable " + "hash of its slug over the platform's enabled exits and recorded in `channels//network-exit.json`; only " + "`pnpm ops network-exit` changes it. A recorded exit that is disabled or removed FAILS that channel's listing " + "(naming the reassignment) — it is never re-chosen and never sent direct; a policy whose exits are all disabled " + "fails every listing on the platform (`direct` is used only when the policy names it). Once any policy exists, no " + "child process inherits a proxy variable (common/lib/childEnvGuard.mjs). One job keeps one exit for all its " + "pages. Pacing stays per platform: the job gap, the pace, the cooldown and the hold count every exit together. " + "A rate limit or bot check through an exit rests that exit for the platform (the platform cooldown's length); " + "its channels' listings wait (the job ends deferred) and never move to another exit; when every exit rests " + "after a rate limit the platform cools down as before. A proxy that does not answer, or a listing whose yt-dlp " + "output does not show the proxy, fails naming the exit — never a fallback to direct.", jars: "A cookie jar's one fixed exit, keyed by jar id (`browser:`, `file:`, `login:

: policy | null} merged per platform; null removes // jars: {...} the whole map (refused in the trial unless every value is direct) // echoUrl, credentialsFile, startupProbe export const NETWORK_REQUEST_KEYS = [ "exits", "upsertExit", "removeExit", "platforms", "jars", "echoUrl", "credentialsFile", "startupProbe", ] as const; export class NetworkRequestError extends Error {} export function applyNetworkRequest(current: NetworkSettings, body: Record): Record { const unknown = Object.keys(body).filter((k) => !(NETWORK_REQUEST_KEYS as readonly string[]).includes(k)); if (unknown.length) { throw new NetworkRequestError(`unknown key(s): ${unknown.join(", ")} — accepted: ${NETWORK_REQUEST_KEYS.join(", ")}`); } if (Object.keys(body).length === 0) throw new NetworkRequestError("nothing to change"); let exits: unknown[] = current.exits.map((e) => ({ ...e })); let platforms: Record = Object.fromEntries( Object.entries(current.platforms).map(([p, v]) => [p, { ...v, exits: [...v.exits], kinds: [...v.kinds] }]), ); if (body.exits !== undefined) { if (!Array.isArray(body.exits)) throw new NetworkRequestError("exits: must be a list"); exits = body.exits; } if (body.upsertExit !== undefined) { const u = body.upsertExit; if (!isObject(u) || typeof u.id !== "string") { throw new NetworkRequestError("upsertExit: must be {id, label?, proxy?, credentials?, enabled?}"); } const i = exits.findIndex((e) => isObject(e) && e.id === u.id); const prev = i >= 0 ? (exits[i] as Record) : undefined; let parts: Record = {}; if (u.proxy !== undefined) { const p = parseProxyInput(u.proxy); if ("error" in p) throw new NetworkRequestError(`upsertExit.proxy: ${p.error}`); parts = p; } else { for (const k of ["scheme", "host", "port"]) if (u[k] !== undefined) parts[k] = u[k]; } if (!prev && parts.host === undefined) throw new NetworkRequestError(`upsertExit: a new exit "${u.id}" needs its proxy (scheme://host:port)`); const merged: Record = { id: u.id, label: u.label ?? prev?.label ?? u.id, scheme: parts.scheme ?? prev?.scheme, host: parts.host ?? prev?.host, port: parts.port ?? prev?.port, credentials: u.credentials ?? prev?.credentials ?? false, enabled: u.enabled ?? prev?.enabled ?? true, }; for (const k of Object.keys(u)) if (![...EXIT_KEYS, "proxy"].includes(k)) merged[k] = u[k]; if (i >= 0) exits[i] = merged; else exits.push(merged); } if (body.removeExit !== undefined) { const id = body.removeExit; if (typeof id !== "string" || !exits.some((e) => isObject(e) && e.id === id)) { throw new NetworkRequestError(`removeExit: no exit "${String(id)}"`); } exits = exits.filter((e) => !(isObject(e) && e.id === id)); for (const [p, v] of Object.entries(platforms)) { if (!isObject(v) || !Array.isArray(v.exits)) continue; const left = v.exits.filter((x) => x !== id); if (left.length) platforms[p] = { ...v, exits: left }; else delete platforms[p]; } } if (body.platforms !== undefined) { if (!isObject(body.platforms)) throw new NetworkRequestError("platforms: must be an object keyed by platform"); platforms = { ...platforms }; for (const [p, v] of Object.entries(body.platforms)) { if (v === null) delete platforms[p]; else platforms[p] = v; } } return { exits, platforms, jars: body.jars !== undefined ? body.jars : { ...current.jars }, echoUrl: body.echoUrl !== undefined ? body.echoUrl : current.echoUrl, credentialsFile: body.credentialsFile !== undefined ? body.credentialsFile : current.credentialsFile, startupProbe: body.startupProbe !== undefined ? body.startupProbe : current.startupProbe, }; }