dev-code-proxy

Reference

How it works

dcp is two independent pieces that end at the same app: a local HTTPS proxy for *.localhost names, and the online links: a path-routing proxy behind one Tailscale entry, and, when you turn it on, a host router behind a Cloudflare Tunnel.

Local: only this Mac, through the dcp proxy on port 443Online: anyone with the link, through Tailscale, then dcp

A browser on your Mac opens https://api.acme.localhost; the dcp proxy on port 443 passes the request to the app on 127.0.0.1:4417. A Stripe webhook, your phone or a teammate opens https://bigfoot.tail1234.ts.net/api/; Tailscale Funnel ends TLS on your Mac and dcp sends /api/ to the same app.

The local side (the boot service, the route files, the certificate authority) is described in Local HTTPS names. This page covers the online side.

Online: architecture

One Tailscale entry terminates TLS and forwards plain HTTP to the local dcp proxy, which routes by the first path segment to the matching dev server.

Discovery pipeline

Every --interval seconds:

  1. List listeners — TCP sockets in LISTEN within the port range, with PID (macOS/Linux via lsof+ps, Windows via netstat+tasklist). On macOS/Linux, process details (runtime path, working directory, project root) are cached per PID, so an idle re-scan spawns a single lsof.
  2. Classify runtime from the executable name (node, bun, deno, python/uvicorn/gunicorn, ruby/puma, php, go run, java, dotnet, elixir, docker-proxy, …).
  3. Slug = the nearest project-root folder name (markers: package.json, .git, go.mod, pyproject.toml, Cargo.toml, mix.exs, …).
  4. Resolve duplicates — within one project folder the process on the lowest port is the main service (clean, port-free slug); any other process in the same folder gets a -<port> suffix so it stays reachable. A single process on several ports collapses to its lowest port. Distinct projects sharing a folder name also get a -<port> suffix to stay unique.

Routing

For /<segment>/<rest…>?<query>:

  • Hit → forward to http://127.0.0.1:<port>/<rest…> (segment stripped, Host rewritten to localhost). Streaming flushes immediately; WebSocket upgrades are relayed.
  • Miss / empty → 404 listing the registered services.
  • Dead backend → 502.

Slugs are canonically dash-separated. With --match-separators (on by default), an exact miss is retried with _ folded to -, so /module_api_foo/ and /module-api-foo/ reach the same server. --match-separators=false routes only the exact dash form.

Apps assume they live at the root, so their HTML references absolute paths (/_next/..., /api/..., HMR). When you open …/<slug>/, dcp sets a dcp_route cookie pinning that browser tab to the project, so prefix-less requests follow it to the right backend — and the page renders exactly like localhost.

Lifecycle

  • Sharing is on by default and lives in the local HTTPS proxy, so it starts at boot, survives worker swaps (the entry stays; the next worker carries on) and comes back after a Tailscale restart within seconds.
  • A RouteStore is refreshed on a ticker. A service missing from discovery is kept for deregisterCycles scans before removal (no flapping on restarts).
  • Without the boot service, dcp start runs the same pieces in a terminal; on Ctrl-C the server drains and tailscale serve|funnel reset runs before exit — never leaving a Funnel pointing at a dead port.
  • A single bounded http.Transport keeps connections to dev servers from accumulating.

Online: Cloudflare

With cloudflare on, the local HTTPS proxy keeps three things in place, next to the Tailscale entry:

  • A tunnel named dcp-<machine>, managed from Cloudflare's side. Its one ingress rule sends everything to dcp's host router on 127.0.0.1:<port+1>, which picks the app by hostname and presents the request as a local one, path unchanged.
  • One proxied CNAME per app, <app>-<machine>.<zone> → <tunnel-id>.cfargotunnel.com, with the comment dcp <machine>. dcp deletes only records that point at its own tunnel and refuses a hostname that already has a record it did not make.
  • The connector, cloudflared, started with the tunnel's token in its environment and restarted when it exits.

The tunnel, its ingress and DNS are checked every 5 minutes and whenever the set of apps changes; the tunnel's health every 30 seconds. Turning Cloudflare off stops the connector and keeps the tunnel and DNS, so it comes back fast; dcp cloudflare off --purge deletes them. Setup and the token: Cloudflare.