Reuse bounded channel connections for independently authorized room activities and retire the shared room-tree bootstrap. Keep current physical proof separate from recovery history and activity delivery proof. Preserve operation ownership across room replacement, source selection, microphone changes and native window retirement. Bound data sends before the channel is installed and recheck room grants at the final write.
156 lines
10 KiB
TypeScript
156 lines
10 KiB
TypeScript
// Linux-only kernel controls. Run --list anywhere; the controls need root,
|
|
// iproute2/nftables/conntrack/tcpdump and the normal Server example + Browser WASM.
|
|
import assert from "node:assert/strict";
|
|
import { mkdir, writeFile } from "node:fs/promises";
|
|
import { dirname, join, resolve } from "node:path";
|
|
import { parseArgs } from "node:util";
|
|
import { setTimeout as delay } from "node:timers/promises";
|
|
import { LabNetwork, command, type NatMode } from "./network-lab/network";
|
|
import { echoWorker, udpProbe, filteringProbe } from "./network-lab/udp";
|
|
import { checkCalibration, checkMapping, connectedNativePair } from "./network-lab/scenarios";
|
|
import { readPacketFlows } from "./network-lab/packets";
|
|
import { LabProcess } from "./network-lab/process";
|
|
import { media } from "./network-lab/browser";
|
|
|
|
const cases = {
|
|
ordinary: "preserve NAT: Browser → real Native room → Browser; history/file/audio/decoded video",
|
|
prepared: "sequential NAT: Core-required preparation → connected Native room-host pair",
|
|
recovery: "active Native transit: 50s UDP loss + remap → new physical pair and decoded media within 30s",
|
|
media: "calibrated prepared Native transit: decoded media, 50s UDP loss and recovery through Native root → Native probe",
|
|
"browser-relay": "Browser source → Native room → Browser relay → decoded viewer; 50s UDP loss and recovery",
|
|
};
|
|
const { values } = parseArgs({ options: {
|
|
list: { type: "boolean" }, case: { type: "string" }, output: { type: "string" },
|
|
mapping: { type: "string", default: "sequential" },
|
|
executable: { type: "string" }, worker: { type: "string" },
|
|
} });
|
|
if (values.list) console.log(Object.entries(cases).map(([name, text]) => `${name}: ${text}`).join("\n"));
|
|
else if (values.worker) {
|
|
if (values.worker === "echo") await echoWorker();
|
|
else if (values.worker === "probe") console.log(JSON.stringify(await udpProbe(false)));
|
|
else if (values.worker === "filter") console.log(JSON.stringify(await filteringProbe()));
|
|
else throw Error("Unknown LAB worker");
|
|
} else {
|
|
assert(values.case && Object.hasOwn(cases, values.case), "Choose --case ordinary|prepared|recovery|media|browser-relay (or --list)");
|
|
assert(["sequential", "descending"].includes(values.mapping!), "Choose --mapping sequential|descending");
|
|
assert.equal(process.platform, "linux", "Kernel controls run only on Linux");
|
|
assert.equal(process.getuid?.(), 0, "Kernel controls need root for owned namespaces");
|
|
await run(values.case as keyof typeof cases);
|
|
}
|
|
|
|
async function run(name: keyof typeof cases): Promise<void> {
|
|
const source = resolve(import.meta.dirname, "..");
|
|
const output = resolve(values.output ?? `build/network-lab/${name}-${Date.now()}`);
|
|
const executable = resolve(values.executable ?? (process.env.CARGO_TARGET_DIR
|
|
? join(process.env.CARGO_TARGET_DIR, "debug/examples/network_lab")
|
|
: "prototype/channel-server/target/debug/examples/network_lab"));
|
|
await mkdir(dirname(output), { recursive: true });
|
|
await mkdir(output); // A failed observation is retained; never reuse its output.
|
|
const network = new LabNetwork(), processes: LabProcess[] = [];
|
|
const stopped = new AbortController();
|
|
const cancel = () => stopped.abort();
|
|
process.once("SIGINT", cancel); process.once("SIGTERM", cancel);
|
|
const signal = stopped.signal;
|
|
const start = (label: string, ns: string, program: string, args: string[], env?: NodeJS.ProcessEnv) => {
|
|
const child = new LabProcess(label, ns, program, args, env); processes.push(child); return child;
|
|
};
|
|
// Reuse this entry's worker with the same TypeScript loader (or bundled JS).
|
|
const worker = (label: string, ns: string, task: string) => start(label, ns, process.execPath,
|
|
[...process.execArgv, process.argv[1]!, "--worker", task]);
|
|
const browserCase = name !== "prepared";
|
|
const nativeTransit = name === "media" || name === "recovery";
|
|
const mapping = values.mapping as "sequential" | "descending";
|
|
const modes: NatMode[] = name === "media" ? [mapping, mapping, "preserve", "preserve"]
|
|
: name === "browser-relay" || name === "recovery" ? ["preserve", "preserve", "preserve", "preserve"]
|
|
: name === "ordinary" ? ["preserve", "preserve", "preserve"]
|
|
: [mapping, mapping];
|
|
const result: any = { case: name, scope: cases[name], endpointMappings: modes, passed: false,
|
|
sourceHead: process.env.SOURCE_HEAD ?? command("git", ["-C", source, "rev-parse", "HEAD"]), kernel: command("uname", ["-sr"]), node: process.version };
|
|
let capture: LabProcess | undefined;
|
|
try {
|
|
network.setup(modes);
|
|
const fullConeNodes = nativeTransit ? [2, 3] : name === "browser-relay" ? [1, 2, 3] : [];
|
|
for (const index of fullConeNodes) network.exec(network.router(index), "nft", [
|
|
"add chain ip lab_nat prerouting { type nat hook prerouting priority dstnat; policy accept; }\n"
|
|
+ `add rule ip lab_nat prerouting iifname \"wan0\" ip daddr ${network.publicIP(index)} meta l4proto udp dnat to 10.71.${index + 1}.2\n`
|
|
+ "add rule inet lab_filter forward iifname \"wan0\" meta l4proto udp counter accept",
|
|
]);
|
|
const echo = worker("echo", network.wan, "echo");
|
|
await echo.wait(() => echo.records.some(row => row.ready), signal);
|
|
result.calibration = await Promise.all(modes.map(async (mode, index) => {
|
|
const probe = worker(`mapping-${index}`, network.endpoint(index), "probe");
|
|
assert.equal((await probe.done).code, 0);
|
|
checkMapping(probe.records[0], mode); checkCalibration(probe.records[0], probe.records[0], "none");
|
|
const filter = worker(`filter-${index}`, network.endpoint(index), "filter");
|
|
assert.equal((await filter.done).code, 0);
|
|
const value = filter.records[0], fullCone = fullConeNodes.includes(index);
|
|
assert(value.sameEndpoint && value.changedPort === fullCone && value.changedAddress === fullCone, "NAT filtering calibration");
|
|
return { mapping: probe.records[0], filtering: value };
|
|
}));
|
|
await echo.close();
|
|
// Install the transit topology before joining so connection attempts and
|
|
// route selection see the same reachable peers as the media checks.
|
|
const blocked = name === "ordinary" ? [[1, 2], [2, 1]] : nativeTransit
|
|
? [[0, 3], [3, 0], [1, 2], [2, 1], [2, 3], [3, 2]] : name === "browser-relay"
|
|
? [[1, 2], [2, 1], [1, 3], [3, 1], [0, 3], [3, 0]] : [];
|
|
for (const [index, peer] of blocked) network.exec(network.router(index!), "nft", [
|
|
`add rule inet lab_filter faults ip daddr ${network.publicIP(peer!)} meta l4proto udp counter drop`,
|
|
]);
|
|
result.blockedShortcuts = blocked;
|
|
capture = start("capture", network.wan, "tcpdump", ["-i", "internet", "-n", "-s", "96", "-U", "-w", join(output, "wan.pcap"), "-c", "20000", "udp"]);
|
|
await capture.wait(() => capture!.stderr.includes("listening on"), signal);
|
|
const domain = start("domain", network.wan, executable, ["domain", join(output, "domain")],
|
|
{ ...process.env, PIIK_CHANNEL_PORT: browserCase ? "9001" : "9000" });
|
|
await domain.wait(() => domain.records.some(row => row.event?.type === "domain-ready"), signal);
|
|
if (browserCase) await media({ source, output, executable, network, start, signal, result }, name);
|
|
else {
|
|
start("native-host", network.endpoint(0), executable, ["host", join(output, "host")]);
|
|
const reader = start("native-reader", network.endpoint(1), executable, ["participant"]);
|
|
let initial: ReturnType<typeof connectedNativePair>;
|
|
await reader.wait(() => Boolean(initial = connectedNativePair(reader.records, { prepared: true })), signal, 90_000);
|
|
assert(initial);
|
|
result.nativePair = initial;
|
|
const { peerId } = initial;
|
|
result.plans = reader.records.filter(row => row.event?.type === "plan")
|
|
.flatMap(row => row.event.pairs.filter((pair: { peerId: string }) => pair.peerId === peerId));
|
|
await delay(3000, undefined, { signal });
|
|
}
|
|
result.passed = true;
|
|
} catch (error) { result.error = String(error instanceof Error ? error.stack : error); }
|
|
finally {
|
|
for (const child of [...processes].reverse()) {
|
|
try { await child.close(child.name === "echo" ? undefined : "SIGINT"); }
|
|
catch (error) { (result.cleanupErrors ??= []).push(String(error)); }
|
|
await child.save(output);
|
|
}
|
|
try {
|
|
if (capture) {
|
|
result.packetFlows = await readPacketFlows(join(output, "wan.pcap"));
|
|
result.counters = modes.map((_, index) => network.counters(index));
|
|
if (result.passed) {
|
|
const flows = name === "recovery" ? result.recoveryPacketFlows : result.packetFlows;
|
|
const edges = result.mediaEdges ?? (name === "ordinary" ? [[0, 1], [0, 2]] : nativeTransit ? [[0, 2], [0, 1], [1, 3]] : [[0, 1]]);
|
|
for (const [a, b] of edges) for (const [from, to] of [[a!, b!], [b!, a!]]) assert(flows.some((flow: any) =>
|
|
flow.source === network.publicIP(from) && flow.destination === network.publicIP(to) && (browserCase ? flow.kind === "rtp" : flow.kind === "other")), "Actual peer wire traffic must cross every selected edge");
|
|
if (nativeTransit) {
|
|
const nativeMedia = flows.filter((flow: any) => flow.kind === "rtp"
|
|
&& flow.source === network.publicIP(0) && flow.destination === network.publicIP(1));
|
|
assert(nativeMedia.some((media: any) => flows.some((feedback: any) => feedback.kind === "rtcp"
|
|
&& feedback.source === media.destination && feedback.destination === media.source
|
|
&& feedback.sourcePort === media.destinationPort && feedback.destinationPort === media.sourcePort)),
|
|
"Native media tuple must carry reverse RTCP");
|
|
}
|
|
}
|
|
}
|
|
result.remainingNamespacePids = Object.fromEntries(network.namespaces.map(ns => [ns, command("ip", ["netns", "pids", ns])]));
|
|
assert(Object.values(result.remainingNamespacePids).every(value => !value), "All owned children must retire");
|
|
} catch (error) { (result.cleanupErrors ??= []).push(String(error)); }
|
|
result.cleanup = await network.close();
|
|
if (result.cleanupErrors?.length || Object.values(result.cleanup).some(value => !value)) result.passed = false;
|
|
process.removeListener("SIGINT", cancel); process.removeListener("SIGTERM", cancel);
|
|
await writeFile(join(output, "result.json"), JSON.stringify(result, null, 2));
|
|
console.log(JSON.stringify({ output, passed: result.passed, error: result.error, cleanup: result.cleanup }));
|
|
if (!result.passed) process.exitCode = 1;
|
|
}
|
|
}
|