diff --git a/README.md b/README.md index 81b2d55..05f9fd1 100644 --- a/README.md +++ b/README.md @@ -72,23 +72,48 @@ them over mDNS, and one typed by its IP address goes through the server too. On video goes from the host to the browser directly; only the host's management API passes through the container, on the page's own origin, which is what Safari needs. -Two other shapes, in [`deploy/`](deploy): behind a reverse proxy that already has a certificate -(`compose.proxy.yaml`), and on a tailnet with Tailscale's certificate, which also lets you play -away from home (`compose.tailscale.yaml`). +Three other shapes, in [`deploy/`](deploy): behind a reverse proxy that already has a certificate +(`compose.proxy.yaml`), on a tailnet with Tailscale's certificate (`compose.tailscale.yaml`), and +at your own domain with Caddy in front (`compose.public.yaml`). The last two let you play away +from home. | Setting | Default | | |---|---|---| | `LISTEN` | `0.0.0.0:8443` | | | `TLS` | `self-signed` | `self-signed`, `off` behind a proxy, or `cert.pem,key.pem` | -| `TLS_NAMES` | | Domains the page is opened at. It answers only to these, IP addresses and names no public DNS holds (`localhost`, `desk`, `.local`, `.home.arpa`, `.internal`); with your own certificate and none listed, to any name. The self-signed certificate covers them too | +| `TLS_NAMES` | | The names and addresses you open the page at, which the self-signed certificate covers. The page answers only to these, IP addresses and names no public DNS holds (`localhost`, `desk`, `.local`, `.home.arpa`, `.internal`); with your own certificate and none listed, to any name | | `PUNKTFUNK_HOSTS` | | Hosts mDNS cannot find: `name=address[:port][#fingerprint]`, comma-separated | | `DISCOVER` | on | `0` turns the mDNS browse off | -| `ADD_HOSTS` | on | `0` stops the page reaching a host typed by address. Set it when the page is reachable from outside your network | +| `ADD_HOSTS` | on | `0` stops the page reaching a host typed by its address. Either way, the server relays to a typed address only once a Punktfunk host has proven itself there | The server pins every host before trusting it, as the native clients do: by the fingerprint it was listed with, the one it announces, or the one it presented first (`/data/pins.json`). It adds no credential of its own; the browser's device key stays the only one. +## From outside your network + +Tailscale is the simplest way. Put this machine and your hosts on a tailnet, and the page opens +wherever your devices are on the tailnet. It takes two files from [`deploy/`](deploy), +`compose.tailscale.yaml` and `tailscale-serve.json`, side by side. Before the first start, set +`TLS_NAMES` in the compose file to the page's tailnet name, and `PUNKTFUNK_HOSTS` to your hosts +by their tailnet addresses, since hosts don't announce themselves across a tailnet. + +Without Tailscale, give the page a domain with a real certificate. `compose.public.yaml` puts +Caddy in front, which gets and renews the certificate for you: + +```sh +DOMAIN=play.example.com docker compose -f compose.public.yaml up -d +``` + +Point the domain at your home's address and forward TCP 80 and 443 on your router to this machine. +Keep the hosts' own ports closed: a browser outside your network streams through the server. + +Forwarding the port of the self-signed setup does not work. From the internet, a browser can't +tell a self-made certificate from an impostor's, so the server shows a browser at a public +address a page that says so instead of the client. + +A browser that isn't paired with a host is rate-limited and can't wake that host. + ## Trust, and the one step it costs you Served by the container, a page reaches its hosts on its own origin and none of this section's diff --git a/apps/web/src/app.ts b/apps/web/src/app.ts index 48a8874..1992977 100644 --- a/apps/web/src/app.ts +++ b/apps/web/src/app.ts @@ -23,6 +23,7 @@ import { type LibraryEntry, type ListedProfile, linkFor, + onPass, packaged, type PageLink, pageLog, @@ -35,9 +36,11 @@ import { reachTarget, SEAT_POLL_MS, seatGate, + serverFetch, type Settings, settings, statusOf, + TOO_MANY, VersionSkew, wakeFailed, wakingLine, @@ -165,6 +168,8 @@ class App { /** Whether that server also proxies a host typed here by its IP address. */ private readonly viaServer: boolean, ) { + // A login's pass covers another host: the server now tells this browser its address and wake. + onPass(() => void this.refreshConfigured()); ui.mount({ connect: (address) => { this.adding = false; @@ -487,6 +492,13 @@ class App { void this.probe(cards.map((h) => h.origin)); } + /** The server's list again, as a pass holder sees it. */ + private async refreshConfigured(): Promise { + const again = await configuredHosts(); + for (const [origin, c] of again.listed) this.configured.set(origin, c); + this.redrawLists(); + } + /** Every host this page knows: the server's first, under the name it gives them; then the ones * typed here. Each with what a probe last said about it. */ private cards(): HostCard[] { @@ -496,7 +508,7 @@ class App { ...known.get(origin), origin, name: c.name, - plane: c.plane, + ...(c.plane ? { plane: c.plane } : {}), ...(c.wake ? { wake: c.wake, waking: this.waking.has(origin) } : {}), })), ...[...known.values()].filter((h) => !this.configured.has(h.origin)), @@ -539,7 +551,7 @@ class App { if (known) { const { origin: _old, ...record } = known; hosts.forget(address); - hosts.remember(routed.api, { ...record, plane: routed.plane }); + hosts.remember(routed.api, { ...record, ...(routed.plane ? { plane: routed.plane } : {}) }); } return routed; } @@ -646,7 +658,11 @@ class App { this.waking.add(origin); this.redrawLists(); try { - const r = await fetch(url, { method: "POST" }); + const r = await serverFetch()(url, { method: "POST" }); + if (r.status === 403 || r.status === 429) { + const why = r.status === 429 ? sentence(TOO_MANY) : `Only a browser paired with ${this.nameOf(origin)} can wake it from this page.`; + return this.ui.notify(why, "error"); + } if (!r.ok) throw new Error(`wake refused (${r.status})`); let up = false; for (let tries = 0; tries < 20 && !up; tries++) { @@ -1323,9 +1339,9 @@ interface Configured extends HostTarget { */ async function configuredHosts(): Promise<{ listed: Map; viaServer: boolean }> { try { - const r = await fetch("./config.json", { cache: "no-store" }); + const r = await serverFetch()("./config.json", { cache: "no-store" }); const c = (await r.json()) as { - hosts?: Array<{ name: string; api: string; plane: string; wake?: string; bridge?: string }>; + hosts?: Array<{ name: string; api: string; plane?: string; wake?: string; bridge?: string }>; add?: boolean; }; const listed = new Map( @@ -1333,7 +1349,8 @@ async function configuredHosts(): Promise<{ listed: Map; via const api = new URL(h.api, location.href).href.replace(/\/+$/, ""); const wake = h.wake ? new URL(h.wake, location.href).href : undefined; const bridge = h.bridge ? { bridge: h.bridge } : {}; - return [api, { api, plane: h.plane, name: h.name, ...(wake ? { wake } : {}), ...bridge }]; + const plane = h.plane ? { plane: h.plane } : {}; + return [api, { api, ...plane, name: h.name, ...(wake ? { wake } : {}), ...bridge }]; }), ); return { listed, viaServer: c.add === true }; diff --git a/deploy/compose.proxy.yaml b/deploy/compose.proxy.yaml index a6881f5..51c20ce 100644 --- a/deploy/compose.proxy.yaml +++ b/deploy/compose.proxy.yaml @@ -2,10 +2,11 @@ # proxy's certificate, so no browser warns. Point the proxy at http://:8080. # # The page's origin is your proxy's domain: add it to each host's Browser origins (console, -# Streaming, advanced), or leave that list empty to allow any page. +# Streaming, advanced). A browser on a domain that isn't listed there has to pair with a PIN. # -# On networks that block UDP the page streams through this server. The host then sees every -# such stream as coming from the proxy's address, so it treats it as local. +# The server tells each host who the visitor is, from the X-Forwarded-For or Forwarded header the +# proxy adds (nginx: `proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;`). A proxy +# that sends neither leaves every visitor unknown, which a host treats as the internet. services: punktfunk-web: image: git.unom.io/unom/punktfunk-client-web:latest diff --git a/deploy/compose.public.yaml b/deploy/compose.public.yaml new file mode 100644 index 0000000..85a886e --- /dev/null +++ b/deploy/compose.public.yaml @@ -0,0 +1,50 @@ +# The browser client at your own domain, with a real certificate, reachable from anywhere. +# +# DOMAIN=play.example.com docker compose -f compose.public.yaml up -d +# open https://play.example.com +# +# Point the domain at your home's public address and forward TCP 80 and 443 on the router to this +# machine; Caddy gets and renews the certificate on its own. Keep the hosts' own ports closed on +# the router: a browser outside your network streams through this server, never straight to a host. +services: + caddy: + image: caddy:2 + restart: unless-stopped + # Listens on 80 and 443 of this machine. + network_mode: host + configs: + - source: caddyfile + target: /etc/caddy/Caddyfile + volumes: + - caddy-data:/data + - caddy-config:/config + + punktfunk-web: + image: git.unom.io/unom/punktfunk-client-web:latest + restart: unless-stopped + # The host's network, so the server finds hosts over mDNS and can wake them. It listens on + # loopback only: everything from outside comes through Caddy. + network_mode: host + environment: + TLS: "off" + LISTEN: 127.0.0.1:8080 + TLS_NAMES: ${DOMAIN:?set DOMAIN to the page's domain} + # Hosts mDNS cannot find: name=address[:port][#fingerprint], comma-separated. + # PUNKTFUNK_HOSTS: desk=192.168.1.21 + volumes: + - punktfunk-web:/data + +# Caddy passes the browser's Host header through and sets X-Forwarded-For itself, over whatever +# the browser sent. HSTS belongs here, where the name is the page's own. +configs: + caddyfile: + content: | + ${DOMAIN} { + header Strict-Transport-Security "max-age=31536000" + reverse_proxy 127.0.0.1:8080 + } + +volumes: + caddy-data: + caddy-config: + punktfunk-web: diff --git a/deploy/compose.yaml b/deploy/compose.yaml index 2b100e1..1fdfdcf 100644 --- a/deploy/compose.yaml +++ b/deploy/compose.yaml @@ -3,6 +3,9 @@ # docker compose up -d # open https://:8443 and accept its certificate once # +# The self-made certificate is for your own network. A browser at a public address gets a page +# saying so and nothing else; from outside, use compose.tailscale.yaml or compose.public.yaml. +# # Each host also needs Browser streaming on (its console, Streaming settings) and UDP 9778 open: # video goes from the host to the browser directly, never through this container. services: diff --git a/packages/stream/src/carrier/frame.ts b/packages/stream/src/carrier/frame.ts index 2afd9cf..f74a7fc 100644 --- a/packages/stream/src/carrier/frame.ts +++ b/packages/stream/src/carrier/frame.ts @@ -7,16 +7,28 @@ // 2 host → browser one whole unidirectional stream // 3 both u32 close code, then the UTF-8 reason // 4 both ping: f64 sender ms; pong: the same f64, then u32 the bridge's RTT in µs +// 5 browser → host first: the 32-byte pass, for the paired pool +// 6 browser → host first, empty: a browser without a pass for this host, asking to pair -export const TAG = { DATAGRAM: 0, CONTROL: 1, UNI: 2, CLOSE: 3, PING: 4 } as const; +export const TAG = { DATAGRAM: 0, CONTROL: 1, UNI: 2, CLOSE: 3, PING: 4, PASS: 5, PAIR: 6 } as const; export type Frame = | { tag: typeof TAG.DATAGRAM | typeof TAG.CONTROL | typeof TAG.UNI; bytes: Uint8Array } | { tag: typeof TAG.CLOSE; code: number; reason: string } - | { tag: typeof TAG.PING; sentMs: number; bridgeRttUs?: number }; + | { tag: typeof TAG.PING; sentMs: number; bridgeRttUs?: number } + | { tag: typeof TAG.PASS; pass: Uint8Array } + | { tag: typeof TAG.PAIR }; export function encode(f: Frame): Uint8Array { switch (f.tag) { + case TAG.PASS: { + const out = new Uint8Array(33); + out[0] = f.tag; + out.set(f.pass, 1); + return out; + } + case TAG.PAIR: + return new Uint8Array([f.tag]); case TAG.CLOSE: { const reason = new TextEncoder().encode(f.reason); const out = new Uint8Array(5 + reason.length); @@ -59,6 +71,10 @@ export function decode(msg: Uint8Array): Frame | null { if (msg.length === 9) return { tag, sentMs: view.getFloat64(1) }; if (msg.length === 13) return { tag, sentMs: view.getFloat64(1), bridgeRttUs: view.getUint32(9) }; return null; + case TAG.PASS: + return msg.length === 33 ? { tag, pass: msg.subarray(1) } : null; + case TAG.PAIR: + return msg.length === 1 ? { tag } : null; default: return null; } diff --git a/packages/stream/src/carrier/ws.test.ts b/packages/stream/src/carrier/ws.test.ts index d298bc4..f6fb70e 100644 --- a/packages/stream/src/carrier/ws.test.ts +++ b/packages/stream/src/carrier/ws.test.ts @@ -55,10 +55,10 @@ function fakeFeed() { } /** A carrier whose socket has opened, the socket, and what reached the feed. */ -async function opened(t: TestContext) { +async function opened(t: TestContext, pass: Uint8Array | null = null) { t.mock.timers.enable({ apis: ["setInterval"] }); const { feed, got } = fakeFeed(); - const carrier = new WsCarrier(feed, "wss://page.example/w/abc?port=9778&hash=00"); + const carrier = new WsCarrier(feed, "wss://page.example/w/abc?port=9778&hash=00", pass); const opening = carrier.open(); const ws = FakeSocket.last; ws.open(); @@ -72,14 +72,26 @@ test("the bridge URL is the page server's own, with the plane's port and hash", assert.equal(bridgeUrl("wa/192.168.1.21:47990", 9784, hash, "http://localhost:5173/"), `ws://localhost:5173/wa/192.168.1.21:47990?port=9784&hash=${hash}`); }); +test("a browser without a pass asks to pair before anything else", async (t) => { + const { ws } = await opened(t); + assert.deepEqual(ws.sent[0], { tag: TAG.PAIR }); +}); + +test("a browser with a pass for the host shows it before anything else", async (t) => { + const pass = new Uint8Array(32).fill(7); + const { ws } = await opened(t, pass); + assert.deepEqual(ws.sent[0], { tag: TAG.PASS, pass }); + assert.equal(ws.sent.filter((f) => f.tag === TAG.PASS || f.tag === TAG.PAIR).length, 1); +}); + test("an open bridge opens the control stream with its type byte first", async (t) => { const { carrier, ws, got } = await opened(t); assert.equal(ws.binaryType, "arraybuffer"); assert.deepEqual(got, ["ready"]); - assert.deepEqual(ws.sent[0], { tag: TAG.CONTROL, bytes: new Uint8Array([0]) }); + assert.deepEqual(ws.sent[1], { tag: TAG.CONTROL, bytes: new Uint8Array([0]) }); carrier.sendControl(new Uint8Array([7, 8])); assert.equal(carrier.sendDatagram(new Uint8Array([1])), true); - assert.deepEqual(ws.sent.slice(1), [ + assert.deepEqual(ws.sent.slice(2), [ { tag: TAG.CONTROL, bytes: new Uint8Array([7, 8]) }, { tag: TAG.DATAGRAM, bytes: new Uint8Array([1]) }, ]); diff --git a/packages/stream/src/carrier/ws.ts b/packages/stream/src/carrier/ws.ts index 28a44ab..a554e48 100644 --- a/packages/stream/src/carrier/ws.ts +++ b/packages/stream/src/carrier/ws.ts @@ -32,6 +32,9 @@ export class WsCarrier implements Carrier { constructor( private readonly feed: Feed, private readonly url: string, + /** This browser's pass when it covers the host: the paired pool. Without one, the pairing + * pool, which reaches only the plane the host published. */ + private readonly pass: Uint8Array | null = null, ) {} open(): Promise { @@ -42,6 +45,8 @@ export class WsCarrier implements Carrier { ws.onopen = () => { if (this.ws !== ws) return; this.live = true; + // First, before anything else: which of the bridge's two pools this session is for. + this.send(this.pass ? { tag: TAG.PASS, pass: this.pass } : { tag: TAG.PAIR }); this.ping = setInterval(() => this.send({ tag: TAG.PING, sentMs: performance.now() }), PING_MS); this.openControl(); resolve(); diff --git a/packages/stream/src/engine.ts b/packages/stream/src/engine.ts index f3fd9bf..be30ae4 100644 --- a/packages/stream/src/engine.ts +++ b/packages/stream/src/engine.ts @@ -31,6 +31,7 @@ import { type TunnelFetch, tunnelFetch } from "./tunnel.ts"; import { type Feed, wasmFeed } from "./carrier/feed.ts"; import { WtCarrier } from "./carrier/wt.ts"; import { bridgeUrl, WsCarrier } from "./carrier/ws.ts"; +import { passFor } from "./pass.ts"; export type { AudioSnapshot, AudioState } from "./audio.ts"; export type { MicState } from "./mic.ts"; @@ -431,7 +432,7 @@ export class Engine { this.planeHost = typeof address === "string" ? (this.opts.transportHost ?? new URL(origin).hostname) - : address.plane; + : (address.plane ?? ""); this.set({ kind: "reaching", origin }); const tunnel = typeof address === "object" && address.tunnel === true; @@ -497,7 +498,8 @@ export class Engine { } if (stale()) return; } - pf.hosts.remember(origin, typeof this.target === "object" && this.target ? { plane: this.target.plane } : {}); + const plane = typeof this.target === "object" ? this.target?.plane : undefined; + pf.hosts.remember(origin, plane ? { plane } : {}); // Every management call from here rides the plane, pinned to the hash just verified. The // tunnel is dialled by the first call, not now: the key may still say this host is unpaired. if (tunnel) { @@ -1052,7 +1054,10 @@ export class Engine { const port = this.seatPlane?.port ?? this.plane.port; const hash = this.seatPlane?.certHash ?? this.plane.cert_hash_sha256; const pin = this.opts.carrier; - const wt = pin === "wt" || (!pin && "WebTransport" in globalThis && carrierHint.get(this.origin) !== "ws"); + // No plane address: a stranger outside the server's network, whom only the bridge carries. + const wt = + this.planeHost !== "" && + (pin === "wt" || (!pin && "WebTransport" in globalThis && carrierHint.get(this.origin) !== "ws")); this.openCarrier(wt ? "wt" : "ws", port, hash); } @@ -1075,7 +1080,7 @@ export class Engine { kind === "wt" ? new WtCarrier(this.feed, `https://${this.planeHost}:${port}/pf2`, hash) : bridge - ? new WsCarrier(this.feed, bridgeUrl(bridge, port, hash)) + ? new WsCarrier(this.feed, bridgeUrl(bridge, port, hash), passFor(pf.hosts.fingerprint(origin))) : null; let opening: Promise | null = null; if (carrier) { diff --git a/packages/stream/src/host.ts b/packages/stream/src/host.ts index 4bdf8a4..ebd6f28 100644 --- a/packages/stream/src/host.ts +++ b/packages/stream/src/host.ts @@ -23,6 +23,7 @@ import { Effect } from "effect"; import { SchemaError } from "effect/Schema"; import { type GameEnd, gameEndOf } from "./game-end.ts"; import { PAGE_LIMIT, walkPages } from "./pages.ts"; +import { renewPass, serverFetch, TOO_MANY } from "./pass.ts"; export { DeviceRefused }; export type LibraryEntry = api.OperatorGameEntry; @@ -53,12 +54,24 @@ export class Host { * packaged page. Every call goes through it, the device exchange included. */ fetch?: Fetch, ) { - const via = fetch ? { fetch } : {}; - this.conn = connection({ - url: origin, - credential: deviceKey({ url: origin, hostFingerprint, signer, ...via }), - ...via, - }); + // The plane's tunnel for a packaged page; otherwise this page's own server, told which host + // a token is for, and shown the pass a login renews. + const via = fetch ?? serverFetch(hostFingerprint); + const key = deviceKey({ url: origin, hostFingerprint, signer, fetch: via }); + let earned = ""; + const credential = { + kind: key.kind, + header: async () => { + const header = await key.header(); + if (!fetch && header && header !== earned) { + earned = header; + await renewPass(origin, header, hostFingerprint).catch(() => undefined); + } + return header; + }, + invalidate: () => key.invalidate(), + }; + this.conn = connection({ url: origin, credential, fetch: via }); this.client = Effect.runPromise(httpClientFor(this.conn)).then((http) => api.make(http)); } @@ -210,6 +223,7 @@ async function refusal(res: Response, fallback: string): Promise { /** The SDK's errors, as the three things the app can act on. */ function translate(operation: string, e: unknown): Error { const cause = unwrap(e); + if (statusOf(cause) === 429) return new Error(TOO_MANY); if (cause instanceof DeviceRefused) return cause; if (cause instanceof SchemaError) return new VersionSkew(operation, String(cause)); if (cause instanceof Error) return cause; diff --git a/packages/stream/src/index.ts b/packages/stream/src/index.ts index a72bfe0..3547d50 100644 --- a/packages/stream/src/index.ts +++ b/packages/stream/src/index.ts @@ -50,6 +50,7 @@ export { export { bootstrap, bootstrapUrl, hosts, originOf, reach, reachTarget, type Bootstrap, type KnownHost, type Plane, type Reach } from "./pf-connect.ts"; export { deviceName, exitApp, packaged, remoteKey, tizen, tizenInfo } from "./platform.ts"; export { tunnelFetch, type TunnelFetch } from "./tunnel.ts"; +export { onPass, serverFetch, TOO_MANY } from "./pass.ts"; export { tvBack } from "./input.ts"; export { type LinkError, linkFor, type PageLink, parseLink } from "./links.ts"; export type { VideoPlane, UploadStats } from "./video-surface.ts"; diff --git a/packages/stream/src/pass.test.ts b/packages/stream/src/pass.test.ts new file mode 100644 index 0000000..81872a7 --- /dev/null +++ b/packages/stream/src/pass.test.ts @@ -0,0 +1,70 @@ +// What this page's own server is shown: the pass on every request to it, and the paired host on +// every request for that host. A request anywhere else goes out untouched. + +import { test } from "node:test"; +import assert from "node:assert/strict"; + +const store = new Map(); +Object.assign(globalThis, { + location: { href: "https://page.example/app/" }, + localStorage: { + getItem: (k: string) => store.get(k) ?? null, + setItem: (k: string, v: string) => void store.set(k, v), + removeItem: (k: string) => void store.delete(k), + }, +}); +const { onPass, passFor, renewPass, serverFetch } = await import("./pass.ts"); + +const FP = "cd".repeat(32); +const PASS = "ab".repeat(32); + +/** A fetch that records what it was given, and answers `answer`. */ +function recording(answer: () => Response = () => new Response("{}")) { + const seen: Array<{ url: string; headers: Headers }> = []; + const fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { + seen.push({ url: String(input), headers: new Headers(init?.headers) }); + return answer(); + }) as typeof globalThis.fetch; + return { fetch, seen }; +} + +test("a request to this page's server carries the pass and names the paired host", async () => { + store.clear(); + store.set("punktfunk.pass", JSON.stringify({ pass: PASS, hosts: [FP] })); + const { fetch, seen } = recording(); + const via = serverFetch(FP, fetch); + await via("https://page.example/app/h/desk/api/v1/auth/device/challenge", { method: "POST" }); + await via("https://page.example/app/h/desk/api/v1/status", { headers: { authorization: "Bearer t" } }); + await via("https://desk.example:47990/api/v1/status", { headers: { authorization: "Bearer t" } }); + await serverFetch(undefined, fetch)("https://page.example/app/config.json"); + assert.equal(seen[0]!.headers.get("pf-pass"), PASS); + assert.equal(seen[0]!.headers.get("pf-host"), FP, "the device exchange carries no token"); + assert.equal(seen[1]!.headers.get("pf-host"), FP); + assert.equal(seen[3]!.headers.get("pf-host"), null, "no host known, none named"); + assert.equal(seen[1]!.headers.get("authorization"), "Bearer t"); + assert.equal(seen[2]!.headers.get("pf-pass"), null, "another origin is not this server"); + assert.equal(seen[2]!.headers.get("pf-host"), null); +}); + +test("a stranger's request carries no pass", async () => { + store.clear(); + const { fetch, seen } = recording(); + await serverFetch(undefined, fetch)("./config.json"); + assert.equal(seen[0]!.headers.get("pf-pass"), null); + assert.equal(passFor(FP), null); +}); + +test("a login renews the pass for that host, and only a route on this server asks", async () => { + store.clear(); + let changed = 0; + onPass(() => changed++); + const { fetch, seen } = recording(() => Response.json({ pass: PASS })); + await renewPass("https://page.example/app/a/192.168.1.21:47990", "Bearer t", FP.toUpperCase(), fetch); + assert.equal(seen[0]!.url, "https://page.example/app/pass/a/192.168.1.21:47990"); + assert.equal(seen[0]!.headers.get("pf-host"), FP.toUpperCase()); + assert.deepEqual([...passFor(FP)!], Array(32).fill(0xab)); + assert.equal(passFor("ef".repeat(32)), null, "a pass covers only the hosts it was proven to"); + assert.equal(changed, 1); + await renewPass("https://desk.example:47990", "Bearer t", FP, fetch); + assert.equal(seen.length, 1, "a host reached directly has no pass to ask for"); +}); diff --git a/packages/stream/src/pass.ts b/packages/stream/src/pass.ts new file mode 100644 index 0000000..a0a01c2 --- /dev/null +++ b/packages/stream/src/pass.ts @@ -0,0 +1,108 @@ +// The page server's pass: shown to the server once a host has taken this browser's device token. +// It opens nothing on a host; the server lifts its stranger limits for it. One per page origin, +// kept with the fingerprints of the hosts it was proven to. `serverFetch` is the one place it, +// and the paired host's fingerprint, go on a request. + +const KEY = "punktfunk.pass"; + +/** What a `429` from this page's own server means to the person reading it. */ +export const TOO_MANY = "too many requests from this address. Wait a minute, then try again"; + +interface Held { + pass: string; + /** Lowercase hex fingerprints of the hosts this pass was proven to. */ + hosts: string[]; +} + +function read(): Held | null { + try { + const held = JSON.parse(localStorage.getItem(KEY) ?? "null") as Held | null; + return held && /^[0-9a-f]{64}$/.test(held.pass) && Array.isArray(held.hosts) ? held : null; + } catch { + return null; + } +} + +function write(held: Held): void { + try { + localStorage.setItem(KEY, JSON.stringify(held)); + } catch { + // Kept nowhere: the next login asks for it again. + } +} + +const listeners = new Set<() => void>(); + +/** Call `f` whenever the pass changes or covers another host. Returns the unsubscribe. */ +export function onPass(f: () => void): () => void { + listeners.add(f); + return () => listeners.delete(f); +} + +/** The pass as the bridge's first message carries it, when it covers the host `fingerprint`. */ +export function passFor(fingerprint: string | null | undefined): Uint8Array | null { + const held = read(); + if (!held || !fingerprint || !held.hosts.includes(fingerprint.toLowerCase())) return null; + return Uint8Array.from(held.pass.match(/../g) ?? [], (b) => parseInt(b, 16)); +} + +/** Whether `url` is on this page's own server. */ +function ours(url: string): boolean { + try { + return new URL(url, location.href).origin === new URL(location.href).origin; + } catch { + return false; + } +} + +/** + * `fetch`, with what this page's own server reads: the pass on every request to it and, given + * the paired host's fingerprint, `pf-host`. The server then sends the device exchange and the + * token only over a connection with that host's identity. A request anywhere else goes out as it + * came. + */ +export function serverFetch( + hostFingerprint?: string, + // Unbound: `window.fetch` called as a method of anything but `window` throws "Illegal invocation". + base: typeof fetch = (input, init) => globalThis.fetch(input, init), +): typeof fetch { + return (input, init) => { + const url = input instanceof Request ? input.url : String(input); + if (!ours(url)) return base(input, init); + const headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined)); + const held = read(); + if (held) headers.set("pf-pass", held.pass); + if (hostFingerprint) headers.set("pf-host", hostFingerprint); + return base(input, { ...init, headers }); + }; +} + +/** + * After a device login: ask this page's server for a pass for the host at `origin`, or to extend + * the one held to it. `origin` is the host's route on the server (`…/h/`, `…/a/`); + * a host the page reaches any other way has nothing to ask for. + */ +export async function renewPass( + origin: string, + token: string, + fingerprint: string, + base?: typeof fetch, +): Promise { + const page = new URL(".", location.href).href; + const route = origin.startsWith(page) ? origin.slice(page.length).replace(/\/+$/, "") : ""; + if (!/^[ha]\/[^/]+$/.test(route)) return; + const r = await serverFetch(fingerprint, base)(new URL(`pass/${route}`, page).href, { + method: "POST", + cache: "no-store", + headers: { authorization: token }, + }); + if (!r.ok) return; + const { pass } = (await r.json()) as { pass?: string }; + if (!pass || !/^[0-9a-f]{64}$/.test(pass)) return; + const held = read(); + const fp = fingerprint.toLowerCase(); + const hosts = held?.pass === pass ? held.hosts : []; + if (hosts.includes(fp)) return; + write({ pass, hosts: [...hosts, fp] }); + for (const f of listeners) f(); +} diff --git a/packages/stream/src/pf-connect.ts b/packages/stream/src/pf-connect.ts index c1a2821..953101f 100644 --- a/packages/stream/src/pf-connect.ts +++ b/packages/stream/src/pf-connect.ts @@ -18,6 +18,7 @@ // over the short-lived WebTransport hash, and `verify` refuses to dial a host that cannot produce // it. +import { serverFetch, TOO_MANY } from "./pass.ts"; import type { ProfilePick } from "./profiles.ts"; import { derToRaw, fromBase64, hexToBytes } from "@punktfunk/host/core"; @@ -85,10 +86,11 @@ export async function reachWhy(origin: string, timeoutMs = 4000): Promise<{ reac const abort = new AbortController(); const timer = setTimeout(() => abort.abort(), timeoutMs); try { - const r = await fetch(`${origin}/api/v1/health`, { + const r = await serverFetch()(`${origin}/api/v1/health`, { cache: "no-store", signal: abort.signal, }); + if (r.status === 429) return { reach: "unreachable", said: TOO_MANY }; if (r.status === 502) { // Plain text is the page's server speaking; anything else (a proxy's HTML page) says nothing. const plain = r.headers.get("content-type")?.startsWith("text/plain") ?? false; @@ -122,7 +124,9 @@ export const acceptUrl = (origin: string): string => `${origin}/api/v1/health`; */ export interface HostTarget { readonly api: string; - readonly plane: string; + /** Absent for a stranger outside the server's network, who is not told it: then only the + * bridge reaches the host. */ + readonly plane?: string; readonly tunnel?: boolean; readonly bridge?: string; } @@ -163,14 +167,15 @@ export async function bootstrap(url: string, timeoutMs = 4000): Promise { if (typeof target === "string" || !target.tunnel) return reach(typeof target === "string" ? target : target.api); - const b = await bootstrap(bootstrapUrl(target.api, target.plane)); + const b = await bootstrap(bootstrapUrl(target.api, target.plane ?? new URL(target.api).hostname)); return b.reach === "ok" ? "ok" : b.reach === "no-plane" ? "blocked" : "unreachable"; } /** What the host says about its browser plane. Everything here is public. */ export async function fetchPlane(origin: string): Promise { - const r = await fetch(`${origin}/api/v1/webtransport`, { cache: "no-store" }); + const r = await serverFetch()(`${origin}/api/v1/webtransport`, { cache: "no-store" }); if (r.status === 404) throw new Error("this host does not offer the browser plane"); + if (r.status === 429) throw new Error(TOO_MANY); if (!r.ok) throw new Error(`the host answered ${r.status}`); return (await r.json()) as Plane; } diff --git a/server/Cargo.lock b/server/Cargo.lock index 9164fc5..2b37325 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -544,7 +544,7 @@ checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" [[package]] name = "fec-rs" version = "0.1.0" -source = "git+https://git.unom.io/unom/punktfunk?rev=12e4dfd1ba7855d0043a15c2939fc923a85771ef#12e4dfd1ba7855d0043a15c2939fc923a85771ef" +source = "git+https://git.unom.io/unom/punktfunk?rev=f29f933fa365cb737bd57ae9079adf2c96d8fda8#f29f933fa365cb737bd57ae9079adf2c96d8fda8" [[package]] name = "find-msvc-tools" @@ -1382,7 +1382,11 @@ dependencies = [ "anyhow", "axum", "axum-server", + "base64 0.22.1", "futures-util", + "getrandom 0.3.4", + "hyper-util", + "if-addrs", "mdns-sd", "punktfunk-core", "rcgen", @@ -1390,6 +1394,7 @@ dependencies = [ "rustls", "serde", "serde_json", + "sha2", "time", "tokio", "tokio-tungstenite", @@ -1402,8 +1407,8 @@ dependencies = [ [[package]] name = "punktfunk-core" -version = "0.45.0" -source = "git+https://git.unom.io/unom/punktfunk?rev=12e4dfd1ba7855d0043a15c2939fc923a85771ef#12e4dfd1ba7855d0043a15c2939fc923a85771ef" +version = "0.46.0" +source = "git+https://git.unom.io/unom/punktfunk?rev=f29f933fa365cb737bd57ae9079adf2c96d8fda8#f29f933fa365cb737bd57ae9079adf2c96d8fda8" dependencies = [ "aes-gcm", "chacha20poly1305", @@ -1414,6 +1419,7 @@ dependencies = [ "libc", "reed-solomon-simd", "rustls", + "rustls-webpki", "sha2", "socket2", "thiserror", diff --git a/server/Cargo.toml b/server/Cargo.toml index 3c489b8..adaa141 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -12,6 +12,13 @@ publish = false anyhow = "1" axum = { version = "0.8", features = ["ws"] } axum-server = { version = "0.8", features = ["tls-rustls-no-provider"] } +# A host's attestation carries its certificate in base64. +base64 = "0.22" +# The listener's header-read timeout needs hyper's timer. +hyper-util = { version = "0.1", features = ["tokio"] } +# A pass is random, and kept as its SHA-256. +getrandom = "0.3" +sha2 = "0.11" futures-util = { version = "0.3", default-features = false, features = ["sink"] } rcgen = "0.14" reqwest = { version = "0.13", default-features = false, features = ["stream", "rustls-no-provider", "http2"] } @@ -22,6 +29,9 @@ time = "0.3" # The browse behind pf-client-core's discovery, used directly: that crate would bring core's whole # QUIC stack (and libopus) along on Linux for thirty lines of mDNS. mdns-sd = "0.21" +# Which interface a route leaves by, and which addresses are this machine's own. Link-local ones +# count too. +if-addrs = { version = "0.15", features = ["link-local"] } tokio = { version = "1", features = ["rt-multi-thread", "macros", "net", "fs", "signal", "sync", "time"] } tower-http = { version = "0.7", features = ["fs"] } tracing = "0.1" @@ -29,9 +39,10 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] } # The bridge dials the plane the way a browser does (`bridge/plane.rs`). aws-lc-rs, the provider # core's `tls` installs: a second rustls provider in the tree makes `ClientConfig::builder()` panic. wtransport = { version = "0.7", default-features = false, features = ["aws-lc-rs"] } -# Pinned to the rev packages/stream builds against: the same PinVerify and address ranking the -# native clients use, so this server trusts and reaches a host exactly the way they do. -punktfunk-core = { git = "https://git.unom.io/unom/punktfunk", rev = "12e4dfd1ba7855d0043a15c2939fc923a85771ef", default-features = false, features = ["tls"] } +# The same PinVerify and address ranking the native clients use, and the route table, address +# classes and plane attestation check the host itself reads. Ahead of packages/stream's pin, +# which moves with the wire. +punktfunk-core = { git = "https://git.unom.io/unom/punktfunk", rev = "f29f933fa365cb737bd57ae9079adf2c96d8fda8", default-features = false, features = ["tls"] } [dev-dependencies] tower = { version = "0.5", features = ["util"] } diff --git a/server/src/bridge.rs b/server/src/bridge.rs index 8cbb3fa..b5fe631 100644 --- a/server/src/bridge.rs +++ b/server/src/bridge.rs @@ -10,28 +10,31 @@ mod frame; mod plane; use crate::hosts::Hosts; +use crate::lanes::{Slot, PAIRING_LIFE}; +#[cfg(test)] +use crate::lanes::{PAIRING_FULL, PAIRING_HOURLY}; +use crate::proof::Outcome; use crate::proxy; +use crate::visitor::Visitor; use axum::body::Bytes; use axum::extract::ws::{close_code, CloseFrame, Message, WebSocket, WebSocketUpgrade}; -use axum::extract::{ConnectInfo, FromRequestParts, Path, Query, Request, State}; +use axum::extract::{FromRequestParts, Path, Query, Request, State}; use axum::http::{header, request::Parts, StatusCode}; use axum::response::{IntoResponse, Response}; use frame::Frame; use futures_util::stream::{SplitSink, SplitStream}; use futures_util::{FutureExt, SinkExt, StreamExt}; -use std::net::{IpAddr, SocketAddr}; +use punktfunk_core::provenance::CLIENT_ADDR_UNKNOWN; +use std::net::IpAddr; use std::sync::atomic::{AtomicU64, Ordering::Relaxed}; use std::sync::Arc; use std::time::{Duration, Instant}; use tokio::sync::mpsc::{self, Receiver, Sender}; -use tokio::sync::{OwnedSemaphorePermit, Semaphore}; use tokio::task::JoinSet; use tokio::time::timeout; use wtransport::error::ConnectionError; use wtransport::{Connection, RecvStream, SendStream, VarInt}; -/// Bridged sessions at once: each is a QUIC connection and a relay. -pub const SESSIONS: usize = 16; const MAX_MESSAGE: usize = 1 << 20; const DIAL_TIMEOUT: Duration = Duration::from_secs(5); /// A WebSocket ping this often; three unanswered and the TCP leg is dead. @@ -48,45 +51,78 @@ struct Dial { hash: String, } -/// A host this server lists. +/// The first message's deadline. Before it the plane is not dialled. +const FIRST: Duration = Duration::from_secs(5); + +/// Where the bridge dials: a host at its management port and pin. One the operator did not +/// list must be proven first; `typed` is one the page named, whose proof a stranger pays for. +struct To { + addr: String, + mgmt: u16, + pin: Option<[u8; 32]>, + listed: bool, + typed: bool, +} + +/// A host this server lists; one mDNS found, once proven. pub async fn listed( State(hosts): State>, Path(id): Path, - ConnectInfo(peer): ConnectInfo, req: Request, ) -> Response { - let addr = hosts.get(&id).map(|h| h.addr).ok_or(StatusCode::NOT_FOUND); - open(addr, id, peer, req).await + let to = hosts.get(&id).ok_or(StatusCode::NOT_FOUND).map(|h| To { + listed: h.fullname.is_none(), + typed: false, + addr: h.addr, + mgmt: h.port, + pin: h.pin, + }); + open(hosts, to, id, req).await } -/// A host the page names by its private address, as `/a/` reaches one. +/// A host the page names by its address, as `/a/` reaches one. pub async fn typed( State(hosts): State>, Path(target): Path, - ConnectInfo(peer): ConnectInfo, req: Request, ) -> Response { - let addr = if !hosts.add_hosts { - Err(StatusCode::NOT_FOUND) - } else { - match proxy::private_target(&target) { - Some((IpAddr::V4(v4), _)) => Ok(v4.to_string()), - Some((IpAddr::V6(v6), _)) => Ok(format!("[{v6}]")), - None => Err(StatusCode::BAD_REQUEST), + let to = match proxy::typed_target(&hosts, &target) { + _ if !hosts.add_hosts => Err(StatusCode::NOT_FOUND), + Ok((ip, mgmt)) => { + let addr = proxy::url_host(ip); + let pin = hosts.pin_at(&format!("{addr}:{mgmt}")); + Ok(To { + addr, + mgmt, + pin, + listed: false, + typed: true, + }) } + Err(_) => Err(StatusCode::BAD_REQUEST), }; - open(addr, target, peer, req).await + open(hosts, to, target, req).await +} + +/// What admission needs once the socket is open: the host's identity, the plane it published +/// when that is known, and who is asking. +struct Admit { + hosts: Arc, + to: To, + identity: Option<[u8; 32]>, + published: Option<(u16, [u8; 32])>, + who: Option, } async fn open( - addr: Result, + hosts: Arc, + to: Result, host: String, - peer: SocketAddr, req: Request, ) -> Response { let (mut parts, _) = req.into_parts(); - let addr = match addr { - Ok(a) => a, + let to = match to { + Ok(to) => to, Err(status) => return status.into_response(), }; let Some((port, hash)) = Query::::try_from_uri(&parts.uri) @@ -95,26 +131,97 @@ async fn open( else { return StatusCode::BAD_REQUEST.into_response(); }; - let Some(slots) = parts.extensions.get::>().cloned() else { - return StatusCode::INTERNAL_SERVER_ERROR.into_response(); - }; - let Ok(slot) = slots.try_acquire_owned() else { - return StatusCode::SERVICE_UNAVAILABLE.into_response(); - }; + let (mut identity, mut published) = (to.pin, None); + if !to.listed { + let key = format!("{}:{}", to.addr, to.mgmt); + if to.typed { + if let Some(refused) = crate::proof::over_meter(&hosts, &parts.extensions, &key, false) + { + return refused; + } + } + match crate::proof::prove(&hosts, &to.addr, to.mgmt, to.pin, false).await { + Outcome::Host { + fp, + plane_port, + plane_hash, + .. + } => (identity, published) = (Some(fp), Some((plane_port, plane_hash))), + refused => { + return refused + .refusal() + .unwrap_or_else(|| StatusCode::BAD_GATEWAY.into_response()) + } + } + } let ws = match WebSocketUpgrade::from_request_parts(&mut parts, &()).await { Ok(ws) => ws, Err(refused) => return refused.into_response(), }; + let visitor = parts.extensions.get::(); let target = plane::Target { - addr, + addr: to.addr.clone(), port, hash, origin: header_str(&parts, header::ORIGIN.as_str()).map(str::to_owned), - client: peer.ip().to_canonical(), + client: visitor.map_or_else(|| CLIENT_ADDR_UNKNOWN.into(), Visitor::header), + }; + let admit = Admit { + who: visitor.and_then(|v| v.addr), + hosts, + to, + identity, + published, }; ws.max_message_size(MAX_MESSAGE) .max_frame_size(MAX_MESSAGE) - .on_upgrade(move |socket| session(socket, target, host, slot)) + .on_upgrade(move |socket| session(socket, target, host, admit)) +} + +const PASS_REFUSED: &str = + "this page doesn't know this browser's pass for the host. Reload the page and connect again"; +const PAIRING_OVER: &str = + "this page gives a browser three minutes to pair with a host. Connect again to try once more"; +const NOT_PUBLISHED: &str = + "this page reaches only the host's own streaming port for a browser that isn't paired with it"; + +/// The first message picks the pool: tag 5 with a pass for this host, any port on it; or tag 6 +/// to pair, only to the plane the host published. The refusal is a close code and the page's +/// sentence. +async fn admission( + socket: &mut WebSocket, + target: &plane::Target, + a: &Admit, +) -> Result<(Slot, bool), (u16, &'static str)> { + let first = match timeout(FIRST, socket.recv()).await { + Ok(Some(Ok(Message::Binary(m)))) => m, + _ => return Err((close_code::POLICY, "no pool asked for")), + }; + let pools = &a.hosts.lanes.pools; + match frame::decode(&first) { + Some(Frame::Pass(pass)) => { + let pass = crate::hosts::hex(pass); + if !a.hosts.lanes.passes.hosts(&pass).covers(a.identity) { + return Err((close_code::POLICY, PASS_REFUSED)); + } + let slot = pools.paired().map_err(|say| (close_code::AGAIN, say))?; + Ok((slot, false)) + } + Some(Frame::Pair) => { + let published = match a.published { + Some(p) => Some(p), + None => crate::proof::published(&a.to.addr, a.to.mgmt, a.to.pin).await, + }; + if published != Some((target.port, target.hash)) { + return Err((close_code::POLICY, NOT_PUBLISHED)); + } + let slot = pools + .pairing(a.who) + .map_err(|say| (close_code::AGAIN, say))?; + Ok((slot, true)) + } + _ => Err((close_code::PROTOCOL, "unknown message")), + } } fn header_str<'a>(parts: &'a Parts, name: &str) -> Option<&'a str> { @@ -129,12 +236,15 @@ struct Counts { to_page_dropped: AtomicU64, } -async fn session( - mut socket: WebSocket, - target: plane::Target, - host: String, - _slot: OwnedSemaphorePermit, -) { +async fn session(mut socket: WebSocket, target: plane::Target, host: String, admit: Admit) { + let (_slot, pairing) = match admission(&mut socket, &target, &admit).await { + Ok(admitted) => admitted, + Err((code, reason)) => { + tracing::info!(host = %host, reason, "bridge session refused"); + let _ = socket.send(close(code, reason)).await; + return; + } + }; let started = Instant::now(); let conn = match timeout(DIAL_TIMEOUT, plane::dial(&target)).await { Ok(Ok(conn)) => conn, @@ -149,7 +259,16 @@ async fn session( } }; let counts = Arc::new(Counts::default()); - let code = relay(socket, &conn, &counts).await; + // A stranger's session lasts as long as the host parks a knock, and no longer. + let relay = relay(socket, &conn, &counts); + tokio::pin!(relay); + let code = tokio::select! { + code = &mut relay => code, + _ = tokio::time::sleep(PAIRING_LIFE), if pairing => { + conn.close(VarInt::from_u32(0), b""); + relay.await + } + }; tracing::info!( host = %host, secs = started.elapsed().as_secs(), @@ -205,6 +324,8 @@ async fn relay(socket: WebSocket, conn: &Connection, counts: &Arc) -> u3 vec![Message::Binary(tag3.into()), close(close_code::NORMAL, "")], ) } + // Only the pairing session's deadline closes the connection from outside `relay`. + Err(ConnectionError::LocallyClosed) => (0, vec![close(close_code::AGAIN, PAIRING_OVER)]), Err(_) => (0, vec![close(close_code::ERROR, "host connection lost")]), Ok(End::Left(code, reason)) => { conn.close(VarInt::from_u32(code), &reason); @@ -281,7 +402,7 @@ async fn from_page( } } Some(Frame::Pong { .. }) => {} - Some(Frame::Uni(_)) | None => return End::Protocol, + Some(Frame::Uni(_) | Frame::Pass(_) | Frame::Pair) | None => return End::Protocol, } } } @@ -367,6 +488,7 @@ fn close(code: u16, reason: &'static str) -> Message { mod tests { use super::*; use crate::config::Listed; + use std::net::SocketAddr; use tokio_tungstenite::tungstenite::{self, client::IntoClientRequest, Message as Ws}; use wtransport::endpoint::endpoint_side::Server; use wtransport::{Endpoint, Identity, ServerConfig}; @@ -390,17 +512,30 @@ mod tests { (server, port, hash) } - /// This server on loopback, listing `desk` at 127.0.0.1. - async fn page_server(name: &str) -> SocketAddr { + /// This server on loopback, and a pass for its one host. + struct Served { + at: SocketAddr, + pass: [u8; 32], + } + + async fn page_server(name: &str) -> Served { + serve_desk(name, crate::config::MGMT_PORT, [7; 32]).await + } + + /// This server on loopback, listing `desk` at 127.0.0.1:`mgmt` pinned to `fp`. + async fn serve_desk(name: &str, mgmt: u16, fp: [u8; 32]) -> Served { let dir = std::env::temp_dir().join(format!("pf-bridge-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); let desk = Listed { name: "desk".into(), addr: "127.0.0.1".into(), - port: crate::config::MGMT_PORT, - pin: None, + port: mgmt, + pin: Some(fp), }; - let hosts = Hosts::new(vec![desk], &dir, true).unwrap(); - let app = crate::router(hosts, dir, None); + let hosts = Hosts::new(vec![desk], &dir, true, crate::visitor::TEST).unwrap(); + let pass = hosts.lanes.passes.prove(None, fp); + let pass = crate::config::parse_fp(&pass).unwrap(); + let app = crate::router(hosts, dir, Default::default()); let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); let at = listener.local_addr().unwrap(); tokio::spawn(async move { @@ -410,7 +545,44 @@ mod tests { ) .await }); - at + Served { at, pass } + } + + /// The bridge opened in the paired pool. + async fn paired(s: &Served, path: &str, headers: &[(&'static str, &str)]) -> Page { + let mut ws = open(s.at, path, headers).await.unwrap(); + send(&mut ws, Frame::Pass(&s.pass)).await; + ws + } + + /// The bridge opened in the pairing pool, as a stranger at `from`. + async fn pairing(s: &Served, path: &str, from: &str) -> Page { + let mut ws = open(s.at, path, &[("x-forwarded-for", from)]) + .await + .unwrap(); + send(&mut ws, Frame::Pair).await; + ws + } + + /// The session dialled the plane: a ping comes back as a pong. + async fn admitted(ws: &mut Page) -> bool { + send(ws, Frame::Ping(1f64.to_be_bytes())).await; + match next(ws).await { + Ws::Binary(b) => matches!(frame::decode(&b), Some(Frame::Pong { .. })), + _ => false, + } + } + + /// A plane that takes every session and holds it until the other side goes. + fn plane_accepting() -> (u16, [u8; 32]) { + let (server, port, hash) = plane(); + tokio::spawn(async move { + loop { + let conn = server.accept().await.await.unwrap().accept().await.unwrap(); + tokio::spawn(async move { conn.closed().await }); + } + }); + (port, hash) } /// Open the bridge as a page would; the refusal's status when it is not upgraded. @@ -495,10 +667,10 @@ mod tests { conn.close(VarInt::from_u32(0x51), b"bye"); seen }); - let at = page_server("whole").await; + let s = page_server("whole").await; let path = format!("/w/desk?port={port}&hash={}", crate::hosts::hex(&hash)); - let origin = own(at); - let mut ws = open(at, &path, &[("origin", &origin)]).await.unwrap(); + let origin = own(s.at); + let mut ws = paired(&s, &path, &[("origin", &origin)]).await; send(&mut ws, Frame::Control(b"hello")).await; let mut ctl = Vec::new(); @@ -554,10 +726,10 @@ mod tests { } codes }); - let at = page_server("page-close").await; + let s = page_server("page-close").await; let path = format!("/w/desk?port={port}&hash={}", crate::hosts::hex(&hash)); - let mut quit = open(at, &path, &[]).await.unwrap(); + let mut quit = paired(&s, &path, &[]).await; send( &mut quit, Frame::Close { @@ -568,7 +740,7 @@ mod tests { .await; assert_eq!(close_code(&mut quit).await.0, 1000); - let gone = open(at, &path, &[]).await.unwrap(); + let gone = paired(&s, &path, &[]).await; drop(gone); let codes = host.await.unwrap(); @@ -591,7 +763,7 @@ mod tests { port, hash, origin: None, - client: [127, 0, 0, 1].into(), + client: "127.0.0.1".into(), }) .await .unwrap(); @@ -614,7 +786,7 @@ mod tests { #[tokio::test] async fn only_this_page_with_a_good_hash_gets_in() { let (_server, port, hash) = plane(); - let at = page_server("admission").await; + let at = page_server("admission").await.at; let good = format!("/w/desk?port={port}&hash={}", crate::hosts::hex(&hash)); assert_eq!( open(at, &good, &[("sec-fetch-site", "cross-site")]) @@ -649,10 +821,10 @@ mod tests { let (server, port, _) = plane(); tokio::spawn(async move { server.accept().await.await }); let silent = std::net::UdpSocket::bind("127.0.0.1:0").unwrap(); - let at = page_server("dial").await; + let s = page_server("dial").await; - let wrong = format!("/wa/127.0.0.1:47990?port={port}&hash={}", "00".repeat(32)); - let mut ws = open(at, &wrong, &[]).await.unwrap(); + let wrong = format!("/w/desk?port={port}&hash={}", "00".repeat(32)); + let mut ws = paired(&s, &wrong, &[]).await; assert_eq!( close_code(&mut ws).await, (1011, "host refused the session".into()) @@ -663,7 +835,7 @@ mod tests { silent.local_addr().unwrap().port(), "00".repeat(32) ); - let mut ws = open(at, &quiet, &[]).await.unwrap(); + let mut ws = paired(&s, &quiet, &[]).await; assert_eq!( close_code(&mut ws).await, (1011, "host did not answer".into()) @@ -671,20 +843,87 @@ mod tests { } #[tokio::test] - async fn the_seventeenth_session_is_refused() { - let (server, port, hash) = plane(); - tokio::spawn(async move { - loop { - let conn = server.accept().await.await.unwrap().accept().await.unwrap(); - tokio::spawn(async move { conn.closed().await }); - } - }); - let at = page_server("cap").await; + async fn strangers_holding_sockets_leave_a_paired_stream_running() { + let (port, hash) = plane_accepting(); + let s = page_server("strangers").await; let path = format!("/w/desk?port={port}&hash={}", crate::hosts::hex(&hash)); let mut held = Vec::new(); - for _ in 0..SESSIONS { - held.push(open(at, &path, &[]).await.unwrap()); + for i in 0..16 { + let from = format!("203.0.113.{i}"); + held.push( + open(s.at, &path, &[("x-forwarded-for", &from)]) + .await + .unwrap(), + ); + } + let mut ws = paired(&s, &path, &[]).await; + assert!(admitted(&mut ws).await); + // A socket that never says which pool goes after five seconds, undialled. + assert_eq!(close_code(&mut held[0]).await.0, 1008); + // A pass for another host is a stranger's. + let mut other = open(s.at, &path, &[]).await.unwrap(); + send(&mut other, Frame::Pass(&[9; 32])).await; + assert_eq!(close_code(&mut other).await, (1008, PASS_REFUSED.into())); + } + + /// A fake host's management API publishing this plane, and this server listing it. + async fn published_desk(name: &str) -> (Served, String) { + let (port, hash) = plane_accepting(); + let mgmt = + crate::testing::upstream_publishing(crate::testing::Plane::Attested, (port, hash)) + .await; + let s = serve_desk(name, mgmt.port, mgmt.fp).await; + ( + s, + format!("/w/desk?port={port}&hash={}", crate::hosts::hex(&hash)), + ) + } + + #[tokio::test] + async fn the_pairing_pool_holds_four_at_once_and_twenty_an_hour() { + punktfunk_core::tls::install_default_provider(); + let (s, path) = published_desk("pairing").await; + let mut four = Vec::new(); + for i in 1..=4 { + let mut ws = pairing(&s, &path, &format!("10.0.0.{i}")).await; + assert!(admitted(&mut ws).await, "{i}"); + four.push(ws); + } + let mut fifth = pairing(&s, &path, "10.0.0.5").await; + assert_eq!(close_code(&mut fifth).await, (1013, PAIRING_FULL.into())); + // One at a time per address. + drop(four.pop()); + tokio::time::sleep(Duration::from_millis(200)).await; + let mut again = pairing(&s, &path, "10.0.0.1").await; + assert_eq!(close_code(&mut again).await.0, 1013); + for ws in &mut four { + let _ = ws.close(None).await; } - assert_eq!(open(at, &path, &[]).await.err(), Some(503)); + drop(four); + // Four have started; sixteen more make the hour's twenty. + for i in 10..26 { + tokio::time::sleep(Duration::from_millis(200)).await; + let mut ws = pairing(&s, &path, &format!("10.0.1.{i}")).await; + assert!(admitted(&mut ws).await, "{i}"); + let _ = ws.close(None).await; + } + tokio::time::sleep(Duration::from_millis(200)).await; + let mut late = pairing(&s, &path, "10.0.2.1").await; + assert_eq!(close_code(&mut late).await, (1013, PAIRING_HOURLY.into())); + } + + #[tokio::test] + async fn a_stranger_reaches_only_the_published_plane() { + punktfunk_core::tls::install_default_provider(); + let (s, path) = published_desk("published").await; + let hash = path.split("&hash=").nth(1).unwrap(); + let elsewhere = format!("/w/desk?port=9&hash={hash}"); + let mut ws = pairing(&s, &elsewhere, "10.0.0.1").await; + assert_eq!(close_code(&mut ws).await, (1008, NOT_PUBLISHED.into())); + let mut ws = pairing(&s, &path, "10.0.0.2").await; + assert!(admitted(&mut ws).await); + // A pass holder may name any port on the host: this one is dialled, and nothing answers. + let mut ws = paired(&s, &elsewhere, &[]).await; + assert_eq!(close_code(&mut ws).await.0, 1011); } } diff --git a/server/src/bridge/frame.rs b/server/src/bridge/frame.rs index 66dafdd..056b669 100644 --- a/server/src/bridge/frame.rs +++ b/server/src/bridge/frame.rs @@ -1,12 +1,15 @@ //! One WebSocket message: a tag byte, then its payload (`web-client-tcp-bridge.md` §3.3). //! -//! Integers are big-endian. A ping's `f64` is echoed byte for byte, never read. +//! Integers are big-endian. A ping's `f64` is echoed byte for byte, never read. Tags 5 and 6 go +//! once, first, from the page: which of the two pools the session joins. const DATAGRAM: u8 = 0; const CONTROL: u8 = 1; const UNI: u8 = 2; const CLOSE: u8 = 3; const PING: u8 = 4; +const PASS: u8 = 5; +const PAIR: u8 = 6; #[derive(Debug, PartialEq)] pub enum Frame<'a> { @@ -22,6 +25,10 @@ pub enum Frame<'a> { Ping([u8; 8]), /// The ping's clock, then the bridge's smoothed WebTransport RTT. Pong { sent: [u8; 8], rtt_us: u32 }, + /// Page to bridge, first: a pass, for the paired pool. + Pass(&'a [u8; 32]), + /// Page to bridge, first: a stranger asking to pair. + Pair, } /// `None` for an unknown tag or a truncated close or ping. @@ -49,6 +56,8 @@ pub fn decode(msg: &[u8]) -> Option> { } _ => return None, }, + PASS => Frame::Pass(body.try_into().ok()?), + PAIR if body.is_empty() => Frame::Pair, _ => return None, }) } @@ -61,6 +70,8 @@ pub fn encode(frame: &Frame) -> Vec { Frame::Close { code, reason } => (CLOSE, [&code.to_be_bytes(), reason]), Frame::Ping(sent) => (PING, [sent, &[]]), Frame::Pong { sent, rtt_us } => (PING, [sent, &rtt_us.to_be_bytes()]), + Frame::Pass(pass) => (PASS, [*pass, &[]]), + Frame::Pair => (PAIR, [&[], &[]]), }; [&[tag][..], parts[0], parts[1]].concat() } @@ -85,6 +96,8 @@ mod tests { sent: t, rtt_us: 900, }, + Frame::Pass(&[7; 32]), + Frame::Pair, ] { assert_eq!(decode(&encode(&f)), Some(f)); } @@ -112,6 +125,8 @@ mod tests { fn unknown_and_truncated_are_refused() { assert_eq!(decode(b""), None); assert_eq!(decode(b"\x05abc"), None); + assert_eq!(decode(b"\x06x"), None); + assert_eq!(decode(b"\x07"), None); assert_eq!(decode(b"\x03\x00\x00\x51"), None); assert_eq!(decode(b"\x04\x00\x00\x00"), None); assert_eq!(decode(b"\x04123456789"), None); diff --git a/server/src/bridge/plane.rs b/server/src/bridge/plane.rs index 51b2038..284ff20 100644 --- a/server/src/bridge/plane.rs +++ b/server/src/bridge/plane.rs @@ -1,6 +1,7 @@ //! The dial a browser would make to the plane, made from here. use anyhow::{Context, Result}; +use punktfunk_core::provenance::CLIENT_ADDR_HEADER; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}; use wtransport::endpoint::ConnectOptions; use wtransport::tls::Sha256Digest; @@ -14,8 +15,8 @@ pub struct Target { pub hash: [u8; 32], /// The browser's `Origin`, so the host's origin list applies as on a direct dial. pub origin: Option, - /// The WebSocket peer, so the host classifies the knock by the real client. - pub client: IpAddr, + /// The visitor's address, or `unknown`, so the host classifies the knock by the real client. + pub client: String, } /// `https://:/pf2`, pinned to `hash`. One endpoint per dial: wtransport binds the @@ -36,7 +37,7 @@ pub async fn dial(t: &Target) -> Result { .build(), )?; let mut opts = ConnectOptions::builder(format!("https://{to}/pf2")) - .add_header("pf-client-addr", t.client.to_string()); + .add_header(CLIENT_ADDR_HEADER, &t.client); if let Some(origin) = &t.origin { opts = opts.add_header("origin", origin); } diff --git a/server/src/hosts.rs b/server/src/hosts.rs index d99e786..cdf5b05 100644 --- a/server/src/hosts.rs +++ b/server/src/hosts.rs @@ -6,8 +6,12 @@ //! LAN able to answer in the host's place. use crate::config::Listed; +use crate::lanes::Lanes; +use crate::proof::{Proofs, KEPT}; +use crate::visitor::Machine; use anyhow::{Context, Result}; use std::collections::BTreeMap; +use std::net::IpAddr; use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex, RwLock}; @@ -35,8 +39,15 @@ impl Host { pub struct Hosts { /// Whether `/a/
/…` proxies a host the page names. See [`crate::config::Config`]. pub add_hosts: bool, + /// What this machine knows of its networks: who is at home, and which addresses are its own. + pub machine: Machine, + /// Which unlisted addresses are Punktfunk hosts. + pub proofs: Proofs, + /// Passes, and the limits strangers share. + pub lanes: Lanes, list: RwLock>, - pins: Mutex>, + /// Hex fingerprints by `addr:port`, least recently used first. + pins: Mutex>, pins_path: PathBuf, /// Wake-capable MACs a host announced, by where it answers. Kept in `wake.json`: a host is /// woken exactly when it is too asleep to announce them. @@ -45,7 +56,12 @@ pub struct Hosts { } impl Hosts { - pub fn new(listed: Vec, data: &Path, add_hosts: bool) -> Result> { + pub fn new( + listed: Vec, + data: &Path, + add_hosts: bool, + machine: Machine, + ) -> Result> { std::fs::create_dir_all(data).with_context(|| format!("create {}", data.display()))?; let pins_path = data.join("pins.json"); // Only a missing file means no pins: one that cannot be read must not re-trust every host. @@ -80,8 +96,11 @@ impl Hosts { } Ok(Arc::new(Self { add_hosts, + machine, + proofs: Proofs::default(), + lanes: Lanes::new(data), list: RwLock::new(list), - pins: Mutex::new(pins), + pins: Mutex::new(pins.into_iter().collect()), pins_path, macs: Mutex::new(macs), macs_path, @@ -147,31 +166,53 @@ impl Hosts { /// The pin kept for a host at `addr:port`, whoever named it. pub fn pin_at(&self, key: &str) -> Option<[u8; 32]> { - self.pins - .lock() - .unwrap() - .get(key) - .and_then(|hex| crate::config::parse_fp(hex).ok()) + let pins = self.pins.lock().unwrap(); + let (_, hex) = pins.iter().find(|(k, _)| k == key)?; + crate::config::parse_fp(hex).ok() } - /// Keep a first-contact pin. An address already pinned keeps its first certificate. + /// Keep a listed host's first-contact pin, or a proven host's. An address already pinned keeps + /// its first certificate. Unlisted addresses keep at most [`KEPT`], least recently used out. pub fn keep(&self, key: &str, fp: [u8; 32]) { + let listed: Vec = self + .list + .read() + .unwrap() + .iter() + .filter(|h| h.fullname.is_none()) + .map(Host::pin_key) + .collect(); let mut pins = self.pins.lock().unwrap(); - if pins.contains_key(key) { - return; + if let Some(i) = pins.iter().position(|(k, _)| k == key) { + let used = pins.remove(i); + return pins.push(used); } - tracing::info!(host = key, fingerprint = %hex(&fp), "pinned on first contact"); - pins.insert(key.to_owned(), hex(&fp)); + tracing::info!(host = key, fingerprint = %hex(&fp), "pinned"); + pins.push((key.to_owned(), hex(&fp))); + let unlisted = |(k, _): &&(String, String)| !listed.contains(k); + while pins.iter().filter(unlisted).count() > KEPT { + let Some(i) = pins.iter().position(|p| unlisted(&p)) else { + break; + }; + pins.remove(i); + } + let file: BTreeMap<_, _> = pins.iter().cloned().collect(); if let Err(e) = write_whole( &self.pins_path, - &serde_json::to_vec_pretty(&*pins).unwrap_or_default(), + &serde_json::to_vec_pretty(&file).unwrap_or_default(), ) { tracing::warn!(error = %e, "pins.json was not written; the pin lasts until restart"); } } + /// An address this server may reach without the operator's word: on a home network, and not + /// loopback. + pub fn may_dial(&self, ip: IpAddr) -> bool { + crate::proxy::home_range(ip) && !(self.machine.loopback)(ip) + } + /// An mDNS advert. A host already listed, by fingerprint or address, stays as listed, but - /// what it wakes on is kept for it. + /// what it wakes on is kept for it. Any other joins only at an address this server may dial. fn found(&self, a: Advert) { let mut list = self.list.write().unwrap(); if let Some(h) = list.iter().find(|h| { @@ -182,6 +223,9 @@ impl Hosts { drop(list); return self.remember_macs(&key, a.macs); } + if !a.addr.parse().is_ok_and(|ip| self.may_dial(ip)) { + return tracing::debug!(addr = %a.addr, "advert at an address this server does not dial"); + } self.remember_macs(&format!("{}:{}", a.addr, a.port), a.macs.clone()); if let Some(h) = list .iter_mut() @@ -273,7 +317,7 @@ pub fn discover(hosts: Arc) { } /// Write `path` whole or not at all: a crash mid-write leaves the old file, never half of one. -fn write_whole(path: &Path, bytes: &[u8]) -> std::io::Result<()> { +pub(crate) fn write_whole(path: &Path, bytes: &[u8]) -> std::io::Result<()> { let tmp = path.with_extension("tmp"); std::fs::write(&tmp, bytes)?; std::fs::rename(&tmp, path) @@ -310,6 +354,7 @@ pub fn hex(b: &[u8]) -> String { #[cfg(test)] mod tests { use super::*; + use crate::visitor::TEST; fn listed(name: &str, addr: &str) -> Listed { Listed { @@ -330,6 +375,7 @@ mod tests { ], &dir, true, + TEST, ) .unwrap(); let ids: Vec<_> = hosts.all().into_iter().map(|h| h.id).collect(); @@ -342,7 +388,7 @@ mod tests { let dir = std::env::temp_dir().join(format!("pf-adverts-{}", std::process::id())); let mut desk = listed("desk", "10.0.0.2"); desk.pin = Some([1; 32]); - let hosts = Hosts::new(vec![desk], &dir, true).unwrap(); + let hosts = Hosts::new(vec![desk], &dir, true, TEST).unwrap(); let advert = |name: &str, addr: &str, pin| Advert { fullname: format!("{name}._punktfunk._udp.local."), name: name.into(), @@ -365,13 +411,56 @@ mod tests { let _ = std::fs::remove_dir_all(dir); } + #[test] + fn an_advert_at_loopback_or_off_the_home_network_is_ignored() { + let dir = std::env::temp_dir().join(format!("pf-self-adverts-{}", std::process::id())); + let hosts = Hosts::new(Vec::new(), &dir, true, Machine::REAL).unwrap(); + for (name, addr) in [ + ("loop", "127.0.0.1"), + ("far", "203.0.113.5"), + ("couch", "10.0.0.3"), + ] { + hosts.found(Advert { + fullname: format!("{name}._punktfunk._udp.local."), + name: name.into(), + addr: addr.into(), + port: crate::config::MGMT_PORT, + pin: None, + macs: Vec::new(), + }); + } + let ids: Vec<_> = hosts.all().into_iter().map(|h| h.id).collect(); + assert_eq!(ids, ["couch"]); + let _ = std::fs::remove_dir_all(dir); + } + + #[test] + fn unlisted_pins_keep_the_most_recent_64() { + let dir = std::env::temp_dir().join(format!("pf-pin-cap-{}", std::process::id())); + let hosts = Hosts::new(vec![listed("desk", "10.0.0.2")], &dir, true, TEST).unwrap(); + hosts.learned("desk", [1; 32]); + for i in 0..=KEPT { + hosts.keep(&format!("10.1.0.{i}:47990"), [2; 32]); + if i == 1 { + // Used again: the first is now the least recently used. + hosts.keep("10.1.0.0:47990", [2; 32]); + } + } + assert_eq!(hosts.pin_at("10.1.0.1:47990"), None); + assert!(hosts.pin_at("10.1.0.0:47990").is_some()); + assert!(hosts.pin_at(&format!("10.1.0.{KEPT}:47990")).is_some()); + let again = Hosts::new(vec![listed("desk", "10.0.0.2")], &dir, true, TEST).unwrap(); + assert_eq!(again.get("desk").unwrap().pin, Some([1; 32])); + let _ = std::fs::remove_dir_all(dir); + } + #[test] fn a_first_contact_pin_survives_a_restart() { let dir = std::env::temp_dir().join(format!("pf-pins-{}", std::process::id())); - let hosts = Hosts::new(vec![listed("desk", "10.0.0.2")], &dir, true).unwrap(); + let hosts = Hosts::new(vec![listed("desk", "10.0.0.2")], &dir, true, TEST).unwrap(); hosts.learned("desk", [7; 32]); hosts.learned("desk", [9; 32]); // a second certificate does not replace the first - let again = Hosts::new(vec![listed("desk", "10.0.0.2")], &dir, true).unwrap(); + let again = Hosts::new(vec![listed("desk", "10.0.0.2")], &dir, true, TEST).unwrap(); assert_eq!(again.get("desk").unwrap().pin, Some([7; 32])); let _ = std::fs::remove_dir_all(dir); } @@ -379,7 +468,7 @@ mod tests { #[test] fn a_host_that_can_be_woken_outlives_its_advert() { let dir = std::env::temp_dir().join(format!("pf-wake-{}", std::process::id())); - let hosts = Hosts::new(vec![listed("desk", "10.0.0.2")], &dir, true).unwrap(); + let hosts = Hosts::new(vec![listed("desk", "10.0.0.2")], &dir, true, TEST).unwrap(); let advert = |name: &str, addr: &str, macs: &[&str]| Advert { fullname: format!("{name}._punktfunk._udp.local."), name: name.into(), @@ -401,7 +490,7 @@ mod tests { assert_eq!(ids, ["desk", "couch"]); // And its MACs survive a restart of this server. - let again = Hosts::new(vec![listed("desk", "10.0.0.2")], &dir, true).unwrap(); + let again = Hosts::new(vec![listed("desk", "10.0.0.2")], &dir, true, TEST).unwrap(); assert_eq!(again.macs("desk"), ["aa:bb:cc:dd:ee:01"]); let _ = std::fs::remove_dir_all(dir); } diff --git a/server/src/lanes.rs b/server/src/lanes.rs new file mode 100644 index 0000000..b1b7958 --- /dev/null +++ b/server/src/lanes.rs @@ -0,0 +1,450 @@ +//! Who uses this server, and who only knocks at it. +//! +//! A pass is a secret this server gives a browser that proved itself to a host with a device +//! token. It opens nothing on a host. Here it lifts the limits a stranger meets for that host: +//! the meter, the pairing pool, the short `config.json`, no wake. + +use std::collections::{HashMap, HashSet}; +use std::net::IpAddr; +use std::path::{Path, PathBuf}; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant, SystemTime}; +use tokio::sync::{OwnedSemaphorePermit, Semaphore}; + +/// A pass holds for 30 days after it was last proven to a host. +const PASS_LIFE: u64 = 30 * 24 * 60 * 60; +/// Passes kept; the least recently proven goes first. +const PASSES: usize = 256; +/// Bridge sessions for browsers with a pass for the host. +pub const PAIRED: usize = 12; +/// Bridge sessions for strangers. The host keeps 4 pending knocks per address, so a relay can +/// never push a real knock out of its queue. +const PAIRING: usize = 4; +/// A pairing session's longest life: the host parks a knock for 180 s. +pub const PAIRING_LIFE: Duration = Duration::from_secs(180); + +/// The hosts a request's pass was proven to, by identity fingerprint. Empty: a stranger. +#[derive(Clone, Default)] +pub struct Pass(pub Arc<[[u8; 32]]>); + +impl Pass { + pub fn stranger(&self) -> bool { + self.0.is_empty() + } + + /// A pass for the host whose identity is `fp`. + pub fn covers(&self, fp: Option<[u8; 32]>) -> bool { + fp.is_some_and(|fp| self.0.contains(&fp)) + } +} + +/// What `passes.json` keeps of a pass: never the pass, only its SHA-256. +#[derive(Clone, serde::Serialize, serde::Deserialize)] +struct Record { + hash: String, + hosts: Vec, + /// Unix seconds. + proven: u64, +} + +pub struct Passes { + path: PathBuf, + all: Mutex>, +} + +impl Passes { + pub fn load(data: &Path) -> Passes { + let path = data.join("passes.json"); + let all = std::fs::read(&path) + .ok() + .and_then(|b| serde_json::from_slice(&b).ok()) + .unwrap_or_default(); + Passes { + path, + all: Mutex::new(all), + } + } + + /// The hosts `pass` was proven to, while it holds. Empty for one this server never gave. + pub fn hosts(&self, pass: &str) -> Pass { + let all = self.all.lock().unwrap(); + let held = digest(pass).and_then(|h| all.iter().find(|r| r.hash == h)); + let hosts = held + .filter(|r| now() < r.proven + PASS_LIFE) + .map(|r| r.hosts.iter().filter_map(|h| parse(h)).collect()) + .unwrap_or_default(); + Pass(hosts) + } + + /// `pass` proven to the host `fp` again, or a new pass when it is not one of this server's. + pub fn prove(&self, pass: Option<&str>, fp: [u8; 32]) -> String { + let mut all = self.all.lock().unwrap(); + let (now, host) = (now(), crate::hosts::hex(&fp)); + all.retain(|r| now < r.proven + PASS_LIFE); + let held = pass.and_then(|p| Some((p, digest(p)?))); + let held = held.and_then(|(p, h)| Some((p, all.iter().position(|r| r.hash == h)?))); + let (pass, mut record) = match held { + Some((p, i)) => (p.to_owned(), all.remove(i)), + None => { + let mut fresh = [0u8; 32]; + getrandom::fill(&mut fresh).expect("the system has randomness"); + let pass = crate::hosts::hex(&fresh); + let hash = digest(&pass).expect("a pass this server made"); + let record = Record { + hash, + hosts: Vec::new(), + proven: now, + }; + (pass, record) + } + }; + if !record.hosts.contains(&host) { + record.hosts.push(host); + } + record.proven = now; + all.push(record); + if all.len() > PASSES { + all.remove(0); + } + let json = serde_json::to_vec_pretty(&*all).unwrap_or_default(); + if let Err(e) = crate::hosts::write_whole(&self.path, &json) { + tracing::warn!(error = %e, "passes.json was not written; the pass lasts until restart"); + } + pass + } +} + +/// A pass is 64 hex digits; what is kept is the hex of its SHA-256. +fn digest(pass: &str) -> Option { + use sha2::Digest as _; + let bytes = parse(pass)?; + Some(crate::hosts::hex(&sha2::Sha256::digest(bytes))) +} + +fn parse(hex: &str) -> Option<[u8; 32]> { + crate::config::parse_fp(hex).ok() +} + +fn now() -> u64 { + SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap_or_default() + .as_secs() +} + +/// `n` requests a `per`, refilled evenly. +#[derive(Clone, Copy)] +struct Rate { + n: f64, + per: Duration, +} + +#[derive(Clone, Copy)] +struct Bucket { + tokens: f64, + at: Instant, +} + +impl Bucket { + fn full(rate: Rate, now: Instant) -> Bucket { + Bucket { + tokens: rate.n, + at: now, + } + } + + fn refill(&mut self, rate: Rate, now: Instant) { + let gained = now.duration_since(self.at).as_secs_f64() * rate.n / rate.per.as_secs_f64(); + self.tokens = (self.tokens + gained).min(rate.n); + self.at = now; + } + + /// How long until one token is back. + fn wait(&self, rate: Rate) -> Duration { + rate.per.mul_f64(((1.0 - self.tokens) / rate.n).max(0.0)) + } +} + +/// Addresses a meter tracks. Past this, a new one shares the unknown visitors' bucket. +const KEYS: usize = 4096; +/// How often a full meter forgets the addresses whose buckets have refilled. +const SWEEP: Duration = Duration::from_secs(1); + +/// The key a per-address limit counts under. One IPv6 subscriber holds a whole /64. +pub fn subscriber(ip: IpAddr) -> IpAddr { + match ip.to_canonical() { + IpAddr::V6(v6) => IpAddr::V6((u128::from(v6) & !u128::from(u64::MAX)).into()), + v4 => v4, + } +} + +/// A token bucket per visitor address, and one for all of them. Unknown visitors share a key. +pub struct Meter { + each: Rate, + all: Rate, + buckets: Mutex, +} + +#[derive(Default)] +struct Buckets { + each: HashMap, Bucket>, + all: Option, + swept: Option, +} +/// The addresses with a pairing session open. +type Pairing = Arc>>>; + +impl Meter { + fn new(each: u32, all: u32, per: Duration) -> Meter { + let rate = |n| Rate { + n: f64::from(n), + per, + }; + Meter { + each: rate(each), + all: rate(all), + buckets: Mutex::default(), + } + } + + /// One request from `who`, or how long until there is room for it. + pub fn take(&self, who: Option) -> Result<(), Duration> { + self.take_at(who, Instant::now()) + } + + fn take_at(&self, who: Option, now: Instant) -> Result<(), Duration> { + let mut guard = self.buckets.lock().unwrap(); + let b = &mut *guard; + let (e, a) = (self.each, self.all); + let due = b.swept.is_none_or(|t| now.duration_since(t) >= SWEEP); + if b.each.len() >= KEYS && due { + b.swept = Some(now); + b.each.retain(|_, b| { + b.refill(e, now); + b.tokens < e.n + }); + } + let who = who.map(subscriber); + let who = match b.each.len() >= KEYS && !b.each.contains_key(&who) { + true => None, + false => who, + }; + let mine = b.each.entry(who).or_insert(Bucket::full(e, now)); + let all = b.all.get_or_insert(Bucket::full(a, now)); + mine.refill(e, now); + all.refill(a, now); + if mine.tokens < 1.0 || all.tokens < 1.0 { + return Err(mine.wait(e).max(all.wait(a))); + } + mine.tokens -= 1.0; + all.tokens -= 1.0; + Ok(()) + } +} + +/// The bridge's two pools. +pub struct Pools { + paired: Arc, + pairing: Arc, + pairing_at: Pairing, + /// 20 pairing sessions an hour, for everyone together. + starts: Meter, +} + +/// A bridge session's place in its pool, given back when the session ends. +pub struct Slot { + _permit: OwnedSemaphorePermit, + pairing: Option<(Pairing, Option)>, +} + +impl Drop for Slot { + fn drop(&mut self) { + if let Some((at, who)) = &self.pairing { + at.lock().unwrap().remove(who); + } + } +} + +const PAIRED_FULL: &str = + "this page is carrying as many streams as it can. Try again in a few minutes"; +pub const PAIRING_FULL: &str = + "too many browsers are pairing through this page right now. Try again in a few minutes"; +const PAIRING_HERE: &str = + "a browser at this address is already pairing through this page. Try again when it's done"; +pub const PAIRING_HOURLY: &str = + "this page has taken all the pairing requests it allows for this hour. Try again later"; + +impl Pools { + /// A slot in the paired pool, or the page's sentence. + pub fn paired(&self) -> Result { + let permit = self.paired.clone().try_acquire_owned(); + let permit = permit.map_err(|_| PAIRED_FULL)?; + Ok(Slot { + _permit: permit, + pairing: None, + }) + } + + /// A slot in the pairing pool for a stranger at `who`, or the page's sentence. + pub fn pairing(&self, who: Option) -> Result { + let who = who.map(subscriber); + let mut at = self.pairing_at.lock().unwrap(); + if at.contains(&who) { + return Err(PAIRING_HERE); + } + let permit = self.pairing.clone().try_acquire_owned(); + let permit = permit.map_err(|_| PAIRING_FULL)?; + self.starts.take(None).map_err(|_| PAIRING_HOURLY)?; + at.insert(who); + Ok(Slot { + _permit: permit, + pairing: Some((self.pairing_at.clone(), who)), + }) + } +} + +/// The limits strangers share, and the passes that lift them. +pub struct Lanes { + pub passes: Passes, + /// Every non-static request, and every refusal, from a stranger. + pub strangers: Meter, + /// Proving an address a stranger typed. + pub proofs: Meter, + pub pools: Pools, +} + +impl Lanes { + pub fn new(data: &Path) -> Lanes { + const MINUTE: Duration = Duration::from_secs(60); + const HOUR: Duration = Duration::from_secs(3600); + // 6 a minute each, 30 an hour together: the hourly bucket refills at 0.5 a minute. + let mut proofs = Meter::new(6, 30, MINUTE); + proofs.all = Rate { n: 30.0, per: HOUR }; + Lanes { + passes: Passes::load(data), + strangers: Meter::new(30, 300, MINUTE), + proofs, + pools: Pools { + paired: Arc::new(Semaphore::new(PAIRED)), + pairing: Arc::new(Semaphore::new(PAIRING)), + pairing_at: Arc::default(), + starts: Meter::new(20, 20, HOUR), + }, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_pass_holds_for_its_hosts_until_it_lapses_and_survives_a_restart() { + let dir = std::env::temp_dir().join(format!("pf-passes-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let passes = Passes::load(&dir); + let pass = passes.prove(None, [1; 32]); + assert!(passes.hosts(&pass).covers(Some([1; 32]))); + assert!(!passes.hosts(&pass).covers(Some([2; 32]))); + // Extended, not replaced. + assert_eq!(passes.prove(Some(&pass), [2; 32]), pass); + // A forged pass, or one this server made for someone else, is a stranger's. + assert!(passes.hosts(&"ab".repeat(32)).stranger()); + assert!(passes.hosts("not hex").stranger()); + assert_ne!( + passes.prove(Some(&"ab".repeat(32)), [1; 32]), + "ab".repeat(32) + ); + + let again = Passes::load(&dir); + assert!(again.hosts(&pass).covers(Some([2; 32]))); + let kept = std::fs::read_to_string(dir.join("passes.json")).unwrap(); + assert!(!kept.contains(&pass), "the pass itself is never written"); + + again.all.lock().unwrap()[0].proven = now() - PASS_LIFE; + assert!(again.hosts(&pass).stranger(), "lapsed"); + let _ = std::fs::remove_dir_all(dir); + } + + #[test] + fn the_meter_refuses_then_refills() { + let meter = Meter::new(30, 300, Duration::from_secs(60)); + let (t0, me, you) = (Instant::now(), Some([10, 0, 0, 1].into()), None); + for _ in 0..30 { + meter.take_at(me, t0).unwrap(); + } + let wait = meter.take_at(me, t0).unwrap_err(); + assert_eq!(wait, Duration::from_secs(2)); + meter.take_at(you, t0).unwrap(); + meter.take_at(me, t0 + Duration::from_secs(2)).unwrap(); + // All strangers together. + let all = Meter::new(30, 300, Duration::from_secs(60)); + for i in 0..300u32 { + all.take_at(Some(IpAddr::from(i.to_be_bytes())), t0) + .unwrap(); + } + assert!(all.take_at(Some([10, 9, 9, 9].into()), t0).is_err()); + + // One /64 is one subscriber. + let v6 = Meter::new(30, 300, Duration::from_secs(60)); + for i in 0..30u16 { + let ip = std::net::Ipv6Addr::new(0x2001, 0xdb8, 0, 1, i, i, i, i); + v6.take_at(Some(ip.into()), t0).unwrap(); + } + let next = std::net::Ipv6Addr::new(0x2001, 0xdb8, 0, 1, 9, 9, 9, 99); + assert!(v6.take_at(Some(next.into()), t0).is_err()); + let other = std::net::Ipv6Addr::new(0x2001, 0xdb8, 0, 2, 0, 0, 0, 1); + v6.take_at(Some(other.into()), t0).unwrap(); + } + + #[test] + fn a_full_meter_shares_one_bucket_among_new_addresses() { + let meter = Meter::new(2, 1 << 20, Duration::from_secs(60)); + let t0 = Instant::now(); + let at = |i: u32| Some(IpAddr::from(i.to_be_bytes())); + for i in 1..=KEYS as u32 { + meter.take_at(at(i), t0).unwrap(); + } + // Full, and nothing has refilled: two newcomers share the unknown visitors' two. + let new = KEYS as u32 + 1; + meter.take_at(at(new), t0).unwrap(); + meter.take_at(at(new + 1), t0).unwrap(); + assert!(meter.take_at(at(new + 2), t0).is_err()); + assert!(meter.buckets.lock().unwrap().each.len() <= KEYS + 1); + // A minute on, the sweep forgets the refilled ones and a newcomer has its own again. + let later = t0 + Duration::from_secs(60); + meter.take_at(at(new + 3), later).unwrap(); + meter.take_at(at(new + 3), later).unwrap(); + assert!(meter.buckets.lock().unwrap().each.len() < 8); + } + + #[test] + fn the_pairing_pool_holds_four_one_per_address_twenty_an_hour() { + let lanes = Lanes::new(&std::env::temp_dir()); + let at = |i: u8| Some(IpAddr::from([10, 0, 0, i])); + let held: Vec<_> = (0..4) + .map(|i| lanes.pools.pairing(at(i)).unwrap()) + .collect(); + assert_eq!(lanes.pools.pairing(at(9)).err(), Some(PAIRING_FULL)); + drop(held); + let one = lanes.pools.pairing(at(1)).unwrap(); + assert_eq!(lanes.pools.pairing(at(1)).err(), Some(PAIRING_HERE)); + drop(one); + let v6 = |i: u16| Some(IpAddr::from([0x2001, 0xdb8, 0, 1, 0, 0, 0, i])); + let one = lanes.pools.pairing(v6(1)).unwrap(); + assert_eq!( + lanes.pools.pairing(v6(2)).err(), + Some(PAIRING_HERE), + "one /64" + ); + drop(one); + for i in 7..21 { + drop(lanes.pools.pairing(at(i)).unwrap()); + } + assert_eq!(lanes.pools.pairing(at(30)).err(), Some(PAIRING_HOURLY)); + let paired: Vec<_> = (0..PAIRED).map(|_| lanes.pools.paired().unwrap()).collect(); + assert_eq!(lanes.pools.paired().err(), Some(PAIRED_FULL)); + drop(paired); + } +} diff --git a/server/src/listen.rs b/server/src/listen.rs new file mode 100644 index 0000000..ed5e6a7 --- /dev/null +++ b/server/src/listen.rs @@ -0,0 +1,201 @@ +//! The listener's own limits, the host's numbers: the part that faces the internet is no softer +//! than the host behind it. + +use axum::Router; +use axum_server::accept::Accept; +use axum_server::tls_rustls::{RustlsAcceptor, RustlsConfig}; +use axum_server::Handle; +use std::collections::HashMap; +use std::io; +use std::net::{IpAddr, SocketAddr}; +use std::pin::Pin; +use std::sync::{Arc, Mutex}; +use std::task::{Context, Poll}; +use std::time::Duration; +use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; +use tokio::net::TcpStream; +use tokio::sync::{OwnedSemaphorePermit, Semaphore}; + +pub const CONNS: usize = 256; +pub const PER_PEER: usize = 32; +const HANDSHAKE: Duration = Duration::from_secs(10); +/// Per request on a kept-alive connection: a peer that never finishes its headers holds nothing. +const HEADERS: Duration = Duration::from_secs(30); + +type Open = Arc>>; + +/// Admits a connection while there is room: `total` at once, `per_peer` from one TCP peer (an +/// IPv6 peer by its /64). One past either is dropped at accept, before TLS. +#[derive(Clone)] +pub struct Capped { + total: Arc, + per_peer: usize, + open: Open, +} + +impl Capped { + pub fn new(total: usize, per_peer: usize) -> Capped { + Capped { + total: Arc::new(Semaphore::new(total)), + per_peer, + open: Open::default(), + } + } + + fn admit(&self, stream: TcpStream) -> io::Result { + let peer = crate::lanes::subscriber(stream.peer_addr()?.ip()); + let permit = self.total.clone().try_acquire_owned(); + let permit = permit.map_err(|_| io::Error::other("connection ceiling"))?; + let mut open = self.open.lock().unwrap(); + let n = open.entry(peer).or_insert(0); + if *n >= self.per_peer { + return Err(io::Error::other("per-peer connection ceiling")); + } + *n += 1; + let peer = Peer(self.open.clone(), peer); + Ok(Guarded { + inner: stream, + _held: (permit, peer), + }) + } +} + +impl Accept for Capped { + type Stream = Guarded; + type Service = S; + type Future = std::future::Ready>; + + fn accept(&self, stream: TcpStream, service: S) -> Self::Future { + std::future::ready(self.admit(stream).map(|s| (s, service))) + } +} + +/// One peer's count, given back with the connection. +struct Peer(Open, IpAddr); + +impl Drop for Peer { + fn drop(&mut self) { + let mut open = self.0.lock().unwrap(); + if let Some(n) = open.get_mut(&self.1) { + *n -= 1; + if *n == 0 { + open.remove(&self.1); + } + } + } +} + +/// A connection holding its place until it closes. +pub struct Guarded { + inner: TcpStream, + _held: (OwnedSemaphorePermit, Peer), +} + +impl AsyncRead for Guarded { + fn poll_read( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + buf: &mut ReadBuf<'_>, + ) -> Poll> { + Pin::new(&mut self.inner).poll_read(cx, buf) + } +} + +impl AsyncWrite for Guarded { + fn poll_write( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + buf: &[u8], + ) -> Poll> { + Pin::new(&mut self.inner).poll_write(cx, buf) + } + + fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + Pin::new(&mut self.inner).poll_flush(cx) + } + + fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + Pin::new(&mut self.inner).poll_shutdown(cx) + } +} + +/// Serve `app` on `listener` under `caps`, over TLS when there is a certificate. +pub async fn serve( + listener: std::net::TcpListener, + app: Router, + tls: Option, + handle: Handle, + caps: Capped, +) -> io::Result<()> { + listener.set_nonblocking(true)?; + // Who the visitor is starts from the TCP peer. + let service = app.into_make_service_with_connect_info::(); + let server = axum_server::from_tcp(listener)?.handle(handle); + match tls { + Some(tls) => { + let tls = RustlsAcceptor::new(tls).handshake_timeout(HANDSHAKE); + let mut server = server.acceptor(tls.acceptor(caps)); + timeouts(server.http_builder()); + server.serve(service).await + } + None => { + let mut server = server.acceptor(caps); + timeouts(server.http_builder()); + server.serve(service).await + } + } +} + +fn timeouts(b: &mut hyper_util::server::conn::auto::Builder) { + b.http1() + .timer(hyper_util::rt::TokioTimer::new()) + .header_read_timeout(HEADERS); +} + +#[cfg(test)] +mod tests { + use super::*; + use tokio::io::AsyncReadExt; + + async fn listening(caps: Capped) -> SocketAddr { + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let at = listener.local_addr().unwrap(); + let app = Router::new().route("/", axum::routing::get(|| async { "ok" })); + tokio::spawn(serve(listener, app, None, Handle::new(), caps)); + at + } + + /// Held open by the server (the read waits), or dropped at accept (the read ends). + async fn admitted(conn: &mut TcpStream) -> bool { + let mut b = [0u8; 1]; + let read = tokio::time::timeout(Duration::from_millis(300), conn.read(&mut b)).await; + read.is_err() + } + + #[tokio::test] + async fn one_peer_holds_thirty_two_and_all_hold_the_ceiling() { + let at = listening(Capped::new(CONNS, PER_PEER)).await; + let mut held = Vec::new(); + for _ in 0..PER_PEER { + held.push(TcpStream::connect(at).await.unwrap()); + } + // Accepted in order, admitted in tasks: let all 32 settle before the next. + tokio::time::sleep(Duration::from_millis(100)).await; + assert!(admitted(held.last_mut().unwrap()).await); + let mut over = TcpStream::connect(at).await.unwrap(); + assert!(!admitted(&mut over).await, "the 33rd from one peer"); + drop(held.pop()); + tokio::time::sleep(Duration::from_millis(100)).await; + let mut again = TcpStream::connect(at).await.unwrap(); + assert!(admitted(&mut again).await, "room once one closed"); + + let at = listening(Capped::new(3, PER_PEER)).await; + let mut three = Vec::new(); + for _ in 0..3 { + three.push(TcpStream::connect(at).await.unwrap()); + } + tokio::time::sleep(Duration::from_millis(100)).await; + let mut fourth = TcpStream::connect(at).await.unwrap(); + assert!(!admitted(&mut fourth).await, "past the ceiling"); + } +} diff --git a/server/src/main.rs b/server/src/main.rs index eace969..8c08eba 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -9,10 +9,17 @@ mod bridge; mod config; mod hosts; +mod lanes; +mod listen; mod origin; +mod proof; mod proxy; +#[cfg(test)] +mod testing; mod tls; +mod visitor; +use anyhow::Context as _; use axum::extract::{Path, Request, State}; use axum::http::{header, HeaderValue, StatusCode}; use axum::middleware::Next; @@ -20,10 +27,12 @@ use axum::response::{IntoResponse, Response}; use axum::routing::{any, get, post}; use axum::{Extension, Json, Router}; use hosts::Hosts; +use lanes::Pass; +use punktfunk_core::provenance::KnockSource; use std::path::PathBuf; use std::sync::Arc; use std::time::Duration; -use tokio::sync::Semaphore; +use visitor::Visitor; #[tokio::main] async fn main() -> anyhow::Result<()> { @@ -34,44 +43,49 @@ async fn main() -> anyhow::Result<()> { .init(); punktfunk_core::tls::install_default_provider(); let cfg = config::Config::from_env()?; - let hosts = Hosts::new(cfg.hosts.clone(), &cfg.data, cfg.add_hosts)?; + let machine = visitor::Machine::REAL; + let hosts = Hosts::new(cfg.hosts.clone(), &cfg.data, cfg.add_hosts, machine)?; for h in hosts.all() { tracing::info!(host = %h.id, addr = %h.addr, port = h.port, pinned = h.pin.is_some(), "listed"); + // Through loopback every visitor reaches the host as one on its own machine. + let ip = h.addr.trim_matches(['[', ']']).parse::(); + if ip.is_ok_and(|ip| ip.is_loopback()) { + tracing::warn!(host = %h.id, "listed at loopback; list it at its LAN address instead"); + } } if cfg.discover { hosts::discover(hosts.clone()); } - let app = router(hosts, cfg.dist.clone(), cfg.names()); + let front = origin::Front { + names: cfg.names(), + proxied: matches!(cfg.tls, config::Tls::Off), + self_signed: matches!(cfg.tls, config::Tls::SelfSigned), + }; + let app = router(hosts, cfg.dist.clone(), front); let handle = axum_server::Handle::new(); tokio::spawn(shutdown(handle.clone())); - // The bridge tells the host who the real client is. - let service = app.into_make_service_with_connect_info::(); - match tls::config(&cfg).await? { - Some(tls) => { - tracing::info!("serving the browser client at https://{}", cfg.listen); - axum_server::bind_rustls(cfg.listen, tls) - .handle(handle) - .serve(service) - .await?; - } - None => { - tracing::info!( - "serving the browser client at http://{} (TLS is off)", - cfg.listen - ); - axum_server::bind(cfg.listen) - .handle(handle) - .serve(service) - .await?; - } - } + let tls = tls::config(&cfg).await?; + let scheme = if tls.is_some() { "https" } else { "http" }; + tracing::info!("serving the browser client at {scheme}://{}", cfg.listen); + let listener = + std::net::TcpListener::bind(cfg.listen).with_context(|| format!("bind {}", cfg.listen))?; + // Behind a proxy (`TLS=off`) every connection is the proxy's: only the total holds. + let per_peer = if tls.is_some() { + listen::PER_PEER + } else { + listen::CONNS + }; + let caps = listen::Capped::new(listen::CONNS, per_peer); + listen::serve(listener, app, tls, handle, caps).await?; Ok(()) } /// Every route but the static files answers only this server's own page; every response, the -/// files included, answers only the names in `names`. -pub(crate) fn router(hosts: Arc, dist: PathBuf, names: origin::Names) -> Router { +/// files included, answers only the names `front` allows, and knows its visitor. +pub(crate) fn router(hosts: Arc, dist: PathBuf, front: origin::Front) -> Router { + let visitor = (hosts.clone(), front.proxied); + let gate = (front, hosts.clone()); Router::new() .route("/config.json", get(config_json)) .route("/h/{id}/{*rest}", any(proxy::listed)) @@ -79,24 +93,39 @@ pub(crate) fn router(hosts: Arc, dist: PathBuf, names: origin::Names) -> .route("/wake/{id}", post(wake)) .route("/w/{id}", get(bridge::listed)) .route("/wa/{target}", get(bridge::typed)) + .route("/pass/h/{id}", post(proxy::pass_listed)) + .route("/pass/a/{target}", post(proxy::pass_typed)) .route_layer(axum::middleware::from_fn(origin::own_page)) .with_state(hosts) - .layer(Extension(Arc::new(Semaphore::new(bridge::SESSIONS)))) .fallback_service(tower_http::services::ServeDir::new(dist)) .layer(axum::middleware::from_fn(cache)) - .layer(axum::middleware::from_fn_with_state(names, origin::guard)) + .layer(axum::middleware::from_fn_with_state(gate, origin::gate)) + .layer(axum::middleware::from_fn_with_state(visitor, visitor::find)) + .layer(axum::middleware::from_fn(origin::headers)) } /// The hosts the page lists, and whether it may reach others by address through `/a/`. `api` and /// `bridge` are relative, so a page served under a path still resolves them. A typed host's bridge /// is `wa/`, which the page composes itself, as it does `a/`. -async fn config_json(State(state): State>) -> impl IntoResponse { +/// +/// A host's LAN address (`plane`) goes to a browser with a pass for it, or one at home; `wake` +/// only to the first. +async fn config_json( + State(state): State>, + Extension(pass): Extension, + Extension(visitor): Extension, +) -> impl IntoResponse { + let home = visitor.class == KnockSource::Lan; let hosts: Vec<_> = state .all() .into_iter() .map(|h| { - let mut j = serde_json::json!({ "id": h.id, "name": h.name, "api": format!("h/{}", h.id), "bridge": format!("w/{}", h.id), "plane": h.addr }); - if !state.macs(&h.id).is_empty() { + let mut j = serde_json::json!({ "id": h.id, "name": h.name, "api": format!("h/{}", h.id), "bridge": format!("w/{}", h.id) }); + let ours = pass.covers(h.pin); + if ours || home { + j["plane"] = h.addr.clone().into(); + } + if ours && !state.macs(&h.id).is_empty() { j["wake"] = format!("wake/{}", h.id).into(); } j @@ -107,11 +136,18 @@ async fn config_json(State(state): State>) -> impl IntoResponse { } /// Wake a listed host: a magic packet to every MAC it announced while awake, from this machine, -/// which is on the host's network where a browser cannot send one. -async fn wake(State(state): State>, Path(id): Path) -> StatusCode { +/// which is on the host's network where a browser cannot send one. Only for a pass for that host. +async fn wake( + State(state): State>, + Path(id): Path, + Extension(pass): Extension, +) -> StatusCode { let Some(host) = state.get(&id) else { return StatusCode::NOT_FOUND; }; + if !pass.covers(host.pin) { + return StatusCode::FORBIDDEN; + } let macs: Vec<_> = state .macs(&id) .iter() @@ -179,60 +215,91 @@ mod tests { use axum::http::Request; use tower::ServiceExt; - #[tokio::test] - async fn config_lists_hosts_and_whether_typed_ones_are_proxied() { - let dir = std::env::temp_dir().join(format!("pf-config-{}", std::process::id())); - let desk = Listed { + fn desk() -> Listed { + Listed { name: "Desk".into(), addr: "10.0.0.2".into(), port: 47990, - pin: None, - }; + pin: Some([7; 32]), + } + } + + /// From `peer`, with `pass` when there is one. + fn from(path: &str, peer: &str, pass: Option<&str>) -> Request { + let mut req = Request::get(path); + if let Some(pass) = pass { + req = req.header("pf-pass", pass); + } + let mut req = req.body(Body::empty()).unwrap(); + let peer: std::net::SocketAddr = format!("{peer}:50000").parse().unwrap(); + req.extensions_mut() + .insert(axum::extract::ConnectInfo(peer)); + req + } + + #[tokio::test] + async fn config_tells_a_stranger_names_and_a_pass_holder_everything() { + let dir = std::env::temp_dir().join(format!("pf-config-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); for add in [true, false] { - let hosts = crate::Hosts::new(vec![desk.clone()], &dir, add).unwrap(); - let res = crate::router(hosts, dir.clone(), None) - .oneshot(Request::get("/config.json").body(Body::empty()).unwrap()) - .await - .unwrap(); - let body = axum::body::to_bytes(res.into_body(), 1 << 16) - .await - .unwrap(); - let json: serde_json::Value = serde_json::from_slice(&body).unwrap(); - assert_eq!(json["add"], add); - assert_eq!(json["hosts"][0]["api"], "h/desk"); - assert_eq!(json["hosts"][0]["bridge"], "w/desk"); - assert_eq!(json["hosts"][0]["plane"], "10.0.0.2"); + let hosts = crate::Hosts::new(vec![desk()], &dir, add, crate::visitor::TEST).unwrap(); + let pass = hosts.lanes.passes.prove(None, [7; 32]); + let elsewhere = hosts.lanes.passes.prove(None, [8; 32]); + let config = |req: Request| { + let app = crate::router(hosts.clone(), dir.clone(), Default::default()); + async move { + let res = app.oneshot(req).await.unwrap(); + let body = axum::body::to_bytes(res.into_body(), 1 << 16).await; + let json: serde_json::Value = serde_json::from_slice(&body.unwrap()).unwrap(); + assert_eq!(json["add"], add); + assert_eq!(json["hosts"][0]["api"], "h/desk"); + assert_eq!(json["hosts"][0]["bridge"], "w/desk"); + json["hosts"][0]["plane"].as_str().map(str::to_owned) + } + }; + let plane = Some("10.0.0.2".to_string()); + assert_eq!( + config(from("/config.json", "203.0.113.9", None)).await, + None + ); + let at_home = from("/config.json", "192.168.1.20", None); + assert_eq!(config(at_home).await, plane); + let theirs = from("/config.json", "203.0.113.9", Some(&elsewhere)); + assert_eq!(config(theirs).await, None); + let ours = from("/config.json", "203.0.113.9", Some(&pass)); + assert_eq!(config(ours).await, plane); + let forged = from("/config.json", "203.0.113.9", Some(&"ab".repeat(32))); + assert_eq!(config(forged).await, None); } let _ = std::fs::remove_dir_all(dir); } #[tokio::test] - async fn wake_goes_only_to_a_known_host_from_this_page() { + async fn wake_goes_only_to_a_known_host_for_a_pass_holder() { let dir = std::env::temp_dir().join(format!("pf-wake-route-{}", std::process::id())); - let desk = Listed { - name: "Desk".into(), - addr: "10.0.0.2".into(), - port: 47990, - pin: None, - }; - let hosts = crate::Hosts::new(vec![desk], &dir, true).unwrap(); - let wake = |path: &str, site: &str| { - Request::post(path) - .header("sec-fetch-site", site) - .body(Body::empty()) - .unwrap() + let _ = std::fs::remove_dir_all(&dir); + let hosts = crate::Hosts::new(vec![desk()], &dir, true, crate::visitor::TEST).unwrap(); + let pass = hosts.lanes.passes.prove(None, [7; 32]); + let wake = |path: &str, site: &str, pass: Option<&str>| { + let mut req = Request::post(path).header("sec-fetch-site", site); + if let Some(pass) = pass { + req = req.header("pf-pass", pass); + } + req.body(Body::empty()).unwrap() }; let status = |req: Request| { - let app = crate::router(hosts.clone(), dir.clone(), None); + let app = crate::router(hosts.clone(), dir.clone(), Default::default()); async move { app.oneshot(req).await.unwrap().status() } }; - assert_eq!(status(wake("/wake/desk", "cross-site")).await, 403); - assert_eq!(status(wake("/wake/nowhere", "same-origin")).await, 404); + let ours = Some(pass.as_str()); + assert_eq!(status(wake("/wake/desk", "cross-site", ours)).await, 403); + assert_eq!(status(wake("/wake/desk", "same-origin", None)).await, 403); assert_eq!( - status(wake("/wake/desk", "same-origin")).await, - 409, - "no MAC known yet" + status(wake("/wake/nowhere", "same-origin", ours)).await, + 404 ); + let known = status(wake("/wake/desk", "same-origin", ours)).await; + assert_eq!(known, 409, "no MAC known yet"); let _ = std::fs::remove_dir_all(dir); } } diff --git a/server/src/origin.rs b/server/src/origin.rs index 93c8556..fe12c81 100644 --- a/server/src/origin.rs +++ b/server/src/origin.rs @@ -3,17 +3,33 @@ //! The device key lives in the origin, so a site that rebinds a name to this server, or a page on //! another site that calls its routes, must get nothing back. +use crate::hosts::Hosts; +use crate::lanes::Pass; use crate::proxy::own_authority; -use axum::extract::{Request, State}; +use crate::visitor::{Machine, Visitor}; +use axum::extract::{ConnectInfo, Request, State}; use axum::http::{header, HeaderMap, HeaderValue, StatusCode, Uri}; use axum::middleware::Next; use axum::response::{IntoResponse, Response}; -use std::net::{Ipv4Addr, Ipv6Addr}; +use punktfunk_core::provenance::is_local_name; +use punktfunk_core::provenance::KnockSource; +use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr}; use std::sync::Arc; +use std::time::Duration; /// `TLS_NAMES`, lowercase, without a trailing dot. `None` checks no name at all. pub type Names = Option>; +/// What stands in front of the server, from its TLS settings. +#[derive(Clone, Default)] +pub struct Front { + pub names: Names, + /// `TLS=off`: a proxy is in front by definition. + pub proxied: bool, + /// `TLS=self-signed`: a certificate a browser cannot tell from an impostor's. + pub self_signed: bool, +} + /// The page's own scripts and wasm, nothing injected, never framed. `blob:` scripts are the audio /// worklets; `blob:` fetches and images, the art read from a host; `data:` images, its cursor; /// `https:`, every host the page dials and every cover it shows. Inline styles are `index.html`'s @@ -23,18 +39,10 @@ const CSP: &str = "default-src 'self'; script-src 'self' 'wasm-unsafe-eval' blob connect-src 'self' https: blob:; object-src 'none'; base-uri 'none'; \ form-action 'none'; frame-ancestors 'none'"; -/// Every request: the name first, then this server's word on the origin. A host's reply keeps the -/// `sandbox` CSP the relay gave it; the rest is set over whatever the host sent. -pub async fn guard(State(names): State, req: Request, next: Next) -> Response { - let name = own_authority(req.uri(), req.headers()).map(name_of); - let mut res = match name { - Some(name) if !answers(names.as_deref(), &name) => { - tracing::warn!(name, "refused a name not in TLS_NAMES"); - let say = format!("Add {name} to TLS_NAMES to open the page at that name."); - (StatusCode::MISDIRECTED_REQUEST, say).into_response() - } - _ => next.run(req).await, - }; +/// Every response this server sends: its word on the origin. A host's reply keeps the `sandbox` +/// CSP the relay gave it; the rest is set over whatever the host sent. +pub async fn headers(req: Request, next: Next) -> Response { + let mut res = next.run(req).await; let h = res.headers_mut(); h.entry(header::CONTENT_SECURITY_POLICY) .or_insert(HeaderValue::from_static(CSP)); @@ -51,6 +59,89 @@ pub async fn guard(State(names): State, req: Request, next: Next) -> Resp res } +/// Every request: the certificate rule, the name, then the stranger meter. A stranger pays for +/// every routed request and every refusal, so a flood meets `429` before it reaches the log; a +/// `401` or `403` back costs one more. +pub async fn gate( + State((front, hosts)): State<(Front, Arc)>, + req: Request, + next: Next, +) -> Response { + let name = own_authority(req.uri(), req.headers()).map(name_of); + let refused = name.filter(|n| !answers(front.names.as_deref(), n)); + let routed = routed(req.uri().path()); + let outside = front.self_signed && straight_from_outside(&req, &hosts.machine); + let stranger = req.extensions().get::().is_none_or(Pass::stranger); + let who = req.extensions().get::().and_then(|v| v.addr); + if stranger && (routed || refused.is_some() || outside) { + if let Err(wait) = hosts.lanes.strangers.take(who) { + return too_many(wait); + } + } + if outside { + let html = [(header::CONTENT_TYPE, "text/html; charset=utf-8")]; + return (StatusCode::FORBIDDEN, html, OUTSIDE).into_response(); + } + if let Some(name) = refused { + tracing::warn!(name, "refused a name not in TLS_NAMES"); + let say = format!("Add {name} to TLS_NAMES to open the page at that name."); + return (StatusCode::MISDIRECTED_REQUEST, say).into_response(); + } + let res = next.run(req).await; + let refusal = matches!( + res.status(), + StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN + ); + if stranger && routed && refusal { + let _ = hosts.lanes.strangers.take(who); + } + res +} + +/// A browser on a public address that sees the self-made certificate itself: nothing there tells +/// this server from an impostor. With a forwarding header, a proxy ended the browser's TLS. +fn straight_from_outside(req: &Request, m: &Machine) -> bool { + let h = req.headers(); + let forwarded = h.contains_key("x-forwarded-for") || h.contains_key(header::FORWARDED); + let peer = req.extensions().get::>(); + !forwarded && peer.is_some_and(|p| m.class(p.0.ip()) == KnockSource::Wan) +} + +/// What a browser at a public address gets instead of the page: no script, no route. +const OUTSIDE: &str = r#" + + + +Punktfunk + +

This page can't be opened from the internet

+

A self-made certificate gives a browser no way to tell this server from an impostor, so the +page stays closed to addresses outside your network.

+

Reach it over Tailscale, or give it a real certificate. +The documentation shows both.

+ +"#; + +/// A route of this server's own, not a static file. +fn routed(path: &str) -> bool { + path == "/config.json" + || ["/h/", "/a/", "/w/", "/wa/", "/wake/", "/pass/"] + .iter() + .any(|p| path.starts_with(p)) +} + +/// `429`, and when to come back. +pub fn too_many(wait: Duration) -> Response { + let secs = wait.as_secs_f64().ceil().max(1.0).to_string(); + let say = "Too many requests from this address. Wait a minute, then try again."; + ( + StatusCode::TOO_MANY_REQUESTS, + [(header::RETRY_AFTER, secs)], + say, + ) + .into_response() +} + /// Every route but the static files: only this server's own page, or no page at all. pub async fn own_page(req: Request, next: Next) -> Response { if !same_origin(req.uri(), req.headers()) { @@ -96,11 +187,7 @@ fn answers(listed: Option<&[String]>, name: &str) -> bool { Some(v6) => v6.parse::().is_ok(), None => name.parse::().is_ok(), }; - ip || !name.contains('.') - || [".local", ".home.arpa", ".internal"] - .iter() - .any(|zone| name.ends_with(zone)) - || listed.iter().any(|n| n == name) + ip || is_local_name(name) || listed.iter().any(|n| n == name) } #[cfg(test)] @@ -122,11 +209,21 @@ mod tests { port: 47990, pin: None, }; - let hosts = Hosts::new(vec![desk], &dir, true).unwrap(); - crate::router(hosts, dir, names) + let hosts = Hosts::new(vec![desk], &dir, true, crate::visitor::TEST).unwrap(); + let front = super::Front { + names, + ..Default::default() + }; + crate::router(hosts, dir, front) } - async fn send(app: &axum::Router, req: Request) -> axum::response::Response { + /// Each from a peer of its own, so no one visitor's meter runs out. + async fn send(app: &axum::Router, mut req: Request) -> axum::response::Response { + static NEXT: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(1); + let n = NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let peer = std::net::SocketAddr::from((((10u32 << 24) | n).to_be_bytes(), 1)); + req.extensions_mut() + .insert(axum::extract::ConnectInfo(peer)); app.clone().oneshot(req).await.unwrap() } @@ -152,6 +249,7 @@ mod tests { ("box.internal:8443", 200), ("localhost:8443", 200), ("desk", 200), + ("0x8080808", 421), ] { let res = send(&listed, at(host, "/")).await; assert_eq!(res.status(), status, "{host}"); @@ -244,4 +342,74 @@ mod tests { assert_eq!(h["cross-origin-opener-policy"], "same-origin"); } } + + #[tokio::test] + async fn a_self_made_certificate_is_never_shown_to_the_internet() { + let dir = std::env::temp_dir().join(format!("pf-origin-outside-{}", std::process::id())); + std::fs::create_dir_all(dir.join("assets")).unwrap(); + std::fs::write(dir.join("index.html"), "app").unwrap(); + std::fs::write(dir.join("assets/app.js"), "app").unwrap(); + // This machine's own IPv6 prefix is 2001:db8:1::/64. + let machine = crate::visitor::Machine { + on_own_prefix: |v6| v6.segments()[..4] == [0x2001, 0xdb8, 1, 0], + ..crate::visitor::TEST + }; + let hosts = Hosts::new(Vec::new(), &dir, true, machine).unwrap(); + let app = |self_signed, proxied| { + let front = super::Front { + names: None, + proxied, + self_signed, + }; + crate::router(hosts.clone(), dir.clone(), front) + }; + let from = |peer: &str, path: &str, forwarded: bool| { + let mut req = Request::get(path); + if forwarded { + req = req.header("x-forwarded-for", "203.0.113.9"); + } + let mut req = req.body(Body::empty()).unwrap(); + let peer: std::net::SocketAddr = peer.parse().unwrap(); + req.extensions_mut() + .insert(axum::extract::ConnectInfo(peer)); + req + }; + let body = |res: axum::response::Response| async move { + let b = axum::body::to_bytes(res.into_body(), 1 << 16).await; + String::from_utf8(b.unwrap().to_vec()).unwrap() + }; + let public = "203.0.113.9:5000"; + for path in ["/", "/assets/app.js", "/config.json"] { + let res = app(true, false).oneshot(from(public, path, false)).await; + let res = res.unwrap(); + assert_eq!(res.status(), 403, "{path}"); + assert_eq!(res.headers()["content-security-policy"], super::CSP); + let page = body(res).await; + assert!( + page.contains("Tailscale") && !page.contains(" Option { + let say = match self { + Outcome::Host { .. } => return None, + Outcome::NoPlane(_) => NO_PLANE, + Outcome::Refused(say) => say, + Outcome::PinChanged => crate::proxy::PIN_CHANGED, + Outcome::Unreachable => { + let nothing = (StatusCode::GATEWAY_TIMEOUT, "Couldn't reach this host."); + return Some(nothing.into_response()); + } + }; + Some((StatusCode::BAD_GATEWAY, say).into_response()) + } +} + +struct Entry { + outcome: Outcome, + until: Instant, + used: Instant, +} + +/// What each address turned out to be, while it holds. +#[derive(Default)] +pub struct Proofs(Mutex>); + +impl Proofs { + /// Whether proving `key` now would send the address its request. + pub fn would_ask(&self, key: &str, fresh: bool) -> bool { + let all = self.0.lock().unwrap(); + all.get(key).is_none_or(|e| { + e.until <= Instant::now() || (fresh && matches!(e.outcome, Outcome::Host { .. })) + }) + } + + fn recall(&self, key: &str, fresh: bool) -> Option { + let mut all = self.0.lock().unwrap(); + let entry = all.get_mut(key)?; + let stale = entry.until <= Instant::now(); + if stale || (fresh && matches!(entry.outcome, Outcome::Host { .. })) { + return None; + } + entry.used = Instant::now(); + Some(entry.outcome.clone()) + } + + fn remember(&self, key: &str, outcome: &Outcome) { + let now = Instant::now(); + let until = match outcome { + Outcome::Host { expires, .. } => now + secs_until(*expires), + Outcome::NoPlane(_) | Outcome::Refused(_) => now + REFUSED_FOR, + Outcome::PinChanged | Outcome::Unreachable => return, + }; + let mut all = self.0.lock().unwrap(); + all.retain(|_, e| e.until > now); + let entry = Entry { + outcome: outcome.clone(), + until, + used: now, + }; + all.insert(key.to_owned(), entry); + let host = |e: &Entry| matches!(e.outcome, Outcome::Host { .. }); + while all.values().filter(|e| host(e)).count() > KEPT { + let oldest = all + .iter() + .filter(|(_, e)| host(e)) + .min_by_key(|(_, e)| e.used); + let Some(oldest) = oldest.map(|(k, _)| k.clone()) else { + break; + }; + all.remove(&oldest); + } + } +} + +/// A stranger pays the proof meter for each request proving an address they typed would send. +/// The `429` when they are over it. +pub fn over_meter(hosts: &Hosts, ext: &Extensions, key: &str, fresh: bool) -> Option { + let stranger = ext.get::().is_none_or(Pass::stranger); + if !stranger || !hosts.proofs.would_ask(key, fresh) { + return None; + } + let who = ext.get::().and_then(|v| v.addr); + hosts + .lanes + .proofs + .take(who) + .err() + .map(crate::origin::too_many) +} + +/// The proof for `addr:port`: remembered, or fetched now under `pin`. `fresh` passes over a +/// remembered host, for the page's own plane request: a host that restarted has a new plane. +pub async fn prove( + hosts: &Hosts, + addr: &str, + port: u16, + pin: Option<[u8; 32]>, + fresh: bool, +) -> Outcome { + let key = format!("{addr}:{port}"); + if let Some(known) = hosts.proofs.recall(&key, fresh) { + return known; + } + let outcome = fetch(addr, port, pin).await; + match &outcome { + Outcome::Host { + fp, + plane_port, + plane_hash, + .. + } => { + let plane_hash = crate::hosts::hex(plane_hash); + tracing::info!( + host = key, + plane_port, + plane_hash, + "proven a punktfunk host" + ); + hosts.keep(&key, *fp); + } + Outcome::Refused(_) | Outcome::NoPlane(_) => { + tracing::info!( + host = key, + "refused an address that is not a host to relay to" + ); + } + Outcome::PinChanged | Outcome::Unreachable => {} + } + hosts.proofs.remember(&key, &outcome); + outcome +} + +/// One `GET /api/v1/webtransport` under `pin`, with nothing of a visitor's in it. +struct Answer { + status: StatusCode, + json: bool, + body: Bytes, + /// The TLS leaf the address presented. + presented: Option<[u8; 32]>, +} + +async fn ask(addr: &str, port: u16, pin: Option<[u8; 32]>) -> Result { + let seen: Arc>> = Arc::default(); + let client = crate::proxy::client(pin, seen.clone()).map_err(|e| { + tracing::warn!(host = %addr, cause = %format!("{e:#}"), "proof client did not build"); + Outcome::Unreachable + })?; + let url = format!("https://{addr}:{port}/api/v1/webtransport"); + let sent = client.get(url).timeout(TIMEOUT).send().await; + let presented = *seen.lock().unwrap(); + let mut res = match sent { + Ok(res) => res, + Err(_) if pin.is_some() && presented.is_some() && presented != pin => { + return Err(Outcome::PinChanged) + } + Err(e) => { + let cause = format!("{:#}", anyhow::Error::new(e)); + tracing::warn!(host = %addr, cause, "host did not answer the proof"); + return Err(Outcome::Unreachable); + } + }; + let json = res + .headers() + .get(header::CONTENT_TYPE) + .and_then(|v| v.to_str().ok()) + .is_some_and(|t| t.starts_with("application/json")); + let status = res.status(); + let mut body = Vec::new(); + loop { + match res.chunk().await { + Ok(Some(c)) if body.len() + c.len() <= MAX_BODY => body.extend_from_slice(&c), + Ok(None) => break, + _ => return Err(Outcome::Refused(NOT_A_HOST)), + } + } + Ok(Answer { + status, + json, + body: body.into(), + presented, + }) +} + +async fn fetch(addr: &str, port: u16, pin: Option<[u8; 32]>) -> Outcome { + let a = match ask(addr, port, pin).await { + Ok(a) => a, + Err(outcome) => return outcome, + }; + match a.status { + StatusCode::OK if a.json => attested(a.body, a.presented), + StatusCode::NOT_FOUND if a.json && host_error(&a.body) => Outcome::NoPlane(a.body), + _ => Outcome::Refused(NOT_A_HOST), + } +} + +/// The plane a listed host publishes, as it says: the operator's list vouches for the host, so +/// no signature is asked for. +pub async fn published(addr: &str, port: u16, pin: Option<[u8; 32]>) -> Option<(u16, [u8; 32])> { + let a = ask(addr, port, pin).await.ok()?; + let p = (a.status == StatusCode::OK && a.json) + .then(|| serde_json::from_slice::(&a.body).ok())??; + Some((p.port, crate::config::parse_fp(&p.cert_hash_sha256).ok()?)) +} + +/// The host's error shape: `{"error": "…"}`. +fn host_error(body: &[u8]) -> bool { + serde_json::from_slice::(body) + .is_ok_and(|v| v.get("error").is_some_and(|e| e.is_string())) +} + +#[derive(serde::Deserialize)] +struct Published { + port: u16, + cert_hash_sha256: String, + expires_at: u64, + cert_hash_sig: Option, + host_cert_der: Option, +} + +/// A host's plane, signed by the identity that `presented` the TLS leaf: anything less is not a +/// host. One without a signature has an RSA identity, which only an operator's list reaches. +fn attested(body: Bytes, presented: Option<[u8; 32]>) -> Outcome { + let Ok(p) = serde_json::from_slice::(&body) else { + return Outcome::Refused(NOT_A_HOST); + }; + let (Some(sig), Some(der)) = (&p.cert_hash_sig, &p.host_cert_der) else { + return Outcome::Refused(UNATTESTED); + }; + let checked = || { + let hash = crate::config::parse_fp(&p.cert_hash_sha256).ok()?; + let sig = unhex(sig)?; + let der = base64::engine::general_purpose::STANDARD.decode(der).ok()?; + let fp = verify_plane_attestation(&p.cert_hash_sha256, &sig, &der).ok()?; + (Some(fp) == presented).then_some((fp, hash)) + }; + match checked() { + Some((fp, plane_hash)) => Outcome::Host { + fp, + plane_port: p.port, + plane_hash, + expires: p.expires_at, + body, + }, + None => Outcome::Refused(NOT_A_HOST), + } +} + +fn unhex(s: &str) -> Option> { + if s.len() % 2 != 0 || !s.is_ascii() { + return None; + } + (0..s.len()) + .step_by(2) + .map(|i| u8::from_str_radix(&s[i..i + 2], 16).ok()) + .collect() +} + +fn secs_until(unix: u64) -> Duration { + let now = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap_or_default(); + Duration::from_secs(unix).saturating_sub(now) +} diff --git a/server/src/proxy.rs b/server/src/proxy.rs index f505c67..c3f89c5 100644 --- a/server/src/proxy.rs +++ b/server/src/proxy.rs @@ -1,19 +1,23 @@ -//! `/h//…` to `https://:/…`: pinned, streamed both ways, adding nothing. +//! `/h//…` to `https://:/…`: pinned, streamed both ways. //! -//! Nothing is injected. The browser's own device token is the only credential that reaches a -//! host, so this server can never do more than the page could. +//! The only thing added is `pf-client-addr`, who the visitor is. The browser's own device token is +//! the only credential that reaches a host, so this server can never do more than the page could. use crate::hosts::Hosts; -use axum::body::{Body, HttpBody as _}; +use crate::proof::Outcome; +use crate::visitor::Visitor; +use axum::body::{Body, Bytes, HttpBody as _}; use axum::extract::{Path, Request, State}; -use axum::http::{header, HeaderMap, HeaderName, HeaderValue, StatusCode, Uri}; +use axum::http::{header, HeaderMap, HeaderName, HeaderValue, Method, StatusCode, Uri}; use axum::response::{IntoResponse, Response}; use futures_util::StreamExt; +use punktfunk_core::mgmt_routes::{open_route, paired_may_access}; +use punktfunk_core::provenance::{CLIENT_ADDR_HEADER, CLIENT_ADDR_UNKNOWN}; use punktfunk_core::tls::PinVerify; use std::collections::HashMap; use std::net::{IpAddr, SocketAddr}; use std::sync::{Arc, LazyLock, Mutex}; -use std::time::Duration; +use std::time::{Duration, Instant}; use tokio::sync::Semaphore; type Seen = Arc>>; @@ -31,25 +35,35 @@ struct Upstream { } type UpstreamKey = (String, u16, Option<[u8; 32]>); -static UPSTREAMS: LazyLock>>> = - LazyLock::new(Default::default); +/// Kept clients, with when each was last used; at most [`KEPT`](crate::proof::KEPT). +type Kept = HashMap, Instant)>; +static UPSTREAMS: LazyLock> = LazyLock::new(Default::default); fn upstream(addr: &str, port: u16, pin: Option<[u8; 32]>) -> anyhow::Result> { let mut all = UPSTREAMS.lock().unwrap(); - if let Some(up) = all.get(&(addr.to_string(), port, pin)) { + let key = (addr.to_string(), port, pin); + if let Some((up, used)) = all.get_mut(&key) { + *used = Instant::now(); return Ok(up.clone()); } + if all.len() >= crate::proof::KEPT { + let oldest = all.iter().min_by_key(|(_, (_, used))| *used); + if let Some(oldest) = oldest.map(|(k, _)| k.clone()) { + all.remove(&oldest); + } + } let seen: Seen = Arc::default(); let up = Arc::new(Upstream { client: client(pin, seen.clone())?, seen, gate: Arc::new(Semaphore::new(IN_FLIGHT)), }); - all.insert((addr.to_string(), port, pin), up.clone()); + all.insert(key, (up.clone(), Instant::now())); Ok(up) } -/// A host this server lists: `/h//…`. +/// A host this server lists: `/h//…`. One the operator listed is relayed to as it is; one +/// mDNS found, once proven. pub async fn listed( State(hosts): State>, Path((id, _)): Path<(String, String)>, @@ -58,15 +72,19 @@ pub async fn listed( let Some(host) = hosts.get(&id) else { return (StatusCode::NOT_FOUND, "This server has no such host.").into_response(); }; - let Some(url) = upstream_url(&host.addr, host.port, &req) else { - return StatusCode::BAD_REQUEST.into_response(); + let url = match upstream_url(&host.addr, host.port, &req) { + Ok(url) => url, + Err((status, why)) => return api_error(status, why), }; - let learn = Learn::Listed(host.id.clone()); + if host.fullname.is_some() { + return unlisted(&hosts, &host.addr, host.port, host.pin, url, req).await; + } + let learn = host.pin.is_none().then_some(host.id); relay(&hosts, &host.addr, host.port, host.pin, learn, url, req).await } -/// A host the page names by address: `/a//api/…`. Only a private address, and only -/// the management API, so the route cannot be aimed anywhere else on or off the network. +/// A host the page names by address: `/a//…`. Only a home-network address that is +/// not loopback, and only once proven. pub async fn typed( State(hosts): State>, Path((target, _)): Path<(String, String)>, @@ -79,45 +97,169 @@ pub async fn typed( ) .into_response(); } - let Some((ip, port)) = private_target(&target) else { - return ( - StatusCode::BAD_REQUEST, - "Add a host by its IP address on this network, like 192.168.1.25.", - ) - .into_response(); - }; // The page's own server, named as a host, would answer itself with the page. - if own_authority(req.uri(), req.headers()) == Some(target.as_str()) { - return ( - StatusCode::BAD_REQUEST, - "That is this server's own address, not a host.", - ) - .into_response(); - } - let addr = match ip { - IpAddr::V4(v4) => v4.to_string(), - IpAddr::V6(v6) => format!("[{v6}]"), + let own = own_authority(req.uri(), req.headers()) == Some(target.as_str()); + let (ip, port) = match typed_target(&hosts, &target) { + Ok(_) if own => return (StatusCode::BAD_REQUEST, OWN_ADDRESS).into_response(), + Ok(target) => target, + Err(say) => return (StatusCode::BAD_REQUEST, say).into_response(), }; - // Checked on the URL that goes out, after `..` and `%2e%2e` have collapsed. - let Some(url) = upstream_url(&addr, port, &req).filter(|u| u.path().starts_with("/api/")) - else { - return StatusCode::NOT_FOUND.into_response(); + let addr = url_host(ip); + let url = match upstream_url(&addr, port, &req) { + Ok(url) => url, + Err((status, why)) => return api_error(status, why), }; let key = format!("{addr}:{port}"); + let fresh = plane(&req, &url); + if let Some(refused) = crate::proof::over_meter(&hosts, req.extensions(), &key, fresh) { + return refused; + } let pin = hosts.pin_at(&key); - relay(&hosts, &addr, port, pin, Learn::Address(key), url, req).await + unlisted(&hosts, &addr, port, pin, url, req).await +} + +/// `POST /pass/h/`: see [`mint`]. +pub async fn pass_listed( + State(hosts): State>, + Path(id): Path, + req: Request, +) -> Response { + let (ask, asked) = asking(req, &format!("/h/{id}/api/v1/host")); + let answer = listed(State(hosts.clone()), Path((id, String::new())), ask).await; + mint(&hosts, answer, &asked) +} + +/// `POST /pass/a/`: see [`mint`]. +pub async fn pass_typed( + State(hosts): State>, + Path(target): Path, + req: Request, +) -> Response { + let (ask, asked) = asking(req, &format!("/a/{target}/api/v1/host")); + let answer = typed(State(hosts.clone()), Path((target, String::new())), ask).await; + mint(&hosts, answer, &asked) +} + +/// The page's pass request as the host question it stands for: `GET /api/v1/host` with the +/// same token, visitor and pass. The headers come back too, for [`mint`]. +fn asking(req: Request, path: &str) -> (Request, HeaderMap) { + let (parts, _) = req.into_parts(); + let mut ask = Request::new(Body::empty()); + *ask.uri_mut() = path.parse().unwrap_or_default(); + *ask.headers_mut() = parts.headers.clone(); + *ask.extensions_mut() = parts.extensions; + (ask, parts.headers) +} + +/// A pass for a browser whose token the host just accepted: `GET /api/v1/host` answered `200` +/// over a connection whose leaf is the identity the page named. A valid pass the page already +/// holds is extended to this host; otherwise a new one is made. Any other answer is the host's. +fn mint(hosts: &Hosts, answer: Response, asked: &HeaderMap) -> Response { + let Some(host) = paired_with(asked).filter(|_| answer.status() == StatusCode::OK) else { + return answer; + }; + let held = asked + .get(crate::visitor::PASS_HEADER) + .and_then(|v| v.to_str().ok()); + let pass = hosts.lanes.passes.prove(held, host); + let body = axum::Json(serde_json::json!({ "pass": pass })); + ([(header::CACHE_CONTROL, "no-store")], body).into_response() +} + +const OWN_ADDRESS: &str = "That is this server's own address, not a host."; + +/// A host's address as it goes in a URL: an IPv6 one in brackets. +pub(crate) fn url_host(ip: IpAddr) -> String { + match ip { + IpAddr::V4(v4) => v4.to_string(), + IpAddr::V6(v6) => format!("[{v6}]"), + } +} + +/// A host the operator did not list: relayed to once proven, and the page's own plane request +/// answered from a fresh proof. +async fn unlisted( + hosts: &Hosts, + addr: &str, + port: u16, + pin: Option<[u8; 32]>, + url: reqwest::Url, + req: Request, +) -> Response { + let plane = plane(&req, &url); + match crate::proof::prove(hosts, addr, port, pin, plane).await { + Outcome::Host { body, .. } if plane => host_reply(StatusCode::OK, body), + Outcome::NoPlane(body) if plane => host_reply(StatusCode::NOT_FOUND, body), + Outcome::Host { fp, .. } => relay(hosts, addr, port, Some(fp), None, url, req).await, + refused => refused + .refusal() + .unwrap_or_else(|| StatusCode::BAD_GATEWAY.into_response()), + } +} + +/// The page's own plane request, which a proof answers fresh. +fn plane(req: &Request, url: &reqwest::Url) -> bool { + req.method() == Method::GET && url.path() == "/api/v1/webtransport" +} + +/// A host's JSON answer, served as [`relay`] serves one. +fn host_reply(status: StatusCode, body: Bytes) -> Response { + let mut res = (status, body).into_response(); + let h = res.headers_mut(); + h.insert( + header::CONTENT_TYPE, + HeaderValue::from_static("application/json"), + ); + h.insert( + header::CONTENT_SECURITY_POLICY, + HeaderValue::from_static("sandbox"), + ); + h.insert( + header::X_CONTENT_TYPE_OPTIONS, + HeaderValue::from_static("nosniff"), + ); + res } /// The host's URL for `///`: the rest and query as the browser sent them, -/// still percent-encoded, so a decoded `%2f` or `%3f` cannot reshape the path. -fn upstream_url(addr: &str, port: u16, req: &Request) -> Option { - let rest = req.uri().path().splitn(4, '/').nth(3)?; +/// still percent-encoded, so a decoded `%2f` or `%3f` cannot reshape the path. Only a route in +/// core's table goes out, checked on the URL after `..` and `%2e%2e` have collapsed: the open +/// four, and a paired device's own with its token. +fn upstream_url( + addr: &str, + port: u16, + req: &Request, +) -> Result { + let rest = req.uri().path().splitn(4, '/').nth(3); let query = req .uri() .query() .map(|q| format!("?{q}")) .unwrap_or_default(); - reqwest::Url::parse(&format!("https://{addr}:{port}/{rest}{query}")).ok() + let url = rest + .and_then(|rest| reqwest::Url::parse(&format!("https://{addr}:{port}/{rest}{query}")).ok()) + .ok_or((StatusCode::BAD_REQUEST, "not a path a host serves"))?; + let (method, path) = (req.method().as_str(), url.path()); + if open_route(method, path) { + return Ok(url); + } + let token = req.headers().contains_key(header::AUTHORIZATION); + match paired_may_access(method, path) { + true if token => Ok(url), + true => Err(( + StatusCode::UNAUTHORIZED, + "a paired device's token is needed", + )), + false => Err(( + StatusCode::NOT_FOUND, + "the page server does not relay this route", + )), + } +} + +/// An answer in the host's error shape, for a request that never reached one. +fn api_error(status: StatusCode, message: &str) -> Response { + (status, axum::Json(serde_json::json!({ "error": message }))).into_response() } /// This server's `host[:port]` as the browser addressed it: the URI authority over HTTP/2, `Host` @@ -128,23 +270,21 @@ pub(crate) fn own_authority<'a>(uri: &'a Uri, headers: &'a HeaderMap) -> Option< .or_else(|| headers.get(header::HOST).and_then(|h| h.to_str().ok())) } -/// Where a first-contact pin is kept. -enum Learn { - Listed(String), - Address(String), -} - -/// `ip`, `ip:port` or `[v6]:port`, when the address is one a home network uses: RFC 1918, -/// loopback, link-local, the 100.64/10 range Tailscale hands out, or IPv6 unique-local. -pub(crate) fn private_target(s: &str) -> Option<(IpAddr, u16)> { - let (ip, port) = match s.parse::() { - Ok(sa) => (sa.ip(), sa.port()), - Err(_) => ( +/// `ip`, `ip:port` or `[v6]:port`; the management port when none is given. +fn parse_target(s: &str) -> Option<(IpAddr, u16)> { + match s.parse::() { + Ok(sa) => Some((sa.ip(), sa.port())), + Err(_) => Some(( s.trim_matches(|c| c == '[' || c == ']').parse().ok()?, crate::config::MGMT_PORT, - ), - }; - let private = match ip { + )), + } +} + +/// An address a home network or this machine uses: RFC 1918, loopback, link-local, the 100.64/10 +/// range Tailscale hands out, or IPv6 unique-local. +pub(crate) fn home_range(ip: IpAddr) -> bool { + match ip { IpAddr::V4(v) => { v.is_private() || v.is_loopback() @@ -152,19 +292,58 @@ pub(crate) fn private_target(s: &str) -> Option<(IpAddr, u16)> { || (v.octets()[0] == 100 && v.octets()[1] & 0xc0 == 64) } IpAddr::V6(v) => v.is_loopback() || v.is_unique_local() || v.is_unicast_link_local(), - }; - private.then_some((ip, port)) + } +} + +/// A target the page typed: a home-network address that is not loopback. The error is the page's +/// sentence. +pub(crate) fn typed_target(hosts: &Hosts, s: &str) -> Result<(IpAddr, u16), &'static str> { + let (ip, port) = parse_target(s) + .filter(|(ip, _)| home_range(*ip)) + .ok_or("Add a host by its IP address on this network, like 192.168.1.25.")?; + if (hosts.machine.loopback)(ip) { + return Err(OWN_ADDRESS); + } + Ok((ip, port)) } +/// `learn`: the listed host whose first-contact certificate becomes its pin. A token, and the +/// device exchange that earns one, go only over a connection whose leaf is the identity the page +/// paired with, named in `pf-host`. Only health and the plane's hash are asked without it. A +/// request without a token carries at most [`OPEN_BODY`]. async fn relay( hosts: &Hosts, addr: &str, port: u16, pin: Option<[u8; 32]>, - learn: Learn, + learn: Option, url: reqwest::Url, req: Request, ) -> Response { + let (parts, body) = req.into_parts(); + let token = parts.headers.contains_key(header::AUTHORIZATION); + let bound = token || !matches!(url.path(), "/api/v1/health" | "/api/v1/webtransport"); + let pin = match (bound, paired_with(&parts.headers), pin) { + (false, _, pin) => pin, + (true, None, _) => return (StatusCode::BAD_REQUEST, RELOAD).into_response(), + (true, Some(paired), Some(known)) if paired != known => { + tracing::warn!(host = %addr, "a request named another host than this address's"); + return (StatusCode::BAD_GATEWAY, NOT_PAIRED).into_response(); + } + (true, Some(paired), _) => Some(paired), + }; + // HTTP/2 can carry a body with neither header. + let body = match body.is_end_stream() { + true => None, + false if token => Some(reqwest::Body::wrap_stream(body.into_data_stream())), + false => match axum::body::to_bytes(body, OPEN_BODY).await { + Ok(bytes) => Some(reqwest::Body::from(bytes)), + Err(_) => { + let why = "a request without a device token carries at most 4 KiB"; + return api_error(StatusCode::PAYLOAD_TOO_LARGE, why); + } + }, + }; let up = match upstream(addr, port, pin) { Ok(up) => up, Err(e) => return gateway(addr, "the proxy client did not build", &format!("{e:#}")), @@ -175,24 +354,21 @@ async fn relay( .acquire_owned() .await .expect("the gate is never closed"); - let (parts, body) = req.into_parts(); - // HTTP/2 can carry a body with neither header. - let has_body = !body.is_end_stream(); - let mut out = up - .client - .request(parts.method, url) - .headers(only(&parts.headers, &UP)); - if has_body { - out = out.body(reqwest::Body::wrap_stream(body.into_data_stream())); + let mut sent = only(&parts.headers, &UP); + let visitor = parts.extensions.get::(); + let named = visitor.map_or_else(|| CLIENT_ADDR_UNKNOWN.into(), Visitor::header); + if let Ok(v) = HeaderValue::from_str(&named) { + sent.insert(CLIENT_ADDR_HEADER, v); + } + let mut out = up.client.request(parts.method, url).headers(sent); + if let Some(body) = body { + out = out.body(body); } match out.send().await { Ok(resp) => { - if pin.is_none() { + if let Some(id) = learn { if let Some(fp) = *up.seen.lock().unwrap() { - match learn { - Learn::Listed(id) => hosts.learned(&id, fp), - Learn::Address(key) => hosts.keep(&key, fp), - } + hosts.learned(&id, fp); // Pinned from here on: the next request builds that client instead. UPSTREAMS .lock() @@ -233,13 +409,8 @@ async fn relay( presented = %crate::hosts::hex(&presented.unwrap_or_default()), "certificate does not match the pin" ); - return ( - StatusCode::BAD_GATEWAY, - "This host's certificate changed since it was pinned. If you reinstalled it \ - or moved it to a new identity, remove its entry from pins.json and restart \ - the server.", - ) - .into_response(); + let say = if bound { NOT_PAIRED } else { PIN_CHANGED }; + return (StatusCode::BAD_GATEWAY, say).into_response(); } // The chain, not the top line: reqwest's own message is only "error sending request". gateway( @@ -251,13 +422,30 @@ async fn relay( } } +/// What a request without a device token may carry: the device exchange's JSON is far less. +const OPEN_BODY: usize = 4 << 10; +/// The header the page names its paired host in, by identity fingerprint. Never forwarded. +pub const HOST_HEADER: &str = "pf-host"; +const RELOAD: &str = "This page is out of date. Reload it and try again."; +const NOT_PAIRED: &str = "The host at this address isn't the one this browser paired with."; + +/// The identity the page paired with, from `pf-host`. +fn paired_with(headers: &HeaderMap) -> Option<[u8; 32]> { + let named = headers.get(HOST_HEADER)?.to_str().ok()?; + crate::config::parse_fp(named).ok() +} + +pub(crate) const PIN_CHANGED: &str = + "This host's certificate changed since it was pinned. If you reinstalled it or moved it to a \ + new identity, remove its entry from pins.json and restart the server."; + /// 504, so the page reads it as "nothing answered". A 502 carries a refusal the page shows as said. fn gateway(host: &str, what: &str, cause: &str) -> Response { tracing::warn!(host, cause, "{what}"); (StatusCode::GATEWAY_TIMEOUT, "Couldn't reach this host.").into_response() } -fn client(pin: Option<[u8; 32]>, seen: Seen) -> anyhow::Result { +pub(crate) fn client(pin: Option<[u8; 32]>, seen: Seen) -> anyhow::Result { let mut tls = rustls::ClientConfig::builder() .dangerous() .with_custom_certificate_verifier(Arc::new(PinVerify::with_observed(pin, seen))) @@ -314,66 +502,31 @@ fn only(from: &HeaderMap, names: &[HeaderName]) -> HeaderMap { mod tests { use crate::config::Listed; use crate::hosts::Hosts; + use crate::testing::{upstream, Plane}; + use crate::visitor::{Machine, TEST}; use axum::body::Body; use axum::http::{Request, StatusCode}; + use std::sync::Arc; use tower::ServiceExt; - /// A host with a self-signed certificate on a loopback port, and that certificate's pin. - async fn upstream() -> (u16, [u8; 32]) { - let key = rcgen::KeyPair::generate().unwrap(); - let cert = rcgen::CertificateParams::new(vec!["localhost".into()]) - .unwrap() - .self_signed(&key) - .unwrap(); - let fp = punktfunk_core::tls::cert_fingerprint(cert.der()); - let tls = axum_server::tls_rustls::RustlsConfig::from_pem( - cert.pem().into(), - key.serialize_pem().into(), - ) - .await - .unwrap(); - let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); - listener.set_nonblocking(true).unwrap(); - let port = listener.local_addr().unwrap().port(); - let app = axum::Router::new() - .route("/api/v1/health", axum::routing::get(|| async { "ok" })) - .route( - "/api/v1/hop", - axum::routing::get(|| async { axum::response::Redirect::to("/secret") }), - ) - .route("/secret", axum::routing::get(|| async { "secret" })) - .route( - "/api/v1/headers", - axum::routing::get(|h: axum::http::HeaderMap| async move { - let names: Vec<_> = h.keys().map(|k| k.as_str()).collect(); - let reply = [ - ("content-type", "text/plain"), - ("etag", "\"v1\""), - ("clear-site-data", "\"storage\""), - ("www-authenticate", "Basic realm=\"desk\""), - ("alt-svc", "h3=\":443\""), - ("location", "https://elsewhere.example/"), - ("set-cookie", "a=b"), - ("access-control-allow-origin", "*"), - ]; - (reply, names.join(",")) - }), - ); - let server = axum_server::from_tcp_rustls(listener, tls).unwrap(); - tokio::spawn(server.serve(app.into_make_service())); - (port, fp) + fn hosts(name: &str, listed: Vec, machine: Machine) -> Arc { + let dir = std::env::temp_dir().join(format!("pf-proxy-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + Hosts::new(listed, &dir, true, machine).unwrap() } - async fn get_health(hosts: std::sync::Arc) -> (StatusCode, String) { - get(hosts, "/h/desk/api/v1/health").await + fn desk(port: u16, pin: Option<[u8; 32]>) -> Vec { + vec![Listed { + name: "desk".into(), + addr: "127.0.0.1".into(), + port, + pin, + }] } - async fn get(hosts: std::sync::Arc, path: &str) -> (StatusCode, String) { - let app = crate::router(hosts, std::env::temp_dir(), None); - let res = app - .oneshot(Request::get(path).body(Body::empty()).unwrap()) - .await - .unwrap(); + async fn send(hosts: &Arc, req: Request) -> (StatusCode, String) { + let app = crate::router(hosts.clone(), std::env::temp_dir(), Default::default()); + let res = app.oneshot(req).await.unwrap(); let status = res.status(); let body = axum::body::to_bytes(res.into_body(), 1 << 16) .await @@ -381,85 +534,178 @@ mod tests { (status, String::from_utf8_lossy(&body).into_owned()) } + async fn get(hosts: &Arc, path: &str) -> (StatusCode, String) { + send(hosts, Request::get(path).body(Body::empty()).unwrap()).await + } + + /// A request with a token, naming the host it was paired with. + async fn get_with_token(hosts: &Arc, path: &str, fp: [u8; 32]) -> (StatusCode, String) { + let req = Request::get(path) + .header("authorization", "Bearer x") + .header("pf-host", crate::hosts::hex(&fp)); + send(hosts, req.body(Body::empty()).unwrap()).await + } + + /// The operator's list holds at any address, loopback included. #[tokio::test] - async fn a_host_is_reached_only_under_its_pin() { + async fn a_listed_host_is_reached_only_under_its_pin() { punktfunk_core::tls::install_default_provider(); - let (port, fp) = upstream().await; - let dir = std::env::temp_dir().join(format!("pf-proxy-{}", std::process::id())); - let desk = |pin| { - vec![Listed { - name: "desk".into(), - addr: "127.0.0.1".into(), - port, - pin, - }] - }; + let up = upstream(Plane::Off).await; + let health = "/h/desk/api/v1/health"; - let pinned = Hosts::new(desk(Some(fp)), &dir.join("a"), true).unwrap(); - assert_eq!(get_health(pinned).await, (StatusCode::OK, "ok".into())); + let pinned = hosts("pinned", desk(up.port, Some(up.fp)), Machine::REAL); + assert_eq!(get(&pinned, health).await, (StatusCode::OK, "ok".into())); - let wrong = Hosts::new(desk(Some([0; 32])), &dir.join("b"), true).unwrap(); - let (status, body) = get_health(wrong).await; + let wrong = hosts("wrong", desk(up.port, Some([0; 32])), Machine::REAL); + let (status, body) = get(&wrong, health).await; assert_eq!(status, StatusCode::BAD_GATEWAY); assert!(body.contains("certificate changed"), "{body}"); - let first = Hosts::new(desk(None), &dir.join("c"), true).unwrap(); - assert_eq!(get_health(first.clone()).await.0, StatusCode::OK); - assert_eq!(first.get("desk").unwrap().pin, Some(fp)); + let first = hosts("first", desk(up.port, None), Machine::REAL); + assert_eq!(get(&first, health).await.0, StatusCode::OK); + assert_eq!(first.get("desk").unwrap().pin, Some(up.fp)); let closed = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); - let gone = Listed { - name: "desk".into(), - addr: "127.0.0.1".into(), - port: closed.local_addr().unwrap().port(), - pin: None, - }; + let gone = closed.local_addr().unwrap().port(); drop(closed); - let silent = Hosts::new(vec![gone], &dir.join("d"), true).unwrap(); - assert_eq!(get_health(silent).await.0, StatusCode::GATEWAY_TIMEOUT); - let _ = std::fs::remove_dir_all(dir); + let silent = hosts("silent", desk(gone, None), Machine::REAL); + assert_eq!(get(&silent, health).await.0, StatusCode::GATEWAY_TIMEOUT); } #[tokio::test] - async fn a_typed_address_reaches_only_a_private_host_api() { + async fn only_the_route_table_reaches_a_host() { punktfunk_core::tls::install_default_provider(); - let (port, fp) = upstream().await; - let dir = std::env::temp_dir().join(format!("pf-typed-{}", std::process::id())); - let hosts = Hosts::new(Vec::new(), &dir, true).unwrap(); + let up = upstream(Plane::Off).await; + let hosts = hosts("table", desk(up.port, Some(up.fp)), TEST); + // A paired route without a token, and routes off the table with or without one. + assert_eq!(get(&hosts, "/h/desk/api/v1/status").await.0, 401); + for path in [ + "/h/desk/metrics", + "/h/desk/secret", + "/h/desk/api/v1/clients", + "/h/desk/api/v1/pairing/pending", + "/h/desk/api/v1/%2e%2e/secret", + "/h/desk/api/..%2fsecret", + "/h/desk/api/v1/health/../../../secret", + ] { + let (status, body) = get_with_token(&hosts, path, up.fp).await; + assert_ne!(body, "secret", "{path}"); + assert!(matches!(status.as_u16(), 400 | 404), "{path}: {status}"); + } + let pin = Request::post("/h/desk/api/v1/pairing/pin").header("authorization", "Bearer x"); + assert_eq!(send(&hosts, pin.body(Body::empty()).unwrap()).await.0, 404); + assert_eq!(up.asked(), Vec::::new()); + + // A redirect is relayed as its status, without a target, and never followed. + let (status, body) = get_with_token(&hosts, "/h/desk/api/v1/compositors", up.fp).await; + assert!(status.is_redirection(), "{status}"); + assert_ne!(body, "secret"); + assert_eq!(up.asked(), ["GET /api/v1/compositors"]); + } - let health = format!("/a/127.0.0.1:{port}/api/v1/health"); - assert_eq!( - get(hosts.clone(), &health).await, - (StatusCode::OK, "ok".into()) - ); - assert_eq!(hosts.pin_at(&format!("127.0.0.1:{port}")), Some(fp)); - let res = crate::router(hosts.clone(), std::env::temp_dir(), None) + #[tokio::test] + async fn a_decoy_hears_one_fixed_request() { + punktfunk_core::tls::install_default_provider(); + let decoy = upstream(Plane::Decoy).await; + let hosts = hosts("decoy", Vec::new(), TEST); + let at = format!("127.0.0.1:{}", decoy.port); + let hash = "00".repeat(32); + for _ in 0..2 { + for (path, token) in [ + (format!("/a/{at}/api/v1/health"), false), + (format!("/a/{at}/api/v1/webtransport"), false), + (format!("/a/{at}/api/v1/status"), true), + (format!("/a/{at}/api/v1/library"), true), + (format!("/wa/{at}?port=9778&hash={hash}"), false), + ] { + let (status, body) = if token { + get_with_token(&hosts, &path, [0; 32]).await + } else { + get(&hosts, &path).await + }; + assert_eq!(status, StatusCode::BAD_GATEWAY, "{path}"); + assert!(body.is_empty() || body.contains("Punktfunk host"), "{body}"); + } + let challenge = Request::post(format!("/a/{at}/api/v1/auth/device/challenge")); + let (status, _) = send(&hosts, challenge.body(Body::empty()).unwrap()).await; + assert_eq!(status, StatusCode::BAD_GATEWAY); + } + assert_eq!(decoy.asked(), ["GET /api/v1/webtransport"]); + assert_eq!(hosts.pin_at(&at), None); + } + + #[tokio::test] + async fn a_proven_host_is_relayed_to() { + punktfunk_core::tls::install_default_provider(); + let up = upstream(Plane::Attested).await; + let hosts = hosts("proven", Vec::new(), TEST); + let at = format!("127.0.0.1:{}", up.port); + + let (status, plane) = get(&hosts, &format!("/a/{at}/api/v1/webtransport")).await; + assert_eq!(status, StatusCode::OK); + assert!(plane.contains("cert_hash_sig"), "{plane}"); + assert_eq!(hosts.pin_at(&at), Some(up.fp)); + let health = format!("/a/{at}/api/v1/health"); + assert_eq!(get(&hosts, &health).await, (StatusCode::OK, "ok".into())); + let res = crate::router(hosts.clone(), std::env::temp_dir(), Default::default()) .oneshot(Request::get(&health).body(Body::empty()).unwrap()) .await .unwrap(); assert_eq!(res.headers()["content-security-policy"], "sandbox"); + // The page's own plane request proves again: a host that restarted has a new plane. + assert_eq!( + get(&hosts, &format!("/a/{at}/api/v1/webtransport")).await.0, + 200 + ); + let want = [ + "GET /api/v1/webtransport", + "GET /api/v1/health", + "GET /api/v1/health", + "GET /api/v1/webtransport", + ]; + assert_eq!(up.asked(), want); + } + + #[tokio::test] + async fn an_attestation_by_another_identity_is_refused() { + punktfunk_core::tls::install_default_provider(); + let up = upstream(Plane::Borrowed).await; + let hosts = hosts("borrowed", Vec::new(), TEST); + let at = format!("127.0.0.1:{}", up.port); + let (status, body) = get(&hosts, &format!("/a/{at}/api/v1/health")).await; + assert_eq!(status, StatusCode::BAD_GATEWAY); + assert!(body.contains("Punktfunk host"), "{body}"); + assert_eq!(up.asked(), ["GET /api/v1/webtransport"]); + assert_eq!(hosts.pin_at(&at), None); + } - let off_api = format!("/a/127.0.0.1:{port}/metrics"); - assert_eq!(get(hosts.clone(), &off_api).await.0, StatusCode::NOT_FOUND); - // Out of `api/` by a dot segment, encoded or not, or by a redirect. + #[tokio::test] + async fn an_unattested_host_is_reached_only_when_listed() { + punktfunk_core::tls::install_default_provider(); + let up = upstream(Plane::Unattested).await; + let hosts = hosts("unattested", Vec::new(), TEST); + let at = format!("127.0.0.1:{}", up.port); + for path in ["api/v1/health", "api/v1/webtransport"] { + let (status, body) = get(&hosts, &format!("/a/{at}/{path}")).await; + assert_eq!(status, StatusCode::BAD_GATEWAY, "{path}"); + assert!(body.contains("List it on this page's server"), "{body}"); + } + assert_eq!(up.asked(), ["GET /api/v1/webtransport"]); + assert_eq!(hosts.pin_at(&at), None); + } + + #[tokio::test] + async fn a_typed_address_must_be_a_home_one() { + let hosts = hosts("typed", Vec::new(), Machine::REAL); for path in [ - "api/%2e%2e/secret", - "api/..%2fsecret", - "api/%2E%2E/secret", - "api/v1/hop", + "/a/8.8.8.8/api/v1/health", + "/a/example.com/api/v1/health", + "/a/127.0.0.1/api/v1/health", + "/a/[::1]:47990/api/v1/health", + "/wa/127.0.0.1?port=9778&hash=00", ] { - let (status, body) = get(hosts.clone(), &format!("/a/127.0.0.1:{port}/{path}")).await; - assert_ne!(body, "secret", "{path} reached {status}"); + assert_eq!(get(&hosts, path).await.0, StatusCode::BAD_REQUEST, "{path}"); } - assert_eq!( - get(hosts.clone(), "/a/8.8.8.8/api/v1/health").await.0, - StatusCode::BAD_REQUEST - ); - assert_eq!( - get(hosts.clone(), "/a/example.com/api/v1/health").await.0, - StatusCode::BAD_REQUEST - ); - let own = Request::get("/a/192.168.1.233:8443/api/v1/health") .header("host", "192.168.1.233:8443") .body(Body::empty()) @@ -468,39 +714,30 @@ mod tests { .body(Body::empty()) .unwrap(); for req in [own, own_h2] { - let res = crate::router(hosts.clone(), std::env::temp_dir(), None) - .oneshot(req) - .await - .unwrap(); - assert_eq!(res.status(), StatusCode::BAD_REQUEST); + assert_eq!(send(&hosts, req).await.0, StatusCode::BAD_REQUEST); } - - let closed = Hosts::new(Vec::new(), &dir.join("closed"), false).unwrap(); - assert_eq!(get(closed, &health).await.0, StatusCode::NOT_FOUND); + let dir = std::env::temp_dir().join(format!("pf-closed-{}", std::process::id())); + let closed = Hosts::new(Vec::new(), &dir, false, TEST).unwrap(); + assert_eq!(get(&closed, "/a/10.0.0.9/api/v1/health").await.0, 404); let _ = std::fs::remove_dir_all(dir); } #[tokio::test] async fn a_visitor_vouches_for_nothing_and_a_host_opens_nothing() { punktfunk_core::tls::install_default_provider(); - let (port, fp) = upstream().await; - let dir = std::env::temp_dir().join(format!("pf-scrub-{}", std::process::id())); - let desk = Listed { - name: "desk".into(), - addr: "127.0.0.1".into(), - port, - pin: Some(fp), - }; - let hosts = Hosts::new(vec![desk], &dir, true).unwrap(); - let mut req = Request::get("/h/desk/api/v1/headers") + let up = upstream(Plane::Off).await; + let hosts = hosts("scrub", desk(up.port, Some(up.fp)), TEST); + let mut req = Request::get("/h/desk/api/v1/status") .header("host", "192.168.1.5:8443") .header("origin", "https://192.168.1.5:8443"); for (name, value) in [ ("authorization", "Bearer x"), + ("pf-host", &crate::hosts::hex(&up.fp)), + ("pf-pass", &"ab".repeat(32)), ("accept", "application/json"), ("if-none-match", "\"v0\""), ("cookie", "a=b"), - ("pf-client-addr", "203.0.113.9"), + ("pf-client-addr", "10.9.9.9"), ("x-punktfunk-device", "x"), ("x-forwarded-for", "203.0.113.9"), ("accept-encoding", "gzip"), @@ -508,8 +745,17 @@ mod tests { ] { req = req.header(name, value); } - let res = crate::router(hosts, dir.clone(), Some([].into())) - .oneshot(req.body(Body::empty()).unwrap()) + let mut req = req.body(Body::empty()).unwrap(); + let peer: std::net::SocketAddr = "127.0.0.1:50000".parse().unwrap(); + req.extensions_mut() + .insert(axum::extract::ConnectInfo(peer)); + let front = crate::origin::Front { + names: Some([].into()), + proxied: true, + self_signed: false, + }; + let res = crate::router(hosts, std::env::temp_dir(), front) + .oneshot(req) .await .unwrap(); assert_eq!(res.status(), StatusCode::OK); @@ -531,25 +777,167 @@ mod tests { .await .unwrap(); let seen = String::from_utf8_lossy(&body).into_owned(); - let mut seen: Vec<_> = seen.split(',').collect(); + let mut seen: Vec<_> = seen.lines().collect(); // The upstream's own `Host`, which the HTTP client adds for HTTP/1.1, is not the visitor's. - seen.retain(|n| *n != "host"); + seen.retain(|n| !n.starts_with("host=")); seen.sort_unstable(); - assert_eq!(seen, ["accept", "authorization", "if-none-match"]); - let _ = std::fs::remove_dir_all(dir); + let want = [ + "accept=application/json", + "authorization=Bearer x", + "if-none-match=\"v0\"", + "pf-client-addr=203.0.113.9", + ]; + assert_eq!(seen, want); + } + + fn post_pass(path: &str, token: &str, fp: [u8; 32], pass: Option<&str>) -> Request { + let mut req = Request::post(path) + .header("authorization", format!("Bearer {token}")) + .header("pf-host", crate::hosts::hex(&fp)); + if let Some(pass) = pass { + req = req.header("pf-pass", pass); + } + req.body(Body::empty()).unwrap() + } + + fn pass_of(body: &str) -> String { + let json: serde_json::Value = serde_json::from_str(body).unwrap(); + json["pass"].as_str().unwrap().to_owned() + } + + #[tokio::test] + async fn a_pass_is_minted_only_for_a_token_the_host_takes() { + punktfunk_core::tls::install_default_provider(); + let up = upstream(Plane::Off).await; + let hosts = hosts("mint", desk(up.port, Some(up.fp)), TEST); + let refused = post_pass("/pass/h/desk", "bad", up.fp, None); + assert_eq!(send(&hosts, refused).await.0, StatusCode::UNAUTHORIZED); + let (status, body) = send(&hosts, post_pass("/pass/h/desk", "good", up.fp, None)).await; + assert_eq!(status, StatusCode::OK, "{body}"); + let pass = pass_of(&body); + assert!(hosts.lanes.passes.hosts(&pass).covers(Some(up.fp))); + assert_eq!(up.asked(), ["GET /api/v1/host", "GET /api/v1/host"]); + + // A typed host, proven first: the same pass is extended to it. + let other = upstream(Plane::Attested).await; + let at = format!("/pass/a/127.0.0.1:{}", other.port); + let (status, body) = send(&hosts, post_pass(&at, "good", other.fp, Some(&pass))).await; + assert_eq!((status, pass_of(&body)), (StatusCode::OK, pass.clone())); + let held = hosts.lanes.passes.hosts(&pass); + assert!(held.covers(Some(up.fp)) && held.covers(Some(other.fp))); + } + + #[tokio::test] + async fn a_token_goes_only_to_the_host_it_was_paired_with() { + punktfunk_core::tls::install_default_provider(); + let up = upstream(Plane::Off).await; + let pinned = hosts("bound", desk(up.port, Some(up.fp)), TEST); + let unpinned = hosts("bound-first", desk(up.port, None), TEST); + for hosts in [&pinned, &unpinned] { + let (status, body) = get_with_token(hosts, "/h/desk/api/v1/status", [9; 32]).await; + assert_eq!(status, StatusCode::BAD_GATEWAY); + assert!( + body.contains("isn't the one this browser paired with"), + "{body}" + ); + } + let unnamed = Request::get("/h/desk/api/v1/status").header("authorization", "Bearer x"); + let (status, body) = send(&pinned, unnamed.body(Body::empty()).unwrap()).await; + assert_eq!(status, StatusCode::BAD_REQUEST); + assert!(body.contains("Reload"), "{body}"); + assert_eq!(up.asked(), Vec::::new()); + + // The browser's own host, first contact included: reached, and pinned to it. + let (status, _) = get_with_token(&unpinned, "/h/desk/api/v1/status", up.fp).await; + assert_eq!(status, StatusCode::OK); + assert_eq!(unpinned.get("desk").unwrap().pin, Some(up.fp)); + + // A proven host that is not the paired one. + let other = upstream(Plane::Attested).await; + let typed = hosts("bound-typed", Vec::new(), TEST); + let path = format!("/a/127.0.0.1:{}/api/v1/status", other.port); + assert_eq!(get_with_token(&typed, &path, up.fp).await.0, 502); + assert_eq!(other.asked(), ["GET /api/v1/webtransport"]); + } + + #[tokio::test] + async fn an_impostor_never_hears_the_device_exchange() { + punktfunk_core::tls::install_default_provider(); + let real = upstream(Plane::Attested).await; + let impostor = upstream(Plane::Attested).await; + let hosts = hosts("impostor", Vec::new(), TEST); + let at = format!("/a/127.0.0.1:{}/api/v1/auth/device", impostor.port); + for step in ["challenge", "token"] { + let named = Request::post(format!("{at}/{step}")) + .header("pf-host", crate::hosts::hex(&real.fp)) + .body(Body::from("{}")) + .unwrap(); + let (status, body) = send(&hosts, named).await; + assert_eq!(status, StatusCode::BAD_GATEWAY, "{step}"); + assert!( + body.contains("isn't the one this browser paired with"), + "{body}" + ); + let unnamed = Request::post(format!("{at}/{step}")).body(Body::from("{}")); + assert_eq!(send(&hosts, unnamed.unwrap()).await.0, 400, "{step}"); + } + assert_eq!(impostor.asked(), ["GET /api/v1/webtransport"]); + assert_eq!(real.asked(), Vec::::new()); + } + + #[tokio::test] + async fn a_stranger_is_metered_and_a_pass_holder_is_not() { + punktfunk_core::tls::install_default_provider(); + let up = upstream(Plane::Off).await; + let hosts = hosts("meter", desk(up.port, Some(up.fp)), TEST); + for _ in 0..30 { + assert_eq!(get(&hosts, "/h/desk/api/v1/health").await.0, 200); + } + let app = crate::router(hosts.clone(), std::env::temp_dir(), Default::default()); + let health = || Request::get("/h/desk/api/v1/health"); + let res = app + .clone() + .oneshot(health().body(Body::empty()).unwrap()) + .await; + let res = res.unwrap(); + assert_eq!(res.status(), StatusCode::TOO_MANY_REQUESTS); + assert_eq!(res.headers()["retry-after"], "2"); + assert_eq!(up.asked().len(), 30); + + let pass = hosts.lanes.passes.prove(None, up.fp); + for _ in 0..40 { + let req = health() + .header("pf-pass", &pass) + .body(Body::empty()) + .unwrap(); + assert_eq!(app.clone().oneshot(req).await.unwrap().status(), 200); + } + + // Without a token a request body stops at 4 KiB. + let big = Request::post("/h/desk/api/v1/auth/device/token") + .header("pf-pass", &pass) + .header("pf-host", crate::hosts::hex(&up.fp)) + .body(Body::from(vec![b'x'; 5000])) + .unwrap(); + assert_eq!(app.oneshot(big).await.unwrap().status(), 413); + assert_eq!(up.asked().len(), 70); } #[test] - fn private_means_a_home_network() { + fn typed_targets_are_home_addresses_and_not_this_machine() { + let dir = std::env::temp_dir().join(format!("pf-targets-{}", std::process::id())); + let hosts = Hosts::new(Vec::new(), &dir, true, Machine::REAL).unwrap(); for ok in [ "192.168.1.21", "10.0.0.5:47991", "172.16.4.2", "100.101.102.103", "[fd00::5]:47990", - "127.0.0.1", + "[fe80::5]", + // This machine's own LAN address is a home address like any other. + "10.0.0.77", ] { - assert!(super::private_target(ok).is_some(), "{ok}"); + assert!(super::typed_target(&hosts, ok).is_ok(), "{ok}"); } for no in [ "8.8.8.8", @@ -557,12 +945,19 @@ mod tests { "[2001:db8::1]:47990", "desk.local", "192.168.1.21:notaport", + "0.0.0.0", + "[::ffff:127.0.0.1]", + // Loopback: this machine to itself. + "127.0.0.1", + "[::1]", ] { - assert!(super::private_target(no).is_none(), "{no}"); + assert!(super::typed_target(&hosts, no).is_err(), "{no}"); } assert_eq!( - super::private_target("192.168.1.21").unwrap().1, + super::typed_target(&hosts, "192.168.1.21").unwrap().1, crate::config::MGMT_PORT ); + + let _ = std::fs::remove_dir_all(dir); } } diff --git a/server/src/testing.rs b/server/src/testing.rs new file mode 100644 index 0000000..82aa09d --- /dev/null +++ b/server/src/testing.rs @@ -0,0 +1,160 @@ +//! A fake host for the tests: TLS on a loopback port, answering as a Punktfunk host would, or not. + +use axum::extract::State; +use std::sync::{Arc, Mutex}; + +/// What a fake host's `/api/v1/webtransport` answers. +#[derive(PartialEq)] +pub enum Plane { + /// Nothing: an older host, or a test that never asks. + Off, + /// Its plane, signed by the identity that serves its TLS. + Attested, + /// A plane signed by some other identity. + Borrowed, + /// A plane and no signature: an RSA identity. + Unattested, + /// Not a host at all: `200` to anything. + Decoy, +} + +/// A TLS server on a loopback port: its port, its certificate's pin, and what it was asked. +pub struct Up { + pub port: u16, + pub fp: [u8; 32], + asked: Arc>>, +} + +impl Up { + pub fn asked(&self) -> Vec { + self.asked.lock().unwrap().clone() + } +} + +fn identity() -> (rcgen::Certificate, rcgen::KeyPair) { + let key = rcgen::KeyPair::generate().unwrap(); + let cert = rcgen::CertificateParams::new(vec!["localhost".into()]) + .unwrap() + .self_signed(&key) + .unwrap(); + (cert, key) +} + +/// The plane a fake host publishes unless a test names one. +const PLANE: (u16, [u8; 32]) = (9778, [0xab; 32]); + +/// The host's `/api/v1/webtransport` answer for `plane`, signed by `key`, whose certificate is +/// `cert`. +fn published( + cert: &rcgen::Certificate, + key: &rcgen::KeyPair, + plane: (u16, [u8; 32]), +) -> serde_json::Value { + use base64::Engine as _; + use rcgen::SigningKey as _; + let hash = crate::hosts::hex(&plane.1); + let msg = format!("{}{hash}", punktfunk_core::tls::PLANE_ATTESTATION_CONTEXT); + let sig = key.sign(msg.as_bytes()).unwrap(); + serde_json::json!({ + "port": plane.0, + "cert_hash_sha256": hash, + "expires_at": 4_000_000_000u64, + "allow_pooling": false, + "cert_hash_sig": crate::hosts::hex(&sig), + "host_cert_der": base64::engine::general_purpose::STANDARD.encode(cert.der()), + }) +} + +pub async fn upstream(plane: Plane) -> Up { + upstream_publishing(plane, PLANE).await +} + +/// A fake host on a loopback port. `/api/v1/host` takes the token `Bearer good` and no other. +pub async fn upstream_publishing(plane: Plane, publish: (u16, [u8; 32])) -> Up { + let (cert, key) = identity(); + let fp = punktfunk_core::tls::cert_fingerprint(cert.der()); + let answer = match plane { + Plane::Attested => Some(published(&cert, &key, publish)), + Plane::Borrowed => { + let (other, other_key) = identity(); + Some(published(&other, &other_key, publish)) + } + Plane::Unattested => { + let mut plane = published(&cert, &key, publish); + let fields = plane.as_object_mut().unwrap(); + fields.remove("cert_hash_sig"); + fields.remove("host_cert_der"); + Some(plane) + } + Plane::Off | Plane::Decoy => None, + }; + let asked: Arc>> = Arc::default(); + let mut app = axum::Router::new() + .route("/api/v1/health", axum::routing::get(|| async { "ok" })) + .route( + "/api/v1/host", + axum::routing::get(|h: axum::http::HeaderMap| async move { + match h.get("authorization").and_then(|v| v.to_str().ok()) { + Some("Bearer good") => (axum::http::StatusCode::OK, "{}"), + _ => (axum::http::StatusCode::UNAUTHORIZED, "{}"), + } + }), + ) + .route( + "/api/v1/compositors", + axum::routing::get(|| async { axum::response::Redirect::to("/secret") }), + ) + .route("/secret", axum::routing::get(|| async { "secret" })) + .route( + "/api/v1/status", + axum::routing::get(|h: axum::http::HeaderMap| async move { + let seen: Vec<_> = h + .iter() + .map(|(k, v)| format!("{k}={}", v.to_str().unwrap_or("?"))) + .collect(); + let reply = [ + ("content-type", "text/plain"), + ("etag", "\"v1\""), + ("clear-site-data", "\"storage\""), + ("www-authenticate", "Basic realm=\"desk\""), + ("alt-svc", "h3=\":443\""), + ("location", "https://elsewhere.example/"), + ("set-cookie", "a=b"), + ("access-control-allow-origin", "*"), + ]; + (reply, seen.join("\n")) + }), + ); + if let Some(answer) = answer { + let answer = axum::Json(answer); + app = app.route( + "/api/v1/webtransport", + axum::routing::get(move || async move { answer }), + ); + } + if plane == Plane::Decoy { + app = axum::Router::new().fallback(|| async { "hello" }); + } + let app = app.layer(axum::middleware::from_fn_with_state( + asked.clone(), + |State(asked): State>>>, + req: axum::extract::Request, + next: axum::middleware::Next| async move { + let line = format!("{} {}", req.method(), req.uri().path()); + asked.lock().unwrap().push(line); + next.run(req).await + }, + )); + let tls = axum_server::tls_rustls::RustlsConfig::from_pem( + cert.pem().into(), + key.serialize_pem().into(), + ) + .await + .unwrap(); + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + listener.set_nonblocking(true).unwrap(); + let port = listener.local_addr().unwrap().port(); + let server = axum_server::from_tcp_rustls(listener, tls).unwrap(); + tokio::spawn(server.serve(app.into_make_service())); + Up { port, fp, asked } +} diff --git a/server/src/visitor.rs b/server/src/visitor.rs new file mode 100644 index 0000000..7220233 --- /dev/null +++ b/server/src/visitor.rs @@ -0,0 +1,252 @@ +//! Who sent a request: an address, or none, and the class the host's own rules use for it. +//! +//! A proxy appends what it saw on the right; anything to its left could be a caller's invention. +//! So the class is the strictest of every address on offer, and an invented entry can only make +//! the visitor stricter. The address named is the right-most one that classes as the internet. + +use crate::hosts::Hosts; +use axum::extract::{ConnectInfo, Request, State}; +use axum::http::{header, HeaderMap}; +use axum::middleware::Next; +use axum::response::Response; +use punktfunk_core::provenance::{classify_with, is_mesh_iface, KnockSource, CLIENT_ADDR_UNKNOWN}; +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket}; +use std::sync::Arc; + +/// What this machine knows of its own networks. Injected, so a test can fake it. +#[derive(Clone, Copy)] +pub struct Machine { + /// Replies to this 100.64/10 address leave through a mesh VPN's interface. + pub over_mesh: fn(Ipv4Addr) -> bool, + /// This address is on one of this machine's own IPv6 /64s. + pub on_own_prefix: fn(Ipv6Addr) -> bool, + /// Loopback: this machine talking to itself, never a host to dial for a visitor. + pub loopback: fn(IpAddr) -> bool, +} + +impl Machine { + pub const REAL: Machine = Machine { + over_mesh: routes_over_mesh, + on_own_prefix, + loopback: |ip| ip.to_canonical().is_loopback(), + }; + + /// The host's classification, plus one rule of this server's: a home network that hands out + /// global IPv6 addresses is still at home. + pub fn class(&self, ip: IpAddr) -> KnockSource { + match ip.to_canonical() { + IpAddr::V6(v6) if (self.on_own_prefix)(v6) => KnockSource::Lan, + ip => classify_with(Some(ip), self.over_mesh), + } + } +} + +/// A machine with no mesh VPN, no global IPv6 prefix and no loopback, so a test's loopback +/// upstream stands in for a host on the network. +#[cfg(test)] +pub const TEST: Machine = Machine { + over_mesh: |_| false, + on_own_prefix: |_| false, + loopback: |_| false, +}; + +/// Who a request came from, found once per request and kept in its extensions. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Visitor { + /// `None`: unknown. + pub addr: Option, + pub class: KnockSource, +} + +impl Visitor { + const UNKNOWN: Visitor = Visitor { + addr: None, + class: KnockSource::Wan, + }; + + /// The value of `pf-client-addr`. + pub fn header(&self) -> String { + self.addr + .map_or_else(|| CLIENT_ADDR_UNKNOWN.into(), |a| a.to_string()) + } + + /// `proxied`: a proxy is in front by definition (`TLS=off`), so a request it forwarded + /// without saying for whom names nobody. + pub fn of(peer: Option, headers: &HeaderMap, proxied: bool, m: &Machine) -> Visitor { + let Some(forwarded) = forwarded(headers) else { + return Visitor::UNKNOWN; + }; + if forwarded.is_empty() && proxied { + return Visitor::UNKNOWN; + } + let all: Vec = forwarded.iter().copied().chain(peer).collect(); + let Some(class) = all.iter().map(|ip| m.class(*ip)).max() else { + return Visitor::UNKNOWN; + }; + let addr = all + .iter() + .rev() + .find(|ip| m.class(**ip) == KnockSource::Wan) + .or(forwarded.last()) + .or(peer.as_ref()) + .map(|ip| ip.to_canonical()); + Visitor { addr, class } + } +} + +/// The header a page sends its pass in. Never forwarded: the relay passes an allowlist. +pub const PASS_HEADER: &str = "pf-pass"; + +/// Every route: the visitor, as [`Visitor::of`] finds them, and the hosts their pass opens. +pub async fn find( + State((hosts, proxied)): State<(Arc, bool)>, + mut req: Request, + next: Next, +) -> Response { + let peer = req + .extensions() + .get::>() + .map(|c| c.0.ip()); + let visitor = Visitor::of(peer, req.headers(), proxied, &hosts.machine); + let pass = req.headers().get(PASS_HEADER).and_then(|v| v.to_str().ok()); + let pass = pass + .map(|p| hosts.lanes.passes.hosts(p)) + .unwrap_or_default(); + req.extensions_mut().insert(visitor); + req.extensions_mut().insert(pass); + next.run(req).await +} + +/// Every address in `X-Forwarded-For` and in `Forwarded`'s `for=`, left to right. `None` when an +/// entry is not an address: an `unknown` or obfuscated hop hides who the visitor is. +fn forwarded(headers: &HeaderMap) -> Option> { + let mut out = Vec::new(); + for value in headers.get_all("x-forwarded-for") { + for entry in value.to_str().ok()?.split(',') { + out.push(address(entry)?); + } + } + for value in headers.get_all(header::FORWARDED) { + for pair in value.to_str().ok()?.split([',', ';']) { + match pair.split_once('=') { + Some((key, v)) if key.trim().eq_ignore_ascii_case("for") => out.push(address(v)?), + _ => {} + } + } + } + Some(out) +} + +/// `ip`, `ip:port`, `[v6]` or `[v6]:port`, quoted or not. +fn address(s: &str) -> Option { + let s = s.trim().trim_matches('"'); + s.parse::() + .or_else(|_| s.parse::().map(|sa| sa.ip())) + .ok() + .or_else(|| s.strip_prefix('[')?.strip_suffix(']')?.parse().ok()) +} + +/// Replies to `peer` leave through a mesh VPN's interface, which a CGNAT neighbour borrowing a +/// mesh address cannot arrange. A UDP `connect` finds the route without sending anything. +fn routes_over_mesh(peer: Ipv4Addr) -> bool { + let Ok(src) = UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 0)).and_then(|s| { + s.connect((peer, 9))?; + s.local_addr() + }) else { + return false; + }; + if_addrs::get_if_addrs() + .unwrap_or_default() + .iter() + .any(|i| i.ip() == src.ip() && is_mesh_iface(&i.name, i.ip())) +} + +fn on_own_prefix(v6: Ipv6Addr) -> bool { + if_addrs::get_if_addrs() + .unwrap_or_default() + .iter() + .any(|i| match i.ip() { + IpAddr::V6(own) => !own.is_loopback() && own.segments()[..4] == v6.segments()[..4], + IpAddr::V4(_) => false, + }) +} + +#[cfg(test)] +mod tests { + use super::*; + use axum::http::HeaderValue; + use KnockSource::{Lan, Wan}; + + fn on_prefix(v6: Ipv6Addr) -> bool { + v6.segments()[..4] == [0x2001, 0xdb8, 1, 2] + } + + const MESH: Machine = Machine { + over_mesh: |_| true, + on_own_prefix: on_prefix, + loopback: |_| false, + }; + const NO_MESH: Machine = Machine { + over_mesh: |_| false, + on_own_prefix: on_prefix, + loopback: |_| false, + }; + const UNKNOWN: (Option, KnockSource) = (None, Wan); + + fn visit(peer: &str, h: HeaderMap, proxied: bool, m: Machine) -> (Option, KnockSource) { + let v = Visitor::of(peer.parse().ok(), &h, proxied, &m); + (v.addr.map(|a| a.to_string()), v.class) + } + /// Behind a proxy on loopback, with `TLS=off`. + fn behind(h: HeaderMap) -> (Option, KnockSource) { + visit("127.0.0.1", h, true, NO_MESH) + } + fn direct(peer: &str, m: Machine) -> (Option, KnockSource) { + visit(peer, HeaderMap::new(), false, m) + } + fn named(addr: &str, class: KnockSource) -> (Option, KnockSource) { + (Some(addr.into()), class) + } + fn header(name: &'static str, value: &'static str) -> HeaderMap { + HeaderMap::from_iter([(name.parse().unwrap(), HeaderValue::from_static(value))]) + } + fn xff(value: &'static str) -> HeaderMap { + header("x-forwarded-for", value) + } + + #[test] + fn the_visitor_is_the_strictest_address_and_the_right_most_public_one() { + // A proxy that appends what it saw. + assert_eq!(behind(xff("203.0.113.9")), named("203.0.113.9", Wan)); + assert_eq!(behind(xff("192.168.1.20")), named("192.168.1.20", Lan)); + let fwd = header("forwarded", "for=\"[2001:db8:9::17]:4711\";proto=https"); + assert_eq!(behind(fwd), named("2001:db8:9::17", Wan)); + // Default nginx sends nothing: behind `TLS=off` that names nobody. + assert_eq!(behind(HeaderMap::new()), UNKNOWN); + // No proxy: the peer. + assert_eq!(direct("192.168.1.20", NO_MESH), named("192.168.1.20", Lan)); + assert_eq!(direct("203.0.113.9", NO_MESH), named("203.0.113.9", Wan)); + assert_eq!( + direct("::ffff:203.0.113.9", NO_MESH), + named("203.0.113.9", Wan) + ); + // A caller's entry on the left only tightens, and is never named over the proxy's. + let forged = behind(xff("192.168.1.5, 203.0.113.9")); + assert_eq!(forged, named("203.0.113.9", Wan)); + assert_eq!(behind(xff("8.8.8.8, 192.168.1.20")), named("8.8.8.8", Wan)); + let forged = visit("203.0.113.9", xff("192.168.1.5"), false, NO_MESH); + assert_eq!(forged, named("203.0.113.9", Wan)); + // An entry that is not an address hides the visitor. + assert_eq!(behind(xff("unknown, 192.168.1.20")), UNKNOWN); + assert_eq!(behind(header("forwarded", "for=_hidden")), UNKNOWN); + // A global IPv6 address on this machine's own /64 is at home; one elsewhere is not. + let home = "2001:db8:1:2::50"; + assert_eq!(direct(home, NO_MESH), named(home, Lan)); + let away = "2001:db8:1:3::50"; + assert_eq!(direct(away, NO_MESH), named(away, Wan)); + // 100.64/10 is at home only over a mesh VPN. + assert_eq!(direct("100.96.0.7", MESH), named("100.96.0.7", Lan)); + assert_eq!(direct("100.96.0.7", NO_MESH), named("100.96.0.7", Wan)); + assert_eq!(Visitor::UNKNOWN.header(), "unknown"); + } +}