Files
archived-orca/cloud/apps/relay-edge/src/splice-relay.ts
Neil 3f0c565994 relay-edge: Cloudflare Worker + Durable Objects relay (#39363)
* spike(relay-edge): local Durable Object relay prototype and de-risk harness

Stateless Worker routes control/phone/data sockets to one object per host.
Harness drives the shipped desktop control client and data transport against
wrangler dev. Not wired into builds or deploys.

* spike(relay-edge): one Durable Object per splice isolates slow phones

The host object keeps control, session and connection bookkeeping. Each
phone connection and its host data socket live in their own object and talk
to the host over DO RPC. Harness adds flood isolation, splice reset, budget
reclaim and RPC latency checks; the silent-bytes guard is measured and left
disabled.

* spike(relay-edge): Cloudflare de-risk kit (spike env, load, deploy, canary, rtt tools, runbook)

* spike(relay-edge): rebind after an object reset keeps the generation

* spike(relay-edge): pool splice objects per host to avoid object creation per phone connect

* spike(relay-edge): slow-phone flood test for Cloudflare

* relay-edge: MVP backend (credential store over Hyperdrive, JWKS, epoch check, host commands)

* relay-edge: MVP checks on local Postgres (credentials, commands, epoch, DB-down)

* relay-edge: refusal and database-latency metrics; Analytics Engine on the spike

* relay-edge: alerts cron Worker over Analytics Engine; edge.relay.orca.dev on the spike

* relay-edge: slow assignment check closes 4429, never 4401; staging env over Hyperdrive; db latency debug route

* relay-edge alerts: silence only after traffic

* relay-edge: Durable Object SQLite adapter; relay credential-store tests pass on it (P1)

* relay-edge phase 2: credential store on HostRelay SQLite, signed director push/export/erase; 17/17 in workerd

* relay-edge phase 2: Cloudflare env, push-assignments load prep, remote mode for phase-2 checks

* relay-edge: export freezes the host and is refused while connected; move-on replaces host data; stale/conflict carry the edge epoch; push location hint; admin content-length check

* relay-edge: daily store sweep on the object alarm (armed on every wake and admin call), wipe of objects whose host never arrived or left 30 days ago, erase guard ignores expired bases, admin and sweep metrics

* relay-edge: admin key renamed ADMIN_SIGNING_KEY; optional erase-only signer for the erasure job; a move onto the edge also voids audit rows from an earlier stay

* relay-edge: phase2 test env names the erasure issuer

* relay-edge: director ⇄ cell ⇄ edge round trip with the shipped desktop and phone clients

* relay-edge: round trip resolves the live phone first (a failed resolve rate-limits the host)

* relay-edge: per-source pre-auth limits; phones for unpushed hosts answered without storage

Phone and host data upgrades are counted per source (CF-Connecting-IP, IPv6 by /64)
through a Rate Limiting binding, failing open. A phone for a host the director never
pushed (or one since wiped) gets relay-hello 4409 from the Worker, so neither the host
nor a splice object writes storage. Local 25/25, phase2 21/21.

* relay-edge: production env on edge.relay.orca.dev; object schema version; frame caps; place on metrics

- production env (orca-relay-edge): custom domain edge.relay.orca.dev moved off the
  spike env; object backend, production auth JWKS, no debug/reset routes.
- Object storage carries a schema version; code older than an object's schema refuses
  it; a test pins the version to a hash of the tables so changes need a migration.
- Frames over the cells' limits close 1009 (control 1 MiB, splices 8 MiB).
- Metrics carry the client's ingress '<continent>/<colo>'.
Local 26/26, phase2 22/22, unit 24/24.

* relay-edge: production alerts env (reads the production dataset)

* relay-edge: keep only local dev config in this repo

Deployed environments (Worker names, hostname, account id, dataset names,
rate limits, Hyperdrive id) move to the private deploy config. The local
config and tests are unchanged.

* cloud: keep relay-edge out of the pnpm workspace (own npm lockfile)

* Revert "cloud: keep relay-edge out of the pnpm workspace (own npm lockfile)"

This reverts commit a55955d3c023964ccd2dfb644e2d09ca67322722.

* cloud: relay-edge as a pnpm workspace member (lockfile; workerd binary-only)

* relay-edge: follow main's relay module moves; alias workspace packages by directory so subpath imports resolve

* relay-edge: splice byte counters on the socket attachment (survive hibernation)

* relay-edge: keep deployed-target load tools out of the public tree; abort routes only on the spike backend

The local harness (spike/run-all.ts, lib.ts, keys.ts) stays. The tools that drive a deployed
Worker and the Cloudflare runbook live with the private deploy config.

* relay-edge: satisfy the changed-lines lint gate

Braces on every if, type aliases over interfaces, template strings, no explicit any,
!== -1 index checks, and a justified max-lines header on the per-host object, the splice
object and the end-to-end harnesses. No behaviour change: unit 19/19, object MVP 26/26.

* relay-edge: split the host object and harnesses under the 300-line limit

HostRelay is layered base → handshake → control → phones → HostRelay (one object, one state;
method bodies moved verbatim, private → protected). SpliceRelay's state types move to
splice-state.ts. The harnesses' helpers move to sibling modules. No max-lines disables.
Unit 19/19, object MVP 26/26, local spike harness 19/19.

* relay-edge: run its vitest suite from cloud's pnpm -r test and register the runner
2026-11-07 18:47:21 -08:00

346 lines
14 KiB
TypeScript

// Pooled splice objects: each host has up to SPLICE_SLOTS of them (`<relayHostId>.<slot>`), reused
// across phone connects so a connect never pays for creating a new object. An object holds the
// phone socket and host data socket of every splice in its slot (normally one; more only when the
// host's slot hint collides) and forwards frames between them. A slow or flooding phone can only
// exhaust (or get reset) its own slot's object, never the host's control socket or other slots.
import { DurableObject } from 'cloudflare:workers'
import { HostDataAuthSchema } from '../../../packages/relay-contract/src/control-messages'
import { RelayAuthSchema } from '../../../packages/relay-contract/src/credential-messages'
import { RELAY_CLOSE_CODE } from '../../../packages/relay-contract/src/close-codes'
import { RELAY_PROTOCOL_LIMITS } from '../../../packages/relay-contract/src/protocol-limits'
import { MESSAGE_TOO_BIG, frameOver, timingSafeEqualStr } from './bytes'
import type { Env } from './env'
import { metric } from './metrics'
import { parseConnId } from './splice-slot'
import { KEY_PREFIX, NO_DEADLINE, type GuardAttachment, type SocketKind, type State } from './splice-state'
export class SpliceRelay extends DurableObject<Env> {
async fetch(request: Request): Promise<Response> {
const kind = request.headers.get('x-edge-kind')
if (kind === 'abort' && this.env.SPIKE_ADMIN === '1') { this.ctx.abort('spike reset') }
const connId = request.headers.get('x-edge-conn-id') ?? ''
const parsed = parseConnId(connId)
if (!parsed) { return new Response(null, { status: 404 }) }
const pair = new WebSocketPair()
const [client, server] = Object.values(pair) as [WebSocket, WebSocket]
const now = Date.now()
if (kind === 'phone') {
if ((await this.state(connId)) !== undefined) { return new Response(null, { status: 409 }) }
await this.sweepOrphans()
this.ctx.acceptWebSocket(server, ['phone', connId])
const place = request.headers.get('x-edge-place') ?? undefined
metric(this.env, 'phone_open', 1, '', [], '', place)
server.serializeAttachment({ silentBytes: 0, windowStart: now, place } satisfies GuardAttachment)
await this.save({
connId,
relayHostId: parsed.relayHostId,
phase: 'auth',
deadline: now + RELAY_PROTOCOL_LIMITS.firstFrameDeadlineMs
})
} else if (kind === 'data') {
const state = await this.state(connId)
if (!state || state.phase !== 'pending' || this.socket(connId, 'data')) {
// Unknown or already-attached connection: same answer as a bad ticket.
this.ctx.acceptWebSocket(server, ['reject'])
server.close(RELAY_CLOSE_CODE.BAD_OUTER_CREDENTIAL, 'invalid host data ticket')
return new Response(null, { status: 101, webSocket: client })
}
this.ctx.acceptWebSocket(server, ['data', connId])
server.serializeAttachment({ silentBytes: 0, windowStart: now, place: request.headers.get('x-edge-place') ?? undefined } satisfies GuardAttachment)
} else {
return new Response(null, { status: 404 })
}
await this.reschedule()
return new Response(null, { status: 101, webSocket: client })
}
async webSocketMessage(ws: WebSocket, message: string | ArrayBuffer): Promise<void> {
const id = this.identify(ws)
if (!id) { return }
const state = await this.state(id.connId)
if (!state) { return }
if (state.phase === 'spliced') { return this.forward(ws, id.connId, id.kind, message) }
if (id.kind === 'phone') {
if (state.phase === 'auth') { await this.onRelayAuth(ws, state, message) }
return // pending: the phone waits for relay-hello before sending E2EE frames
}
await this.onHostDataAuth(ws, state, message)
}
async webSocketClose(ws: WebSocket, code: number): Promise<void> {
await this.teardown(ws, code)
}
async webSocketError(ws: WebSocket): Promise<void> {
await this.teardown(ws, 1006)
}
// ---------------- RPC from HostRelay ----------------
async closeSplice(connId: string, code: number, reason: string): Promise<void> {
for (const ws of this.ctx.getWebSockets(connId)) { this.close(ws, code, reason) }
await this.forget(connId)
await this.reschedule()
}
async isAlive(connId: string): Promise<boolean> {
if ((await this.state(connId)) === undefined) { return false }
if (this.socket(connId, 'phone') !== null) { return true }
await this.forget(connId) // its sockets died in a reset; drop the leftover state
await this.reschedule()
return false
}
// ---------------- phone auth ----------------
private async onRelayAuth(ws: WebSocket, state: State, message: string | ArrayBuffer): Promise<void> {
if (typeof message !== 'string') {
this.close(ws, RELAY_CLOSE_CODE.BAD_OUTER_CREDENTIAL, 'binary relay-auth')
return
}
let credential: string
try {
const { type, ...rest } = JSON.parse(message) as Record<string, unknown>
const auth = RelayAuthSchema.safeParse(rest)
if (type !== 'relay-auth' || !auth.success) { throw new Error('relay-auth') }
credential = auth.data.credential
} catch {
this.phoneFail(ws, RELAY_CLOSE_CODE.BAD_OUTER_CREDENTIAL)
return
}
const started = Date.now()
const host = this.host(state.relayHostId)
let result: Awaited<ReturnType<typeof host.authorizePhone>>
try {
result = await host.authorizePhone(state.connId, credential)
} catch {
// Transient edge failure must never look like a bad credential (4401 disables phones).
this.phoneFail(ws, RELAY_CLOSE_CODE.DRAINING)
return
}
if (this.env.SPIKE_ADMIN === '1') { console.log(`spike_rpc_authorize_ms=${Date.now() - started}`) }
if (!result.ok) {
this.phoneFail(ws, result.code, result.reason)
return
}
await this.save({
...state,
phase: 'pending',
connTicket: result.connTicket,
generation: result.generation,
deadline: Date.now() + RELAY_PROTOCOL_LIMITS.hostAttachDeadlineMs
})
await this.reschedule()
}
// ---------------- host data auth ----------------
private async onHostDataAuth(ws: WebSocket, state: State, message: string | ArrayBuffer): Promise<void> {
let ok = false
if (typeof message === 'string' && state.phase === 'pending') {
try {
const { type, ...body } = JSON.parse(message) as Record<string, unknown>
const auth = HostDataAuthSchema.safeParse(body)
ok =
type === 'host-data-auth' &&
auth.success &&
timingSafeEqualStr(auth.data.connTicket, state.connTicket ?? '') &&
auth.data.generation === state.generation
} catch {
ok = false
}
}
const phone = this.socket(state.connId, 'phone')
if (!ok || !phone) {
this.close(ws, RELAY_CLOSE_CODE.BAD_OUTER_CREDENTIAL, 'invalid host data ticket')
return
}
// The host object persists the connection basis, then hands back the phone's relay-hello.
const confirmed = await this.host(state.relayHostId)
.confirmSplice(state.connId, state.generation ?? -1)
.catch(() => ({ ok: false as const, code: RELAY_CLOSE_CODE.LIMIT_EXCEEDED }))
if (!confirmed.ok) {
if (confirmed.code === RELAY_CLOSE_CODE.LIMIT_EXCEEDED) {
// As the cells when the basis write fails: both legs 4429.
this.phoneFail(phone, RELAY_CLOSE_CODE.LIMIT_EXCEEDED)
this.close(ws, RELAY_CLOSE_CODE.LIMIT_EXCEEDED, 'basis persistence failed')
await this.forget(state.connId)
await this.reschedule()
} else {
this.close(ws, RELAY_CLOSE_CODE.BAD_OUTER_CREDENTIAL, 'invalid host data ticket')
}
return
}
await this.save({ ...state, phase: 'spliced', connTicket: undefined, deadline: NO_DEADLINE })
this.send(phone, { type: 'relay-hello', ...confirmed.hello })
await this.reschedule()
}
// ---------------- splice ----------------
private forward(from: WebSocket, connId: string, kind: SocketKind, message: string | ArrayBuffer): void {
if (frameOver(message, RELAY_PROTOCOL_LIMITS.maxFrameBytes)) {
for (const ws of this.ctx.getWebSockets(connId)) { this.close(ws, MESSAGE_TOO_BIG, 'frame too large') }
return
}
const peer = this.socket(connId, kind === 'phone' ? 'data' : 'phone')
if (!peer) { return }
// The sender spoke, so its own inbound direction is evidently being read.
const fromGuard = from.deserializeAttachment() as GuardAttachment
const size = typeof message === 'string' ? message.length : message.byteLength
from.serializeAttachment({
...fromGuard,
...(fromGuard.silentBytes !== 0 ? { silentBytes: 0, windowStart: Date.now() } : {}),
sentBytes: (fromGuard.sentBytes ?? 0) + size,
sentFrames: (fromGuard.sentFrames ?? 0) + 1
})
const budget = Number(this.env.SPLICE_SILENT_BYTES_MAX ?? '0')
if (budget > 0) {
const g = peer.deserializeAttachment() as GuardAttachment
const windowMs = Number(this.env.SPLICE_SILENT_WINDOW_MS ?? '10000')
const now = Date.now()
const next =
now - g.windowStart > windowMs
? { ...g, silentBytes: size, windowStart: now }
: { ...g, silentBytes: g.silentBytes + size }
peer.serializeAttachment(next)
if (next.silentBytes > budget) {
for (const ws of this.ctx.getWebSockets(connId)) { this.close(ws, RELAY_CLOSE_CODE.LIMIT_EXCEEDED, 'splice flood') }
return
}
}
try {
peer.send(message)
} catch {
// Peer closing; teardown follows.
}
}
// ---------------- lifecycle ----------------
private async teardown(ws: WebSocket, code: number): Promise<void> {
const id = this.identify(ws)
if (!id) { return }
const state = await this.state(id.connId)
if (!state) { return }
// [bytes to host, frames to host, bytes to phone, frames to phone], from each side's attachment.
const sent = (k: SocketKind) => {
const peer = k === id.kind ? ws : this.socket(id.connId, k)
const g = peer?.deserializeAttachment() as GuardAttachment | undefined
return [g?.sentBytes ?? 0, g?.sentFrames ?? 0]
}
const counters = [...sent('phone'), ...sent('data')]
const place = this.place(ws)
metric(this.env, 'phone_close', 1, `${id.kind}:${code}`, [], '', place)
metric(this.env, 'splice_bytes', counters[0] + counters[2], '', counters, '', place)
const reason = code === 1000 ? '' : 'peer dropped'
for (const other of this.ctx.getWebSockets(id.connId)) {
if (other !== ws) { this.close(other, RELAY_CLOSE_CODE.PEER_DROPPED, reason) }
}
await this.forget(id.connId)
await this.reschedule()
await this.host(state.relayHostId).spliceClosed(state.connId).catch(() => {})
}
async alarm(): Promise<void> {
const now = Date.now()
for (const state of await this.states()) {
if (now < state.deadline) { continue }
const phone = this.socket(state.connId, 'phone')
if (state.phase === 'pending') {
// OTA phones redial this edge once, quickly: the control is still seated here.
if (phone) { this.phoneFail(phone, RELAY_CLOSE_CODE.HOST_OFFLINE, 'attach-timeout') }
} else if (phone) {
this.close(phone, RELAY_CLOSE_CODE.BAD_OUTER_CREDENTIAL, 'first frame timeout')
}
const data = this.socket(state.connId, 'data')
if (data) { this.close(data, RELAY_CLOSE_CODE.BAD_OUTER_CREDENTIAL, 'first frame timeout') }
await this.forget(state.connId)
await this.host(state.relayHostId).spliceClosed(state.connId).catch(() => {})
}
await this.reschedule()
}
private async reschedule(): Promise<void> {
const next = Math.min(NO_DEADLINE, ...(await this.states()).map((s) => s.deadline))
if (next === NO_DEADLINE) {
await this.ctx.storage.deleteAlarm()
return
}
await this.ctx.storage.setAlarm(Math.max(next, Date.now() + 1))
}
// ---------------- helpers ----------------
private async state(connId: string): Promise<State | undefined> {
return this.ctx.storage.get<State>(KEY_PREFIX + connId)
}
private async states(): Promise<State[]> {
return [...(await this.ctx.storage.list<State>({ prefix: KEY_PREFIX })).values()]
}
private async save(state: State): Promise<void> {
await this.ctx.storage.put<State>(KEY_PREFIX + state.connId, state)
}
// A reset drops sockets without close events. Every live splice has its phone socket from the
// start, so state without one is left over and can go (the host prunes its own bookkeeping).
private async sweepOrphans(): Promise<void> {
for (const state of await this.states()) {
if (this.socket(state.connId, 'phone') === null) { await this.forget(state.connId) }
}
}
// Closed splices are deleted, not kept: the object is reused and connIds are never reused.
private async forget(connId: string): Promise<void> {
await this.ctx.storage.delete(KEY_PREFIX + connId)
}
private host(relayHostId: string) {
return this.env.HOST_RELAY.get(this.env.HOST_RELAY.idFromName(relayHostId))
}
private identify(ws: WebSocket): { kind: SocketKind; connId: string } | null {
const [kind, connId] = this.ctx.getTags(ws)
if ((kind !== 'phone' && kind !== 'data') || !connId) { return null }
return { kind, connId }
}
private socket(connId: string, kind: SocketKind): WebSocket | null {
return this.ctx.getWebSockets(connId).find((ws) => this.ctx.getTags(ws)[0] === kind) ?? null
}
// As the cells' rejectClient: a strict relay-hello with only ok/code, the reason on the close.
private phoneFail(ws: WebSocket, code: number, reason?: string): void {
this.send(ws, { type: 'relay-hello', ok: false, code })
this.close(ws, code, reason ?? 'relay connection rejected')
}
private send(ws: WebSocket, payload: Record<string, unknown>): void {
try {
ws.send(JSON.stringify(payload))
} catch {
// Socket already closing.
}
}
private place(ws: WebSocket): string | undefined {
try {
return (ws.deserializeAttachment() as GuardAttachment | null)?.place
} catch {
return undefined
}
}
private close(ws: WebSocket, code: number, reason: string): void {
const kind = this.identify(ws)?.kind ?? 'splice'
metric(this.env, 'refuse', 1, `${kind}:${code}`, [], reason, this.place(ws))
try {
ws.close(code, reason)
} catch {
// Already closed.
}
}
}