@bom0792/herdgate 0.9.31 → 0.9.35

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,79 +1,6 @@
1
1
  #!/usr/bin/env bash
2
- # herdgate → VPS: dựng gateway trực tiếp trên VPS (không Cloudflare tunnel).
3
- # Dùng: bash scripts/deploy-vps.sh root@<VPS-IP> <gate-hostname>
4
- # Yêu cầu bên VPS : Ubuntu/Debian, root (qua key herdgate_deploy), port 80/443/2299 mở.
5
- # Yêu cầu bên DNS : gate. + ssh. trỏ A → VPS (mây xám) — làm TRƯỚC khi chạy để Caddy xin cert được.
6
- set -euo pipefail
7
- TARGET="${1:?dùng: deploy-vps.sh root@<vps-ip> <gate-hostname>}"
8
- GATE_HOST="${2:?dùng: deploy-vps.sh root@<vps-ip> <gate-hostname>}"
9
- if [[ ! "$GATE_HOST" =~ ^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$ ]] || (( ${#GATE_HOST} > 253 )); then
10
- echo "gate-hostname phải là tên DNS, không có giao thức hay cổng" >&2
11
- exit 2
12
- fi
13
- KEY="$HOME/.ssh/herdgate_deploy"
14
- DIR=$(cd "$(dirname "$0")/.." && pwd)
15
-
16
- echo "==> 1/5 sync source + node_modules + state"
17
- rsync -az --delete -e "ssh -i $KEY" \
18
- --exclude .git --exclude test --exclude app-mod --exclude plugin/herdr-plugin.toml \
19
- "$DIR/src" "$DIR/package.json" "$DIR/tsconfig.json" "$DIR/node_modules" "$TARGET:/opt/herdgate/"
20
- rsync -az -e "ssh -i $KEY" "$HOME/.local/state/herdgate/" "$TARGET:/opt/herdgate-state/"
21
-
22
- echo "==> 2/5 node 22 (nvm, per-root) + systemd herdgate"
23
- ssh -i "$KEY" "$TARGET" 'set -e
24
- if ! command -v node >/dev/null || [ "$(node -v | cut -c2- | cut -d. -f1)" -lt 22 ]; then
25
- curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash >/dev/null
26
- . ~/.nvm/nvm.sh
27
- nvm install 22 --silent
28
- fi
29
- NODE=$(command -v node)
30
- mkdir -p ~/.config/systemd/user 2>/dev/null || true
31
- cat > /etc/systemd/system/herdgate.service <<EOF2
32
- [Unit]
33
- Description=herdgate gateway (cloud relay hub)
34
- After=network-online.target
35
- Wants=network-online.target
36
-
37
- [Service]
38
- ExecStart=$NODE /opt/herdgate/src/cloud/main.ts
39
- Environment=HERDGATE_BIND=127.0.0.1
40
- Environment=HERDGATE_PORT=8833
41
- Environment=HERDGATE_STATE=/opt/herdgate-state/state.json
42
- Environment=HERDGATE_SSH_PORT=2299
43
- Environment=HERDGATE_SSH_BIND=0.0.0.0
44
- Environment=HERDGATE_ACCOUNT_TOKEN=__TOKEN__
45
- WorkingDirectory=/opt/herdgate
46
- Restart=always
47
- RestartSec=3
48
-
49
- [Install]
50
- WantedBy=multi-user.target
51
- EOF2
52
- '
53
-
54
- echo "==> 3/5 chèn account token từ unit nhà (base64 qua đường, khỏi sợ ký tự đặc biệt)"
55
- TOKEN=$(grep -oP 'HERDGATE_ACCOUNT_TOKEN=\K.*' ~/.config/systemd/user/herdgate.service | tr -d '"')
56
- TOKEN_B64=$(printf %s "$TOKEN" | base64 -w0)
57
- ssh -i "$KEY" "$TARGET" "python3 -c \"import base64; tok=base64.b64decode('$TOKEN_B64').decode(); p=open('/etc/systemd/system/herdgate.service').read().replace('__TOKEN__',tok); open('/etc/systemd/system/herdgate.service','w').write(p)\""
58
-
59
- echo "==> 4/5 caddy (TLS tự động) + firewall"
60
- ssh -i "$KEY" "$TARGET" "GATE_HOST=$GATE_HOST "'set -e
61
- if ! command -v caddy >/dev/null; then
62
- apt-get update -qq && apt-get install -y -qq debian-keyring debian-archive-keyring apt-transport-https curl >/dev/null
63
- curl -1sLf https://dl.cloudsmith.io/public/caddy/stable/gpg.key | gpg --batch --yes --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
64
- curl -1sLf https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt | tee /etc/apt/sources.list.d/caddy-stable.list >/dev/null
65
- apt-get update -qq && apt-get install -y -qq caddy >/dev/null
66
- fi
67
- cat > /etc/caddy/Caddyfile <<EOF2
68
- $GATE_HOST {
69
- reverse_proxy 127.0.0.1:8833
70
- }
71
- EOF2
72
- systemctl enable --now caddy >/dev/null 2>&1 || systemctl restart caddy
73
- command -v ufw >/dev/null && ufw allow 80/tcp && ufw allow 443/tcp && ufw allow 2299/tcp comment herdgate-ssh || true
74
- '
75
-
76
- echo "==> 5/5 start herdgate"
77
- ssh -i "$KEY" "$TARGET" 'systemctl daemon-reload && systemctl enable --now herdgate && sleep 3 && systemctl is-active herdgate'
78
-
79
- echo "OK — kiểm tra: curl https://$GATE_HOST/healthz (sau khi DNS đã trỏ)"
2
+ # Retired: this used to copy a local Gate's credentials and state to the target.
3
+ set -eu
4
+ printf '%s
5
+ ' 'deploy-vps.sh was retired; it does not connect to the target.' 'For a clean deployment, run npm run build:release and follow the Cloud archive README.' 'For migration, back up and restore state and keys explicitly. See DEPLOY.md.' >&2
6
+ exit 2
@@ -17,9 +17,8 @@
17
17
  */
18
18
 
19
19
  import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http";
20
- import { request as httpRequest } from "node:http";
21
- import { request as httpsRequest } from "node:https";
22
- import { Duplex, type Stream } from "node:stream";
20
+ import { isIP } from "node:net";
21
+ import { Duplex } from "node:stream";
23
22
  import { WebSocket, WebSocketServer } from "ws";
24
23
  import {
25
24
  decode,
@@ -39,7 +38,8 @@ import { type Account, type DeviceRecord, type RoomRecord, State } from "./store
39
38
  import { readJsonBody, randomId, sameSecret, sha256Hex } from "./util.ts";
40
39
  import { serveStatic } from "./static.ts";
41
40
  import { startSshd, type SshHandle, type SshConfig } from "./sshd.ts";
42
- import { endpointTag, vapidHeader, type VapidKeys } from "./vapid.ts";
41
+ import { pushEndpoint, sendWake } from "./push.ts";
42
+ import { type VapidKeys } from "./vapid.ts";
43
43
 
44
44
  export interface CloudConfig {
45
45
  accounts: { id: string; name: string; token: string }[];
@@ -53,12 +53,27 @@ export interface CloudConfig {
53
53
  publicUrl?: string | null;
54
54
  /** Directory of the PWA (src/web). Served on `/`; omit to serve no UI. */
55
55
  webRoot?: string;
56
- /** The SSH leg phones dial. `port: 0` disables. */
56
+ /** The SSH leg; omit to disable, or set `port: 0` for an ephemeral listener. */
57
57
  ssh?: SshConfig;
58
- /** The hostname phones should dial for the SSH leg (e.g. an AAAA/DNS-only name). */
58
+ /** Public address advertised to clients, when an edge maps a different port. */
59
59
  sshHost?: string;
60
+ sshPublicPort?: number;
60
61
  /** VAPID keys for silent wake pushes; omit to disable the push endpoints. */
61
62
  push?: { keys: VapidKeys; /** mailto contact required by the VAPID spec. */ sub: string } | null;
63
+ /**
64
+ * Trust a reverse-proxy header (e.g. CF-Connecting-IP) as the client address for
65
+ * abuse limits. Without it, loopback connections (a same-host proxy such as
66
+ * cloudflared) get a per-connection budget so one abusive client can never lock
67
+ * out everyone behind the proxy.
68
+ */
69
+ trustedProxy?: boolean;
70
+ /** Header name read in trusted-proxy mode; default CF-Connecting-IP. */
71
+ realIpHeader?: string;
72
+ /** SSH-over-WSS budget. Defaults: 6 per client, 64 total. Raw-TCP edges cannot
73
+ * convey real client addresses, so size these for the shared edge address. */
74
+ sshTunnelLimits?: { perKey?: number; total?: number };
75
+ /** Cap on concurrently pending pairing codes; default 256. */
76
+ maxPendingCodes?: number;
62
77
  heartbeatMs?: number;
63
78
  log?: (line: string) => void;
64
79
  }
@@ -86,7 +101,7 @@ export interface Cloud {
86
101
  export function startCloud(cfg: CloudConfig): Promise<Cloud> {
87
102
  const log = cfg.log ?? (() => {});
88
103
  const state = new State(cfg.accounts, cfg.statePath);
89
- const codes = new AgentCodes(Date.now, cfg.publicUrl ?? null);
104
+ const codes = new AgentCodes(Date.now, cfg.publicUrl ?? null, cfg.maxPendingCodes ?? 256);
90
105
  let sshd: SshHandle | null = null;
91
106
 
92
107
  const agentByRoom = new Map<string, AgentConn>(); // roomId → conn
@@ -143,6 +158,7 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
143
158
  msg: Record<string, unknown>;
144
159
  bytes: number;
145
160
  at: number;
161
+ device?: string;
146
162
  }
147
163
  interface RoomStream {
148
164
  gen: string;
@@ -161,42 +177,17 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
161
177
  }
162
178
  return s;
163
179
  };
164
- /** Silent wake push to a device's stored endpoint (VAPID, no payload). */
180
+ const pendingWakes = new Set<string>();
165
181
  const wakeDevice = (deviceId: string): void => {
166
182
  const push = cfg.push;
167
- const endpoint = state.deviceById(deviceId)?.push?.endpoint;
168
- if (!push || !endpoint) return;
169
- try {
170
- const u = new URL(endpoint);
171
- const send = u.protocol === "https:" ? httpsRequest : httpRequest;
172
- const req = send(
173
- {
174
- protocol: u.protocol,
175
- hostname: u.hostname,
176
- port: u.port || (u.protocol === "https:" ? 443 : 80),
177
- path: `${u.pathname}${u.search}`,
178
- method: "POST",
179
- headers: {
180
- TTL: "60",
181
- Urgency: "normal",
182
- "content-length": "0",
183
- Authorization: vapidHeader(endpoint, push.keys, push.sub),
184
- },
185
- },
186
- (res) => {
187
- res.resume();
188
- if (res.statusCode && res.statusCode >= 400) log(`push: wake ${endpointTag(endpoint)} answered ${res.statusCode}`);
189
- },
190
- );
191
- req.on("error", (e) => log(`push: wake ${endpointTag(endpoint)} failed (${e instanceof Error ? e.message : String(e)})`));
192
- req.end();
193
- } catch (e) {
194
- // A bad endpoint must never take the gateway down.
195
- log(`push: wake ${endpointTag(endpoint)} failed (${e instanceof Error ? e.message : String(e)})`);
196
- }
183
+ const endpoint = pushEndpoint(state.deviceById(deviceId)?.push?.endpoint ?? "");
184
+ if (!push || !endpoint || pendingWakes.has(deviceId) || pendingWakes.size >= 8) return;
185
+ pendingWakes.add(deviceId);
186
+ void sendWake(endpoint, push.keys, push.sub).catch(() => log("push: wake failed"))
187
+ .finally(() => pendingWakes.delete(deviceId));
197
188
  };
198
189
 
199
- const streamEmit = (roomId: string, msg: Record<string, unknown>): void => {
190
+ const streamEmit = (roomId: string, msg: Record<string, unknown>, device?: string): void => {
200
191
  const s = streamOf(roomId);
201
192
  s.seq += 1;
202
193
  const stamped = { ...msg, seq: s.seq };
@@ -215,45 +206,69 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
215
206
  if (s.seq > s.evictedUpTo) s.evictedUpTo = s.seq;
216
207
  } else {
217
208
  while (s.bytes + bytes > STREAM_MAX_BYTES) evict();
218
- s.buf.push({ seq: s.seq, msg: stamped, bytes, at });
209
+ s.buf.push({ seq: s.seq, msg: stamped, bytes, at, device });
219
210
  s.bytes += bytes;
220
211
  }
221
212
  for (const c of deviceConns) {
222
- if (c.device?.roomId !== roomId) continue;
223
- try {
224
- c.ws.send(text);
225
- } catch {
226
- /* closing */
227
- }
213
+ if (c.device?.roomId !== roomId || (device !== undefined && c.device.id !== device)) continue;
214
+ sendText(c.ws, text);
228
215
  }
229
216
  };
230
217
 
231
- // ---- rate limiting: failed hellos per IP ----
232
- const fails = new Map<string, { n: number; until: number }>();
233
- const ipOf = (req: IncomingMessage): string => req.socket.remoteAddress ?? "?";
234
- const strike = (ip: string): void => {
235
- const f = fails.get(ip) ?? { n: 0, until: 0 };
218
+ // ---- rate limiting: failed hellos per client ----
219
+ // The key is the client address, with two guards against a shared-address
220
+ // collapse (audit gateway:shared-address-ip-controls): loopback connections
221
+ // (a same-host proxy like cloudflared) always get a per-connection key, and a
222
+ // trusted-proxy header restores real per-client keys behind an edge.
223
+ const fails = new Map<string, { n: number; until: number; at: number }>();
224
+ const FAILS_MAX = 4_096;
225
+ const loopbackOf = (addr: string): boolean => addr === "127.0.0.1" || addr === "::1" || addr === "::ffff:127.0.0.1";
226
+ const clientKeyOf = (req: IncomingMessage): string => {
227
+ const addr = req.socket.remoteAddress ?? "?";
228
+ if (cfg.trustedProxy) {
229
+ const raw = cfg.realIpHeader ? req.headers[cfg.realIpHeader.toLowerCase()] : undefined;
230
+ const value = (Array.isArray(raw) ? raw[0] : raw) ?? "";
231
+ const ip = String(value).trim();
232
+ if (ip && !ip.includes(",") && isIP(ip) !== 0) return ip;
233
+ return `${addr}#${randomId("k")}`; // no usable header: never share a budget
234
+ }
235
+ if (loopbackOf(addr)) return `${addr}#${randomId("k")}`;
236
+ return addr;
237
+ };
238
+ const pruneFails = (): void => {
239
+ if (fails.size <= FAILS_MAX) return;
240
+ const now = Date.now();
241
+ for (const [k, f] of fails) if (now - f.at > 60_000) fails.delete(k);
242
+ if (fails.size > FAILS_MAX) fails.clear();
243
+ };
244
+ const strike = (key: string): void => {
245
+ pruneFails();
246
+ const f = fails.get(key) ?? { n: 0, until: 0, at: Date.now() };
236
247
  f.n += 1;
248
+ f.at = Date.now();
237
249
  if (f.n >= 5) {
238
250
  f.until = Date.now() + 60_000;
239
251
  f.n = 0;
240
252
  }
241
- fails.set(ip, f);
253
+ fails.set(key, f);
242
254
  };
243
- const blocked = (ip: string): boolean => {
244
- const f = fails.get(ip);
255
+ const blocked = (key: string): boolean => {
256
+ const f = fails.get(key);
245
257
  return !!f && f.until > Date.now();
246
258
  };
247
259
 
248
- const send = (ws: WebSocket, msg: Record<string, unknown>): void => {
249
- if (ws.readyState === ws.OPEN) {
250
- try {
251
- ws.send(encode(msg));
252
- } catch {
253
- /* closing */
254
- }
255
- }
260
+ // Per-client pairing-code mint rate (audit pairing:unbounded-code-mint).
261
+ const MINT_PER_KEY = 10;
262
+ const MINT_WINDOW_MS = 10 * 60_000;
263
+ const mints = new Map<string, { n: number; until: number; at: number }>();
264
+
265
+ const sendText = (ws: WebSocket, text: string | Buffer): void => {
266
+ if (ws.readyState !== WebSocket.OPEN) return;
267
+ const size = typeof text === "string" ? Buffer.byteLength(text) : text.length;
268
+ if (ws.bufferedAmount + size > 1024 * 1024) return ws.terminate();
269
+ try { ws.send(text); } catch { ws.terminate(); }
256
270
  };
271
+ const send = (ws: WebSocket, msg: Record<string, unknown>): void => sendText(ws, encode(msg));
257
272
  const fail = (ws: WebSocket, code: string, message: string): void => send(ws, { t: "error", code, message });
258
273
 
259
274
  // ---- registry helpers ----
@@ -311,17 +326,19 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
311
326
  };
312
327
 
313
328
  const upgrade = (req: IncomingMessage, socket: import("node:stream").Duplex, head: Buffer, leg: "agent" | "device" | "bridge" | "ssh-ws"): void => {
314
- const ip = ipOf(req);
315
- if (blocked(ip)) {
329
+ const key = clientKeyOf(req);
330
+ if (blocked(key)) {
316
331
  socket.write("HTTP/1.1 429 Too Many Requests\r\n\r\n");
317
332
  socket.destroy();
318
333
  return;
319
334
  }
320
335
  wss.handleUpgrade(req, socket, head, (ws) => {
321
- if (leg === "agent") attachAgent(ws, ip);
322
- else if (leg === "bridge") attachBridge(ws, ip);
323
- else if (leg === "ssh-ws") attachSshTunnel(ws, ip);
324
- else attachDevice(ws, ip);
336
+ // ws emits receiver errors on the connection, including before its app hello.
337
+ ws.on("error", () => ws.terminate());
338
+ if (leg === "agent") attachAgent(ws, key);
339
+ else if (leg === "bridge") attachBridge(ws, key);
340
+ else if (leg === "ssh-ws") attachSshTunnel(ws, key);
341
+ else attachDevice(ws, key);
325
342
  });
326
343
  };
327
344
 
@@ -367,6 +384,7 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
367
384
  }
368
385
 
369
386
  const room = conn.room;
387
+ if (agentByRoom.get(room.id)?.ws !== ws) return;
370
388
  switch (m.t) {
371
389
  case "frame": {
372
390
  const device = typeof m.device === "string" ? m.device : null;
@@ -381,16 +399,12 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
381
399
  // Ephemeral: raw, unstamped, never buffered (see room streams above).
382
400
  const text = encode({ t: "frame", payload });
383
401
  for (const c of deviceConns) {
384
- if (c.device?.id !== device) continue;
385
- try {
386
- c.ws.send(text);
387
- } catch {
388
- /* closing */
389
- }
402
+ if (c.device?.id === device) sendText(c.ws, text);
390
403
  }
391
404
  return;
392
405
  }
393
- streamEmit(room.id, { t: "frame", payload });
406
+ if (state.deviceById(device)?.roomId !== room.id) return;
407
+ streamEmit(room.id, { t: "frame", payload }, device);
394
408
  return;
395
409
  }
396
410
  case "presence": {
@@ -537,50 +551,48 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
537
551
  * into the sshd via `inject`. For clients whose network cannot reach the TCP
538
552
  * leg (IPv4-only vs an AAAA-only host) — the tunnel rides port 443 like the
539
553
  * web app. No pre-auth: the SSH protocol itself authenticates (device id +
540
- * publickey/password) and the sshd's strike map keys on the socket's address —
541
- * which, behind a CDN, is the edge IP; documented, and bounded here.
554
+ * publickey/password) and the sshd's strike map keys on the tunnel's client key —
555
+ * a real address, a trusted-proxy header value, or a per-connection key for
556
+ * same-host proxies (audit gateway:shared-address-ip-controls).
542
557
  */
543
558
  const sshTunnels = new Map<string, number>();
544
- const SSH_TUNNELS_PER_IP = 6;
545
- const SSH_TUNNELS_TOTAL = 64;
546
- const attachSshTunnel = (ws: WebSocket, ip: string): void => {
547
- const n = sshTunnels.get(ip) ?? 0;
559
+ const SSH_TUNNELS_PER_KEY = Math.max(1, Math.min(1_024, cfg.sshTunnelLimits?.perKey ?? 6));
560
+ const SSH_TUNNELS_TOTAL = Math.max(1, Math.min(1_024, cfg.sshTunnelLimits?.total ?? 64));
561
+ const attachSshTunnel = (ws: WebSocket, key: string): void => {
562
+ const n = sshTunnels.get(key) ?? 0;
548
563
  const total = [...sshTunnels.values()].reduce((a, b) => a + b, 0);
549
- if (!sshd || n >= SSH_TUNNELS_PER_IP || total >= SSH_TUNNELS_TOTAL) {
564
+ if (!sshd || n >= SSH_TUNNELS_PER_KEY || total >= SSH_TUNNELS_TOTAL) {
550
565
  ws.close(1013, !sshd ? "ssh leg disabled" : "busy");
551
566
  return;
552
567
  }
553
- sshTunnels.set(ip, n + 1);
568
+ sshTunnels.set(key, n + 1);
554
569
  keepAlive(ws);
555
- const sock = new VirtualSshSocket(ip);
570
+ const sock = new VirtualSshSocket(key);
556
571
  let closed = false;
557
572
  const bye = (): void => {
558
573
  if (closed) return;
559
574
  closed = true;
560
- const left = (sshTunnels.get(ip) ?? 1) - 1;
561
- if (left > 0) sshTunnels.set(ip, left);
562
- else sshTunnels.delete(ip);
575
+ const left = (sshTunnels.get(key) ?? 1) - 1;
576
+ if (left > 0) sshTunnels.set(key, left);
577
+ else sshTunnels.delete(key);
563
578
  };
564
579
  ws.on("message", (data, isBinary) => {
565
580
  if (!isBinary) return ws.close(1003, "binary only");
566
- if (!sock.push(Buffer.from(data as Buffer))) return; // sshd not reading: leave the buffering to ws
581
+ if (!sock.push(Buffer.from(data as Buffer))) ws.terminate();
567
582
  });
568
583
  ws.on("close", () => {
569
584
  bye();
570
585
  sock.push(null); // EOF: the sshd hangs up cleanly
571
586
  sock.destroy();
572
587
  });
573
- ws.on("error", () => { /* onclose follows */ });
574
- sock.on("tobytes", (chunk: Buffer) => {
575
- if (ws.readyState === 1) ws.send(chunk); // 1 = OPEN, instance-safe
576
- });
588
+ sock.on("tobytes", (chunk: Buffer) => sendText(ws, chunk));
577
589
  sock.on("close", () => {
578
590
  bye();
579
- if (ws.readyState === 1) {
580
- try { ws.close(1000); } catch { /* undici: some codes throw */ }
581
- }
591
+ // Do not let a peer hold the transport open after the SSH deadline or
592
+ // revocation by declining to complete a WebSocket close handshake.
593
+ ws.terminate();
582
594
  });
583
- sshd.inject(sock as unknown as import("node:net").Socket);
595
+ sshd.inject(sock);
584
596
  };
585
597
 
586
598
  const attachDevice = (ws: WebSocket, ip: string): void => {
@@ -651,7 +663,7 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
651
663
  // Resume: a returning device replays what it missed (bounded, best effort).
652
664
  // No match (no resume, wrong room, stale gen, seq from another life) falls
653
665
  // through to a fresh hello — the device re-requests state on its own.
654
- const wantResume = ((): { room: string; gen: string; seq: number } | null => {
666
+ const wantResume = ((): { room: string; device: string; gen: string; seq: number } | null => {
655
667
  if (!device) return null;
656
668
  const r = m.resume;
657
669
  if (typeof r !== "object" || r === null) return null;
@@ -660,15 +672,16 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
660
672
  const seq = (r as { seq?: unknown }).seq;
661
673
  if (room !== device.roomId || typeof gen !== "string") return null;
662
674
  if (typeof seq !== "number" || !Number.isSafeInteger(seq) || seq < 0) return null;
663
- return { room, gen, seq };
675
+ return { room, device: device.id, gen, seq };
664
676
  })();
665
677
  const resuming = ((): { replay: StreamMsg[]; gap: boolean } | null => {
666
678
  if (!wantResume) return null;
667
679
  const s = streams.get(wantResume.room);
668
680
  if (!s || s.gen !== wantResume.gen || wantResume.seq > s.seq) return null;
669
- const replay = s.buf.filter((b) => b.seq > wantResume.seq);
681
+ const tail = s.buf.filter((b) => b.seq > wantResume.seq);
682
+ const replay = tail.filter((b) => b.device === undefined || b.device === wantResume.device);
670
683
  // Missed more than we kept: say so, the device re-syncs its state.
671
- const gap = replay.length < s.seq - wantResume.seq;
684
+ const gap = tail.length < s.seq - wantResume.seq;
672
685
  return { replay, gap };
673
686
  })();
674
687
 
@@ -680,11 +693,7 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
680
693
  stream: { gen: s.gen, seq: s.seq, gap: resuming.gap },
681
694
  });
682
695
  for (const b of resuming.replay) {
683
- try {
684
- ws.send(encode(b.msg));
685
- } catch {
686
- /* closing */
687
- }
696
+ send(ws, b.msg);
688
697
  }
689
698
  if (resuming.gap) log(`device ${device.id} resumed with a gap (re-requests state)`);
690
699
  } else {
@@ -699,7 +708,7 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
699
708
  if (!cfg.ssh) return {};
700
709
  for (const b of bridges) if (b.advertise) return { ssh: { ...b.advertise } };
701
710
  // The real port (the listener may be ephemeral), falling back to config.
702
- return { ssh: { port: sshd?.port ?? cfg.ssh.port, ...(cfg.sshHost ? { host: cfg.sshHost } : {}) } };
711
+ return { ssh: { port: cfg.sshPublicPort ?? sshd?.port ?? cfg.ssh.port, ...(cfg.sshHost ? { host: cfg.sshHost } : {}) } };
703
712
  })(),
704
713
  });
705
714
  }
@@ -710,7 +719,7 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
710
719
  switch (m.t) {
711
720
  case "frame": {
712
721
  const device = conn.device;
713
- if (!device) return fail(ws, "not-paired", "pair.begin first");
722
+ if (!device || state.deviceById(device.id) !== device) return fail(ws, "not-paired", "pair.begin first");
714
723
  const op = (m.payload as { op?: unknown } | null)?.op;
715
724
  if (typeof op === "string" && op.startsWith("ssh.")) return fail(ws, "bad-message", "SSH frames require an authenticated SSH channel");
716
725
  const agent = agentByRoom.get(device.roomId);
@@ -801,7 +810,9 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
801
810
  res.writeHead(421);
802
811
  return res.end("wrong host");
803
812
  }
804
- const url = new URL(req.url ?? "/", "http://x");
813
+ let url: URL;
814
+ try { url = new URL(req.url ?? "/", "http://x"); }
815
+ catch { return json(res, 400, { error: "bad request target" }); }
805
816
  const path = url.pathname;
806
817
 
807
818
  if (req.method === "GET" && path === "/healthz") {
@@ -810,10 +821,31 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
810
821
  }
811
822
 
812
823
  if (req.method === "POST" && path === "/v1/agent/code") {
813
- if (blocked(ipOf(req))) return json(res, 429, { error: "rate limited" });
824
+ const key = clientKeyOf(req);
825
+ if (blocked(key)) return json(res, 429, { error: "rate limited" });
826
+ // Two budgets so minting cannot be driven without limit (audit
827
+ // pairing:unbounded-code-mint): a small per-client rate and a global
828
+ // pending cap inside AgentCodes.
829
+ const m = mints.get(key) ?? { n: 0, until: 0, at: Date.now() };
830
+ const now = Date.now();
831
+ if (m.until > now) {
832
+ mints.set(key, m);
833
+ return json(res, 429, { error: "too many pairing codes; retry shortly" });
834
+ }
835
+ if (now - m.at > MINT_WINDOW_MS) m.n = 0;
836
+ if (m.n >= MINT_PER_KEY) {
837
+ m.until = now + MINT_WINDOW_MS;
838
+ mints.set(key, m);
839
+ return json(res, 429, { error: "too many pairing codes; retry shortly" });
840
+ }
841
+ m.n += 1;
842
+ m.at = now;
843
+ mints.set(key, m);
814
844
  return void readJsonBody(req).then((body) => {
815
845
  const name = typeof body?.name === "string" && body.name.trim() ? body.name.trim().slice(0, 64) : "host";
816
- json(res, 200, codes.mint(name));
846
+ const minted = codes.mint(name);
847
+ if (!minted) return json(res, 429, { error: "too many pending pairing codes" });
848
+ json(res, 200, minted);
817
849
  });
818
850
  }
819
851
 
@@ -834,7 +866,7 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
834
866
 
835
867
  const statusMatch = path.match(/^\/v1\/agent\/code\/([A-Za-z0-9]+)\/status$/);
836
868
  if (req.method === "GET" && statusMatch) {
837
- if (blocked(ipOf(req))) return json(res, 429, { error: "rate limited" });
869
+ if (blocked(clientKeyOf(req))) return json(res, 429, { error: "rate limited" });
838
870
  const st = codes.status(statusMatch[1]);
839
871
  if (st.status === "unknown") return json(res, 404, { status: st.status });
840
872
  return json(res, 200, st);
@@ -844,7 +876,7 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
844
876
  return json(res, 200, { publicKey: cfg.push.keys.publicBase64Url });
845
877
  }
846
878
  if (cfg.push && path === "/v1/device/push") {
847
- if (blocked(ipOf(req))) return json(res, 429, { error: "rate limited" });
879
+ if (blocked(clientKeyOf(req))) return json(res, 429, { error: "rate limited" });
848
880
  // Two identities, same as the WS leg: the paired device (id + its own secret in
849
881
  // the body) or the owner (account token in Authorization, device id in the body).
850
882
  return void readJsonBody(req).then((body) => {
@@ -861,15 +893,11 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
861
893
  state.setDevicePush(device.id, null);
862
894
  return json(res, 200, { ok: true });
863
895
  }
864
- const endpoint = typeof body?.endpoint === "string" ? body.endpoint.trim().slice(0, 2048) : "";
865
- let u: URL;
866
- try {
867
- u = new URL(endpoint);
868
- } catch {
869
- return json(res, 400, { error: "bad endpoint" });
870
- }
871
- if (u.protocol !== "https:" && u.protocol !== "http:") return json(res, 400, { error: "bad endpoint" });
872
- state.setDevicePush(device.id, endpoint);
896
+ const endpoint = typeof body?.endpoint === "string" && body.endpoint.length <= 2048 ? body.endpoint.trim() : "";
897
+ if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
898
+ const validated = pushEndpoint(endpoint);
899
+ if (!validated) return json(res, 400, { error: "unsupported push endpoint" });
900
+ state.setDevicePush(device.id, validated.href);
873
901
  return json(res, 200, { ok: true });
874
902
  });
875
903
  }
@@ -877,7 +905,7 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
877
905
  // The owner's device roster (per room) and un-pairing. Account-token auth —
878
906
  // the same identity that approved the machine.
879
907
  if (path === "/v1/devices") {
880
- if (blocked(ipOf(req))) return json(res, 429, { error: "rate limited" });
908
+ if (blocked(clientKeyOf(req))) return json(res, 429, { error: "rate limited" });
881
909
  if (req.method !== "GET") return json(res, 405, { error: "method not allowed" });
882
910
  const account = accountByAuth(req);
883
911
  if (!account) return json(res, 401, { error: "account token required" });
@@ -893,7 +921,7 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
893
921
  });
894
922
  }
895
923
  if (path === "/v1/device/revoke") {
896
- if (blocked(ipOf(req))) return json(res, 429, { error: "rate limited" });
924
+ if (blocked(clientKeyOf(req))) return json(res, 429, { error: "rate limited" });
897
925
  if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
898
926
  const account = accountByAuth(req);
899
927
  if (!account) return json(res, 401, { error: "account token required" });
@@ -909,6 +937,7 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
909
937
  try { c.ws.close(4001, "revoked"); } catch { /* already gone */ }
910
938
  }
911
939
  }
940
+ sshd?.revoke(device.id);
912
941
  pushRoomUpdate(device.roomId);
913
942
  log(`device ${device.id} (${device.name}) revoked by ${account.name}`);
914
943
  return json(res, 200, { ok: true });
@@ -919,7 +948,7 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
919
948
  // add` this device, so system ssh logs in without a password prompt. Same two
920
949
  // identities as /v1/device/push (the device's own secret, or the owner's token).
921
950
  if (path === "/v1/device/sshkey") {
922
- if (blocked(ipOf(req))) return json(res, 429, { error: "rate limited" });
951
+ if (blocked(clientKeyOf(req))) return json(res, 429, { error: "rate limited" });
923
952
  return void readJsonBody(req).then((body) => {
924
953
  // GET carries no body: the device id rides the query, its secret a header
925
954
  // (never a query string — those end up in proxy logs).
@@ -943,6 +972,7 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
943
972
  const b64 = typeof body?.b64 === "string" ? body.b64 : "";
944
973
  if (!b64) return json(res, 400, { error: "b64 required" });
945
974
  const removed = state.removeDeviceSshKey(device.id, b64);
975
+ if (removed) sshd?.revoke(device.id, sshKeyFingerprint(b64));
946
976
  return json(res, removed ? 200 : 404, { ok: removed });
947
977
  }
948
978
  if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
@@ -971,7 +1001,9 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
971
1001
  socket.write("HTTP/1.1 421 Misdirected Request\r\n\r\n");
972
1002
  return socket.destroy();
973
1003
  }
974
- const path = new URL(req.url ?? "/", "http://x").pathname;
1004
+ let path: string;
1005
+ try { path = new URL(req.url ?? "/", "http://x").pathname; }
1006
+ catch { socket.end("HTTP/1.1 400 Bad Request\r\n\r\n"); return; }
975
1007
  if (path === "/v1/agent/ws") return upgrade(req, socket, head, "agent");
976
1008
  if (path === "/v1/device/ws") return upgrade(req, socket, head, "device");
977
1009
  if (path === "/v1/bridge/ws") return upgrade(req, socket, head, "bridge");
@@ -985,9 +1017,8 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
985
1017
  const addr = server.address();
986
1018
  const port = typeof addr === "object" && addr ? addr.port : 0;
987
1019
 
988
- // The SSH leg shares this process (state + device sockets). Disabled when ssh is
989
- // false or its port is 0.
990
- // `ssh` unset = disabled (tests and API-only runs); main.ts turns it on by default.
1020
+ // The SSH leg shares this process (state + device sockets). `ssh` unset
1021
+ // disables it; port 0 gives an ephemeral listener.
991
1022
  // The cloud resolves only once the leg is up or has failed, so callers see the truth.
992
1023
  const sshPromise: Promise<SshHandle | null> = cfg.ssh
993
1024
  ? startSshd({
@@ -1017,12 +1048,10 @@ export function startCloud(cfg: CloudConfig): Promise<Cloud> {
1017
1048
  get ssh() {
1018
1049
  return sshd;
1019
1050
  },
1020
- close: () =>
1021
- new Promise<void>((done) => {
1022
- for (const ws of wss.clients) ws.terminate();
1023
- void sshd?.close();
1024
- server.close(() => done());
1025
- }),
1051
+ close: async () => {
1052
+ for (const ws of wss.clients) ws.terminate();
1053
+ await Promise.all([sshd?.close(), new Promise<void>((done) => server.close(() => done()))]);
1054
+ },
1026
1055
  });
1027
1056
  });
1028
1057
  });