AtlatestRepositorysigil-nat

sigil-nat / tree / scriptsnat-probe.mjs

1// Bounded client for a public reflector controlled by the caller.
2import { readFileSync, lstatSync, mkdirSync, writeFileSync } from 'node:fs';
3import { hostname } from 'node:os';
4import { isIP } from 'node:net';
5import { isAbsolute, resolve, join } from 'node:path';
6import { gatewayRoute } from '../tools/nat/discovery.mjs';
7import { runNatProbe } from '../tools/nat/nat-probe.mjs';
8
9const configPath = resolve(process.argv[2] ?? '');
10const file = lstatSync(configPath);
11if (!file.isFile() || file.uid !== process.getuid() ||
12 file.size > 16384 || (file.mode & 0o077) !== 0)
13 throw new Error('NAT probe requires a private owned config');
14const config = JSON.parse(readFileSync(configPath, 'utf8'));
15if (config.schema !== 1 || config.expectedHost !== hostname() ||
16 !/^[0-9a-f]{64}$/.test(config.tokenHex ?? '') || !isAbsolute(config.runDirectory ?? ''))
17 throw new Error('Invalid NAT probe host/schema/token/run directory');
18const address = config.reflectors?.a1?.address;
19const parts = isIP(address) === 4 ? address.split('.').map(Number) : [];
20if (parts.length !== 4 || parts[0] === 0 || parts[0] === 10 || parts[0] === 127 || parts[0] >= 224 ||
21 (parts[0] === 100 && parts[1] >= 64 && parts[1] <= 127) ||
22 (parts[0] === 169 && parts[1] === 254) || (parts[0] === 172 && parts[1] >= 16 && parts[1] <= 31) ||
23 (parts[0] === 192 && parts[1] === 168) || config.reflectors.b !== undefined)
24 throw new Error('Expected one public IPv4 reflector');
25const route = gatewayRoute();
26if (config.localAddress !== route.localAddress) throw new Error('Probe source must match the owned default-route interface');
27mkdirSync(config.runDirectory, { mode: 0o700 });
28const abort = new AbortController();
29const stop = () => abort.abort(new Error('NAT probe interrupted'));
30process.once('SIGTERM', stop); process.once('SIGINT', stop);
31const state = { processId: process.pid, startedAt: new Date().toISOString() };
32writeFileSync(join(config.runDirectory, 'starting.json'), JSON.stringify(state) + '\n', { mode: 0o600 });
33try {
34 const result = await runNatProbe({ localAddress: route.localAddress, reflectors: config.reflectors,
35 token: Buffer.from(config.tokenHex, 'hex'), mappingSamples: config.mappingSamples ?? 3,
36 signal: abort.signal });
37 writeFileSync(join(config.runDirectory, 'details.json'), JSON.stringify(result.details, null, 2) + '\n', { mode: 0o600 });
38 state.summary = result.summary;
39 state.completed = true;
40 console.log(JSON.stringify(result.summary));
41} catch (error) {
42 state.completed = false;
43 state.errorCode = error.code ?? 'PROBE_FAILED';
44 state.controls = error.controls;
45 console.log(`RESULT nat-probe=${state.errorCode} completed=false`);
46 process.exitCode = 1;
47} finally {
48 process.off('SIGTERM', stop); process.off('SIGINT', stop);
49 state.endedAt = new Date().toISOString();
50 writeFileSync(join(config.runDirectory, 'result.json'), JSON.stringify(state, null, 2) + '\n', { mode: 0o600 });