A Cloudflare Worker that lets the Haven PWA reach IMAP, SMTP (submission) and CalDAV/CardDAV servers. Browsers have no TCP sockets and most DAV servers send no CORS headers; Haven Desktop and the native apps solve that in their shell, the PWA uses this relay.
Security: see SECURITY.md for why the Worker and Cloudflare never see mail credentials or content.
The Worker is a byte pipe: it opens the TCP connection (connect() from cloudflare:sockets) and forwards bytes over a WebSocket. TLS runs in the browser, end to end with the mail server, so the Worker never sees passwords or mail, only host names, timing and sizes.
| Name | Kind | Meaning |
|---|---|---|
RELAY_TOKEN |
secret | Shared secret Haven sends to open a tunnel (openssl rand -base64 32). Comma-separate several for rotation. |
TICKET_PUBLIC_KEYS |
var | Alternatively (Mindoo's default relay): Ed25519 public keys (raw, base64, comma-separated) of MindooDB servers. A tunnel then needs a 5-minute relay ticket the server issues to tenant members (POST /:tenantId/relay-tickets, MindooDB MINDOODB_RELAY_TICKET_KEY). |
TURNSTILE_SECRET + SESSION_KEY |
secrets | Mindoo's default relay: POST /v1/session with a solved Cloudflare Turnstile token returns a session (HMAC-signed with SESSION_KEY, SESSION_MINUTES default 30) that opens tunnels. Keeps bots out without any account. |
TURNSTILE_SITE_KEY |
var | The widget's site key. With it, GET /v1/turnstile?origin=<Haven origin> serves the check page that Haven frames: it runs Turnstile on the relay's own origin (Haven's script policy stays closed) and posts the session to the Haven window. The widget's hostname list needs the relay's hostname. |
RELAY_RATE_LIMIT |
rate-limit binding | Optional; limits sessions per IP, tunnels per session and IP, per tenant (tickets) or for the token. |
ALLOWED_HOSTS |
var | Optional host patterns, e.g. imap.gmx.net, *.example.com. Empty: every public DNS name. |
ALLOWED_PORTS |
var | Optional. Default 143, 993, 465, 587, 443. Port 25 is never allowed (Cloudflare blocks it anyway). |
ALLOWED_ORIGINS |
var | Optional. Your Haven's origin, e.g. https://haven.example.com. |
IP literals and local names (localhost, *.local, *.internal, …) are always refused.
GET /v1/status → JSON with version and effective policy.
POST /v1/session with {"turnstile":"…"} → {"session":"…","expiresAt":…}.
GET /v1/turnstile?origin=… → the check page for a frame of an allowed origin; it posts {"type":"haven-relay-session","session","expiresAt"} (or error, or interactive: true when Turnstile wants a click) to that origin.
/v1/tunnel (WebSocket): first message (text) {"v":1,"token":"…","host":"imap.example.com","port":993} (or "session":"…" or "ticket":"…" instead of "token"); answer {"ok":true} or {"ok":false,"error":"…"}; then binary frames are the TCP stream.
client/relaySockets.ts: theSocketProvidermindoomail-facade takes (connect,read,write,startTls,close), TLS 1.2/1.3 in the browser via@reclaimprotocol/tlson WebCrypto.client/strictCertificates.ts: chain checks the TLS library lacks (issuers must be CAs, leaf must allowserverAuth).client/relayFetch.ts: HTTP/1.1 over the tunnel for CalDAV/CardDAV.
npm install
cp .dev.vars.example .dev.vars # set RELAY_TOKEN; ALLOW_LOOPBACK=1 and extra ALLOWED_PORTS for local tests
npx wrangler dev --port 8797
npm test # policy unit tests
RELAY_TOKEN=… npx tsx test/e2e-local.ts # facade IMAP/SMTP against local Stalwart
RELAY_TOKEN=… npx tsx test/e2e-forged-chain.ts # forged certificate chain is refused
RELAY_URL=wss://…/v1/tunnel RELAY_TOKEN=… npx tsx test/e2e-remote.ts