Skip to content

Add per-session dev servers behind a wildcard hostname router - #58

Open
devbdavis wants to merge 2 commits into
mainfrom
workshop/dev-server-router
Open

devbdavis wants to merge 2 commits into
mainfrom
workshop/dev-server-router

Conversation

@devbdavis

Copy link
Copy Markdown
Owner

Summary

Each session gets a Dev Server button. It opens https://<track>-<sid>.$WORKSHOP_DEV_DOMAIN, and Workshop starts the track's dev command for that session on demand and serves it through its existing port 3002. Docker publishes no new ports. The feature is off unless WORKSHOP_DEV_DOMAIN is set.

  • Routing (src/server/dev-server-router.ts, dev-host.ts). Any request whose Host is <slug>-<12-hex sid>.<domain> is handled entirely by the router, including /api/* and WebSocket upgrades. On each request the router:

    1. authenticates;
    2. looks up the live session and its track's devCommand;
    3. starts the server if needed;
    4. proxies to 127.0.0.1:<port>.

    While the server is starting, HTML requests get a page that reloads itself and other requests get 503. A failed server shows its exit code, the last 50 log lines and a Retry link, and is never restarted automatically.

  • Auth. The button fetches a ticket valid for 60 s (HMAC(auth token, sid, exp)). /__workshop/auth swaps it for a workshop_dev cookie that only that host receives, valid for 7 days. Workshop's own token never reaches a dev host.

  • Origin guard (auth-middleware.ts). Dev hosts count as the same site as Workshop, so the browser would attach Workshop's SameSite=Strict cookie to requests made by a dev app's pages, including a request to open the terminal WebSocket. extractToken now ignores Workshop credentials when the request's Origin is a dev host, or when Sec-Fetch-Site is same-site and the request is not a top-level navigation.

  • Process (src/infrastructure/dev-server/dev-server.ts).

    • Each server runs in a tmux session named dev-<sid>, in the session's workspace.
    • It gets the same environment-cleaning flags as agent sessions (now in terminal/tmux-flags.ts) plus PORT, and loads direnv when there's an .envrc.
    • The real exit code is captured.
    • After 120 s without the port answering, the start is treated as a timeout.
    • The server is stopped when its session is deleted, soft-deleted or purged.
  • Proxy (src/server/reverse-proxy.ts). The forwarding code was extracted from the code-server proxy, and /code behaves the same. For dev hosts it rewrites Host, replaces client-supplied x-forwarded-*, and strips Workshop cookies, a Bearer carrying the Workshop token, and a ?token= holding it.

  • Data and UI.

    • New tracks.dev_command column, editable in track settings.
    • New route GET /api/sessions/:sessionId/dev-server returns { url }.
    • The button pre-opens a tab (to get past popup blockers) and sets opener = null.
  • Deployment and docs.

    • WORKSHOP_DEV_DOMAIN added to the compose file and the unraid template.
    • New "Dev servers" section in docs/docker.md.
    • The design and its review are in docs/plans/.

What the reviewer should look at first:

  • auth-middleware.ts isFromDevHost: it changes how every Workshop request is authenticated once the domain is set.
  • reverse-proxy.ts: confirm /code is unchanged.

Setup needed before this works: a wildcard DNS record and certificate for *.<WORKSHOP_DEV_DOMAIN>, pointing at port 3002 with the Host header kept. Cloudflare's free certificate only covers one level below the zone (see docs/docker.md).

This replaces #57, which published a range of unauthenticated ports.

Test plan

  • npx vitest run: all 895 tests pass. New tests cover:
    • hostnames, tickets and the Origin guard;
    • the router: auth, handoff, 404s, starting/exited pages, proxying with credentials stripped, /api/* on a dev host, a malformed // path;
    • the API route;
    • the devCommand round-trip through the repository;
    • the tmux-backed manager against a real tmux and python3 -m http.server, including exit-code capture.
  • tsc -p tsconfig.server.json passes. tsc -p tsconfig.json passes apart from @xterm/addon-webgl, which is missing from the shared node_modules in this environment and fails the same way on main.
  • Not run: a real deployment with wildcard DNS and a certificate, Vite hot reload through the proxy, and npm run build (blocked by the same missing module).

🤖 Generated with Claude Code

devbdavis and others added 2 commits November 23, 2026 01:22
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A track's dev command now runs on demand for each session in tmux, and
Workshop serves it at <track>-<sid>.$WORKSHOP_DEV_DOMAIN through the same
port 3002. A browser signs in to a dev host with a short-lived HMAC
ticket, exchanged for a cookie that only that host receives. Workshop's
own credentials are ignored on requests that come from dev-host pages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant