feat(relay): rendezvous-relay service — 7 packages + plans (contracts/transport/agent/control-plane/e2e/auth/web)

Multi-tenant reverse-tunnel service ("ngrok for Claude Code" with E2E): a
host-agent dials OUT to an operator-run relay; external devices reach the host
THROUGH the relay, routed by per-tenant subdomain, forwarding ciphertext only
(the relay never sees plaintext). Lets a customer reach their own self-hosted
web-terminal from anywhere with zero networking setup.

Packages — all tsc-strict + vitest green (656 tests), cross-package integration verified:
- relay-contracts: frozen shared contracts (mux frame codec, data model,
  capability token, E2E envelope, pairing) — the src/types.ts analog
- term-relay:   native WS mux + stateless data plane (subdomain routing, ciphertext forward)
- agent:        host-agent (pairing, per-host Ed25519 + mTLS dial-out, forwards to 127.0.0.1:3000)
- control-plane: accounts/hosts registry, pairing-code flow, routing table, provisioning
- relay-e2e:    browser<->agent E2E (X25519 ECDH through relay, AEAD, anti-replay, recoverable replay key)
- relay-auth:   Passkey/WebAuthn, capability tokens, per-host certs, deny-by-default tenant isolation
- relay-web:    browser login + Web Crypto E2E + client-side preview rendering

Security invariants INV1-15 enforced; cross-tenant isolation CI tripwire live
(.github/workflows/relay-tripwire.yml). Design + implementation-level plans in
docs/PLAN_RELAY_*.md and docs/EXPLORE_RELAY_SERVICE.md.

NOTE: generated autonomously per the reviewed plans. The security-critical
packages (relay-e2e, relay-auth) REQUIRE expert security audit before any real
deployment — passing tests prove self-consistency, not resistance to attackers.
Base app (src/, public/) unchanged; concurrent desktop work left uncommitted.
This commit is contained in:
Yaojia Wang
2026-07-02 06:10:16 +02:00
parent e4c327e25e
commit 2af57e6686
326 changed files with 40877 additions and 0 deletions

View File

@@ -0,0 +1,96 @@
/**
* T5 · Heartbeat / liveness (§4.1) — 15s PING/PONG with an injectable timer.
*
* PING carries a fresh 8-byte crypto-random token every interval; the matching PONG must echo
* the EXACT outstanding token to reset the miss counter. An unmatched/stale/replayed PONG is
* ignored (INV13-adjacent: a replayed PONG can't keep a dead/hijacked tunnel "alive").
* `HEARTBEAT_MISS_LIMIT` consecutive missed PONGs (~45s) ⇒ `onDead` fires exactly once.
*/
import { randomBytes } from 'node:crypto'
export const HEARTBEAT_INTERVAL_MS = 15_000
export const HEARTBEAT_MISS_LIMIT = 3
const TOKEN_BYTES = 8
export interface ScheduleHandle {
cancel(): void
}
export interface HeartbeatDeps {
sendPing(token: Uint8Array): void
onDead(): void
now?: () => number
schedule?: (fn: () => void, ms: number) => ScheduleHandle
intervalMs?: number
}
export interface Heartbeat {
start(): void
stop(): void
onPong(token: Uint8Array): void
}
function defaultSchedule(fn: () => void, ms: number): ScheduleHandle {
const id = setInterval(fn, ms)
return { cancel: () => clearInterval(id) }
}
function bytesEqual(a: Uint8Array, b: Uint8Array): boolean {
if (a.length !== b.length) return false
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false
}
return true
}
export function createHeartbeat(deps: HeartbeatDeps): Heartbeat {
const schedule = deps.schedule ?? defaultSchedule
const intervalMs = deps.intervalMs ?? HEARTBEAT_INTERVAL_MS
let handle: ScheduleHandle | null = null
let outstanding: Uint8Array | null = null // token awaiting a PONG, or null once matched
let misses = 0
let dead = false
function tick(): void {
if (dead) return
// If the previous PING was never answered, count a miss.
if (outstanding !== null) {
misses += 1
if (misses >= HEARTBEAT_MISS_LIMIT) {
dead = true
stop()
deps.onDead()
return
}
}
const token = new Uint8Array(randomBytes(TOKEN_BYTES))
outstanding = token
deps.sendPing(token)
}
function stop(): void {
if (handle !== null) {
handle.cancel()
handle = null
}
}
return {
start() {
if (handle !== null || dead) return
// Send the first PING immediately, then on every interval.
tick()
handle = schedule(tick, intervalMs)
},
stop,
onPong(token) {
if (dead || outstanding === null) return
if (bytesEqual(token, outstanding)) {
outstanding = null
misses = 0
}
// A stale/unknown token is ignored — it does NOT reset the miss counter.
},
}
}