Files
web-terminal/relay-web/src/protocol.ts
Yaojia Wang 2af57e6686 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.
2026-07-02 06:10:16 +02:00

61 lines
2.5 KiB
TypeScript

/**
* Base-app WebSocket protocol shapes, as the browser bundle SPEAKS them end-to-end.
*
* The agent (P2) forwards each logical stream to the UNCHANGED web-terminal at `127.0.0.1:3000`,
* which speaks this exact JSON protocol (TECH_DOC §4 / src/protocol.ts). relay-web is a legitimate
* consumer of that contract, so it declares the minimal shapes locally — it does NOT import the
* base `src/` (that stays byte-for-byte untouched; verification greps `src` clean).
*
* These frames are the PLAINTEXT that the E2E layer (T8) later seals; in v0.8 they ride the
* passthrough transport unencrypted.
*/
/** client → server (agent → base app). `attach` MUST be the first message. */
export type ClientMessage =
| { readonly type: 'attach'; readonly sessionId: string | null; readonly cwd?: string }
| { readonly type: 'input'; readonly data: string }
| { readonly type: 'resize'; readonly cols: number; readonly rows: number }
/** server → client. */
export type ServerMessage =
| { readonly type: 'attached'; readonly sessionId: string }
| { readonly type: 'output'; readonly data: string }
| { readonly type: 'exit'; readonly code: number; readonly reason?: string }
const encoder = new TextEncoder()
const decoder = new TextDecoder()
/** Encode a client message as UTF-8 JSON bytes for `TerminalTransport.send`. */
export function encodeClientMessage(msg: ClientMessage): Uint8Array {
return encoder.encode(JSON.stringify(msg))
}
/**
* Decode UTF-8 JSON bytes into a ServerMessage. NEVER throws (mirrors base invariant #3): an
* unparseable / unknown frame yields `null` so the caller drops it rather than crashing the view.
*/
export function decodeServerMessage(bytes: Uint8Array): ServerMessage | null {
let parsed: unknown
try {
parsed = JSON.parse(decoder.decode(bytes))
} catch {
return null
}
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) return null
const obj = parsed as Record<string, unknown>
const type = obj['type']
if (type === 'attached' && typeof obj['sessionId'] === 'string') {
return { type: 'attached', sessionId: obj['sessionId'] }
}
if (type === 'output' && typeof obj['data'] === 'string') {
return { type: 'output', data: obj['data'] }
}
if (type === 'exit' && typeof obj['code'] === 'number') {
const reason = obj['reason']
return typeof reason === 'string'
? { type: 'exit', code: obj['code'], reason }
: { type: 'exit', code: obj['code'] }
}
return null
}