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.
63 lines
3.1 KiB
TypeScript
63 lines
3.1 KiB
TypeScript
import { describe, test, expect } from 'vitest'
|
||
import { createMemoryStores } from '../src/store/memory.js'
|
||
import { createHostRegistry } from '../src/registry/hosts.js'
|
||
import { createMeteringCollector, MeteringError } from '../src/metering/collect.js'
|
||
import { fingerprint } from '../src/ca/fingerprint.js'
|
||
import { generateEd25519 } from '../src/util/crypto.js'
|
||
import type { NodeIdentity } from '../src/node-auth/identity.js'
|
||
|
||
const node: NodeIdentity = { nodeId: 'spiffe://relay/node-a' }
|
||
const ACCOUNT = '11111111-1111-4111-8111-111111111111'
|
||
|
||
async function withHost() {
|
||
const stores = createMemoryStores()
|
||
const hosts = createHostRegistry({ hosts: stores.hosts })
|
||
const { publicKeyRaw } = generateEd25519()
|
||
const host = await hosts.bindHost({ accountId: ACCOUNT, subdomain: 'alice', agentPubkey: publicKeyRaw, enrollFpr: fingerprint(publicKeyRaw) })
|
||
const collector = createMeteringCollector({ metering: stores.metering, hosts })
|
||
return { stores, hosts, host, collector }
|
||
}
|
||
|
||
describe('T12 metering (INV1/INV3-analog/INV8)', () => {
|
||
test('ingestSample derives accountId + nodeId server-side; append-only', async () => {
|
||
const { stores, host, collector } = await withHost()
|
||
await collector.ingestSample(node, { hostId: host.hostId, concurrentViewers: 3, sampledAt: new Date().toISOString() })
|
||
const rows = await stores.metering.query(ACCOUNT, '1970-01-01T00:00:00.000Z', '2999-01-01T00:00:00.000Z')
|
||
expect(rows.length).toBe(1)
|
||
expect(rows[0]?.accountId).toBe(ACCOUNT)
|
||
expect(rows[0]?.nodeId).toBe(node.nodeId)
|
||
})
|
||
|
||
test('forged attribution: unknown hostId rejected (cross-tenant metering impossible, INV1)', async () => {
|
||
const { collector } = await withHost()
|
||
await expect(
|
||
collector.ingestSample(node, { hostId: '99999999-9999-4999-8999-999999999999', concurrentViewers: 1, sampledAt: new Date().toISOString() }),
|
||
).rejects.toBeInstanceOf(MeteringError)
|
||
})
|
||
|
||
test('pairedHostCount excludes revoked hosts', async () => {
|
||
const { hosts, host, collector } = await withHost()
|
||
expect(await collector.pairedHostCount(ACCOUNT)).toBe(1)
|
||
await hosts.setHostStatus(host.hostId, 'revoked')
|
||
expect(await collector.pairedHostCount(ACCOUNT)).toBe(0)
|
||
})
|
||
|
||
test('rollupUsage computes viewer peak + viewer-hours over a window', async () => {
|
||
const { host, collector } = await withHost()
|
||
const t0 = '2026-06-30T00:00:00.000Z'
|
||
const t1 = '2026-06-30T01:00:00.000Z'
|
||
const to = '2026-06-30T02:00:00.000Z'
|
||
await collector.ingestSample(node, { hostId: host.hostId, concurrentViewers: 2, sampledAt: t0 })
|
||
await collector.ingestSample(node, { hostId: host.hostId, concurrentViewers: 4, sampledAt: t1 })
|
||
const usage = await collector.rollupUsage(ACCOUNT, t0, to)
|
||
expect(usage.viewerPeak).toBe(4)
|
||
expect(usage.viewerHours).toBeCloseTo(2 * 1 + 4 * 1, 5) // 2 viewers×1h + 4 viewers×1h
|
||
expect(usage.pairedHostPeak).toBe(1)
|
||
})
|
||
|
||
test('malformed sample → Zod error', async () => {
|
||
const { collector } = await withHost()
|
||
await expect(collector.ingestSample(node, { hostId: 'not-a-uuid', concurrentViewers: -1, sampledAt: 'x' })).rejects.toBeInstanceOf(MeteringError)
|
||
})
|
||
})
|