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.
68 lines
2.5 KiB
TypeScript
68 lines
2.5 KiB
TypeScript
import { describe, test, expect } from 'vitest'
|
|
import {
|
|
initialStreamState,
|
|
nextStreamState,
|
|
isTerminal,
|
|
type StreamState,
|
|
type StreamTransition,
|
|
} from '../mux/stream.js'
|
|
|
|
function legal(t: StreamTransition): StreamState {
|
|
if ('illegal' in t) throw new Error('expected legal transition')
|
|
return t.next
|
|
}
|
|
|
|
describe('stream state machine (T3, §4.1 lifecycle)', () => {
|
|
test('legal path: idle --OPEN--> open --DATA*--> open --CLOSE--> closed', () => {
|
|
let s = initialStreamState()
|
|
expect(s).toBe('idle')
|
|
s = legal(nextStreamState(s, 'open', false, 'outbound'))
|
|
expect(s).toBe('open')
|
|
s = legal(nextStreamState(s, 'data', false, 'outbound'))
|
|
s = legal(nextStreamState(s, 'data', false, 'inbound'))
|
|
expect(s).toBe('open')
|
|
s = legal(nextStreamState(s, 'close', true, 'outbound'))
|
|
expect(s).toBe('closed')
|
|
expect(isTerminal(s)).toBe(true)
|
|
})
|
|
|
|
test('DATA with fin half-closes the SENDING direction; both-side FIN ⇒ closed', () => {
|
|
const outFin = legal(nextStreamState('open', 'data', true, 'outbound'))
|
|
expect(outFin).toBe('halfClosedLocal')
|
|
const inFin = legal(nextStreamState('open', 'data', true, 'inbound'))
|
|
expect(inFin).toBe('halfClosedRemote')
|
|
// The other side FINs ⇒ fully closed.
|
|
expect(legal(nextStreamState('halfClosedLocal', 'data', true, 'inbound'))).toBe('closed')
|
|
expect(legal(nextStreamState('halfClosedRemote', 'data', true, 'outbound'))).toBe('closed')
|
|
})
|
|
|
|
test('illegal: DATA before OPEN', () => {
|
|
expect(nextStreamState('idle', 'data', false, 'inbound')).toEqual({ illegal: true })
|
|
})
|
|
|
|
test('illegal: any frame after closed', () => {
|
|
for (const t of ['data', 'close', 'windowUpdate', 'open'] as const) {
|
|
expect(nextStreamState('closed', t, false, 'inbound')).toEqual({ illegal: true })
|
|
}
|
|
})
|
|
|
|
test('illegal: OPEN on an already-open stream', () => {
|
|
expect(nextStreamState('open', 'open', false, 'inbound')).toEqual({ illegal: true })
|
|
})
|
|
|
|
test('illegal: CLOSE before OPEN', () => {
|
|
expect(nextStreamState('idle', 'close', false, 'inbound')).toEqual({ illegal: true })
|
|
})
|
|
|
|
test('WINDOW_UPDATE keeps state and is legal while flowing', () => {
|
|
expect(legal(nextStreamState('open', 'windowUpdate', false, 'inbound'))).toBe('open')
|
|
expect(legal(nextStreamState('halfClosedLocal', 'windowUpdate', false, 'inbound'))).toBe(
|
|
'halfClosedLocal',
|
|
)
|
|
})
|
|
|
|
test('illegal: re-FIN on the already-closed direction', () => {
|
|
expect(nextStreamState('halfClosedLocal', 'data', true, 'outbound')).toEqual({ illegal: true })
|
|
})
|
|
})
|