The gateway can run as a set of sdme containers, but a private bridge alone is not a trusted transport. This guide describes the production-shaped layout for hosts that also provide an authenticated, encrypted service overlay.
It mirrors the maintainer's reference deployment (the chan-prod-setup repo). It describes the topology and one worked flow end to end, and points at the checked-in manifests (packaging/kube/) for the rest rather than duplicating every config here.
A faster inner loop exists for rapid iteration:
packaging/gateway/scripts/dev/run.shruns the services as hostcargo runbinaries over*.localtest.me(seepackaging/gateway/scripts/dev/README.md). That is handy while editing code, but it is NOT the prod-like shape. This guide is the containerized stack.
Load-bearing transport requirement: an ordinary sdme
--network-zonesupplies network-namespace/L2 isolation, not authenticated encryption. It is therefore not sufficient grounds forCHAN_GATEWAY_INTERNAL_TRANSPORT=protected-overlay; never set that value just to make a cleartext non-loopback bind pass validation. The container commands below assume WireGuard, mTLS, or an equivalent authenticated and encrypted overlay is already in place wherever cleartext crosses a namespace boundary. Without one, use the checked-in local runner: it keeps every cleartext Rust listener on literal loopback and puts verified TLS edges in the same host namespace.
The gateway's cross-tenant isolation is carried by two host-scoped cookies: __Host-id_session (host-only on the identity origin gw.<domain>) and __Host-devserver_gate (host-only on the tenant host {owner}--{disc}.{proxy}.proxy.<domain>, scoped Path=/ for the whole devserver). No parent-domain cookie exists, so a browser never auto-attaches an identity session to a fetch on another tenant's host. The whole-host devserver cookie is safe because the grant is whole-devserver; user-to-user isolation rides the host-only cookie plus the aud claim. That design, plus the reverse-proxy header hygiene (hop-by-hop stripping, dropped inbound Host/Cookie/Authorization, recomputed X-Forwarded-*), only fully exercises behind a real TLS terminator with real subdomains. Running the same services in the same two-plane shape as production is how you exercise it.
Production splits the gateway into an account and control plane and a proxy data plane, and the local stack mirrors that split:
- The gateway pod: postgres, profile, identity, and devserver-control run as one pod sharing a network namespace, so they reach each other over loopback and no inter-service port is published. devserver-control rides inside the pod deliberately: identity and profile reach it on loopback instead of depending on cross-container name resolution. identity has two listeners: the public one behind the TLS edge, and an internal token-validate listener that is never routed publicly.
- A TLS edge for the identity origin: Caddy (
caddy:2.11.9-alpine, sharing the canonicalCaddyfileinpackaging/compose/Caddyfileandpackaging/kube/caddy.yaml) servinggw.<domain>over TLS 1.3 with post-quantum hybrid key exchange (x25519mlkem768preferred overx25519fallback) and ECDSA P-384 certificates;:80handles ACME and HTTPS redirects (the OAuth flow never runs over cleartext),:443proxies to identity's public listener. - The proxy plane: each proxy node runs devserver-proxy behind Caddy. That edge serves the
proxy.<domain>tunnel apex (POST /v1/tunnelis negotiated as h2 externally and forwarded as h2c into the proxy's tunnel listener, with idle body timeouts disabled under The TLS edges) and the node's tenant wildcard*.{proxy}.proxy.<domain>(ordinary HTTP, streaming responses, and WebSocket upgrades, using On-Demand TLS gated by the proxy's internal/tls-asklistener or a DNS-01 wildcard cert). The node connects back to the plane above over the authenticated overlay, and can reach exactly two things there: devserver-control's proxy-control listener and identity's internal token-validate listener. Nothing else (postgres, profile, identity's public listener) is reachable from a proxy node; production treats proxy nodes as compromisable, andpackaging/kube/network-policy.yamlencodes the same reachability contract for clusters.
flowchart TB
browser["browser (https)"]
devsrv["chan devserver (tunnel dial)"]
subgraph account["account + control plane"]
gwnginx["TLS edge: gw.{domain}"]
pod["gateway pod: postgres + profile + identity + control (loopback)"]
end
subgraph node["proxy node"]
pnginx["TLS edge: proxy.{domain} apex + *.p1.proxy.{domain}"]
proxy["devserver-proxy"]
end
browser -->|"sign-in"| gwnginx --> pod
browser -->|"tenant"| pnginx --> proxy
devsrv -->|"/v1/tunnel (h2c)"| pnginx
proxy -->|"overlay: control session + token validate ONLY"| pod
The production shape: one TLS edge per plane, the account pod on loopback, and a proxy node that reaches exactly two internal listeners over the overlay.
Admission fails closed: the proxy admits a tunnel registration only after identity validates the PAT and devserver-control verifies the identity-signed admission lease, so a proxy that cannot reach the control plane serves nothing. devserver-control also rejects a proxy whose package version differs from its own, so every service and every node must run the same release tag.
Install sdme. On Linux, on the host:
curl -fsSL https://sdme.io/install.sh | sudo shOn macOS, sdme runs inside a Lima VM; install Lima and then sdme inside the VM, per macOS only: Lima shim. Either way the sdme ... commands below then work (on macOS through the alias).
The checked-in manifests are the source of truth: packaging/kube/README.md covers building the service images (packaging/docker/build.sh), generating the secret set (every bearer, both Ed25519 key pairs, the per-proxy credential map), applying sdme/gateway-pod.yaml, and proving the stack healthy. The same README documents the cluster-manifest shape for real deployments and the database-role pipeline (the owner credential exists only in the migration job; identity and profile run app roles with CHAN_GATEWAY_MIGRATIONS=external).
The sdme pod is a functional-validation shape: its containers share one network namespace, so it proves wiring and behavior, not credential isolation. The per-service env contract lives in gateway/crates/*/packaging/*.env.
Caddy (caddy:2.11.9-alpine) is the canonical TLS edge across Docker Compose (packaging/compose/Caddyfile), Kubernetes (packaging/kube/caddy.yaml), and the local sdme pod (packaging/kube/sdme/gateway-pod.yaml). One Caddy instance can front both host families on a single host, while multi-node deployments give each proxy node its own Caddy edge. The route map is the topology above: gw.<domain> to identity's public listener (:7000); the tunnel apex (proxy.<domain> or p1.<domain>) and tenant wildcard (*.{proxy}.proxy.<domain> or *.p1.<domain>) to devserver-proxy's HTTP listener (:7002), blocking /readyz; and POST /v1/tunnel to the proxy's tunnel listener (:7100) with transport http { versions h2c }. The internal listeners (identity :7004, control :7003/:7101, and Postgres :5432) are never routed by Caddy; Caddy reaches only devserver-proxy's optional internal /tls-ask listener (:7005) to gate On-Demand TLS.
The edge enforces TLS 1.3 (protocols tls1.3), post-quantum hybrid key exchange (curves x25519mlkem768 x25519 secp384r1 secp256r1, keeping x25519 fallback while chan-tls Rust clients use ring), and ECDSA P-384 leaf certificates (key_type p384, avoiding RSA-2048), with Caddy's admin API disabled (admin off).
The tunnel route needs idle timeouts that fit a tunnel. A registered tunnel is one long-lived POST /v1/tunnel whose request body is the devserver-to-proxy direction, and that body is idle for long stretches when nothing is using the devserver. In Caddy 2.11, read_body_idle defaults to 1m, which keeps slow-body protection active on gw and *.{proxy} while the @tunnel handler explicitly disables per-request body/write idle timeouts (timeouts { read_timeout 0; write_timeout 0 }, alongside flush_interval -1 and read_timeout 0 / write_timeout 0 on the h2c transport). If you deploy nginx instead (as the tunnel e2e harness nginx-edge.sh does), raise client_body_timeout and grpc_read_timeout on the /v1/tunnel location for the same reason.
Both edges decode a bearer they then forward. The tunnel edge decodes every dial's Authorization: Bearer <PAT> header; the identity edge decodes the desktop PATs that /desktop/v1/* carries. Any TLS-terminating edge holds the bearers it decodes, so isolate an edge host the way the gateway pod is isolated, and keep PAT rotation and revocation as the control. The trust-boundary section of crates/chan-tunnel-server/design.md says what a captured tunnel PAT reaches.
For certificates, Compose and Kubernetes use public ACME HTTP-01 for the static apex hosts plus On-Demand HTTP-01 gated by /tls-ask for tenant hosts (zero DNS API credentials needed), with Caddyfile.dns01.example as the wildcard DNS-01 alternative for CT-log privacy or WireGuard-only hosts. Locally in sdme/gateway-pod.yaml, Caddy sets local_certs to issue certificates from its built-in local CA (*.localtest.me resolves every subdomain at any depth to 127.0.0.1 via public DNS); copy /data/caddy/pki/authorities/local/root.crt from the pod's caddy container to trust it in your host browser and chan-tls system trust store.
Whatever crosses a namespace boundary in cleartext must ride an authenticated, encrypted overlay; WireGuard between the proxy node's container and the account plane is the reference shape. Give the proxy reachability to only the two internal listeners it needs, mirroring network-policy.yaml. Every cleartext non-loopback service sets CHAN_GATEWAY_INTERNAL_TRANSPORT=protected-overlay, and that assertion is valid only after the overlay exists.
Sign in at https://gw.localtest.me (or your chosen identity origin). Both feature flags ship default-off, so enrol yourself after the first sign-in with the admin CLI, run from wherever it can reach profile's service API (inside the pod when your image set carries it, or cargo run -p admin -- from gateway/ pointed at a reachable profile listener):
chan-gateway-admin flag grant oauth_login <your-email>
chan-gateway-admin flag grant share_workspaces <your-email>Publish a devserver from the sibling chan repo over the TLS tunnel apex. Registration is per-devserver: one tunnel carries the whole library, so add a workspace first and then dial:
cargo run -p chan -- workspace add <workspace-dir>
export CHAN_TUNNEL_TOKEN=chan_pat_... # mint under the dashboard Tokens tab
cargo run -p chan -- devserver run --tunnel-url=https://proxy.localtest.me/v1/tunnelClicking Open on the dashboard lands on the tenant origin, https://<owner>--<disc>.p1.proxy.localtest.me/<slug>-<8hex>/ (the workspace's tenant mount inside the devserver).
Because the local stack already has the prod shape, going to a real host changes only what is environment-specific:
- DNS. Real records for
gw.<domain>, the tunnel apex (p1.<domain>orproxy.<domain>), and each node's tenant wildcard (*.p1.<domain>or*.{proxy}.proxy.<domain>). - Certificates. Replace
local_certswith public ACME viapackaging/compose/Caddyfile(On-Demand HTTP-01 gated by/tls-ask) orpackaging/compose/Caddyfile.dns01.example(wildcard DNS-01). - Secrets. Real per-service secrets from
packaging/compose/init.sh, Kubernetes per-workload Secrets, or/etc/chan-gateway/; keepCOOKIE_SECURE=true. - Lockstep. Deploy every service and every proxy node on the same release tag; a version-mismatched node is rejected at the control handshake.
On macOS, sdme runs inside a Lima VM because it needs systemd. Lima uses host networking, so container ports show up on macOS localhost exactly as on a native Linux host. macOS $HOME is bind-mounted into the VM read-only via virtiofs: edit and build on macOS, sdme sees the result.
brew install lima
limactl start default # Ubuntu, host networking
# install sdme inside the VM:
limactl shell default -- sh -c \
'curl -fsSL https://sdme.io/install.sh | sudo sh'
alias sdme='limactl shell default sudo sdme' # then every sdme example runs verbatimThe bare limactl shell default sudo sdme ... form works too (useful for scripts and agents, where the interactive alias does not resolve).
cd gateway
export TEST_DATABASE_URL=postgres://chan:chan@127.0.0.1:5432/chan_gateway_test
(cd ../web && npm ci && npm run build -w @chan/profile) # gateway identity SPA (rust-embed input)
cargo test # profile + identity need the DBdevserver-proxy and all cargo test --lib unit tests need no database; only profile and identity integration tests do. The pod's postgres is internal to the pod, so host-run tests use a separately published local Postgres (the dev runner's, or a one-off container with -p 5432:5432 seeding chan_gateway and chan_gateway_test). Per-test schema isolation means a cargo test run never clobbers the chan_gateway DB a running stack uses. The gate job of Gateway CI builds and checks the profile SPA and runs Rust formatting, clippy, rustdoc and all gateway tests with a postgres:16 service on ubuntu-latest (x86_64), the reference run; local sdme is the fast loop. A separate packaging job checks the Kubernetes and Compose manifests and package isolation and runs the database-role suite against its own PostgreSQL 16 service, because that suite rewrites cluster-wide roles. The containers job builds the five gateway OCI images without publishing them. The workflow runs on matching pushes to main and pull requests, and by manual dispatch. Its filter covers gateway/** plus the root Rust dependencies and shared build inputs, the profile SPA and web package files, the Compose, Docker, and package files, the Kubernetes manifests, the devserver sdme provisioner, and the workflow itself; see the workflow for the exact paths and CONTRIBUTING.md for the input set.
A flaky cargo test can panic mid-test and orphan sqlx pool connections; the role goes idle holding slots and the next run hits PoolTimedOut. tests-shared/pg_reaper.rs (wired into every DB-backed TestApp::new()) opens one durable connection and pg_terminate_backend()s its own role's idle peers on first use, then holds that connection so the role never falls fully idle. It recovers the realistic case automatically. The one case it cannot is full exhaustion (all non-superuser slots pinned): it panics pointing here. Reap manually as the postgres superuser against whichever Postgres your tests target, for example:
psql -U postgres -h 127.0.0.1 -c \
"SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE usename='chan';"Safe whenever no live stack is connected to chan_gateway.
- Full container name: pass the name you created (
chan-gateway, ...). sdme also accepts an unambiguous prefix, but the full name keeps the examples copy-pasteable. - Full paths after
--:machinectl shellsets noPATH. Use/usr/bin/psql,/usr/bin/runuser,/usr/bin/systemctl. - Interactive shell:
sdme join chan-gatewaydrops you into a real shell inside the pod. - Pod exec:
sdme exec chan-gateway --oci -- <cmd>runs a command inside the pod's shared namespace (the health probes in the kube README use this).
- A service can't reach another: inside the pod they resolve over loopback; across containers they resolve by name only on the shared zone. Check
sdme psand the hostname-based URLs in each unit's env. - Browser rejects the local cert: trust Caddy's local root CA (
/data/caddy/pki/authorities/local/root.crtfrom the pod'scaddycontainer), orpackaging/gateway/scripts/dev/secrets/tls/ca.crtwhen using the host dev runner. - Signed-in but the tenant 404s: confirm Caddy serves https and
FORWARDED_PROTO=httpsis set on devserver-proxy; a scheme mismatch makes the__Host-devserver_gatecookie fail to attach. - The proxy admits nothing: fail-closed is working. Check that the proxy reaches devserver-control over the overlay, that its bearer matches control's per-proxy credential map, and that both sides run the same release tag.
- Tests pass locally but break on CI: the same migration set must run (
migrations/0001..Nin order); a forgotten file shows up as missing-column errors on first use.