@twinsearth/w2m-dsh-plugin 1.2.2 → 1.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1044 +1,1109 @@
1
- /**
2
- * W2M Rabbit relay — node:http / node:https server.
3
- *
4
- * PROTOCOL.md §1 endpoints, §3 SSE, §7 errors
5
- * PROTOCOL-v0.1.2.md §3 deployment params, §4 /healthz, §5 operator token,
6
- * §6 pair rate limit, §7 persistence, §8 SSE hardening.
7
- *
8
- * Zero third-party dependencies: only `node:` builtins.
9
- */
10
-
11
- import http from 'node:http';
12
- import https from 'node:https';
13
- import fs from 'node:fs';
14
- import os from 'node:os';
15
- import path from 'node:path';
16
- import { createHash, randomBytes } from 'node:crypto';
17
-
18
- import {
19
- MAX_FRAME_BYTES,
20
- PROTOCOL_VERSION,
21
- ProtocolError,
22
- RabbitState,
23
- rfc3339,
1
+ /**
2
+ * W2M Rabbit relay — node:http / node:https server.
3
+ *
4
+ * PROTOCOL.md §1 endpoints, §3 SSE, §7 errors
5
+ * PROTOCOL-v0.1.2.md §3 deployment params, §4 /healthz, §5 operator token,
6
+ * §6 pair rate limit, §7 persistence, §8 SSE hardening.
7
+ *
8
+ * Zero third-party dependencies: only `node:` builtins.
9
+ */
10
+
11
+ import http from 'node:http';
12
+ import https from 'node:https';
13
+ import fs from 'node:fs';
14
+ import os from 'node:os';
15
+ import path from 'node:path';
16
+ import { createHash, randomBytes } from 'node:crypto';
17
+
18
+ import {
19
+ MAX_FRAME_BYTES,
20
+ PROTOCOL_VERSION,
21
+ ProtocolError,
22
+ RabbitState,
23
+ rfc3339,
24
24
  } from './state.mjs';
25
25
  import { createFleetMessages } from './fleet-messages.mjs';
26
- import { createFleetMemory } from './fleet-memory.mjs';
27
- import { createJoinChallenges } from './join-challenge.mjs';
28
- import { createAdmission } from './admission.mjs';
29
- import { Persistence } from './persistence.mjs';
30
- import { PeerRegistry } from './peers.mjs';
31
- import { aggregateTask, buildReport } from './report.mjs';
32
- import { renderMetrics, METRICS_CONTENT_TYPE } from './metrics.mjs';
33
- // v0.3.0 §9: request signing. This import is the entire point of the module --
34
- // until the HTTP layer calls it, HMAC support does not exist for any real request.
35
- import { DEFAULT_SKEW_SECONDS, NonceCache, verifyRequest } from '../signing.mjs';
36
- // v0.7.11: per-device Ed25519 verification, tried before the shared secret.
37
- import {
38
- SIGNATURE_HEADER as DEVICE_SIGNATURE_HEADER,
39
- verifyDeviceSignature,
40
- } from '../chatops/device-signing.mjs';
41
-
42
- export const DEFAULT_SSE_KEEPALIVE_MS = 15_000; // §3.2: every 15s a `: keepalive` line
43
- export const DEFAULT_SWEEP_INTERVAL_MS = 1_000;
44
- export const DEFAULT_PAIR_RATE_LIMIT_PER_MINUTE = 5; // v0.1.2 §6
45
- // The window length is a unit and lives with the limiter. Both names are kept: `DEFAULT_WINDOW_MS` is the
46
- // local binding this module uses, and `RATE_LIMIT_WINDOW_MS` is the name this module has always exported.
47
- // `export ... from` would NOT have worked here -- it re-exports without creating a local binding, so the
48
- // constructor below saw `RATE_LIMIT_WINDOW_MS is not defined` while the export itself looked fine.
49
- import { DEFAULT_WINDOW_MS, SlidingWindowRateLimiter } from '../util/rate-limit.mjs';
50
- export { DEFAULT_WINDOW_MS as RATE_LIMIT_WINDOW_MS };
51
- export const DEFAULT_STATE_DIR = path.join(os.homedir(), '.w2m');
52
-
53
- /**
54
- * The environment variable that opts in to printing the operator token on a start that did not
55
- * generate it (MEDIUM-6 of the v0.5.9 review).
56
- *
57
- * The value is printed **once, on the start that generated it**, because that is the only moment at
58
- * which it exists nowhere else yet; every other start has `<state>/operator-token.txt` to read it
59
- * from, and a relay's stdout ends up in journald, a container log or a CI transcript — where a
60
- * credential that authorises dispatching to the whole fleet does not belong. `W2M_PRINT_OPERATOR_TOKEN=1`
61
- * (or `printOperatorToken: true`, which is the same switch for an embedder) restores the old
62
- * behaviour for an operator who genuinely needs it in the log, and says so in the banner when it does.
63
- */
64
- export const PRINT_OPERATOR_TOKEN_ENV = 'W2M_PRINT_OPERATOR_TOKEN';
65
-
66
- /**
67
- * Resolve that opt-in, so the relay and the CLI agree on one answer.
68
- *
69
- * @param {object} [options] Relay options; `printOperatorToken: true` is the programmatic spelling.
70
- * @returns {'option'|'env'|null} Why printing was asked for, or null.
71
- */
72
- export function printOperatorTokenOptIn(options = {}) {
73
- if (options.printOperatorToken === true) return 'option';
74
- const raw = process.env[PRINT_OPERATOR_TOKEN_ENV];
75
- if (raw === '1' || raw === 'true') return 'env';
76
- return null;
77
- }
78
-
79
- /* ------------------------------------------------------------------ */
80
- /* helpers */
81
- /* ------------------------------------------------------------------ */
82
-
83
- /** v0.1.2 §3: `/w2m/` → `/w2m`; '' / '/' → '/'. Never returns a trailing slash. */
84
- export function normalizeBasePath(value) {
85
- if (value === undefined || value === null || value === '') return '/';
86
- let p = String(value).trim();
87
- if (p === '' || p === '/') return '/';
88
- if (!p.startsWith('/')) p = `/${p}`;
89
- p = p.replace(/\/{2,}/g, '/').replace(/\/+$/, '');
90
- return p === '' ? '/' : p;
91
- }
92
-
93
- function normalizePath(pathname) {
94
- if (!pathname) return '/';
95
- return pathname.length > 1 ? pathname.replace(/\/+$/, '') : pathname;
96
- }
97
-
98
- function sendJson(res, status, body, extraHeaders = {}) {
99
- if (res.writableEnded) return undefined;
100
- const payload = Buffer.from(JSON.stringify(body), 'utf8');
101
- res.writeHead(status, {
102
- 'content-type': 'application/json; charset=utf-8',
103
- 'content-length': payload.length,
104
- 'cache-control': 'no-store',
105
- ...extraHeaders,
106
- });
107
- return res.end(payload);
108
- }
109
-
110
- function sendError(res, err) {
111
- if (err instanceof ProtocolError) {
112
- const body = err.toBody();
113
- // §6 just says the 429 "carries retry_after_seconds" without pinning the
114
- // location, so it is emitted both inside error.detail (the §7 shape) and as a
115
- // top-level convenience field. Both are additive.
116
- const retryAfter = err.detail?.retry_after_seconds;
117
- if (typeof retryAfter === 'number') body.retry_after_seconds = retryAfter;
118
- sendJson(res, err.status, body, err.headers ?? {});
119
- return;
120
- }
121
- sendJson(res, 500, {
122
- error: {
123
- code: 'INTERNAL',
124
- message: err?.message ? String(err.message) : 'internal error',
125
- detail: {},
126
- },
127
- });
128
- }
129
-
130
- function bearerToken(req) {
131
- const header = req.headers.authorization;
132
- if (typeof header !== 'string') return null;
133
- const m = /^Bearer\s+(.+)$/i.exec(header.trim());
134
- return m ? m[1].trim() : null;
135
- }
136
-
137
- function firstHeaderValue(value) {
138
- const raw = Array.isArray(value) ? value[0] : value;
139
- if (typeof raw !== 'string') return null;
140
- const first = raw.split(',')[0].trim();
141
- return first === '' ? null : first;
142
- }
143
-
144
- /**
145
- * Read one header case-insensitively from a plain Node headers object.
146
- *
147
- * `signing.mjs` does this internally, but the device-key path needs it before deciding whether that
148
- * module applies at all, so it is needed here too. Node lower-cases incoming header names, and a
149
- * test that builds a request by hand may not, so both spellings are tried.
150
- */
151
- function readHeader(headers, name) {
152
- if (headers === null || headers === undefined) return null;
153
- const found = Object.keys(headers).find((k) => k.toLowerCase() === name);
154
- return found === undefined ? null : firstHeaderValue(headers[found]);
155
- }
156
-
157
- /**
158
- * Read a request body with a hard byte cap.
159
- * @returns {Promise<{tooLarge?:boolean, buffer?:Buffer, aborted?:boolean}>}
160
- */
161
- function readBody(req, limit) {
162
- return new Promise((resolve) => {
163
- const declared = Number(req.headers['content-length']);
164
- if (Number.isFinite(declared) && declared > limit) {
165
- req.resume();
166
- resolve({ tooLarge: true });
167
- return;
168
- }
169
- const chunks = [];
170
- let total = 0;
171
- let settled = false;
172
- const finish = (value) => {
173
- if (settled) return;
174
- settled = true;
175
- resolve(value);
176
- };
177
- req.on('data', (chunk) => {
178
- if (settled) return;
179
- total += chunk.length;
180
- if (total > limit) {
181
- finish({ tooLarge: true });
182
- req.resume();
183
- return;
184
- }
185
- chunks.push(chunk);
186
- });
187
- req.on('end', () => finish({ buffer: Buffer.concat(chunks) }));
188
- req.on('error', () => finish({ aborted: true }));
189
- req.on('aborted', () => finish({ aborted: true }));
190
- });
191
- }
192
-
193
- /**
194
- * Read the raw body bytes with a hard byte cap.
195
- *
196
- * v0.3.0 §9: the body is read exactly ONCE per request and the bytes are kept,
197
- * because an HMAC covers the exact received octets -- re-serialising parsed JSON
198
- * would produce different bytes (key order, whitespace) and break every signature.
199
- */
200
- async function readRawBody(req, limit) {
201
- const body = await readBody(req, limit);
202
- if (body.tooLarge) {
203
- throw new ProtocolError('FRAME_TOO_LARGE', `request frame exceeds ${limit} bytes`, { limit_bytes: limit });
204
- }
205
- if (body.aborted) throw new ProtocolError('BAD_REQUEST', 'request aborted before the body was complete');
206
- return body.buffer;
207
- }
208
-
209
- /** Parse an already-read body buffer as a JSON object. */
210
- function parseJsonBody(buffer) {
211
- const raw = buffer === null || buffer === undefined ? '' : buffer.toString('utf8');
212
- if (raw.trim() === '') return {};
213
- try {
214
- const parsed = JSON.parse(raw);
215
- if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
216
- throw new ProtocolError('BAD_REQUEST', 'request body must be a JSON object');
217
- }
218
- return parsed;
219
- } catch (err) {
220
- if (err instanceof ProtocolError) throw err;
221
- throw new ProtocolError('BAD_REQUEST', 'request body is not valid JSON', { parse_error: String(err.message) });
222
- }
223
- }
224
-
225
- /** Methods that can carry a body the signature must cover. */
226
- function methodHasBody(method) {
227
- return method === 'POST' || method === 'PUT' || method === 'PATCH' || method === 'DELETE';
228
- }
229
-
230
- /* ------------------------------------------------------------------ */
231
- /* request signing (v0.3.0 §9) */
232
- /* ------------------------------------------------------------------ */
233
-
234
- /**
235
- * Operator-facing hints. The `code` says what happened; the hint says what to do
236
- * about it, because "signature mismatch" without a direction is a support ticket.
237
- */
238
- const SIGNATURE_HINTS = Object.freeze({
239
- SIGNATURE_REQUIRED: 'this relay runs with --require-signature; sign the request (PROTOCOL-v0.3.0.md §9)',
240
- SIGNATURE_INCOMPLETE: 'all three of X-W2M-Signature, X-W2M-Timestamp and X-W2M-Nonce must be present together',
241
- SIGNATURE_MISMATCH: 'the signature matches no configured secret — check for a stale or half-rotated secret',
242
- SIGNATURE_EXPIRED: 'the timestamp is outside the accepted clock-skew window — check the sending machine clock',
243
- SIGNATURE_REPLAY: 'this nonce was already used — generate a fresh nonce for every request',
244
- SIGNATURE_BAD_TIMESTAMP: 'X-W2M-Timestamp must be whole unix seconds',
245
- SIGNATURE_BAD_NONCE: 'X-W2M-Nonce must be at least 8 characters',
246
- SIGNING_NOT_CONFIGURED:
247
- 'the relay has --require-signature enabled but no --signing-secret configured; '
248
- + 'this is a RELAY configuration error, not a client problem',
249
- });
250
-
251
- const SIGNATURE_PROTOCOL_CODES = new Set(Object.keys(SIGNATURE_HINTS));
252
-
253
- /** Turn a `verifyRequest` rejection into the §7 error envelope.
254
- * `ProtocolError` resolves the HTTP status from `ERROR_STATUS`, so the mapping
255
- * table lives in exactly one place (state.mjs) instead of being duplicated here. */
256
- function signatureError(decision) {
257
- const code = SIGNATURE_PROTOCOL_CODES.has(decision.code) ? decision.code : 'SIGNATURE_MISMATCH';
258
- return new ProtocolError(code, decision.error, {
259
- code,
260
- reason: decision.error,
261
- hint: SIGNATURE_HINTS[code] ?? null,
262
- });
263
- }
264
-
265
- /**
266
- * Read an optional secret, failing loudly on a value that is present but unusable.
267
- *
268
- * `signing.mjs` calls a silently-disabled secret the failure mode it exists to
269
- * prevent. Treating `signingSecret: 12345` (or `{}`) as "not configured" would do
270
- * exactly that at the wiring layer, so it is an error instead.
271
- */
272
- function readSecretValue(value, label) {
273
- if (value === undefined || value === null || value === '') return null;
274
- if (typeof value !== 'string') {
275
- throw new ProtocolError('BAD_REQUEST',
276
- `${label} must be a non-empty string when provided (got ${typeof value}); `
277
- + 'refusing to silently run without the signing you asked for', { option: label, received_type: typeof value });
278
- }
279
- return value;
280
- }
281
-
282
- /* ------------------------------------------------------------------ */
283
- /* pair rate limiting (v0.1.2 §6) */
284
- /* ------------------------------------------------------------------ */
285
-
286
- /**
287
- * Per-IP sliding window. Successful pairs count too, otherwise a working pairing
288
- * code could be used to flush the failure budget.
289
- */
290
- /*
291
- * The limiter MOVED to `src/util/rate-limit.mjs` in v0.30.0.
292
- *
293
- * It never used anything from this file, and when the approval gate needed the same window, importing it
294
- * from here made an `agent` module depend on the `relay` layer -- which `scripts/layer-model.mjs` refused,
295
- * correctly. `agent` is rank 2 and `relay` is rank 3, so the edge points up; moving the class to `util`
296
- * (rank 0, a leaf) lets both depend on it in the only direction the model allows.
297
- *
298
- * Re-exported rather than merely imported, because `SlidingWindowRateLimiter` and `RATE_LIMIT_WINDOW_MS` are
299
- * part of this module's published surface and `test/relay.test.mjs` imports the class from here.
300
- */
301
- export { SlidingWindowRateLimiter };
302
-
303
- /* ------------------------------------------------------------------ */
304
- /* server */
305
- /* ------------------------------------------------------------------ */
306
-
307
- export class RelayServer {
308
- constructor(options = {}) {
309
- this.options = options;
310
- this.basePath = normalizeBasePath(options.basePath);
311
- this.trustProxy = options.trustProxy === true;
312
- this.frameLimitBytes = options.frameLimitBytes ?? MAX_FRAME_BYTES;
313
- this.keepaliveMs = options.keepaliveMs ?? DEFAULT_SSE_KEEPALIVE_MS;
314
- this.sweepIntervalMs = options.sweepIntervalMs ?? DEFAULT_SWEEP_INTERVAL_MS;
315
- this.pairRateLimitPerMinute = Number.isFinite(options.pairRateLimitPerMinute)
316
- ? options.pairRateLimitPerMinute : DEFAULT_PAIR_RATE_LIMIT_PER_MINUTE;
317
-
318
- // §3: `logger: null` silences. The default writes to STDERR on purpose —
319
- // stdout belongs to protocol output (e.g. the CLI's `--json` line), and mixing
320
- // prose into it breaks `| ConvertFrom-Json` / `| jq`.
321
- this.logger = options.logger === undefined
322
- ? ((msg) => process.stderr.write(`${msg}\n`))
323
- : options.logger;
324
- this.startupMessages = [];
325
-
326
- /* ---- persistence (§7) ---- */
327
- const rawStateDir = options.stateDir ?? (typeof options.state === 'string' ? options.state : null);
328
- const persistEnabled = options.persist !== false && rawStateDir !== '';
329
- const stateDir = persistEnabled
330
- ? path.resolve(rawStateDir === null || rawStateDir === undefined ? DEFAULT_STATE_DIR : String(rawStateDir))
331
- : null;
332
- this.persistence = new Persistence({
333
- dir: stateDir,
334
- now: () => (this.state ? this.state.nowMs() : Date.now()),
335
- });
336
-
337
- /* ---- state ---- */
338
- const injected = options.state && typeof options.state === 'object' ? options.state : null;
339
- this.state = injected ?? new RabbitState({ ...options, persistence: this.persistence });
340
- this.state.setPersistence(this.persistence);
341
-
342
- // v0.3.9: P2P signalling. Ephemeral by design -- see src/relay/peers.mjs.
343
- this.peers = options.peers ?? new PeerRegistry({ nowMs: () => this.state.nowMs() });
344
-
345
- this.relayId = this.state.relayId;
346
- this._startedAtMs = this.state.nowMs();
347
-
348
- /* ---- startup recovery (§7) ---- */
349
- if (this.persistence.enabled) {
350
- const snapshot = this.persistence.loadDevices();
351
- this.persistence.devicesMissing = snapshot.missing;
352
- const ledger = this.persistence.loadLedger();
353
- const revived = this.state.restore({ devices: snapshot.devices, ledger });
354
- this.persistence.revivedDevices = revived.revived_devices;
355
- this.persistence.revivedTasks = revived.revived_tasks;
356
- }
357
-
358
- /* ---- operator token (§5) ---- */
359
- this._initOperatorToken(options);
360
-
361
- /* ---- v0.3.0 §9 request signing ---- */
362
- this.signingSecrets = [
363
- readSecretValue(options.signingSecret, 'signingSecret'),
364
- readSecretValue(options.signingSecretPrevious, 'signingSecretPrevious'),
365
- ].filter((s) => s !== null); // newest first: keyIndex 0 = current, 1 = previous
366
- this.signingConfigured = this.signingSecrets.length > 0;
367
- this.requireSignature = options.requireSignature === true;
368
- /**
369
- * v0.3.3: whether `GET /metrics` exists.
370
- *
371
- * Defaults to off. A monitoring endpoint that appears without being asked for is a surface nobody
372
- * chose, and the operator can turn it on with `--metrics` when a scraper exists.
373
- */
374
- this.metricsEnabled = options.metrics === true;
375
- this.signatureSkewSeconds = Number.isFinite(options.signatureSkewSeconds)
376
- ? options.signatureSkewSeconds : DEFAULT_SKEW_SECONDS;
377
- // One cache per relay instance: replay defence is stateful, and two relays
378
- // sharing a cache would let a nonce blocked on one be replayed on the other.
379
- this.nonceCache = options.nonceCache ?? new NonceCache({
380
- max: options.nonceCacheMax ?? 20_000,
381
- retentionMs: options.nonceCacheRetentionMs ?? 10 * 60_000,
382
- });
383
- this.nonceRetentionMs = options.nonceCacheRetentionMs ?? 10 * 60_000;
384
-
385
- /* ---- /v1/pair rate limit (§6) ---- */
386
- this.pairLimiter = new SlidingWindowRateLimiter({
387
- limit: this.pairRateLimitPerMinute,
388
- windowMs: DEFAULT_WINDOW_MS,
389
- now: () => this.state.nowMs(),
390
- });
391
-
392
- /* ---- transport ---- */
393
- this.tlsCertPath = options.tlsCert ?? null;
394
- this.tlsKeyPath = options.tlsKey ?? null;
395
- if ((this.tlsCertPath && !this.tlsKeyPath) || (!this.tlsCertPath && this.tlsKeyPath)) {
396
- throw new ProtocolError('BAD_REQUEST', 'tlsCert and tlsKey must be provided together', {
397
- tls_cert: this.tlsCertPath, tls_key: this.tlsKeyPath,
398
- });
399
- }
400
- const handler = (req, res) => {
401
- this.handle(req, res).catch((err) => this.reportError(req, res, err));
402
- };
403
- if (this.tlsCertPath) {
404
- let cert;
405
- let key;
406
- try {
407
- cert = fs.readFileSync(this.tlsCertPath);
408
- key = fs.readFileSync(this.tlsKeyPath);
409
- } catch (err) {
410
- // Never silently fall back to plain HTTP: that would be a security downgrade.
411
- throw new ProtocolError('BAD_REQUEST', `cannot read TLS material: ${err.message}`, {
412
- tls_cert: this.tlsCertPath, tls_key: this.tlsKeyPath,
413
- });
414
- }
415
- this.server = https.createServer({ cert, key }, handler);
416
- this.scheme = 'https';
417
- } else {
418
- this.server = http.createServer(handler);
419
- this.scheme = 'http';
420
- }
421
-
422
- // v0.1.2 BUG-1: an externally supplied code must be REGISTERED, not merely
423
- // assigned — otherwise the banner advertises a code /v1/pair does not know.
424
- this.pairingCode = typeof options.pairingCode === 'string' && options.pairingCode.trim() !== ''
425
- ? this.state.registerPairingCode(options.pairingCode, { ttlMs: options.pairingCodeTtlMs })
426
- : this.state.createPairingCode();
427
- this.subscribers = new Set();
428
- this.host = null;
429
- this.port = null;
430
- this._sweepTimer = null;
431
-
432
- // SSE connections are long-lived: never let Node time the request out.
433
- this.server.requestTimeout = options.requestTimeoutMs ?? 0;
434
- this.server.headersTimeout = options.headersTimeoutMs ?? 60_000;
435
-
436
- this._buildStartupMessages();
437
- }
438
-
439
- /**
440
- * Diagnostics sink. `logger: null` is the documented "silent" value, and a
441
- * caller-supplied logger may throw — neither may break a request. (A direct
442
- * `this.logger(...)` call after `res.writeHead()` once threw a TypeError that
443
- * surfaced as "Cannot write headers after they are sent to the client".)
444
- */
445
- _log(message) {
446
- if (typeof this.logger !== 'function') return;
447
- try {
448
- this.logger(message);
449
- } catch { /* a broken logger must never break the relay */ }
450
- }
451
-
452
- /**
453
- * Last-resort error path for a request whose handler rejected.
454
- * Once the status line is on the wire (SSE, streamed report) the response can no
455
- * longer be rewritten — the only honest action is to end the stream, loudly.
456
- */
457
- reportError(req, res, err) {
458
- if (res.headersSent) {
459
- this._log(`[w2m-rabbit] !! error after the response had started on ${req.method} ${req.url}: `
460
- + `${err?.stack ?? err}`);
461
- try { res.end(); } catch { /* already gone */ }
462
- return;
463
- }
464
- sendError(res, err);
465
- }
466
-
467
- _initOperatorToken(options) {
468
- const wantsRequired = options.operatorTokenRequired !== false;
469
- const explicit = options.operatorToken;
470
- const persisted = this.persistence.enabled ? this.persistence.readOperatorToken() : null;
471
- /**
472
- * Why the token may be printed, when it may be: `'generated'` is the one start whose token exists
473
- * nowhere else until this process writes it, and `'option'`/`'env'` are the documented opt-ins.
474
- * @type {'generated'|'option'|'env'|null}
475
- */
476
- this.operatorTokenPrintReason = printOperatorTokenOptIn(options);
477
-
478
- if (!wantsRequired || explicit === '') {
479
- this.operatorToken = null;
480
- this.operatorTokenRequired = false;
481
- this.operatorTokenSource = 'disabled';
482
- this.operatorTokenPrintable = false;
483
- this.operatorTokenPrintReason = null;
484
- return;
485
- }
486
- this.operatorTokenRequired = true;
487
- if (typeof explicit === 'string' && explicit.length > 0) {
488
- this.operatorToken = explicit;
489
- this.operatorTokenSource = 'option';
490
- this.persistence.writeOperatorToken(explicit);
491
- } else if (persisted) {
492
- // §5.1 + §7: keep the token stable across restarts, otherwise every restart
493
- // would silently invalidate the plugin's configured operatorToken.
494
- this.operatorToken = persisted;
495
- this.operatorTokenSource = 'restored';
496
- } else {
497
- this.operatorToken = randomBytes(32).toString('hex');
498
- this.operatorTokenSource = 'generated';
499
- this.persistence.writeOperatorToken(this.operatorToken);
500
- }
501
- // MEDIUM-6: the material is printed only by the start that generated it, or when the operator has
502
- // explicitly asked for it. "The posture, never the material" still holds everywhere else.
503
- if (this.operatorTokenSource === 'generated') {
504
- this.operatorTokenPrintReason = this.operatorTokenPrintReason ?? 'generated';
505
- }
506
- this.operatorTokenPrintable = this.operatorTokenPrintReason !== null;
507
- }
508
-
509
- _buildStartupMessages() {
510
- const m = this.startupMessages;
511
- m.push(`[w2m-rabbit] relay_id=${this.relayId}`);
512
- m.push(`[w2m-rabbit] base_path=${this.basePath}${this.basePath === '/' ? ' (root)' : ''}`);
513
- m.push(`[w2m-rabbit] trust_proxy=${this.trustProxy}${this.trustProxy ? '' : ' (X-Forwarded-* ignored)'}`);
514
- m.push(`[w2m-rabbit] pair_rate_limit=${this.pairRateLimitPerMinute === 0 ? 'disabled' : `${this.pairRateLimitPerMinute}/min/IP`}`);
515
- // §9: report the posture, never the material.
516
- if (this.signingConfigured) {
517
- m.push(`[w2m-rabbit] request signing=ON (${this.signingSecrets.length} secret(s)`
518
- + `${this.signingSecrets.length > 1 ? ', rotation active' : ''})`
519
- + `${this.requireSignature ? ' - REQUIRED: unsigned /v1/* is rejected with 401' : ' - optional: unsigned requests are still accepted'}`);
520
- } else if (this.requireSignature) {
521
- m.push('[w2m-rabbit] !! CONFIGURATION ERROR: --require-signature is set but no --signing-secret was provided. '
522
- + 'Every /v1/* request will fail with 500 SIGNING_NOT_CONFIGURED (this is the RELAY\'s fault, not the client\'s). '
523
- + 'Either set --signing-secret or drop --require-signature.');
524
- } else {
525
- m.push('[w2m-rabbit] request signing=off (no signing secret configured; behaviour is identical to v0.2.3)');
526
- }
527
- if (this.persistence.enabled) {
528
- m.push(`[w2m-rabbit] persistence=on dir=${this.persistence.dir} `
529
- + `revived_devices=${this.persistence.revivedDevices} revived_tasks=${this.persistence.revivedTasks}`);
530
- } else {
531
- m.push('[w2m-rabbit] persistence=off (in-memory only; devices and tasks are lost on restart)');
532
- }
533
- if (this.persistence.devicesCorrupt) {
534
- m.push('[w2m-rabbit] !! WARNING: devices.json was CORRUPT — started from an EMPTY device table; '
535
- + 'every machine must pair again. The damaged file was left untouched.');
536
- }
537
- for (const w of this.persistence.warnings) {
538
- m.push(`[w2m-rabbit] !! ${w.file}: ${w.message}`);
539
- }
540
- if (this.operatorTokenRequired) {
541
- // MEDIUM-6: the token is material, not posture, so it is printed as rarely as it can be — on the
542
- // start that generated it (`operatorTokenSource === 'generated'`) or when `printOperatorToken` /
543
- // `W2M_PRINT_OPERATOR_TOKEN=1` asked for it — and once, never twice. Where it is stored is said in
544
- // every case, including the case where there is nowhere to store it.
545
- //
546
- // DELIBERATE, do not "fix": on that one start the CLI prints the value a second time, in its own
547
- // stdout banner. Two sinks for one start is not a leak — the defect was printing it on *every*
548
- // start, restored ones included — and the box is the documented first-start handoff a human reads
549
- // (docs/DEPLOY.md §2.4, test/crossnetwork.test.mjs). Collapsing the pair would trade a documented
550
- // behaviour for one saved line on one start.
551
- const tokenFile = this.persistence.operatorTokenFile ?? null;
552
- const stored = tokenFile === null
553
- ? 'persistence is off, so this token exists only in this process and cannot be read back later'
554
- : `stored at ${tokenFile} (0600)`;
555
- if (this.operatorTokenPrintable) {
556
- m.push(`[w2m-rabbit] operator token (${this.operatorTokenSource}) - required for POST /v1/task:`);
557
- m.push(`[w2m-rabbit] ${this.operatorToken}`);
558
- m.push(`[w2m-rabbit] printed once because ${this.operatorTokenPrintReason === 'generated'
559
- ? 'this start generated it' : `${this.operatorTokenPrintReason} asked for it (${PRINT_OPERATOR_TOKEN_ENV})`}`
560
- + ` - and ${stored}`);
561
- } else {
562
- m.push(`[w2m-rabbit] operator token (${this.operatorTokenSource}) - required for POST /v1/task, NOT printed`);
563
- m.push(`[w2m-rabbit] read it from ${tokenFile ?? '<state>/operator-token.txt'} - ${stored}`);
564
- m.push(`[w2m-rabbit] set ${PRINT_OPERATOR_TOKEN_ENV}=1 to print it on this start as well`);
565
- }
566
- } else {
567
- m.push('[w2m-rabbit] !! WARNING: OPERATOR TOKEN DISABLED (--operator-token "") - '
568
- + 'any device that can reach this relay can dispatch tasks to the whole group. '
569
- + 'Only acceptable on a trusted LAN / for debugging; /v1/task then requires a device token.');
570
- }
571
- }
572
-
573
- get url() {
574
- if (this.host === null || this.port === null) return null;
575
- const host = this.host.includes(':') ? `[${this.host}]` : this.host;
576
- const prefix = this.basePath === '/' ? '' : this.basePath;
577
- return `${this.scheme}://${host}:${this.port}${prefix}`;
578
- }
579
-
580
- listen({ host = '127.0.0.1', port = 0 } = {}) {
581
- return new Promise((resolve, reject) => {
582
- const onError = (err) => reject(err);
583
- this.server.once('error', onError);
584
- this.server.listen(port, host, () => {
585
- this.server.removeListener('error', onError);
586
- const addr = this.server.address();
587
- this.host = addr.address;
588
- this.port = addr.port;
589
- this._sweepTimer = setInterval(() => {
590
- this.maintenance();
591
- }, this.sweepIntervalMs);
592
- this._sweepTimer.unref?.();
593
- if (this.logger) {
594
- this._log(`[w2m-rabbit] listening on ${this.url} pairing code: ${this.pairingCode}`);
595
- for (const message of this.startupMessages) this._log(message);
596
- }
597
- resolve({
598
- host: this.host,
599
- port: this.port,
600
- url: this.url,
601
- pairingCode: this.pairingCode,
602
- relayId: this.relayId,
603
- basePath: this.basePath,
604
- operatorToken: this.operatorToken,
605
- operatorTokenRequired: this.operatorTokenRequired,
606
- // The CLI prints its own banner, so it has to reach the same conclusion this class did rather
607
- // than re-deriving one from the token's presence (MEDIUM-6).
608
- operatorTokenSource: this.operatorTokenSource,
609
- operatorTokenPrintable: this.operatorTokenPrintable === true,
610
- operatorTokenPrintReason: this.operatorTokenPrintReason ?? null,
611
- scheme: this.scheme,
612
- });
613
- });
614
- });
615
- }
616
-
617
- sweep() {
618
- return this.state.sweepExpired();
619
- }
620
-
621
- close() {
622
- return new Promise((resolve) => {
623
- if (this._sweepTimer) clearInterval(this._sweepTimer);
624
- this._sweepTimer = null;
625
- // v0.4.0: a deferred P2P offer is a pending timer, and a closed relay must not fire one.
626
- this.state.clearPendingOffers?.();
627
- for (const sub of [...this.subscribers]) this._closeSubscriber(sub);
628
- this.server.close(() => resolve());
629
- this.server.closeAllConnections?.();
630
- });
631
- }
632
-
633
- /* ---------------- proxy awareness (v0.1.2 §3) ---------------- */
634
-
635
- /** `X-Forwarded-For` first hop is honoured ONLY under `trustProxy`. */
636
- clientIp(req) {
637
- if (this.trustProxy) {
638
- const forwarded = firstHeaderValue(req.headers['x-forwarded-for']);
639
- if (forwarded) return forwarded;
640
- }
641
- return req.socket?.remoteAddress ?? 'unknown';
642
- }
643
-
644
- /** `X-Forwarded-Proto` is honoured ONLY under `trustProxy`. */
645
- effectiveScheme(req) {
646
- if (this.trustProxy) {
647
- const proto = firstHeaderValue(req.headers['x-forwarded-proto']);
648
- if (proto) return proto.toLowerCase();
649
- }
650
- return this.scheme;
651
- }
652
-
653
- /* ---------------- request dispatch ---------------- */
654
-
655
- async handle(req, res) {
656
- let url;
657
- try {
658
- url = new URL(req.url ?? '/', `http://${req.headers.host ?? 'localhost'}`);
659
- } catch {
660
- throw new ProtocolError('BAD_REQUEST', 'malformed request URL');
661
- }
662
- const rawPath = url.pathname;
663
- const method = (req.method ?? 'GET').toUpperCase();
664
-
665
- // §3: strip the deployment prefix, then route. `/healthz` also answers at the
666
- // root because health checks are usually pointed straight at the container.
667
- // `stripped` keeps the literal request path (no trailing-slash normalisation)
668
- // because that is what a signature covers; `path` is the normalised route key.
669
- let stripped;
670
- if (this.basePath === '/') {
671
- stripped = rawPath;
672
- } else if (rawPath === this.basePath) {
673
- stripped = '/';
674
- } else if (rawPath.startsWith(`${this.basePath}/`)) {
675
- stripped = rawPath.slice(this.basePath.length);
676
- } else if (rawPath === '/healthz') {
677
- stripped = '/healthz';
678
- } else {
679
- // Outside the prefix: an explicit 404, never a gateway-style 502.
680
- throw new ProtocolError('NOT_FOUND', `path is outside the configured base path ${this.basePath}`, {
681
- path: rawPath, base_path: this.basePath,
682
- });
683
- }
684
- const path = normalizePath(stripped);
685
- const signedPath = `${stripped}${url.search}`;
686
-
687
- // v0.3.0 §9: read the body ONCE, before routing. An HMAC covers the exact
688
- // received bytes, so no route may consume the stream first and re-serialise.
689
- const rawBody = methodHasBody(method) ? await readRawBody(req, this.frameLimitBytes) : null;
690
- const takeJson = () => parseJsonBody(rawBody);
691
-
692
- /* ---- public endpoints ---- */
693
- // `/healthz` and `/v1/pair` are permanently exempt from signature checks:
694
- // the first is an operations probe that must not need credentials, the second
695
- // is the bootstrap step that happens before the two sides share a secret.
696
- if (method === 'GET' && path === '/healthz') {
697
- return sendJson(res, 200, this.healthBody(req));
698
- }
699
- if (method === 'POST' && path === '/v1/pair') {
700
- this.enforcePairRateLimit(req);
701
- const result = this.state.pair(takeJson());
702
- return sendJson(res, 200, result);
703
- }
704
- /**
705
- * v0.3.3 `/metrics`: machine-readable fleet state for a monitoring system.
706
- *
707
- * Token-free, like `/healthz` and for the same reason: a scraper that needs the operator token
708
- * would put that token in a Prometheus config file, which is a credential in a place nobody
709
- * reviews. It is also exempt from signature checks for the same reason -- there is no secret to
710
- * sign with on the scraper side.
711
- *
712
- * Exposed only when the relay was asked for it (`--metrics`). A monitoring endpoint that appears
713
- * by default is a surface nobody chose, and the whole point of the defaults elsewhere is that the
714
- * relay binds nothing the operator did not ask for.
715
- */
716
- if (method === 'GET' && path === '/metrics') {
717
- if (!this.metricsEnabled) {
718
- // 404 rather than 403: a disabled endpoint should look absent, not forbidden. A 403 tells a
719
- // scanner the surface exists.
720
- throw new ProtocolError('NOT_FOUND', 'metrics are not enabled on this relay', { hint: 'start the relay with --metrics' });
721
- }
722
- const body = renderMetrics(this.state, { nowMs: this.state.nowMs() });
723
- res.writeHead(200, {
724
- 'content-type': METRICS_CONTENT_TYPE,
725
- 'content-length': Buffer.byteLength(body, 'utf8'),
726
- 'cache-control': 'no-store',
727
- });
728
- return res.end(body);
729
- }
730
- // §5: dispatching tasks needs the operator token, not a device token.
731
- if (method === 'POST' && path === '/v1/task') {
732
- const principal = this.authorizeTaskDispatch(req);
733
- this.enforceSignature(req, { method, signedPath, body: rawBody });
734
- const body = takeJson();
735
- if (principal.machine_id && !body.created_by) body.created_by = principal.machine_id;
736
- const result = this.state.createTask(body);
737
- return sendJson(res, 200, result);
738
- }
739
-
740
- /* ---- v1.1.6: reachable WITHOUT a device token ---- */
741
- this.joinChallenges ??= createJoinChallenges({
742
- now: () => this.state.nowMs(),
743
- publicKeyFor: (fingerprint) => this.state.accountKeyFor(fingerprint),
744
- });
745
- const admissionPollMatch = /^\/v1\/fleet\/admission\/([^/]+)$/.exec(path);
746
- if (method === 'GET' && admissionPollMatch && url.searchParams.get('poll_token') !== null) {
747
- this.admission ??= createAdmission({ now: () => this.state.nowMs() });
748
- const polled = this.admission.poll(decodeURIComponent(admissionPollMatch[1]), url.searchParams.get('poll_token'));
749
- if (polled.ok !== true) throw new ProtocolError('NOT_FOUND', 'unknown admission request');
750
- return sendJson(res, 200, { protocol_version: PROTOCOL_VERSION, rabbit_time: this.state.nowIso(), ...polled });
751
- }
752
- if (method === 'GET' && path === '/v1/fleet/admission/challenge') {
753
- const issued = this.joinChallenges.issue(url.searchParams.get('fingerprint') ?? '', url.searchParams.get('machine_id') ?? '');
754
- if (issued.ok !== true) throw new ProtocolError('BAD_REQUEST', 'challenge refused: ' + issued.code, { code: issued.code });
755
- return sendJson(res, 200, { protocol_version: PROTOCOL_VERSION, rabbit_time: this.state.nowIso(), ...issued });
756
- }
757
- if (method === 'POST' && path === '/v1/fleet/admission/request') {
758
- const body = takeJson();
759
- this.admission ??= createAdmission({ now: () => this.state.nowMs() });
760
- const proof = this.joinChallenges.consume({ challenge_id: body.challenge_id, machine_id: body.machine_id, signature: body.signature });
761
- if (proof.ok !== true) throw new ProtocolError('UNAUTHORIZED', 'join proof refused: ' + proof.code, { code: proof.code });
762
- const result = this.admission.requestAdmission({ machine_id: body.machine_id, machine_name: body.machine_name ?? null, account_fingerprint: proof.fingerprint });
763
- return sendJson(res, 200, { protocol_version: PROTOCOL_VERSION, rabbit_time: this.state.nowIso(), ...result });
764
- }
765
-
766
- /* ---- everything below requires `Authorization: Bearer <device_token>` ---- */
767
- const token = bearerToken(req);
768
- const device = token ? this.state.authenticate(token) : null;
769
- if (!device) {
770
- throw new ProtocolError('UNAUTHORIZED', 'missing or invalid device_token', {});
771
- }
772
- this.state.touchDevice(device.machine_id);
773
-
774
- // v0.3.0 §9: writes are always checked (tolerating unsigned unless required);
775
- // reads are only checked when the relay was explicitly told to require
776
- // signatures. See `enforceSignature` for the reasoning.
777
- this.enforceSignature(req, { method, signedPath, body: rawBody });
778
-
779
- if (method === 'GET' && path === '/v1/stream') {
780
- return this.handleStream(req, res, url, device);
781
- }
782
- if (method === 'POST' && path === '/v1/heartbeat') {
783
- const body = takeJson();
784
- body.machine_id = body.machine_id ?? device.machine_id;
785
- const result = this.state.heartbeat(body);
786
- return sendJson(res, 200, result);
787
- }
788
- if (method === 'POST' && path === '/v1/result') {
789
- const body = takeJson();
790
- const result = this.state.submitResult(body);
791
- const { _instanceKey, ...clean } = result;
792
- return sendJson(res, 200, clean);
793
- }
794
- /**
795
- * v1.0.7: the content-addressed blob channel (#1).
796
- *
797
- * ## Why the relay may carry bytes at all
798
- *
799
- * It persists `stdout_sha256` and never the text, and `src/agent/storage.mjs` records why: each machine's
800
- * `memory.jsonl` is the only durable copy of what a command printed. **That invariant is about the relay not
801
- * interpreting content** -- it never decides what bytes mean and never asserts they are authentic.
802
- *
803
- * A blob keyed by its own hash preserves exactly that. The relay still interprets nothing; **the hash is what
804
- * says the bytes are the bytes**, exactly as `stdout_sha256` does for a result. What is new is only that a hash
805
- * the relay already holds can have its bytes fetched from the same place.
806
- *
807
- * ## Why the hash is verified before storing
808
- *
809
- * A content-addressed store that accepts bytes under a key they do not hash to is not content-addressed -- the
810
- * key stops being a claim about the content, and every later reader's verification becomes decorative. So a
811
- * mismatch is **refused (422)**, never stored and never corrected: correcting it would silently re-key a blob
812
- * the caller believes it stored under a different name.
813
- *
814
- * ## Fleet scoping
815
- *
816
- * `putFleetBlob`/`getFleetBlob` reuse `scopeFleetId`, the same rule task dispatch obeys. A blob written by one
817
- * fleet is **not readable by another**, and the read refusal is a 404 rather than a 403: a distinguishable
818
- * "not yours" tells a prober that the hash exists here.
819
- */
820
- /**
821
- * v1.0.9: **which blob this fleet published last** -- the discovery half of the channel.
822
- *
823
- * Checked **before** the `<sha256>` matcher deliberately. The matcher only accepts 64 hex characters, so
824
- * `latest` cannot collide with it today; **the ordering is asserted anyway**, because "these two patterns cannot
825
- * conflict" is exactly the reasoning that produces a shadowed route the day one of them is relaxed.
826
- *
827
- * A fleet that has never published gets a 404 rather than an empty object: "there is no current document" and
828
- * "the current document is nothing" are different statements, and only one of them is true here.
829
- */
830
- if (method === 'GET' && path === '/v1/fleet/blobs/latest') {
831
- const latest = this.state.latestFleetBlob(device);
832
- if (latest === null) {
833
- throw new ProtocolError('NOT_FOUND', 'this fleet has published no blob', {});
834
- }
835
- return sendJson(res, 200, {
836
- protocol_version: PROTOCOL_VERSION,
837
- hash: latest.hash,
838
- bytes: latest.bytes,
839
- published_at: new Date(latest.published_at_ms).toISOString(),
840
- published_by: latest.published_by ?? null,
841
- // Stated in the payload, not only in the docs: a pointer can outlive its blob under oldest-first eviction.
842
- hint: 'a hint, not a guarantee -- a pull may answer 404 if the blob has since been evicted',
843
- });
844
- }
845
-
846
- const blobMatch = /^\/v1\/fleet\/blobs\/([0-9a-f]{64})$/.exec(path);
847
- if (blobMatch !== null) {
848
- const hash = blobMatch[1];
849
- if (method === 'PUT') {
850
- const received = rawBody ?? Buffer.alloc(0);
851
- const actual = createHash('sha256').update(received).digest('hex');
852
- if (actual !== hash) {
853
- throw new ProtocolError('BLOB_HASH_MISMATCH', 'the body does not hash to the key it was sent under', {
854
- expected: hash,
855
- actual,
856
- bytes: received.length,
857
- });
858
- }
859
- const stored = this.state.putFleetBlob(device, received);
860
- if (stored.ok !== true) {
861
- throw new ProtocolError(stored.reason ?? 'BLOB_WRITE_FAILED', stored.detail ?? 'the blob was refused', {
862
- hash,
863
- });
864
- }
865
- return sendJson(res, 201, {
866
- protocol_version: PROTOCOL_VERSION,
867
- ok: true,
868
- hash: stored.hash,
869
- bytes: stored.bytes,
870
- evicted: stored.evicted,
871
- });
872
- }
873
- if (method === 'GET') {
874
- const bytes = this.state.getFleetBlob(device, hash);
875
- if (bytes === null) {
876
- // Same code and shape for "no such blob" and "not your fleet's" -- deliberately indistinguishable.
877
- throw new ProtocolError('NOT_FOUND', 'no such blob is readable by this device', { hash });
878
- }
879
- res.writeHead(200, {
880
- 'content-type': 'application/octet-stream',
881
- 'content-length': bytes.length,
882
- // Same reason as `/metrics`: a memory document must not be cached by an intermediary.
883
- 'cache-control': 'no-store',
884
- });
885
- res.end(bytes);
886
- return true;
887
- }
888
- throw new ProtocolError('BAD_REQUEST', 'a blob is addressed with PUT or GET', { method });
889
- }
890
-
891
- if (method === 'GET' && path === '/v1/devices') {
892
- return sendJson(res, 200, {
893
- protocol_version: PROTOCOL_VERSION,
894
- rabbit_time: this.state.nowIso(),
895
- devices: this.state.listDevices(),
896
- });
897
- }
898
- if (method === 'GET' && path === '/v1/tasks') {
899
- const rawLimit = url.searchParams.get('limit');
900
- let limit = 50;
901
- if (rawLimit !== null) {
902
- limit = Number(rawLimit);
903
- if (!Number.isInteger(limit) || limit < 1) {
904
- throw new ProtocolError('BAD_REQUEST', 'limit must be a positive integer', { limit: rawLimit });
905
- }
906
- }
907
- return sendJson(res, 200, {
908
- protocol_version: PROTOCOL_VERSION,
909
- rabbit_time: this.state.nowIso(),
910
- tasks: this.state.listTasks({ limit }),
911
- });
912
- }
913
-
914
- const taskMatch = /^\/v1\/tasks\/([^/]+)$/.exec(path);
915
- const reportMatch = /^\/v1\/tasks\/([^/]+)\/report$/.exec(path);
916
- // v0.3.0: cross-machine RTT status. The plugin may run on a different machine
917
- // than the agent, so this has to come from the relay, not from a local file.
918
- const agentStatusMatch = /^\/v1\/agents\/([^/]+)\/status$/.exec(path);
919
- // v0.3.9: P2P candidate rendezvous. `/announce` is checked before the read route
920
- // so that a machine literally named "announce" cannot shadow it.
921
- const peerReadMatch = /^\/v1\/peer\/([^/]+)$/.exec(path);
922
- /* ---- v1.1.6 admission ---- */
923
- const admissionDecideMatch = /^\/v1\/fleet\/admission\/([^/]+)\/decide$/.exec(path);
924
- const memberRemoveMatch = /^\/v1\/fleet\/members\/([^/]+)\/remove$/.exec(path);
925
- const removalVoteMatch = /^\/v1\/fleet\/removals\/([^/]+)\/vote$/.exec(path);
926
-
927
- /* ---- v0.3.9 P2P signalling ---- */
928
- if (method === 'POST' && path === '/v1/peer/announce') {
929
- const body = takeJson();
930
- try {
931
- // `device.machine_id`, never `body.machine_id`: a paired machine may only
932
- // announce addresses for itself, or it could aim a peer's punch at a third party.
933
- const result = this.peers.announce(device.machine_id, body);
934
- return sendJson(res, 200, {
935
- protocol_version: PROTOCOL_VERSION,
936
- rabbit_time: this.state.nowIso(),
937
- ...result,
938
- });
939
- } catch (error) {
940
- throw new ProtocolError('BAD_REQUEST', error.message, { code: error.code ?? 'BAD_ANNOUNCEMENT' });
941
- }
942
- }
943
-
944
- if (method === 'GET' && peerReadMatch) {
945
- const machineId = decodeURIComponent(peerReadMatch[1]);
946
- const entry = this.peers.get(machineId);
947
- if (!entry) {
948
- // A named 404 rather than an empty list: "this machine has not announced" and
949
- // "this machine announced nothing reachable" call for different responses, and
950
- // an empty candidate list would send the caller into a punch that cannot work.
951
- throw new ProtocolError('NOT_FOUND', `no live P2P announcement for ${machineId}`, {
952
- machine_id: machineId,
953
- hint: 'the peer must POST /v1/peer/announce first, and announcements expire',
954
- });
955
- }
956
- return sendJson(res, 200, {
957
- protocol_version: PROTOCOL_VERSION,
958
- rabbit_time: this.state.nowIso(),
959
- peer: entry,
960
- });
961
- }
962
-
963
- if (method === 'GET' && agentStatusMatch) {
964
- const machineId = decodeURIComponent(agentStatusMatch[1]);
965
- const status = this.state.deviceStatus(machineId);
966
- if (!status) {
967
- throw new ProtocolError('NOT_FOUND', 'unknown machine_id', { machine_id: machineId });
968
- }
969
- return sendJson(res, 200, {
970
- protocol_version: PROTOCOL_VERSION,
971
- rabbit_time: this.state.nowIso(),
972
- ...status,
973
- });
974
- }
975
-
976
- if (method === 'GET' && reportMatch) {
977
- const taskId = decodeURIComponent(reportMatch[1]);
978
- this.state.sweepExpired();
979
- const format = url.searchParams.get('format') ?? 'json';
980
- const report = buildReport(this.state, taskId, { format });
981
- if (res.writableEnded) return undefined;
982
- res.writeHead(200, {
983
- 'content-type': report.contentType,
984
- 'content-length': Buffer.byteLength(report.body, 'utf8'),
985
- 'cache-control': 'no-store',
986
- });
987
- return res.end(report.body);
988
- }
989
-
990
- if (method === 'GET' && taskMatch) {
991
- const taskId = decodeURIComponent(taskMatch[1]);
992
- this.state.sweepExpired();
993
- const task = this.state.requireTask(taskId);
994
- const aggregate = aggregateTask(this.state, taskId);
995
- return sendJson(res, 200, {
996
- protocol_version: PROTOCOL_VERSION,
997
- rabbit_time: this.state.nowIso(),
998
- task: this.state.taskSummary(task),
999
- leases: [...task.leases.values()],
1000
- aggregate,
1001
- });
1002
- }
1003
-
1004
-
1005
- /* ---- v1.1.6 admission: the caller is always `device.machine_id`, never a body field ---- */
1006
- this.admission ??= createAdmission({ now: () => this.state.nowMs() });
1007
-
1008
- if (method === 'GET' && path === '/v1/fleet/admission/pending') {
1009
- return sendJson(res, 200, {
1010
- protocol_version: PROTOCOL_VERSION, rabbit_time: this.state.nowIso(),
1011
- pending: this.admission.pendingFor(),
1012
- });
1013
- }
1014
-
1015
- if (method === 'POST' && admissionDecideMatch) {
1016
- const body = takeJson();
1017
- const result = this.admission.decide(decodeURIComponent(admissionDecideMatch[1]), {
1018
- decision: body.decision, decided_by: device.machine_id, auto: body.auto === true,
1019
- });
1020
- if (result.ok !== true) throw new ProtocolError('BAD_REQUEST', 'admission decision refused: ' + result.code, result);
1021
- return sendJson(res, 200, { protocol_version: PROTOCOL_VERSION, rabbit_time: this.state.nowIso(), ...result });
1022
- }
1023
-
1024
- if (method === 'POST' && memberRemoveMatch) {
1025
- const body = takeJson();
1026
- const result = this.admission.remove(decodeURIComponent(memberRemoveMatch[1]), {
1027
- requested_by: device.machine_id, reason: typeof body.reason === 'string' ? body.reason : null,
1028
- });
1029
- if (result.ok !== true) throw new ProtocolError('BAD_REQUEST', 'removal refused: ' + result.code, result);
1030
- return sendJson(res, 200, { protocol_version: PROTOCOL_VERSION, rabbit_time: this.state.nowIso(), ...result });
1031
- }
1032
-
1033
- if (method === 'POST' && removalVoteMatch) {
1034
- const body = takeJson();
1035
- const result = this.admission.vote(decodeURIComponent(removalVoteMatch[1]), {
1036
- machine_id: device.machine_id, vote: body.vote,
1037
- });
1038
- if (result.ok !== true) throw new ProtocolError('BAD_REQUEST', 'vote refused: ' + result.code, result);
1039
- return sendJson(res, 200, { protocol_version: PROTOCOL_VERSION, rabbit_time: this.state.nowIso(), ...result });
1040
- }
1041
-
26
+ import { createFleetMemory } from './fleet-memory.mjs';
27
+ import { createJoinChallenges } from './join-challenge.mjs';
28
+ import { createAdmission } from './admission.mjs';
29
+ import { Persistence } from './persistence.mjs';
30
+ import { PeerRegistry } from './peers.mjs';
31
+ import { aggregateTask, buildReport } from './report.mjs';
32
+ import { renderMetrics, METRICS_CONTENT_TYPE } from './metrics.mjs';
33
+ // v0.3.0 §9: request signing. This import is the entire point of the module --
34
+ // until the HTTP layer calls it, HMAC support does not exist for any real request.
35
+ import { DEFAULT_SKEW_SECONDS, NonceCache, verifyRequest } from '../signing.mjs';
36
+ // v0.7.11: per-device Ed25519 verification, tried before the shared secret.
37
+ import {
38
+ SIGNATURE_HEADER as DEVICE_SIGNATURE_HEADER,
39
+ verifyDeviceSignature,
40
+ } from '../chatops/device-signing.mjs';
41
+
42
+ export const DEFAULT_SSE_KEEPALIVE_MS = 15_000; // §3.2: every 15s a `: keepalive` line
43
+ export const DEFAULT_SWEEP_INTERVAL_MS = 1_000;
44
+ export const DEFAULT_PAIR_RATE_LIMIT_PER_MINUTE = 5; // v0.1.2 §6
45
+ // The window length is a unit and lives with the limiter. Both names are kept: `DEFAULT_WINDOW_MS` is the
46
+ // local binding this module uses, and `RATE_LIMIT_WINDOW_MS` is the name this module has always exported.
47
+ // `export ... from` would NOT have worked here -- it re-exports without creating a local binding, so the
48
+ // constructor below saw `RATE_LIMIT_WINDOW_MS is not defined` while the export itself looked fine.
49
+ import { DEFAULT_WINDOW_MS, SlidingWindowRateLimiter } from '../util/rate-limit.mjs';
50
+ export { DEFAULT_WINDOW_MS as RATE_LIMIT_WINDOW_MS };
51
+ export const DEFAULT_STATE_DIR = path.join(os.homedir(), '.w2m');
52
+
53
+ /**
54
+ * The environment variable that opts in to printing the operator token on a start that did not
55
+ * generate it (MEDIUM-6 of the v0.5.9 review).
56
+ *
57
+ * The value is printed **once, on the start that generated it**, because that is the only moment at
58
+ * which it exists nowhere else yet; every other start has `<state>/operator-token.txt` to read it
59
+ * from, and a relay's stdout ends up in journald, a container log or a CI transcript — where a
60
+ * credential that authorises dispatching to the whole fleet does not belong. `W2M_PRINT_OPERATOR_TOKEN=1`
61
+ * (or `printOperatorToken: true`, which is the same switch for an embedder) restores the old
62
+ * behaviour for an operator who genuinely needs it in the log, and says so in the banner when it does.
63
+ */
64
+ export const PRINT_OPERATOR_TOKEN_ENV = 'W2M_PRINT_OPERATOR_TOKEN';
65
+
66
+ /**
67
+ * Resolve that opt-in, so the relay and the CLI agree on one answer.
68
+ *
69
+ * @param {object} [options] Relay options; `printOperatorToken: true` is the programmatic spelling.
70
+ * @returns {'option'|'env'|null} Why printing was asked for, or null.
71
+ */
72
+ export function printOperatorTokenOptIn(options = {}) {
73
+ if (options.printOperatorToken === true) return 'option';
74
+ const raw = process.env[PRINT_OPERATOR_TOKEN_ENV];
75
+ if (raw === '1' || raw === 'true') return 'env';
76
+ return null;
77
+ }
78
+
79
+ /* ------------------------------------------------------------------ */
80
+ /* helpers */
81
+ /* ------------------------------------------------------------------ */
82
+
83
+ /** v0.1.2 §3: `/w2m/` → `/w2m`; '' / '/' → '/'. Never returns a trailing slash. */
84
+ export function normalizeBasePath(value) {
85
+ if (value === undefined || value === null || value === '') return '/';
86
+ let p = String(value).trim();
87
+ if (p === '' || p === '/') return '/';
88
+ if (!p.startsWith('/')) p = `/${p}`;
89
+ p = p.replace(/\/{2,}/g, '/').replace(/\/+$/, '');
90
+ return p === '' ? '/' : p;
91
+ }
92
+
93
+ function normalizePath(pathname) {
94
+ if (!pathname) return '/';
95
+ return pathname.length > 1 ? pathname.replace(/\/+$/, '') : pathname;
96
+ }
97
+
98
+ function sendJson(res, status, body, extraHeaders = {}) {
99
+ if (res.writableEnded) return undefined;
100
+ const payload = Buffer.from(JSON.stringify(body), 'utf8');
101
+ res.writeHead(status, {
102
+ 'content-type': 'application/json; charset=utf-8',
103
+ 'content-length': payload.length,
104
+ 'cache-control': 'no-store',
105
+ ...extraHeaders,
106
+ });
107
+ return res.end(payload);
108
+ }
109
+
110
+ function sendError(res, err) {
111
+ if (err instanceof ProtocolError) {
112
+ const body = err.toBody();
113
+ // §6 just says the 429 "carries retry_after_seconds" without pinning the
114
+ // location, so it is emitted both inside error.detail (the §7 shape) and as a
115
+ // top-level convenience field. Both are additive.
116
+ const retryAfter = err.detail?.retry_after_seconds;
117
+ if (typeof retryAfter === 'number') body.retry_after_seconds = retryAfter;
118
+ sendJson(res, err.status, body, err.headers ?? {});
119
+ return;
120
+ }
121
+ sendJson(res, 500, {
122
+ error: {
123
+ code: 'INTERNAL',
124
+ message: err?.message ? String(err.message) : 'internal error',
125
+ detail: {},
126
+ },
127
+ });
128
+ }
129
+
130
+ function bearerToken(req) {
131
+ const header = req.headers.authorization;
132
+ if (typeof header !== 'string') return null;
133
+ const m = /^Bearer\s+(.+)$/i.exec(header.trim());
134
+ return m ? m[1].trim() : null;
135
+ }
136
+
137
+ function firstHeaderValue(value) {
138
+ const raw = Array.isArray(value) ? value[0] : value;
139
+ if (typeof raw !== 'string') return null;
140
+ const first = raw.split(',')[0].trim();
141
+ return first === '' ? null : first;
142
+ }
143
+
144
+ /**
145
+ * Read one header case-insensitively from a plain Node headers object.
146
+ *
147
+ * `signing.mjs` does this internally, but the device-key path needs it before deciding whether that
148
+ * module applies at all, so it is needed here too. Node lower-cases incoming header names, and a
149
+ * test that builds a request by hand may not, so both spellings are tried.
150
+ */
151
+ function readHeader(headers, name) {
152
+ if (headers === null || headers === undefined) return null;
153
+ const found = Object.keys(headers).find((k) => k.toLowerCase() === name);
154
+ return found === undefined ? null : firstHeaderValue(headers[found]);
155
+ }
156
+
157
+ /**
158
+ * Read a request body with a hard byte cap.
159
+ * @returns {Promise<{tooLarge?:boolean, buffer?:Buffer, aborted?:boolean}>}
160
+ */
161
+ function readBody(req, limit) {
162
+ return new Promise((resolve) => {
163
+ const declared = Number(req.headers['content-length']);
164
+ if (Number.isFinite(declared) && declared > limit) {
165
+ req.resume();
166
+ resolve({ tooLarge: true });
167
+ return;
168
+ }
169
+ const chunks = [];
170
+ let total = 0;
171
+ let settled = false;
172
+ const finish = (value) => {
173
+ if (settled) return;
174
+ settled = true;
175
+ resolve(value);
176
+ };
177
+ req.on('data', (chunk) => {
178
+ if (settled) return;
179
+ total += chunk.length;
180
+ if (total > limit) {
181
+ finish({ tooLarge: true });
182
+ req.resume();
183
+ return;
184
+ }
185
+ chunks.push(chunk);
186
+ });
187
+ req.on('end', () => finish({ buffer: Buffer.concat(chunks) }));
188
+ req.on('error', () => finish({ aborted: true }));
189
+ req.on('aborted', () => finish({ aborted: true }));
190
+ });
191
+ }
192
+
193
+ /**
194
+ * Read the raw body bytes with a hard byte cap.
195
+ *
196
+ * v0.3.0 §9: the body is read exactly ONCE per request and the bytes are kept,
197
+ * because an HMAC covers the exact received octets -- re-serialising parsed JSON
198
+ * would produce different bytes (key order, whitespace) and break every signature.
199
+ */
200
+ async function readRawBody(req, limit) {
201
+ const body = await readBody(req, limit);
202
+ if (body.tooLarge) {
203
+ throw new ProtocolError('FRAME_TOO_LARGE', `request frame exceeds ${limit} bytes`, { limit_bytes: limit });
204
+ }
205
+ if (body.aborted) throw new ProtocolError('BAD_REQUEST', 'request aborted before the body was complete');
206
+ return body.buffer;
207
+ }
208
+
209
+ /** Parse an already-read body buffer as a JSON object. */
210
+ function parseJsonBody(buffer) {
211
+ const raw = buffer === null || buffer === undefined ? '' : buffer.toString('utf8');
212
+ if (raw.trim() === '') return {};
213
+ try {
214
+ const parsed = JSON.parse(raw);
215
+ if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
216
+ throw new ProtocolError('BAD_REQUEST', 'request body must be a JSON object');
217
+ }
218
+ return parsed;
219
+ } catch (err) {
220
+ if (err instanceof ProtocolError) throw err;
221
+ throw new ProtocolError('BAD_REQUEST', 'request body is not valid JSON', { parse_error: String(err.message) });
222
+ }
223
+ }
224
+
225
+ /** Methods that can carry a body the signature must cover. */
226
+ function methodHasBody(method) {
227
+ return method === 'POST' || method === 'PUT' || method === 'PATCH' || method === 'DELETE';
228
+ }
229
+
230
+ /* ------------------------------------------------------------------ */
231
+ /* request signing (v0.3.0 §9) */
232
+ /* ------------------------------------------------------------------ */
233
+
234
+ /**
235
+ * Operator-facing hints. The `code` says what happened; the hint says what to do
236
+ * about it, because "signature mismatch" without a direction is a support ticket.
237
+ */
238
+ const SIGNATURE_HINTS = Object.freeze({
239
+ SIGNATURE_REQUIRED: 'this relay runs with --require-signature; sign the request (PROTOCOL-v0.3.0.md §9)',
240
+ SIGNATURE_INCOMPLETE: 'all three of X-W2M-Signature, X-W2M-Timestamp and X-W2M-Nonce must be present together',
241
+ SIGNATURE_MISMATCH: 'the signature matches no configured secret — check for a stale or half-rotated secret',
242
+ SIGNATURE_EXPIRED: 'the timestamp is outside the accepted clock-skew window — check the sending machine clock',
243
+ SIGNATURE_REPLAY: 'this nonce was already used — generate a fresh nonce for every request',
244
+ SIGNATURE_BAD_TIMESTAMP: 'X-W2M-Timestamp must be whole unix seconds',
245
+ SIGNATURE_BAD_NONCE: 'X-W2M-Nonce must be at least 8 characters',
246
+ SIGNING_NOT_CONFIGURED:
247
+ 'the relay has --require-signature enabled but no --signing-secret configured; '
248
+ + 'this is a RELAY configuration error, not a client problem',
249
+ });
250
+
251
+ const SIGNATURE_PROTOCOL_CODES = new Set(Object.keys(SIGNATURE_HINTS));
252
+
253
+ /** Turn a `verifyRequest` rejection into the §7 error envelope.
254
+ * `ProtocolError` resolves the HTTP status from `ERROR_STATUS`, so the mapping
255
+ * table lives in exactly one place (state.mjs) instead of being duplicated here. */
256
+ function signatureError(decision) {
257
+ const code = SIGNATURE_PROTOCOL_CODES.has(decision.code) ? decision.code : 'SIGNATURE_MISMATCH';
258
+ return new ProtocolError(code, decision.error, {
259
+ code,
260
+ reason: decision.error,
261
+ hint: SIGNATURE_HINTS[code] ?? null,
262
+ });
263
+ }
264
+
265
+ /**
266
+ * Read an optional secret, failing loudly on a value that is present but unusable.
267
+ *
268
+ * `signing.mjs` calls a silently-disabled secret the failure mode it exists to
269
+ * prevent. Treating `signingSecret: 12345` (or `{}`) as "not configured" would do
270
+ * exactly that at the wiring layer, so it is an error instead.
271
+ */
272
+ function readSecretValue(value, label) {
273
+ if (value === undefined || value === null || value === '') return null;
274
+ if (typeof value !== 'string') {
275
+ throw new ProtocolError('BAD_REQUEST',
276
+ `${label} must be a non-empty string when provided (got ${typeof value}); `
277
+ + 'refusing to silently run without the signing you asked for', { option: label, received_type: typeof value });
278
+ }
279
+ return value;
280
+ }
281
+
282
+ /* ------------------------------------------------------------------ */
283
+ /* pair rate limiting (v0.1.2 §6) */
284
+ /* ------------------------------------------------------------------ */
285
+
286
+ /**
287
+ * Per-IP sliding window. Successful pairs count too, otherwise a working pairing
288
+ * code could be used to flush the failure budget.
289
+ */
290
+ /*
291
+ * The limiter MOVED to `src/util/rate-limit.mjs` in v0.30.0.
292
+ *
293
+ * It never used anything from this file, and when the approval gate needed the same window, importing it
294
+ * from here made an `agent` module depend on the `relay` layer -- which `scripts/layer-model.mjs` refused,
295
+ * correctly. `agent` is rank 2 and `relay` is rank 3, so the edge points up; moving the class to `util`
296
+ * (rank 0, a leaf) lets both depend on it in the only direction the model allows.
297
+ *
298
+ * Re-exported rather than merely imported, because `SlidingWindowRateLimiter` and `RATE_LIMIT_WINDOW_MS` are
299
+ * part of this module's published surface and `test/relay.test.mjs` imports the class from here.
300
+ */
301
+ export { SlidingWindowRateLimiter };
302
+
303
+ /* ------------------------------------------------------------------ */
304
+ /* server */
305
+ /* ------------------------------------------------------------------ */
306
+
307
+ export class RelayServer {
308
+ constructor(options = {}) {
309
+ this.options = options;
310
+ this.basePath = normalizeBasePath(options.basePath);
311
+ this.trustProxy = options.trustProxy === true;
312
+ this.frameLimitBytes = options.frameLimitBytes ?? MAX_FRAME_BYTES;
313
+ this.keepaliveMs = options.keepaliveMs ?? DEFAULT_SSE_KEEPALIVE_MS;
314
+ this.sweepIntervalMs = options.sweepIntervalMs ?? DEFAULT_SWEEP_INTERVAL_MS;
315
+ this.pairRateLimitPerMinute = Number.isFinite(options.pairRateLimitPerMinute)
316
+ ? options.pairRateLimitPerMinute : DEFAULT_PAIR_RATE_LIMIT_PER_MINUTE;
317
+
318
+ // §3: `logger: null` silences. The default writes to STDERR on purpose —
319
+ // stdout belongs to protocol output (e.g. the CLI's `--json` line), and mixing
320
+ // prose into it breaks `| ConvertFrom-Json` / `| jq`.
321
+ this.logger = options.logger === undefined
322
+ ? ((msg) => process.stderr.write(`${msg}\n`))
323
+ : options.logger;
324
+ this.startupMessages = [];
325
+
326
+ /* ---- persistence (§7) ---- */
327
+ const rawStateDir = options.stateDir ?? (typeof options.state === 'string' ? options.state : null);
328
+ const persistEnabled = options.persist !== false && rawStateDir !== '';
329
+ const stateDir = persistEnabled
330
+ ? path.resolve(rawStateDir === null || rawStateDir === undefined ? DEFAULT_STATE_DIR : String(rawStateDir))
331
+ : null;
332
+ this.persistence = new Persistence({
333
+ dir: stateDir,
334
+ now: () => (this.state ? this.state.nowMs() : Date.now()),
335
+ });
336
+
337
+ /* ---- state ---- */
338
+ const injected = options.state && typeof options.state === 'object' ? options.state : null;
339
+ this.state = injected ?? new RabbitState({ ...options, persistence: this.persistence });
340
+ this.state.setPersistence(this.persistence);
341
+
342
+ // v0.3.9: P2P signalling. Ephemeral by design -- see src/relay/peers.mjs.
343
+ this.peers = options.peers ?? new PeerRegistry({ nowMs: () => this.state.nowMs() });
344
+
345
+ this.relayId = this.state.relayId;
346
+ this._startedAtMs = this.state.nowMs();
347
+
348
+ /* ---- startup recovery (§7) ---- */
349
+ if (this.persistence.enabled) {
350
+ const snapshot = this.persistence.loadDevices();
351
+ this.persistence.devicesMissing = snapshot.missing;
352
+ const ledger = this.persistence.loadLedger();
353
+ const revived = this.state.restore({ devices: snapshot.devices, ledger });
354
+ this.persistence.revivedDevices = revived.revived_devices;
355
+ this.persistence.revivedTasks = revived.revived_tasks;
356
+ }
357
+
358
+ /* ---- operator token (§5) ---- */
359
+ this._initOperatorToken(options);
360
+
361
+ /* ---- v0.3.0 §9 request signing ---- */
362
+ this.signingSecrets = [
363
+ readSecretValue(options.signingSecret, 'signingSecret'),
364
+ readSecretValue(options.signingSecretPrevious, 'signingSecretPrevious'),
365
+ ].filter((s) => s !== null); // newest first: keyIndex 0 = current, 1 = previous
366
+ this.signingConfigured = this.signingSecrets.length > 0;
367
+ this.requireSignature = options.requireSignature === true;
368
+ /**
369
+ * v0.3.3: whether `GET /metrics` exists.
370
+ *
371
+ * Defaults to off. A monitoring endpoint that appears without being asked for is a surface nobody
372
+ * chose, and the operator can turn it on with `--metrics` when a scraper exists.
373
+ */
374
+ this.metricsEnabled = options.metrics === true;
375
+ this.signatureSkewSeconds = Number.isFinite(options.signatureSkewSeconds)
376
+ ? options.signatureSkewSeconds : DEFAULT_SKEW_SECONDS;
377
+ // One cache per relay instance: replay defence is stateful, and two relays
378
+ // sharing a cache would let a nonce blocked on one be replayed on the other.
379
+ this.nonceCache = options.nonceCache ?? new NonceCache({
380
+ max: options.nonceCacheMax ?? 20_000,
381
+ retentionMs: options.nonceCacheRetentionMs ?? 10 * 60_000,
382
+ });
383
+ this.nonceRetentionMs = options.nonceCacheRetentionMs ?? 10 * 60_000;
384
+
385
+ /* ---- /v1/pair rate limit (§6) ---- */
386
+ this.pairLimiter = new SlidingWindowRateLimiter({
387
+ limit: this.pairRateLimitPerMinute,
388
+ windowMs: DEFAULT_WINDOW_MS,
389
+ now: () => this.state.nowMs(),
390
+ });
391
+
392
+ /* ---- transport ---- */
393
+ this.tlsCertPath = options.tlsCert ?? null;
394
+ this.tlsKeyPath = options.tlsKey ?? null;
395
+ if ((this.tlsCertPath && !this.tlsKeyPath) || (!this.tlsCertPath && this.tlsKeyPath)) {
396
+ throw new ProtocolError('BAD_REQUEST', 'tlsCert and tlsKey must be provided together', {
397
+ tls_cert: this.tlsCertPath, tls_key: this.tlsKeyPath,
398
+ });
399
+ }
400
+ const handler = (req, res) => {
401
+ this.handle(req, res).catch((err) => this.reportError(req, res, err));
402
+ };
403
+ if (this.tlsCertPath) {
404
+ let cert;
405
+ let key;
406
+ try {
407
+ cert = fs.readFileSync(this.tlsCertPath);
408
+ key = fs.readFileSync(this.tlsKeyPath);
409
+ } catch (err) {
410
+ // Never silently fall back to plain HTTP: that would be a security downgrade.
411
+ throw new ProtocolError('BAD_REQUEST', `cannot read TLS material: ${err.message}`, {
412
+ tls_cert: this.tlsCertPath, tls_key: this.tlsKeyPath,
413
+ });
414
+ }
415
+ this.server = https.createServer({ cert, key }, handler);
416
+ this.scheme = 'https';
417
+ } else {
418
+ this.server = http.createServer(handler);
419
+ this.scheme = 'http';
420
+ }
421
+
422
+ // v0.1.2 BUG-1: an externally supplied code must be REGISTERED, not merely
423
+ // assigned — otherwise the banner advertises a code /v1/pair does not know.
424
+ this.pairingCode = typeof options.pairingCode === 'string' && options.pairingCode.trim() !== ''
425
+ ? this.state.registerPairingCode(options.pairingCode, { ttlMs: options.pairingCodeTtlMs })
426
+ : this.state.createPairingCode();
427
+ this.subscribers = new Set();
428
+ this.host = null;
429
+ this.port = null;
430
+ this._sweepTimer = null;
431
+
432
+ // SSE connections are long-lived: never let Node time the request out.
433
+ this.server.requestTimeout = options.requestTimeoutMs ?? 0;
434
+ this.server.headersTimeout = options.headersTimeoutMs ?? 60_000;
435
+
436
+ this._buildStartupMessages();
437
+ }
438
+
439
+ /**
440
+ * Diagnostics sink. `logger: null` is the documented "silent" value, and a
441
+ * caller-supplied logger may throw — neither may break a request. (A direct
442
+ * `this.logger(...)` call after `res.writeHead()` once threw a TypeError that
443
+ * surfaced as "Cannot write headers after they are sent to the client".)
444
+ */
445
+ _log(message) {
446
+ if (typeof this.logger !== 'function') return;
447
+ try {
448
+ this.logger(message);
449
+ } catch { /* a broken logger must never break the relay */ }
450
+ }
451
+
452
+ /**
453
+ * Last-resort error path for a request whose handler rejected.
454
+ * Once the status line is on the wire (SSE, streamed report) the response can no
455
+ * longer be rewritten — the only honest action is to end the stream, loudly.
456
+ */
457
+ reportError(req, res, err) {
458
+ if (res.headersSent) {
459
+ this._log(`[w2m-rabbit] !! error after the response had started on ${req.method} ${req.url}: `
460
+ + `${err?.stack ?? err}`);
461
+ try { res.end(); } catch { /* already gone */ }
462
+ return;
463
+ }
464
+ sendError(res, err);
465
+ }
466
+
467
+ _initOperatorToken(options) {
468
+ const wantsRequired = options.operatorTokenRequired !== false;
469
+ const explicit = options.operatorToken;
470
+ const persisted = this.persistence.enabled ? this.persistence.readOperatorToken() : null;
471
+ /**
472
+ * Why the token may be printed, when it may be: `'generated'` is the one start whose token exists
473
+ * nowhere else until this process writes it, and `'option'`/`'env'` are the documented opt-ins.
474
+ * @type {'generated'|'option'|'env'|null}
475
+ */
476
+ this.operatorTokenPrintReason = printOperatorTokenOptIn(options);
477
+
478
+ if (!wantsRequired || explicit === '') {
479
+ this.operatorToken = null;
480
+ this.operatorTokenRequired = false;
481
+ this.operatorTokenSource = 'disabled';
482
+ this.operatorTokenPrintable = false;
483
+ this.operatorTokenPrintReason = null;
484
+ return;
485
+ }
486
+ this.operatorTokenRequired = true;
487
+ if (typeof explicit === 'string' && explicit.length > 0) {
488
+ this.operatorToken = explicit;
489
+ this.operatorTokenSource = 'option';
490
+ this.persistence.writeOperatorToken(explicit);
491
+ } else if (persisted) {
492
+ // §5.1 + §7: keep the token stable across restarts, otherwise every restart
493
+ // would silently invalidate the plugin's configured operatorToken.
494
+ this.operatorToken = persisted;
495
+ this.operatorTokenSource = 'restored';
496
+ } else {
497
+ this.operatorToken = randomBytes(32).toString('hex');
498
+ this.operatorTokenSource = 'generated';
499
+ this.persistence.writeOperatorToken(this.operatorToken);
500
+ }
501
+ // MEDIUM-6: the material is printed only by the start that generated it, or when the operator has
502
+ // explicitly asked for it. "The posture, never the material" still holds everywhere else.
503
+ if (this.operatorTokenSource === 'generated') {
504
+ this.operatorTokenPrintReason = this.operatorTokenPrintReason ?? 'generated';
505
+ }
506
+ this.operatorTokenPrintable = this.operatorTokenPrintReason !== null;
507
+ }
508
+
509
+ _buildStartupMessages() {
510
+ const m = this.startupMessages;
511
+ m.push(`[w2m-rabbit] relay_id=${this.relayId}`);
512
+ m.push(`[w2m-rabbit] base_path=${this.basePath}${this.basePath === '/' ? ' (root)' : ''}`);
513
+ m.push(`[w2m-rabbit] trust_proxy=${this.trustProxy}${this.trustProxy ? '' : ' (X-Forwarded-* ignored)'}`);
514
+ m.push(`[w2m-rabbit] pair_rate_limit=${this.pairRateLimitPerMinute === 0 ? 'disabled' : `${this.pairRateLimitPerMinute}/min/IP`}`);
515
+ // §9: report the posture, never the material.
516
+ if (this.signingConfigured) {
517
+ m.push(`[w2m-rabbit] request signing=ON (${this.signingSecrets.length} secret(s)`
518
+ + `${this.signingSecrets.length > 1 ? ', rotation active' : ''})`
519
+ + `${this.requireSignature ? ' - REQUIRED: unsigned /v1/* is rejected with 401' : ' - optional: unsigned requests are still accepted'}`);
520
+ } else if (this.requireSignature) {
521
+ m.push('[w2m-rabbit] !! CONFIGURATION ERROR: --require-signature is set but no --signing-secret was provided. '
522
+ + 'Every /v1/* request will fail with 500 SIGNING_NOT_CONFIGURED (this is the RELAY\'s fault, not the client\'s). '
523
+ + 'Either set --signing-secret or drop --require-signature.');
524
+ } else {
525
+ m.push('[w2m-rabbit] request signing=off (no signing secret configured; behaviour is identical to v0.2.3)');
526
+ }
527
+ if (this.persistence.enabled) {
528
+ m.push(`[w2m-rabbit] persistence=on dir=${this.persistence.dir} `
529
+ + `revived_devices=${this.persistence.revivedDevices} revived_tasks=${this.persistence.revivedTasks}`);
530
+ } else {
531
+ m.push('[w2m-rabbit] persistence=off (in-memory only; devices and tasks are lost on restart)');
532
+ }
533
+ if (this.persistence.devicesCorrupt) {
534
+ m.push('[w2m-rabbit] !! WARNING: devices.json was CORRUPT — started from an EMPTY device table; '
535
+ + 'every machine must pair again. The damaged file was left untouched.');
536
+ }
537
+ for (const w of this.persistence.warnings) {
538
+ m.push(`[w2m-rabbit] !! ${w.file}: ${w.message}`);
539
+ }
540
+ if (this.operatorTokenRequired) {
541
+ // MEDIUM-6: the token is material, not posture, so it is printed as rarely as it can be — on the
542
+ // start that generated it (`operatorTokenSource === 'generated'`) or when `printOperatorToken` /
543
+ // `W2M_PRINT_OPERATOR_TOKEN=1` asked for it — and once, never twice. Where it is stored is said in
544
+ // every case, including the case where there is nowhere to store it.
545
+ //
546
+ // DELIBERATE, do not "fix": on that one start the CLI prints the value a second time, in its own
547
+ // stdout banner. Two sinks for one start is not a leak — the defect was printing it on *every*
548
+ // start, restored ones included — and the box is the documented first-start handoff a human reads
549
+ // (docs/DEPLOY.md §2.4, test/crossnetwork.test.mjs). Collapsing the pair would trade a documented
550
+ // behaviour for one saved line on one start.
551
+ const tokenFile = this.persistence.operatorTokenFile ?? null;
552
+ const stored = tokenFile === null
553
+ ? 'persistence is off, so this token exists only in this process and cannot be read back later'
554
+ : `stored at ${tokenFile} (0600)`;
555
+ if (this.operatorTokenPrintable) {
556
+ m.push(`[w2m-rabbit] operator token (${this.operatorTokenSource}) - required for POST /v1/task:`);
557
+ m.push(`[w2m-rabbit] ${this.operatorToken}`);
558
+ m.push(`[w2m-rabbit] printed once because ${this.operatorTokenPrintReason === 'generated'
559
+ ? 'this start generated it' : `${this.operatorTokenPrintReason} asked for it (${PRINT_OPERATOR_TOKEN_ENV})`}`
560
+ + ` - and ${stored}`);
561
+ } else {
562
+ m.push(`[w2m-rabbit] operator token (${this.operatorTokenSource}) - required for POST /v1/task, NOT printed`);
563
+ m.push(`[w2m-rabbit] read it from ${tokenFile ?? '<state>/operator-token.txt'} - ${stored}`);
564
+ m.push(`[w2m-rabbit] set ${PRINT_OPERATOR_TOKEN_ENV}=1 to print it on this start as well`);
565
+ }
566
+ } else {
567
+ m.push('[w2m-rabbit] !! WARNING: OPERATOR TOKEN DISABLED (--operator-token "") - '
568
+ + 'any device that can reach this relay can dispatch tasks to the whole group. '
569
+ + 'Only acceptable on a trusted LAN / for debugging; /v1/task then requires a device token.');
570
+ }
571
+ }
572
+
573
+ get url() {
574
+ if (this.host === null || this.port === null) return null;
575
+ const host = this.host.includes(':') ? `[${this.host}]` : this.host;
576
+ const prefix = this.basePath === '/' ? '' : this.basePath;
577
+ return `${this.scheme}://${host}:${this.port}${prefix}`;
578
+ }
579
+
580
+ listen({ host = '127.0.0.1', port = 0 } = {}) {
581
+ return new Promise((resolve, reject) => {
582
+ const onError = (err) => reject(err);
583
+ this.server.once('error', onError);
584
+ this.server.listen(port, host, () => {
585
+ this.server.removeListener('error', onError);
586
+ const addr = this.server.address();
587
+ this.host = addr.address;
588
+ this.port = addr.port;
589
+ this._sweepTimer = setInterval(() => {
590
+ this.maintenance();
591
+ }, this.sweepIntervalMs);
592
+ this._sweepTimer.unref?.();
593
+ if (this.logger) {
594
+ this._log(`[w2m-rabbit] listening on ${this.url} pairing code: ${this.pairingCode}`);
595
+ for (const message of this.startupMessages) this._log(message);
596
+ }
597
+ resolve({
598
+ host: this.host,
599
+ port: this.port,
600
+ url: this.url,
601
+ pairingCode: this.pairingCode,
602
+ relayId: this.relayId,
603
+ basePath: this.basePath,
604
+ operatorToken: this.operatorToken,
605
+ operatorTokenRequired: this.operatorTokenRequired,
606
+ // The CLI prints its own banner, so it has to reach the same conclusion this class did rather
607
+ // than re-deriving one from the token's presence (MEDIUM-6).
608
+ operatorTokenSource: this.operatorTokenSource,
609
+ operatorTokenPrintable: this.operatorTokenPrintable === true,
610
+ operatorTokenPrintReason: this.operatorTokenPrintReason ?? null,
611
+ scheme: this.scheme,
612
+ });
613
+ });
614
+ });
615
+ }
616
+
617
+ sweep() {
618
+ return this.state.sweepExpired();
619
+ }
620
+
621
+ close() {
622
+ return new Promise((resolve) => {
623
+ if (this._sweepTimer) clearInterval(this._sweepTimer);
624
+ this._sweepTimer = null;
625
+ // v0.4.0: a deferred P2P offer is a pending timer, and a closed relay must not fire one.
626
+ this.state.clearPendingOffers?.();
627
+ for (const sub of [...this.subscribers]) this._closeSubscriber(sub);
628
+ this.server.close(() => resolve());
629
+ this.server.closeAllConnections?.();
630
+ });
631
+ }
632
+
633
+ /* ---------------- proxy awareness (v0.1.2 §3) ---------------- */
634
+
635
+ /** `X-Forwarded-For` first hop is honoured ONLY under `trustProxy`. */
636
+ clientIp(req) {
637
+ if (this.trustProxy) {
638
+ const forwarded = firstHeaderValue(req.headers['x-forwarded-for']);
639
+ if (forwarded) return forwarded;
640
+ }
641
+ return req.socket?.remoteAddress ?? 'unknown';
642
+ }
643
+
644
+ /** `X-Forwarded-Proto` is honoured ONLY under `trustProxy`. */
645
+ effectiveScheme(req) {
646
+ if (this.trustProxy) {
647
+ const proto = firstHeaderValue(req.headers['x-forwarded-proto']);
648
+ if (proto) return proto.toLowerCase();
649
+ }
650
+ return this.scheme;
651
+ }
652
+
653
+ /* ---------------- request dispatch ---------------- */
654
+
655
+ async handle(req, res) {
656
+ let url;
657
+ try {
658
+ url = new URL(req.url ?? '/', `http://${req.headers.host ?? 'localhost'}`);
659
+ } catch {
660
+ throw new ProtocolError('BAD_REQUEST', 'malformed request URL');
661
+ }
662
+ const rawPath = url.pathname;
663
+ const method = (req.method ?? 'GET').toUpperCase();
664
+
665
+ // §3: strip the deployment prefix, then route. `/healthz` also answers at the
666
+ // root because health checks are usually pointed straight at the container.
667
+ // `stripped` keeps the literal request path (no trailing-slash normalisation)
668
+ // because that is what a signature covers; `path` is the normalised route key.
669
+ let stripped;
670
+ if (this.basePath === '/') {
671
+ stripped = rawPath;
672
+ } else if (rawPath === this.basePath) {
673
+ stripped = '/';
674
+ } else if (rawPath.startsWith(`${this.basePath}/`)) {
675
+ stripped = rawPath.slice(this.basePath.length);
676
+ } else if (rawPath === '/healthz') {
677
+ stripped = '/healthz';
678
+ } else {
679
+ // Outside the prefix: an explicit 404, never a gateway-style 502.
680
+ throw new ProtocolError('NOT_FOUND', `path is outside the configured base path ${this.basePath}`, {
681
+ path: rawPath, base_path: this.basePath,
682
+ });
683
+ }
684
+ const path = normalizePath(stripped);
685
+ const signedPath = `${stripped}${url.search}`;
686
+
687
+ // v0.3.0 §9: read the body ONCE, before routing. An HMAC covers the exact
688
+ // received bytes, so no route may consume the stream first and re-serialise.
689
+ const rawBody = methodHasBody(method) ? await readRawBody(req, this.frameLimitBytes) : null;
690
+ const takeJson = () => parseJsonBody(rawBody);
691
+
692
+ /* ---- public endpoints ---- */
693
+ // `/healthz` and `/v1/pair` are permanently exempt from signature checks:
694
+ // the first is an operations probe that must not need credentials, the second
695
+ // is the bootstrap step that happens before the two sides share a secret.
696
+ if (method === 'GET' && path === '/healthz') {
697
+ return sendJson(res, 200, this.healthBody(req));
698
+ }
699
+ if (method === 'POST' && path === '/v1/pair') {
700
+ this.enforcePairRateLimit(req);
701
+ const result = this.state.pair(takeJson());
702
+ return sendJson(res, 200, result);
703
+ }
704
+ /**
705
+ * v1.2.4: cancellation over HTTP. state.cancelTask has existed since the task ledger gained a cancel flag, but no route
706
+ * reached it -- the capability was real on paper and unreachable on the wire. Same authorization as dispatch: cancelling
707
+ * a fleet task is an operator act, not something a device token confers.
708
+ */
709
+ const cancelMatch = path.match(/^\/v1\/tasks\/([^/]+)\/cancel$/);
710
+ if (method === 'POST' && cancelMatch !== null) {
711
+ this.authorizeTaskDispatch(req);
712
+ this.enforceSignature(req, { method, signedPath, body: rawBody });
713
+ const body = takeJson();
714
+ const cancelled = this.state.cancelTask(decodeURIComponent(cancelMatch[1]), typeof body.reason === 'string' && body.reason !== '' ? body.reason : undefined);
715
+ return sendJson(res, 200, {
716
+ protocol_version: PROTOCOL_VERSION,
717
+ rabbit_time: this.state.nowIso(),
718
+ ok: true,
719
+ task: { task_id: cancelled.task_id, cancelled: cancelled.cancelled === true, cancel_reason: cancelled.cancel_reason ?? null },
720
+ });
721
+ }
722
+ /**
723
+ * v1.2.5: a role is an operator decision, so this route sits with dispatch — before the device gate, where an operator
724
+ * token is accepted.
725
+ */
726
+ const memberMatch = path.match(/^\/v1\/fleet\/members\/([^/]+)$/);
727
+ if (method === 'PUT' && memberMatch !== null) {
728
+ this.authorizeTaskDispatch(req);
729
+ this.enforceSignature(req, { method, signedPath, body: rawBody });
730
+ const body = takeJson();
731
+ const changed = this.state.setMemberRole(decodeURIComponent(memberMatch[1]), body.role);
732
+ if (changed.ok !== true) {
733
+ return sendJson(res, 400, { protocol_version: PROTOCOL_VERSION, ok: false, error: { code: changed.code }, detail: changed.detail ?? null });
734
+ }
735
+ return sendJson(res, 200, { protocol_version: PROTOCOL_VERSION, rabbit_time: this.state.nowIso(), ok: true, member: changed.member });
736
+ }
737
+ /**
738
+ * v0.3.3 `/metrics`: machine-readable fleet state for a monitoring system.
739
+ *
740
+ * Token-free, like `/healthz` and for the same reason: a scraper that needs the operator token
741
+ * would put that token in a Prometheus config file, which is a credential in a place nobody
742
+ * reviews. It is also exempt from signature checks for the same reason -- there is no secret to
743
+ * sign with on the scraper side.
744
+ *
745
+ * Exposed only when the relay was asked for it (`--metrics`). A monitoring endpoint that appears
746
+ * by default is a surface nobody chose, and the whole point of the defaults elsewhere is that the
747
+ * relay binds nothing the operator did not ask for.
748
+ */
749
+ if (method === 'GET' && path === '/metrics') {
750
+ if (!this.metricsEnabled) {
751
+ // 404 rather than 403: a disabled endpoint should look absent, not forbidden. A 403 tells a
752
+ // scanner the surface exists.
753
+ throw new ProtocolError('NOT_FOUND', 'metrics are not enabled on this relay', { hint: 'start the relay with --metrics' });
754
+ }
755
+ const body = renderMetrics(this.state, { nowMs: this.state.nowMs() });
756
+ res.writeHead(200, {
757
+ 'content-type': METRICS_CONTENT_TYPE,
758
+ 'content-length': Buffer.byteLength(body, 'utf8'),
759
+ 'cache-control': 'no-store',
760
+ });
761
+ return res.end(body);
762
+ }
763
+ // §5: dispatching tasks needs the operator token, not a device token.
764
+ if (method === 'POST' && path === '/v1/task') {
765
+ const principal = this.authorizeTaskDispatch(req);
766
+ this.enforceSignature(req, { method, signedPath, body: rawBody });
767
+ const body = takeJson();
768
+ if (principal.machine_id && !body.created_by) body.created_by = principal.machine_id;
769
+ const result = this.state.createTask(body);
770
+ return sendJson(res, 200, result);
771
+ }
772
+
773
+ /* ---- v1.1.6: reachable WITHOUT a device token ---- */
774
+ this.joinChallenges ??= createJoinChallenges({
775
+ now: () => this.state.nowMs(),
776
+ publicKeyFor: (fingerprint) => this.state.accountKeyFor(fingerprint),
777
+ });
778
+ const admissionPollMatch = /^\/v1\/fleet\/admission\/([^/]+)$/.exec(path);
779
+ if (method === 'GET' && admissionPollMatch && url.searchParams.get('poll_token') !== null) {
780
+ this.admission ??= createAdmission({ now: () => this.state.nowMs() });
781
+ const polled = this.admission.poll(decodeURIComponent(admissionPollMatch[1]), url.searchParams.get('poll_token'));
782
+ if (polled.ok !== true) throw new ProtocolError('NOT_FOUND', 'unknown admission request');
783
+ return sendJson(res, 200, { protocol_version: PROTOCOL_VERSION, rabbit_time: this.state.nowIso(), ...polled });
784
+ }
785
+ if (method === 'GET' && path === '/v1/fleet/admission/challenge') {
786
+ const issued = this.joinChallenges.issue(url.searchParams.get('fingerprint') ?? '', url.searchParams.get('machine_id') ?? '');
787
+ if (issued.ok !== true) throw new ProtocolError('BAD_REQUEST', 'challenge refused: ' + issued.code, { code: issued.code });
788
+ return sendJson(res, 200, { protocol_version: PROTOCOL_VERSION, rabbit_time: this.state.nowIso(), ...issued });
789
+ }
790
+ if (method === 'POST' && path === '/v1/fleet/admission/request') {
791
+ const body = takeJson();
792
+ this.admission ??= createAdmission({ now: () => this.state.nowMs() });
793
+ const proof = this.joinChallenges.consume({ challenge_id: body.challenge_id, machine_id: body.machine_id, signature: body.signature });
794
+ if (proof.ok !== true) throw new ProtocolError('UNAUTHORIZED', 'join proof refused: ' + proof.code, { code: proof.code });
795
+ const result = this.admission.requestAdmission({ machine_id: body.machine_id, machine_name: body.machine_name ?? null, account_fingerprint: proof.fingerprint });
796
+ return sendJson(res, 200, { protocol_version: PROTOCOL_VERSION, rabbit_time: this.state.nowIso(), ...result });
797
+ }
798
+
799
+ /* ---- everything below requires `Authorization: Bearer <device_token>` ---- */
800
+ const token = bearerToken(req);
801
+ const device = token ? this.state.authenticate(token) : null;
802
+ if (!device) {
803
+ throw new ProtocolError('UNAUTHORIZED', 'missing or invalid device_token', {});
804
+ }
805
+ this.state.touchDevice(device.machine_id);
806
+
807
+ // v0.3.0 §9: writes are always checked (tolerating unsigned unless required);
808
+ // reads are only checked when the relay was explicitly told to require
809
+ // signatures. See `enforceSignature` for the reasoning.
810
+ this.enforceSignature(req, { method, signedPath, body: rawBody });
811
+
812
+ /* ---- v1.2.5: members and read watermarks ---- */
813
+ if (method === 'GET' && path === '/v1/fleet/members') {
814
+ return sendJson(res, 200, {
815
+ protocol_version: PROTOCOL_VERSION,
816
+ rabbit_time: this.state.nowIso(),
817
+ ok: true,
818
+ members: this.state.listMembers(),
819
+ });
820
+ }
821
+ const readMatch = path.match(/^\/v1\/fleet\/read\/([^/]+)$/);
822
+ if (readMatch !== null) {
823
+ const sessionId = decodeURIComponent(readMatch[1]);
824
+ if (method === 'PUT') {
825
+ const body = takeJson();
826
+ const marked = this.state.markRead(sessionId, device.machine_id, {
827
+ at_ms: Number.isFinite(body.at_ms) ? body.at_ms : this.state.nowMs(),
828
+ message_id: typeof body.message_id === 'string' ? body.message_id : null,
829
+ });
830
+ if (marked.ok !== true) return sendJson(res, 400, { protocol_version: PROTOCOL_VERSION, ok: false, error: { code: marked.code } });
831
+ return sendJson(res, 200, { protocol_version: PROTOCOL_VERSION, rabbit_time: this.state.nowIso(), ok: true, ...marked });
832
+ }
833
+ if (method === 'GET') {
834
+ this.fleetMessages ??= createFleetMessages({ now: () => this.state.nowMs() });
835
+ const mark = this.state.readMark(sessionId, device.machine_id);
836
+ const listed = this.fleetMessages.list({ session_id: sessionId, limit: 200, for_machine_id: device.machine_id });
837
+ const messages = listed.ok === true ? listed.messages : [];
838
+ const floor = mark === null ? 0 : mark.at_ms;
839
+ const unread = messages.filter((message) => Number.isFinite(message?.at_ms) && message.at_ms > floor && message.machine_id !== device.machine_id).length;
840
+ return sendJson(res, 200, { protocol_version: PROTOCOL_VERSION, rabbit_time: this.state.nowIso(), ok: true, session_id: sessionId, mark, unread });
841
+ }
842
+ return sendJson(res, 405, { protocol_version: PROTOCOL_VERSION, ok: false, error: { code: 'METHOD_NOT_ALLOWED' } });
843
+ }
844
+ if (method === 'GET' && path === '/v1/stream') {
845
+ return this.handleStream(req, res, url, device);
846
+ }
847
+ if (method === 'POST' && path === '/v1/heartbeat') {
848
+ const body = takeJson();
849
+ body.machine_id = body.machine_id ?? device.machine_id;
850
+ const result = this.state.heartbeat(body);
851
+ return sendJson(res, 200, result);
852
+ }
853
+ if (method === 'POST' && path === '/v1/result') {
854
+ const body = takeJson();
855
+ const result = this.state.submitResult(body);
856
+ const { _instanceKey, ...clean } = result;
857
+ return sendJson(res, 200, clean);
858
+ }
859
+ /**
860
+ * v1.0.7: the content-addressed blob channel (#1).
861
+ *
862
+ * ## Why the relay may carry bytes at all
863
+ *
864
+ * It persists `stdout_sha256` and never the text, and `src/agent/storage.mjs` records why: each machine's
865
+ * `memory.jsonl` is the only durable copy of what a command printed. **That invariant is about the relay not
866
+ * interpreting content** -- it never decides what bytes mean and never asserts they are authentic.
867
+ *
868
+ * A blob keyed by its own hash preserves exactly that. The relay still interprets nothing; **the hash is what
869
+ * says the bytes are the bytes**, exactly as `stdout_sha256` does for a result. What is new is only that a hash
870
+ * the relay already holds can have its bytes fetched from the same place.
871
+ *
872
+ * ## Why the hash is verified before storing
873
+ *
874
+ * A content-addressed store that accepts bytes under a key they do not hash to is not content-addressed -- the
875
+ * key stops being a claim about the content, and every later reader's verification becomes decorative. So a
876
+ * mismatch is **refused (422)**, never stored and never corrected: correcting it would silently re-key a blob
877
+ * the caller believes it stored under a different name.
878
+ *
879
+ * ## Fleet scoping
880
+ *
881
+ * `putFleetBlob`/`getFleetBlob` reuse `scopeFleetId`, the same rule task dispatch obeys. A blob written by one
882
+ * fleet is **not readable by another**, and the read refusal is a 404 rather than a 403: a distinguishable
883
+ * "not yours" tells a prober that the hash exists here.
884
+ */
885
+ /**
886
+ * v1.0.9: **which blob this fleet published last** -- the discovery half of the channel.
887
+ *
888
+ * Checked **before** the `<sha256>` matcher deliberately. The matcher only accepts 64 hex characters, so
889
+ * `latest` cannot collide with it today; **the ordering is asserted anyway**, because "these two patterns cannot
890
+ * conflict" is exactly the reasoning that produces a shadowed route the day one of them is relaxed.
891
+ *
892
+ * A fleet that has never published gets a 404 rather than an empty object: "there is no current document" and
893
+ * "the current document is nothing" are different statements, and only one of them is true here.
894
+ */
895
+ if (method === 'GET' && path === '/v1/fleet/blobs/latest') {
896
+ const latest = this.state.latestFleetBlob(device);
897
+ if (latest === null) {
898
+ throw new ProtocolError('NOT_FOUND', 'this fleet has published no blob', {});
899
+ }
900
+ return sendJson(res, 200, {
901
+ protocol_version: PROTOCOL_VERSION,
902
+ hash: latest.hash,
903
+ bytes: latest.bytes,
904
+ published_at: new Date(latest.published_at_ms).toISOString(),
905
+ published_by: latest.published_by ?? null,
906
+ // Stated in the payload, not only in the docs: a pointer can outlive its blob under oldest-first eviction.
907
+ hint: 'a hint, not a guarantee -- a pull may answer 404 if the blob has since been evicted',
908
+ });
909
+ }
910
+
911
+ const blobMatch = /^\/v1\/fleet\/blobs\/([0-9a-f]{64})$/.exec(path);
912
+ if (blobMatch !== null) {
913
+ const hash = blobMatch[1];
914
+ if (method === 'PUT') {
915
+ const received = rawBody ?? Buffer.alloc(0);
916
+ const actual = createHash('sha256').update(received).digest('hex');
917
+ if (actual !== hash) {
918
+ throw new ProtocolError('BLOB_HASH_MISMATCH', 'the body does not hash to the key it was sent under', {
919
+ expected: hash,
920
+ actual,
921
+ bytes: received.length,
922
+ });
923
+ }
924
+ const stored = this.state.putFleetBlob(device, received);
925
+ if (stored.ok !== true) {
926
+ throw new ProtocolError(stored.reason ?? 'BLOB_WRITE_FAILED', stored.detail ?? 'the blob was refused', {
927
+ hash,
928
+ });
929
+ }
930
+ return sendJson(res, 201, {
931
+ protocol_version: PROTOCOL_VERSION,
932
+ ok: true,
933
+ hash: stored.hash,
934
+ bytes: stored.bytes,
935
+ evicted: stored.evicted,
936
+ });
937
+ }
938
+ if (method === 'GET') {
939
+ const bytes = this.state.getFleetBlob(device, hash);
940
+ if (bytes === null) {
941
+ // Same code and shape for "no such blob" and "not your fleet's" -- deliberately indistinguishable.
942
+ throw new ProtocolError('NOT_FOUND', 'no such blob is readable by this device', { hash });
943
+ }
944
+ res.writeHead(200, {
945
+ 'content-type': 'application/octet-stream',
946
+ 'content-length': bytes.length,
947
+ // Same reason as `/metrics`: a memory document must not be cached by an intermediary.
948
+ 'cache-control': 'no-store',
949
+ });
950
+ res.end(bytes);
951
+ return true;
952
+ }
953
+ throw new ProtocolError('BAD_REQUEST', 'a blob is addressed with PUT or GET', { method });
954
+ }
955
+
956
+ if (method === 'GET' && path === '/v1/devices') {
957
+ return sendJson(res, 200, {
958
+ protocol_version: PROTOCOL_VERSION,
959
+ rabbit_time: this.state.nowIso(),
960
+ devices: this.state.listDevices(),
961
+ });
962
+ }
963
+ if (method === 'GET' && path === '/v1/tasks') {
964
+ const rawLimit = url.searchParams.get('limit');
965
+ let limit = 50;
966
+ if (rawLimit !== null) {
967
+ limit = Number(rawLimit);
968
+ if (!Number.isInteger(limit) || limit < 1) {
969
+ throw new ProtocolError('BAD_REQUEST', 'limit must be a positive integer', { limit: rawLimit });
970
+ }
971
+ }
972
+ return sendJson(res, 200, {
973
+ protocol_version: PROTOCOL_VERSION,
974
+ rabbit_time: this.state.nowIso(),
975
+ tasks: this.state.listTasks({ limit }),
976
+ });
977
+ }
978
+
979
+ const taskMatch = /^\/v1\/tasks\/([^/]+)$/.exec(path);
980
+ const reportMatch = /^\/v1\/tasks\/([^/]+)\/report$/.exec(path);
981
+ // v0.3.0: cross-machine RTT status. The plugin may run on a different machine
982
+ // than the agent, so this has to come from the relay, not from a local file.
983
+ const agentStatusMatch = /^\/v1\/agents\/([^/]+)\/status$/.exec(path);
984
+ // v0.3.9: P2P candidate rendezvous. `/announce` is checked before the read route
985
+ // so that a machine literally named "announce" cannot shadow it.
986
+ const peerReadMatch = /^\/v1\/peer\/([^/]+)$/.exec(path);
987
+ /* ---- v1.1.6 admission ---- */
988
+ const admissionDecideMatch = /^\/v1\/fleet\/admission\/([^/]+)\/decide$/.exec(path);
989
+ const memberRemoveMatch = /^\/v1\/fleet\/members\/([^/]+)\/remove$/.exec(path);
990
+ const removalVoteMatch = /^\/v1\/fleet\/removals\/([^/]+)\/vote$/.exec(path);
991
+
992
+ /* ---- v0.3.9 P2P signalling ---- */
993
+ if (method === 'POST' && path === '/v1/peer/announce') {
994
+ const body = takeJson();
995
+ try {
996
+ // `device.machine_id`, never `body.machine_id`: a paired machine may only
997
+ // announce addresses for itself, or it could aim a peer's punch at a third party.
998
+ const result = this.peers.announce(device.machine_id, body);
999
+ return sendJson(res, 200, {
1000
+ protocol_version: PROTOCOL_VERSION,
1001
+ rabbit_time: this.state.nowIso(),
1002
+ ...result,
1003
+ });
1004
+ } catch (error) {
1005
+ throw new ProtocolError('BAD_REQUEST', error.message, { code: error.code ?? 'BAD_ANNOUNCEMENT' });
1006
+ }
1007
+ }
1008
+
1009
+ if (method === 'GET' && peerReadMatch) {
1010
+ const machineId = decodeURIComponent(peerReadMatch[1]);
1011
+ const entry = this.peers.get(machineId);
1012
+ if (!entry) {
1013
+ // A named 404 rather than an empty list: "this machine has not announced" and
1014
+ // "this machine announced nothing reachable" call for different responses, and
1015
+ // an empty candidate list would send the caller into a punch that cannot work.
1016
+ throw new ProtocolError('NOT_FOUND', `no live P2P announcement for ${machineId}`, {
1017
+ machine_id: machineId,
1018
+ hint: 'the peer must POST /v1/peer/announce first, and announcements expire',
1019
+ });
1020
+ }
1021
+ return sendJson(res, 200, {
1022
+ protocol_version: PROTOCOL_VERSION,
1023
+ rabbit_time: this.state.nowIso(),
1024
+ peer: entry,
1025
+ });
1026
+ }
1027
+
1028
+ if (method === 'GET' && agentStatusMatch) {
1029
+ const machineId = decodeURIComponent(agentStatusMatch[1]);
1030
+ const status = this.state.deviceStatus(machineId);
1031
+ if (!status) {
1032
+ throw new ProtocolError('NOT_FOUND', 'unknown machine_id', { machine_id: machineId });
1033
+ }
1034
+ return sendJson(res, 200, {
1035
+ protocol_version: PROTOCOL_VERSION,
1036
+ rabbit_time: this.state.nowIso(),
1037
+ ...status,
1038
+ });
1039
+ }
1040
+
1041
+ if (method === 'GET' && reportMatch) {
1042
+ const taskId = decodeURIComponent(reportMatch[1]);
1043
+ this.state.sweepExpired();
1044
+ const format = url.searchParams.get('format') ?? 'json';
1045
+ const report = buildReport(this.state, taskId, { format });
1046
+ if (res.writableEnded) return undefined;
1047
+ res.writeHead(200, {
1048
+ 'content-type': report.contentType,
1049
+ 'content-length': Buffer.byteLength(report.body, 'utf8'),
1050
+ 'cache-control': 'no-store',
1051
+ });
1052
+ return res.end(report.body);
1053
+ }
1054
+
1055
+ if (method === 'GET' && taskMatch) {
1056
+ const taskId = decodeURIComponent(taskMatch[1]);
1057
+ this.state.sweepExpired();
1058
+ const task = this.state.requireTask(taskId);
1059
+ const aggregate = aggregateTask(this.state, taskId);
1060
+ return sendJson(res, 200, {
1061
+ protocol_version: PROTOCOL_VERSION,
1062
+ rabbit_time: this.state.nowIso(),
1063
+ task: this.state.taskSummary(task),
1064
+ leases: [...task.leases.values()],
1065
+ aggregate,
1066
+ });
1067
+ }
1068
+
1069
+
1070
+ /* ---- v1.1.6 admission: the caller is always `device.machine_id`, never a body field ---- */
1071
+ this.admission ??= createAdmission({ now: () => this.state.nowMs() });
1072
+
1073
+ if (method === 'GET' && path === '/v1/fleet/admission/pending') {
1074
+ return sendJson(res, 200, {
1075
+ protocol_version: PROTOCOL_VERSION, rabbit_time: this.state.nowIso(),
1076
+ pending: this.admission.pendingFor(),
1077
+ });
1078
+ }
1079
+
1080
+ if (method === 'POST' && admissionDecideMatch) {
1081
+ const body = takeJson();
1082
+ const result = this.admission.decide(decodeURIComponent(admissionDecideMatch[1]), {
1083
+ decision: body.decision, decided_by: device.machine_id, auto: body.auto === true,
1084
+ });
1085
+ if (result.ok !== true) throw new ProtocolError('BAD_REQUEST', 'admission decision refused: ' + result.code, result);
1086
+ return sendJson(res, 200, { protocol_version: PROTOCOL_VERSION, rabbit_time: this.state.nowIso(), ...result });
1087
+ }
1088
+
1089
+ if (method === 'POST' && memberRemoveMatch) {
1090
+ const body = takeJson();
1091
+ const result = this.admission.remove(decodeURIComponent(memberRemoveMatch[1]), {
1092
+ requested_by: device.machine_id, reason: typeof body.reason === 'string' ? body.reason : null,
1093
+ });
1094
+ if (result.ok !== true) throw new ProtocolError('BAD_REQUEST', 'removal refused: ' + result.code, result);
1095
+ return sendJson(res, 200, { protocol_version: PROTOCOL_VERSION, rabbit_time: this.state.nowIso(), ...result });
1096
+ }
1097
+
1098
+ if (method === 'POST' && removalVoteMatch) {
1099
+ const body = takeJson();
1100
+ const result = this.admission.vote(decodeURIComponent(removalVoteMatch[1]), {
1101
+ machine_id: device.machine_id, vote: body.vote,
1102
+ });
1103
+ if (result.ok !== true) throw new ProtocolError('BAD_REQUEST', 'vote refused: ' + result.code, result);
1104
+ return sendJson(res, 200, { protocol_version: PROTOCOL_VERSION, rabbit_time: this.state.nowIso(), ...result });
1105
+ }
1106
+
1042
1107
  /* ---- v1.1.17: fleet chat ---- */
1043
1108
  //
1044
1109
  // Both endpoints sit AFTER the device gate on purpose: a message must be attributable to a paired machine, and
@@ -1058,11 +1123,13 @@ export class RelayServer {
1058
1123
  thread_id: body.thread_id,
1059
1124
  reply_to: body.reply_to,
1060
1125
  text: body.text,
1126
+ mentions: body.mentions,
1127
+ idempotency_key: body.idempotency_key,
1061
1128
  });
1062
1129
  if (posted.ok !== true) {
1063
1130
  throw new ProtocolError('BAD_REQUEST', 'message refused: ' + posted.code, { code: posted.code, ...(posted.detail ?? {}) });
1064
1131
  }
1065
- return sendJson(res, 200, { protocol_version: PROTOCOL_VERSION, rabbit_time: this.state.nowIso(), message: posted.message });
1132
+ return sendJson(res, 200, { protocol_version: PROTOCOL_VERSION, rabbit_time: this.state.nowIso(), message: posted.message, ...(posted.deduped === true ? { deduped: true } : {}) });
1066
1133
  }
1067
1134
 
1068
1135
  if (method === 'GET' && path === '/v1/fleet/messages/stream') {
@@ -1088,7 +1155,17 @@ export class RelayServer {
1088
1155
  if (typeof keepalive.unref === 'function') keepalive.unref();
1089
1156
  const unsubscribe = this.fleetMessages.subscribe(sessionId, (event) => {
1090
1157
  try {
1091
- res.write('event: new_message\ndata: ' + JSON.stringify(event) + '\n\n');
1158
+ // v1.2.5: a listener learns whether IT was named, and nothing else about the text.
1159
+ const mentioned = Array.isArray(event.mentions) && event.mentions.includes(device.machine_id);
1160
+ const payload = {
1161
+ session_id: event.session_id,
1162
+ message_id: event.message_id,
1163
+ machine_id: event.machine_id,
1164
+ at_ms: event.at_ms,
1165
+ to: event.to ?? 'all',
1166
+ ...(mentioned ? { mention: true } : {}),
1167
+ };
1168
+ res.write('event: new_message\ndata: ' + JSON.stringify(payload) + '\n\n');
1092
1169
  } catch { /* the reader went away; close() below cleans up */ }
1093
1170
  });
1094
1171
  const done = () => { clearInterval(keepalive); unsubscribe(); try { res.end(); } catch { /* already closed */ } };
@@ -1148,460 +1225,460 @@ export class RelayServer {
1148
1225
  throw new ProtocolError('BAD_REQUEST', 'memory accepts GET or PUT', { method, path });
1149
1226
  }
1150
1227
 
1151
- throw new ProtocolError('NOT_FOUND', `no route for ${method} ${path}`, { method, path });
1152
- }
1153
-
1154
- /** §4: the operations answer — "is the relay there, did it restart, are my machines connected". */
1155
- healthBody(req) {
1156
- return {
1157
- ...this.state.stats(),
1158
- // v0.3.9: how many machines currently have a live P2P announcement. Ephemeral,
1159
- // so this drops to 0 by itself when nobody is refreshing.
1160
- ...this.peers.stats(),
1161
- ok: true,
1162
- /**
1163
- * v0.6.2: hoisted out of `persistence` so it cannot be missed by a reader that only looks at
1164
- * the top level. `degraded: true` means this relay is answering requests while failing to record
1165
- * them -- the one relay failure that presents as health. `ok` deliberately stays true: the relay
1166
- * still serves the fleet, and flipping it would turn a recording problem into an outage.
1167
- */
1168
- degraded: this.persistence.enabled && this.persistence.degraded,
1169
- degraded_reason: this.persistence.degraded
1170
- ? `ledger appends are failing (${this.persistence.lastAppendError ?? 'unknown error'}); results are served but not durably recorded`
1171
- : null,
1172
- protocol_version: PROTOCOL_VERSION,
1173
- rabbit_time: this.state.nowIso(),
1174
- uptime_ms: this.state.nowMs() - this._startedAtMs,
1175
- relay_id: this.relayId,
1176
- started_at: rfc3339(this._startedAtMs),
1177
- effective_scheme: this.effectiveScheme(req),
1178
- base_path: this.basePath,
1179
- operator_token_required: this.operatorTokenRequired,
1180
- pair_rate_limit: this.pairRateLimitPerMinute,
1181
- // v0.3.0: fleet-wide latency. Every numeric field is null (never 0) when no
1182
- // machine has reported a FRESH measurement.
1183
- rtt: this.state.rttSummary(),
1184
- // v0.3.0 §9: signature status only — never the secret, not even a prefix.
1185
- signing: {
1186
- configured: this.signingConfigured,
1187
- required: this.requireSignature,
1188
- previous_secret_accepted: this.signingSecrets.length > 1,
1189
- skew_seconds: this.signatureSkewSeconds,
1190
- },
1191
- persistence: this.persistence.describe(),
1192
- };
1193
- }
1194
-
1195
- /* ---------------- request signing (v0.3.0 §9) ---------------- */
1196
-
1197
- /**
1198
- * Should this request be signature-checked at all?
1199
- *
1200
- * RULING (see PROTOCOL-v0.3.0.md §9.4): every WRITE is checked; READS are
1201
- * checked only when `--require-signature` is on.
1202
- *
1203
- * Reads are not checked in the default modes because unsigned reads are
1204
- * accepted anyway, so "verify when a signature happens to be present" would be
1205
- * theatre -- an attacker simply omits the headers -- while adding a brand new
1206
- * way for a poll or an SSE reconnect to fail. What a read leaks is topology,
1207
- * which the device token already gates; what a write can do is change state,
1208
- * which is what replay and tampering actually threaten.
1209
- */
1210
- shouldCheckSignature(method) {
1211
- if (this.requireSignature) return true; // explicit hard mode: nothing is exempt
1212
- const isRead = method === 'GET' || method === 'HEAD' || method === 'OPTIONS';
1213
- return !isRead;
1214
- }
1215
-
1216
- /** Run `verifyRequest` and throw the mapped §7 error on rejection. */
1217
- enforceSignature(req, { method, signedPath, body }) {
1218
- if (!this.shouldCheckSignature(method)) return { skipped: true };
1219
- const decision = this.verifySignature(req, { method, signedPath, body });
1220
- if (decision.ok !== true) throw signatureError(decision);
1221
- return decision;
1222
- }
1223
-
1224
- /**
1225
- * Thin adapter over the frozen `signing.mjs` contract.
1226
- *
1227
- * `path` is the ROUTED path with the query string, NOT the raw request URL: with
1228
- * `--base-path /w2m` the same client must produce the same signature whether a
1229
- * proxy passes `/w2m/v1/result` through or strips it to `/v1/result`. The routed
1230
- * path is the only value both sides agree on in all three deployment forms, so
1231
- * clients sign `/v1/result` and never `https://host/w2m/v1/result`.
1232
- */
1233
- /**
1234
- * Verify a request's signature (v0.7.11).
1235
- *
1236
- * Two mechanisms, and the order between them is the whole design:
1237
- *
1238
- * **1. Per-device Ed25519, when the sender is a device with a stored public key.** This is the
1239
- * one that proves *which* device signed. It is tried **first** because a device key is strictly
1240
- * stronger evidence than a shared secret, and because a device that registered a key must not
1241
- * fall back to the weaker mechanism by accident.
1242
- *
1243
- * **2. HMAC over a shared secret, otherwise.** Every device that paired before v0.7.10 arrives
1244
- * here — which is every device today — and nothing about its behaviour changes.
1245
- *
1246
- * ## Why this converges with no second deployment
1247
- *
1248
- * A device that has not been updated sends no signature header, so mechanism 1 finds no key and
1249
- * mechanism 2 answers exactly as before. A device that *has* been updated sends a signature, its
1250
- * public key is on file from pairing, and it is verified with it. So a fleet updates one machine
1251
- * at a time and each machine gets the strongest mechanism available **to it** the moment it
1252
- * starts sending one — no flag, no rollout step, no coordination.
1253
- *
1254
- * ## What this deliberately does not do
1255
- *
1256
- * It does not **require** a device key. `requireSignature` still means "a signature must be
1257
- * present", and either mechanism satisfies it. Making a *device key* mandatory is a separate,
1258
- * later switch, because turning it on today would refuse all four machines at once.
1259
- */
1260
- verifySignature(req, { method, signedPath, body }) {
1261
- const viaDevice = this.verifyWithDeviceKey(req, { method, signedPath, body });
1262
- if (viaDevice !== null) return viaDevice;
1263
-
1264
- return verifyRequest({
1265
- headers: req.headers, // plain Node object; signing.mjs reads it case-insensitively
1266
- method,
1267
- path: signedPath,
1268
- body: body ?? '',
1269
- secrets: this.signingSecrets,
1270
- nonces: this.nonceCache,
1271
- nowMs: this.state.nowMs(),
1272
- skewSeconds: this.signatureSkewSeconds,
1273
- required: this.requireSignature,
1274
- });
1275
- }
1276
-
1277
- /**
1278
- * Try the per-device Ed25519 path.
1279
- *
1280
- * Returns `null` when the mechanism does not apply — no signature presented, or the signing
1281
- * machine is not a device whose public key we hold — so the caller falls through. Returns a
1282
- * decision when it *does* apply, **including when it fails**: a device that registered a key and
1283
- * then presented a bad signature must not get a second chance through the shared secret, since
1284
- * that would make the stronger mechanism optional for anyone able to produce a malformed one.
1285
- */
1286
- verifyWithDeviceKey(req, { method, signedPath, body }) {
1287
- const header = readHeader(req.headers, DEVICE_SIGNATURE_HEADER);
1288
- if (typeof header !== 'string' || header === '') return null;
1289
-
1290
- const machineId = this.machineIdForRequest(req);
1291
- if (machineId === null) return null;
1292
- const publicKey = this.state.devices.get(machineId)?.public_key;
1293
- if (typeof publicKey !== 'string' || publicKey === '') return null;
1294
-
1295
- const decision = verifyDeviceSignature({
1296
- headers: req.headers,
1297
- publicKeyPem: publicKey,
1298
- method,
1299
- path: signedPath,
1300
- body: body ?? '',
1301
- nowMs: this.state.nowMs(),
1302
- skewSeconds: this.signatureSkewSeconds,
1303
- // `accept`, not `remember` -- the name this class actually exposes. It reports whether the
1304
- // nonce was new, which is exactly the contract `verifyDeviceSignature` expects.
1305
- nonces: (nonce, at) => this.nonceCache.accept(nonce, at),
1306
- });
1307
- if (decision.ok) return { ...decision, machine_id: machineId, keyIndex: null };
1308
- return decision;
1309
- }
1310
-
1311
- /**
1312
- * Which machine a request claims to be, from either credential.
1313
- *
1314
- * An **operator token is not a machine**, so it resolves to `null` and the device path does not
1315
- * apply — consistent with `authorizeTaskDispatch`, which reports `machine_id: null` for an
1316
- * operator. That is why an operator-token request keeps using the shared secret.
1317
- */
1318
- machineIdForRequest(req) {
1319
- const token = bearerToken(req);
1320
- if (!token) return null;
1321
- if (this.operatorToken !== null && token === this.operatorToken) return null;
1322
- return this.state.authenticate(token)?.machine_id ?? null;
1323
- }
1324
-
1325
- /** Periodic maintenance, called by the sweep timer. */
1326
- maintenance() {
1327
- const expired = this.sweep();
1328
- /**
1329
- * v0.9.1: presence expiry, alongside lease expiry.
1330
- *
1331
- * `this.sweep()` writes off lease *holds*; this writes off *presence*. They were conflated, so a
1332
- * machine that stopped existing was only ever noticed when its SSE stream detached — and a
1333
- * half-open stream never detaches.
1334
- */
1335
- const silent = this.state.sweepSilentDevices();
1336
- if (silent.length > 0) {
1337
- this._log(`[w2m-rabbit] marked ${silent.length} silent device(s) offline: `
1338
- + silent.map((s) => `${s.machine_name} (${Math.round(s.silent_ms / 1000)}s)`).join(', '));
1339
- }
1340
- this.pairLimiter.prune();
1341
- this.peers.sweep();
1342
- // Keep the nonce cache from growing without bound in a long-running process.
1343
- this.nonceCache.prune(this.nonceRetentionMs, this.state.nowMs());
1344
- return expired;
1345
- }
1346
-
1347
- /* ---------------- auth helpers ---------------- */
1348
-
1349
- /** §6: every /v1/pair attempt counts, success included. */
1350
- enforcePairRateLimit(req) {
1351
- if (this.pairRateLimitPerMinute <= 0) return;
1352
- const ip = this.clientIp(req);
1353
- const verdict = this.pairLimiter.hit(ip);
1354
- if (verdict.allowed) return;
1355
- const err = new ProtocolError(
1356
- 'RATE_LIMITED',
1357
- `too many /v1/pair attempts from ${ip}: limit is ${verdict.limit} per ${verdict.window_seconds}s`,
1358
- {
1359
- retry_after_seconds: verdict.retry_after_seconds,
1360
- limit: verdict.limit,
1361
- window_seconds: verdict.window_seconds,
1362
- scope: 'pair',
1363
- },
1364
- );
1365
- err.headers = { 'retry-after': String(verdict.retry_after_seconds) };
1366
- throw err;
1367
- }
1368
-
1369
- /**
1370
- * §5: `POST /v1/task` needs the operator token.
1371
- * - required (default): operator token, else 401 OPERATOR_REQUIRED
1372
- * - disabled (`--operator-token ''`): falls back to v1 semantics, i.e. a paired device token
1373
- */
1374
- authorizeTaskDispatch(req) {
1375
- const token = bearerToken(req);
1376
- if (this.operatorTokenRequired) {
1377
- if (!token) {
1378
- throw new ProtocolError(
1379
- 'OPERATOR_REQUIRED',
1380
- 'POST /v1/task requires the operator token. A device_token is machine credentials and cannot '
1381
- + `dispatch tasks; read the operator token from ${this.persistence.operatorTokenFile ?? '<state>/operator-token.txt'} `
1382
- + 'or pass --operator-token / W2M_OPERATOR_TOKEN.',
1383
- { operator_token_file: this.persistence.operatorTokenFile ?? null },
1384
- );
1385
- }
1386
- if (token !== this.operatorToken) {
1387
- const asDevice = this.state.authenticate(token);
1388
- throw new ProtocolError(
1389
- 'OPERATOR_REQUIRED',
1390
- asDevice
1391
- ? 'this is a device_token (machine credentials); dispatching tasks requires the operator_token — '
1392
- + 'they are two different credentials'
1393
- : 'invalid operator token: it does not match the relay operator token',
1394
- {
1395
- device_token_detected: Boolean(asDevice),
1396
- machine_id: asDevice?.machine_id ?? null,
1397
- operator_token_file: this.persistence.operatorTokenFile ?? null,
1398
- },
1399
- );
1400
- }
1401
- return { role: 'operator', machine_id: null };
1402
- }
1403
- const device = token ? this.state.authenticate(token) : null;
1404
- if (!device) {
1405
- throw new ProtocolError(
1406
- 'UNAUTHORIZED',
1407
- 'the operator token requirement is disabled on this relay, but /v1/task still requires a valid device_token',
1408
- {},
1409
- );
1410
- }
1411
- return { role: 'device', machine_id: device.machine_id };
1412
- }
1413
-
1414
- /* ---------------- SSE (§3) ---------------- */
1415
-
1416
- handleStream(req, res, url, device) {
1417
- const machineId = device.machine_id;
1418
- const requested = url.searchParams.get('machine_id');
1419
- if (requested !== null && requested !== machineId) {
1420
- throw new ProtocolError('BAD_REQUEST', 'machine_id does not match the device_token', {
1421
- machine_id: requested, token_machine_id: machineId,
1422
- });
1423
- }
1424
-
1425
- let from = null;
1426
- const seqParam = url.searchParams.get('seq');
1427
- const lastEventId = req.headers['last-event-id'];
1428
- if (seqParam !== null) {
1429
- from = Number(seqParam);
1430
- if (!Number.isInteger(from) || from < 1) {
1431
- throw new ProtocolError('BAD_REQUEST', 'seq must be a positive integer', { seq: seqParam });
1432
- }
1433
- } else if (typeof lastEventId === 'string' && lastEventId.trim() !== '') {
1434
- const last = Number(lastEventId);
1435
- if (!Number.isInteger(last) || last < 0) {
1436
- throw new ProtocolError('BAD_REQUEST', 'Last-Event-ID must be a non-negative integer', { last_event_id: lastEventId });
1437
- }
1438
- from = last + 1;
1439
- }
1440
-
1441
- // §8.5: these two headers are the fix for "connects but no events" behind
1442
- // nginx-style proxies and tunnels, which buffer event streams by default.
1443
- res.writeHead(200, {
1444
- 'content-type': 'text/event-stream; charset=utf-8',
1445
- 'cache-control': 'no-cache, no-transform',
1446
- connection: 'keep-alive',
1447
- 'x-accel-buffering': 'no',
1448
- });
1449
- if (typeof res.flushHeaders === 'function') res.flushHeaders();
1450
-
1451
- const sub = { machineId, res, write: null, timer: null, closed: false };
1452
-
1453
- const writeEvent = (event) => {
1454
- if (res.writableEnded) return;
1455
- res.write(`id: ${event.seq}\nevent: ${event.type}\ndata: ${JSON.stringify(event)}\n\n`);
1456
- };
1457
-
1458
- let live = false;
1459
- const queue = [];
1460
- const deliver = (entry) => {
1461
- if (entry.target && entry.target !== machineId) return;
1462
- writeEvent(entry.event);
1463
- };
1464
- const unsubscribe = this.state.onEvent((entry) => {
1465
- if (!live) { queue.push(entry); return; }
1466
- deliver(entry);
1467
- });
1468
-
1469
- // §3.1: the first frame MUST be `ready`; §8.3 adds `relay_id`.
1470
- const readySeq = this.state.reserveSeq();
1471
- writeEvent({
1472
- type: 'ready',
1473
- seq: readySeq,
1474
- protocol_version: PROTOCOL_VERSION,
1475
- rabbit_time: this.state.nowIso(),
1476
- machine_id: machineId,
1477
- relay_id: this.relayId,
1478
- });
1479
-
1480
- if (from !== null) {
1481
- const oldest = this.state.oldestBufferedSeq();
1482
- const lastBuffered = this.state.lastBufferedSeq();
1483
- // `fromFuture` is judged against EVERY seq this process has allocated (a client
1484
- // can legitimately hold a `ready` seq, which is not buffered), while the
1485
- // reported window bounds describe the replay ring the client can actually use.
1486
- const lastSeq = this.state.lastSeq();
1487
- // Two distinct ways for a cursor to be out of sync with THIS process:
1488
- //
1489
- // from < oldest the cursor is older than the replay window (buffer rot)
1490
- // from > lastSeq+1 the cursor cannot exist here at all. Sequence numbers
1491
- // restart at 1 in every process, so a cursor from a
1492
- // previous relay process is AHEAD of a freshly started
1493
- // relay. This was the silent case: `13 < 1` is false, the
1494
- // client got no notice, `replayEntries(13)` matched
1495
- // nothing, and the agent then reset its cursor to the
1496
- // `ready` seq -- stepping over every event it had missed.
1497
- const tooOld = from < oldest && oldest > 1;
1498
- const fromFuture = from > lastSeq + 1;
1499
- if (tooOld || fromFuture) {
1500
- // §8.4: tell the client where to re-align, not just that replay failed.
1501
- writeEvent({
1502
- type: 'notice',
1503
- seq: this.state.reserveSeq(),
1504
- rabbit_time: this.state.nowIso(),
1505
- level: 'warn',
1506
- code: 'REPLAY_TRUNCATED',
1507
- message: tooOld
1508
- ? `requested seq ${from} is older than the buffer head ${oldest}`
1509
- : `requested seq ${from} is beyond this process' last seq ${lastSeq}: the cursor came from a `
1510
- + 'previous relay process, whose sequence numbering restarted at 1',
1511
- machine_id: machineId,
1512
- requested_seq: from,
1513
- oldest_available_seq: oldest,
1514
- last_available_seq: lastBuffered,
1515
- relay_id: this.relayId,
1516
- reason: tooOld ? 'buffer_truncated' : 'cursor_ahead_of_relay',
1517
- });
1518
- }
1519
- // A cursor from a previous process is meaningless here, so honouring it
1520
- // literally (replay from `from`) would deliver NOTHING and also lose every
1521
- // event that fell into the gap -- not only offers, but `task.cancel`,
1522
- // `notice`, `peer.*`. Re-align to the head of the window instead: the client
1523
- // asked to be resynchronised, and the ring is exactly what we still have.
1524
- const replayFrom = fromFuture ? oldest : from;
1525
- for (const entry of this.state.replayEntries(replayFrom)) deliver(entry);
1526
- }
1527
-
1528
- live = true;
1529
- for (const entry of queue) deliver(entry);
1530
- queue.length = 0;
1531
-
1532
- // §8.6: re-state work this machine was handed while its stream was down.
1533
- //
1534
- // An offer is a single event; if the stream is not connected at that instant
1535
- // -- after a relay restart, a tunnel drop, or any interruption -- the event
1536
- // is lost and the lease waits out its whole window before being swept. The
1537
- // machine is meanwhile online and idle. Re-delivering on connect is what
1538
- // makes a reconnect recover work instead of silently losing it.
1539
- //
1540
- // Only unclaimed leases are re-offered; `running` is excluded so a task that
1541
- // has already begun cannot be handed out a second time and executed twice.
1542
- const redelivered = this.state.redeliverPendingOffers(machineId);
1543
- if (redelivered.count > 0) {
1544
- this._log(`[w2m-rabbit] re-offered ${redelivered.count} pending task(s) to ${machineId} on reconnect`);
1545
- }
1546
-
1547
- // §3.2: `: keepalive` comment every 15s.
1548
- sub.timer = setInterval(() => {
1549
- if (res.writableEnded) return;
1550
- res.write(': keepalive\n\n');
1551
- }, this.keepaliveMs);
1552
- sub.timer.unref?.();
1553
-
1554
- sub.write = writeEvent;
1555
- sub.unsubscribe = unsubscribe;
1556
- this.subscribers.add(sub);
1557
- device.streams = (device.streams ?? 0) + 1;
1558
- // v0.3.0: relay-observed reconnect counter. A first attach is not a reconnect,
1559
- // so the reported `reconnect_attempts` is (attaches - 1).
1560
- this.state.noteStreamConnect(machineId);
1561
-
1562
- const onClose = () => this._closeSubscriber(sub);
1563
- req.on('close', onClose);
1564
- res.on('close', onClose);
1565
- res.on('error', onClose);
1566
- return undefined;
1567
- }
1568
-
1569
- _closeSubscriber(sub) {
1570
- if (!sub || sub.closed) return;
1571
- sub.closed = true;
1572
- this.subscribers.delete(sub);
1573
- if (sub.timer) clearInterval(sub.timer);
1574
- sub.timer = null;
1575
- sub.unsubscribe?.();
1576
- const device = this.state.devices.get(sub.machineId);
1577
- if (device) {
1578
- device.streams = Math.max(0, (device.streams ?? 1) - 1);
1579
- if (device.streams === 0) this.state.setOnline(sub.machineId, false, 'stream_closed');
1580
- }
1581
- if (!sub.res.writableEnded) {
1582
- try { sub.res.end(); } catch { /* already gone */ }
1583
- }
1584
- }
1585
- }
1586
-
1587
- /** Convenience factory. */
1588
- export function createRelayServer(options = {}) {
1589
- return new RelayServer(options);
1590
- }
1591
-
1592
- /** Create + listen in one call. */
1593
- export async function startRelayServer(options = {}) {
1594
- const relay = createRelayServer(options);
1595
- await relay.listen(options);
1596
- return relay;
1597
- }
1598
-
1599
- export {
1600
- Persistence,
1601
- ProtocolError,
1602
- RabbitState,
1603
- MAX_FRAME_BYTES,
1604
- PROTOCOL_VERSION,
1605
- rfc3339,
1606
- };
1607
- export default createRelayServer;
1228
+ throw new ProtocolError('NOT_FOUND', `no route for ${method} ${path}`, { method, path });
1229
+ }
1230
+
1231
+ /** §4: the operations answer — "is the relay there, did it restart, are my machines connected". */
1232
+ healthBody(req) {
1233
+ return {
1234
+ ...this.state.stats(),
1235
+ // v0.3.9: how many machines currently have a live P2P announcement. Ephemeral,
1236
+ // so this drops to 0 by itself when nobody is refreshing.
1237
+ ...this.peers.stats(),
1238
+ ok: true,
1239
+ /**
1240
+ * v0.6.2: hoisted out of `persistence` so it cannot be missed by a reader that only looks at
1241
+ * the top level. `degraded: true` means this relay is answering requests while failing to record
1242
+ * them -- the one relay failure that presents as health. `ok` deliberately stays true: the relay
1243
+ * still serves the fleet, and flipping it would turn a recording problem into an outage.
1244
+ */
1245
+ degraded: this.persistence.enabled && this.persistence.degraded,
1246
+ degraded_reason: this.persistence.degraded
1247
+ ? `ledger appends are failing (${this.persistence.lastAppendError ?? 'unknown error'}); results are served but not durably recorded`
1248
+ : null,
1249
+ protocol_version: PROTOCOL_VERSION,
1250
+ rabbit_time: this.state.nowIso(),
1251
+ uptime_ms: this.state.nowMs() - this._startedAtMs,
1252
+ relay_id: this.relayId,
1253
+ started_at: rfc3339(this._startedAtMs),
1254
+ effective_scheme: this.effectiveScheme(req),
1255
+ base_path: this.basePath,
1256
+ operator_token_required: this.operatorTokenRequired,
1257
+ pair_rate_limit: this.pairRateLimitPerMinute,
1258
+ // v0.3.0: fleet-wide latency. Every numeric field is null (never 0) when no
1259
+ // machine has reported a FRESH measurement.
1260
+ rtt: this.state.rttSummary(),
1261
+ // v0.3.0 §9: signature status only — never the secret, not even a prefix.
1262
+ signing: {
1263
+ configured: this.signingConfigured,
1264
+ required: this.requireSignature,
1265
+ previous_secret_accepted: this.signingSecrets.length > 1,
1266
+ skew_seconds: this.signatureSkewSeconds,
1267
+ },
1268
+ persistence: this.persistence.describe(),
1269
+ };
1270
+ }
1271
+
1272
+ /* ---------------- request signing (v0.3.0 §9) ---------------- */
1273
+
1274
+ /**
1275
+ * Should this request be signature-checked at all?
1276
+ *
1277
+ * RULING (see PROTOCOL-v0.3.0.md §9.4): every WRITE is checked; READS are
1278
+ * checked only when `--require-signature` is on.
1279
+ *
1280
+ * Reads are not checked in the default modes because unsigned reads are
1281
+ * accepted anyway, so "verify when a signature happens to be present" would be
1282
+ * theatre -- an attacker simply omits the headers -- while adding a brand new
1283
+ * way for a poll or an SSE reconnect to fail. What a read leaks is topology,
1284
+ * which the device token already gates; what a write can do is change state,
1285
+ * which is what replay and tampering actually threaten.
1286
+ */
1287
+ shouldCheckSignature(method) {
1288
+ if (this.requireSignature) return true; // explicit hard mode: nothing is exempt
1289
+ const isRead = method === 'GET' || method === 'HEAD' || method === 'OPTIONS';
1290
+ return !isRead;
1291
+ }
1292
+
1293
+ /** Run `verifyRequest` and throw the mapped §7 error on rejection. */
1294
+ enforceSignature(req, { method, signedPath, body }) {
1295
+ if (!this.shouldCheckSignature(method)) return { skipped: true };
1296
+ const decision = this.verifySignature(req, { method, signedPath, body });
1297
+ if (decision.ok !== true) throw signatureError(decision);
1298
+ return decision;
1299
+ }
1300
+
1301
+ /**
1302
+ * Thin adapter over the frozen `signing.mjs` contract.
1303
+ *
1304
+ * `path` is the ROUTED path with the query string, NOT the raw request URL: with
1305
+ * `--base-path /w2m` the same client must produce the same signature whether a
1306
+ * proxy passes `/w2m/v1/result` through or strips it to `/v1/result`. The routed
1307
+ * path is the only value both sides agree on in all three deployment forms, so
1308
+ * clients sign `/v1/result` and never `https://host/w2m/v1/result`.
1309
+ */
1310
+ /**
1311
+ * Verify a request's signature (v0.7.11).
1312
+ *
1313
+ * Two mechanisms, and the order between them is the whole design:
1314
+ *
1315
+ * **1. Per-device Ed25519, when the sender is a device with a stored public key.** This is the
1316
+ * one that proves *which* device signed. It is tried **first** because a device key is strictly
1317
+ * stronger evidence than a shared secret, and because a device that registered a key must not
1318
+ * fall back to the weaker mechanism by accident.
1319
+ *
1320
+ * **2. HMAC over a shared secret, otherwise.** Every device that paired before v0.7.10 arrives
1321
+ * here — which is every device today — and nothing about its behaviour changes.
1322
+ *
1323
+ * ## Why this converges with no second deployment
1324
+ *
1325
+ * A device that has not been updated sends no signature header, so mechanism 1 finds no key and
1326
+ * mechanism 2 answers exactly as before. A device that *has* been updated sends a signature, its
1327
+ * public key is on file from pairing, and it is verified with it. So a fleet updates one machine
1328
+ * at a time and each machine gets the strongest mechanism available **to it** the moment it
1329
+ * starts sending one — no flag, no rollout step, no coordination.
1330
+ *
1331
+ * ## What this deliberately does not do
1332
+ *
1333
+ * It does not **require** a device key. `requireSignature` still means "a signature must be
1334
+ * present", and either mechanism satisfies it. Making a *device key* mandatory is a separate,
1335
+ * later switch, because turning it on today would refuse all four machines at once.
1336
+ */
1337
+ verifySignature(req, { method, signedPath, body }) {
1338
+ const viaDevice = this.verifyWithDeviceKey(req, { method, signedPath, body });
1339
+ if (viaDevice !== null) return viaDevice;
1340
+
1341
+ return verifyRequest({
1342
+ headers: req.headers, // plain Node object; signing.mjs reads it case-insensitively
1343
+ method,
1344
+ path: signedPath,
1345
+ body: body ?? '',
1346
+ secrets: this.signingSecrets,
1347
+ nonces: this.nonceCache,
1348
+ nowMs: this.state.nowMs(),
1349
+ skewSeconds: this.signatureSkewSeconds,
1350
+ required: this.requireSignature,
1351
+ });
1352
+ }
1353
+
1354
+ /**
1355
+ * Try the per-device Ed25519 path.
1356
+ *
1357
+ * Returns `null` when the mechanism does not apply — no signature presented, or the signing
1358
+ * machine is not a device whose public key we hold — so the caller falls through. Returns a
1359
+ * decision when it *does* apply, **including when it fails**: a device that registered a key and
1360
+ * then presented a bad signature must not get a second chance through the shared secret, since
1361
+ * that would make the stronger mechanism optional for anyone able to produce a malformed one.
1362
+ */
1363
+ verifyWithDeviceKey(req, { method, signedPath, body }) {
1364
+ const header = readHeader(req.headers, DEVICE_SIGNATURE_HEADER);
1365
+ if (typeof header !== 'string' || header === '') return null;
1366
+
1367
+ const machineId = this.machineIdForRequest(req);
1368
+ if (machineId === null) return null;
1369
+ const publicKey = this.state.devices.get(machineId)?.public_key;
1370
+ if (typeof publicKey !== 'string' || publicKey === '') return null;
1371
+
1372
+ const decision = verifyDeviceSignature({
1373
+ headers: req.headers,
1374
+ publicKeyPem: publicKey,
1375
+ method,
1376
+ path: signedPath,
1377
+ body: body ?? '',
1378
+ nowMs: this.state.nowMs(),
1379
+ skewSeconds: this.signatureSkewSeconds,
1380
+ // `accept`, not `remember` -- the name this class actually exposes. It reports whether the
1381
+ // nonce was new, which is exactly the contract `verifyDeviceSignature` expects.
1382
+ nonces: (nonce, at) => this.nonceCache.accept(nonce, at),
1383
+ });
1384
+ if (decision.ok) return { ...decision, machine_id: machineId, keyIndex: null };
1385
+ return decision;
1386
+ }
1387
+
1388
+ /**
1389
+ * Which machine a request claims to be, from either credential.
1390
+ *
1391
+ * An **operator token is not a machine**, so it resolves to `null` and the device path does not
1392
+ * apply — consistent with `authorizeTaskDispatch`, which reports `machine_id: null` for an
1393
+ * operator. That is why an operator-token request keeps using the shared secret.
1394
+ */
1395
+ machineIdForRequest(req) {
1396
+ const token = bearerToken(req);
1397
+ if (!token) return null;
1398
+ if (this.operatorToken !== null && token === this.operatorToken) return null;
1399
+ return this.state.authenticate(token)?.machine_id ?? null;
1400
+ }
1401
+
1402
+ /** Periodic maintenance, called by the sweep timer. */
1403
+ maintenance() {
1404
+ const expired = this.sweep();
1405
+ /**
1406
+ * v0.9.1: presence expiry, alongside lease expiry.
1407
+ *
1408
+ * `this.sweep()` writes off lease *holds*; this writes off *presence*. They were conflated, so a
1409
+ * machine that stopped existing was only ever noticed when its SSE stream detached — and a
1410
+ * half-open stream never detaches.
1411
+ */
1412
+ const silent = this.state.sweepSilentDevices();
1413
+ if (silent.length > 0) {
1414
+ this._log(`[w2m-rabbit] marked ${silent.length} silent device(s) offline: `
1415
+ + silent.map((s) => `${s.machine_name} (${Math.round(s.silent_ms / 1000)}s)`).join(', '));
1416
+ }
1417
+ this.pairLimiter.prune();
1418
+ this.peers.sweep();
1419
+ // Keep the nonce cache from growing without bound in a long-running process.
1420
+ this.nonceCache.prune(this.nonceRetentionMs, this.state.nowMs());
1421
+ return expired;
1422
+ }
1423
+
1424
+ /* ---------------- auth helpers ---------------- */
1425
+
1426
+ /** §6: every /v1/pair attempt counts, success included. */
1427
+ enforcePairRateLimit(req) {
1428
+ if (this.pairRateLimitPerMinute <= 0) return;
1429
+ const ip = this.clientIp(req);
1430
+ const verdict = this.pairLimiter.hit(ip);
1431
+ if (verdict.allowed) return;
1432
+ const err = new ProtocolError(
1433
+ 'RATE_LIMITED',
1434
+ `too many /v1/pair attempts from ${ip}: limit is ${verdict.limit} per ${verdict.window_seconds}s`,
1435
+ {
1436
+ retry_after_seconds: verdict.retry_after_seconds,
1437
+ limit: verdict.limit,
1438
+ window_seconds: verdict.window_seconds,
1439
+ scope: 'pair',
1440
+ },
1441
+ );
1442
+ err.headers = { 'retry-after': String(verdict.retry_after_seconds) };
1443
+ throw err;
1444
+ }
1445
+
1446
+ /**
1447
+ * §5: `POST /v1/task` needs the operator token.
1448
+ * - required (default): operator token, else 401 OPERATOR_REQUIRED
1449
+ * - disabled (`--operator-token ''`): falls back to v1 semantics, i.e. a paired device token
1450
+ */
1451
+ authorizeTaskDispatch(req) {
1452
+ const token = bearerToken(req);
1453
+ if (this.operatorTokenRequired) {
1454
+ if (!token) {
1455
+ throw new ProtocolError(
1456
+ 'OPERATOR_REQUIRED',
1457
+ 'POST /v1/task requires the operator token. A device_token is machine credentials and cannot '
1458
+ + `dispatch tasks; read the operator token from ${this.persistence.operatorTokenFile ?? '<state>/operator-token.txt'} `
1459
+ + 'or pass --operator-token / W2M_OPERATOR_TOKEN.',
1460
+ { operator_token_file: this.persistence.operatorTokenFile ?? null },
1461
+ );
1462
+ }
1463
+ if (token !== this.operatorToken) {
1464
+ const asDevice = this.state.authenticate(token);
1465
+ throw new ProtocolError(
1466
+ 'OPERATOR_REQUIRED',
1467
+ asDevice
1468
+ ? 'this is a device_token (machine credentials); dispatching tasks requires the operator_token — '
1469
+ + 'they are two different credentials'
1470
+ : 'invalid operator token: it does not match the relay operator token',
1471
+ {
1472
+ device_token_detected: Boolean(asDevice),
1473
+ machine_id: asDevice?.machine_id ?? null,
1474
+ operator_token_file: this.persistence.operatorTokenFile ?? null,
1475
+ },
1476
+ );
1477
+ }
1478
+ return { role: 'operator', machine_id: null };
1479
+ }
1480
+ const device = token ? this.state.authenticate(token) : null;
1481
+ if (!device) {
1482
+ throw new ProtocolError(
1483
+ 'UNAUTHORIZED',
1484
+ 'the operator token requirement is disabled on this relay, but /v1/task still requires a valid device_token',
1485
+ {},
1486
+ );
1487
+ }
1488
+ return { role: 'device', machine_id: device.machine_id };
1489
+ }
1490
+
1491
+ /* ---------------- SSE (§3) ---------------- */
1492
+
1493
+ handleStream(req, res, url, device) {
1494
+ const machineId = device.machine_id;
1495
+ const requested = url.searchParams.get('machine_id');
1496
+ if (requested !== null && requested !== machineId) {
1497
+ throw new ProtocolError('BAD_REQUEST', 'machine_id does not match the device_token', {
1498
+ machine_id: requested, token_machine_id: machineId,
1499
+ });
1500
+ }
1501
+
1502
+ let from = null;
1503
+ const seqParam = url.searchParams.get('seq');
1504
+ const lastEventId = req.headers['last-event-id'];
1505
+ if (seqParam !== null) {
1506
+ from = Number(seqParam);
1507
+ if (!Number.isInteger(from) || from < 1) {
1508
+ throw new ProtocolError('BAD_REQUEST', 'seq must be a positive integer', { seq: seqParam });
1509
+ }
1510
+ } else if (typeof lastEventId === 'string' && lastEventId.trim() !== '') {
1511
+ const last = Number(lastEventId);
1512
+ if (!Number.isInteger(last) || last < 0) {
1513
+ throw new ProtocolError('BAD_REQUEST', 'Last-Event-ID must be a non-negative integer', { last_event_id: lastEventId });
1514
+ }
1515
+ from = last + 1;
1516
+ }
1517
+
1518
+ // §8.5: these two headers are the fix for "connects but no events" behind
1519
+ // nginx-style proxies and tunnels, which buffer event streams by default.
1520
+ res.writeHead(200, {
1521
+ 'content-type': 'text/event-stream; charset=utf-8',
1522
+ 'cache-control': 'no-cache, no-transform',
1523
+ connection: 'keep-alive',
1524
+ 'x-accel-buffering': 'no',
1525
+ });
1526
+ if (typeof res.flushHeaders === 'function') res.flushHeaders();
1527
+
1528
+ const sub = { machineId, res, write: null, timer: null, closed: false };
1529
+
1530
+ const writeEvent = (event) => {
1531
+ if (res.writableEnded) return;
1532
+ res.write(`id: ${event.seq}\nevent: ${event.type}\ndata: ${JSON.stringify(event)}\n\n`);
1533
+ };
1534
+
1535
+ let live = false;
1536
+ const queue = [];
1537
+ const deliver = (entry) => {
1538
+ if (entry.target && entry.target !== machineId) return;
1539
+ writeEvent(entry.event);
1540
+ };
1541
+ const unsubscribe = this.state.onEvent((entry) => {
1542
+ if (!live) { queue.push(entry); return; }
1543
+ deliver(entry);
1544
+ });
1545
+
1546
+ // §3.1: the first frame MUST be `ready`; §8.3 adds `relay_id`.
1547
+ const readySeq = this.state.reserveSeq();
1548
+ writeEvent({
1549
+ type: 'ready',
1550
+ seq: readySeq,
1551
+ protocol_version: PROTOCOL_VERSION,
1552
+ rabbit_time: this.state.nowIso(),
1553
+ machine_id: machineId,
1554
+ relay_id: this.relayId,
1555
+ });
1556
+
1557
+ if (from !== null) {
1558
+ const oldest = this.state.oldestBufferedSeq();
1559
+ const lastBuffered = this.state.lastBufferedSeq();
1560
+ // `fromFuture` is judged against EVERY seq this process has allocated (a client
1561
+ // can legitimately hold a `ready` seq, which is not buffered), while the
1562
+ // reported window bounds describe the replay ring the client can actually use.
1563
+ const lastSeq = this.state.lastSeq();
1564
+ // Two distinct ways for a cursor to be out of sync with THIS process:
1565
+ //
1566
+ // from < oldest the cursor is older than the replay window (buffer rot)
1567
+ // from > lastSeq+1 the cursor cannot exist here at all. Sequence numbers
1568
+ // restart at 1 in every process, so a cursor from a
1569
+ // previous relay process is AHEAD of a freshly started
1570
+ // relay. This was the silent case: `13 < 1` is false, the
1571
+ // client got no notice, `replayEntries(13)` matched
1572
+ // nothing, and the agent then reset its cursor to the
1573
+ // `ready` seq -- stepping over every event it had missed.
1574
+ const tooOld = from < oldest && oldest > 1;
1575
+ const fromFuture = from > lastSeq + 1;
1576
+ if (tooOld || fromFuture) {
1577
+ // §8.4: tell the client where to re-align, not just that replay failed.
1578
+ writeEvent({
1579
+ type: 'notice',
1580
+ seq: this.state.reserveSeq(),
1581
+ rabbit_time: this.state.nowIso(),
1582
+ level: 'warn',
1583
+ code: 'REPLAY_TRUNCATED',
1584
+ message: tooOld
1585
+ ? `requested seq ${from} is older than the buffer head ${oldest}`
1586
+ : `requested seq ${from} is beyond this process' last seq ${lastSeq}: the cursor came from a `
1587
+ + 'previous relay process, whose sequence numbering restarted at 1',
1588
+ machine_id: machineId,
1589
+ requested_seq: from,
1590
+ oldest_available_seq: oldest,
1591
+ last_available_seq: lastBuffered,
1592
+ relay_id: this.relayId,
1593
+ reason: tooOld ? 'buffer_truncated' : 'cursor_ahead_of_relay',
1594
+ });
1595
+ }
1596
+ // A cursor from a previous process is meaningless here, so honouring it
1597
+ // literally (replay from `from`) would deliver NOTHING and also lose every
1598
+ // event that fell into the gap -- not only offers, but `task.cancel`,
1599
+ // `notice`, `peer.*`. Re-align to the head of the window instead: the client
1600
+ // asked to be resynchronised, and the ring is exactly what we still have.
1601
+ const replayFrom = fromFuture ? oldest : from;
1602
+ for (const entry of this.state.replayEntries(replayFrom)) deliver(entry);
1603
+ }
1604
+
1605
+ live = true;
1606
+ for (const entry of queue) deliver(entry);
1607
+ queue.length = 0;
1608
+
1609
+ // §8.6: re-state work this machine was handed while its stream was down.
1610
+ //
1611
+ // An offer is a single event; if the stream is not connected at that instant
1612
+ // -- after a relay restart, a tunnel drop, or any interruption -- the event
1613
+ // is lost and the lease waits out its whole window before being swept. The
1614
+ // machine is meanwhile online and idle. Re-delivering on connect is what
1615
+ // makes a reconnect recover work instead of silently losing it.
1616
+ //
1617
+ // Only unclaimed leases are re-offered; `running` is excluded so a task that
1618
+ // has already begun cannot be handed out a second time and executed twice.
1619
+ const redelivered = this.state.redeliverPendingOffers(machineId);
1620
+ if (redelivered.count > 0) {
1621
+ this._log(`[w2m-rabbit] re-offered ${redelivered.count} pending task(s) to ${machineId} on reconnect`);
1622
+ }
1623
+
1624
+ // §3.2: `: keepalive` comment every 15s.
1625
+ sub.timer = setInterval(() => {
1626
+ if (res.writableEnded) return;
1627
+ res.write(': keepalive\n\n');
1628
+ }, this.keepaliveMs);
1629
+ sub.timer.unref?.();
1630
+
1631
+ sub.write = writeEvent;
1632
+ sub.unsubscribe = unsubscribe;
1633
+ this.subscribers.add(sub);
1634
+ device.streams = (device.streams ?? 0) + 1;
1635
+ // v0.3.0: relay-observed reconnect counter. A first attach is not a reconnect,
1636
+ // so the reported `reconnect_attempts` is (attaches - 1).
1637
+ this.state.noteStreamConnect(machineId);
1638
+
1639
+ const onClose = () => this._closeSubscriber(sub);
1640
+ req.on('close', onClose);
1641
+ res.on('close', onClose);
1642
+ res.on('error', onClose);
1643
+ return undefined;
1644
+ }
1645
+
1646
+ _closeSubscriber(sub) {
1647
+ if (!sub || sub.closed) return;
1648
+ sub.closed = true;
1649
+ this.subscribers.delete(sub);
1650
+ if (sub.timer) clearInterval(sub.timer);
1651
+ sub.timer = null;
1652
+ sub.unsubscribe?.();
1653
+ const device = this.state.devices.get(sub.machineId);
1654
+ if (device) {
1655
+ device.streams = Math.max(0, (device.streams ?? 1) - 1);
1656
+ if (device.streams === 0) this.state.setOnline(sub.machineId, false, 'stream_closed');
1657
+ }
1658
+ if (!sub.res.writableEnded) {
1659
+ try { sub.res.end(); } catch { /* already gone */ }
1660
+ }
1661
+ }
1662
+ }
1663
+
1664
+ /** Convenience factory. */
1665
+ export function createRelayServer(options = {}) {
1666
+ return new RelayServer(options);
1667
+ }
1668
+
1669
+ /** Create + listen in one call. */
1670
+ export async function startRelayServer(options = {}) {
1671
+ const relay = createRelayServer(options);
1672
+ await relay.listen(options);
1673
+ return relay;
1674
+ }
1675
+
1676
+ export {
1677
+ Persistence,
1678
+ ProtocolError,
1679
+ RabbitState,
1680
+ MAX_FRAME_BYTES,
1681
+ PROTOCOL_VERSION,
1682
+ rfc3339,
1683
+ };
1684
+ export default createRelayServer;