Skip to content

feat: listen changes that take effect cleanly, keys shown in full, no global cap - #91

Merged
fylorn merged 3 commits into
mainfrom
feat/listen-and-keys
Sep 22, 2026
Merged

fylorn merged 3 commits into
mainfrom
feat/listen-and-keys

Conversation

@fylorn

@fylorn fylorn commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Why

The desktop app is moving the listen settings into Settings behind an explicit Save button, giving keys their own page with the full value and a copy button, and dropping the global concurrency limit. Doing that honestly needed four things from core:

  1. The status lied about the address. Status::gateway_addr was recorded once at startup. After changing the port, the gateway was already serving on the new one while the sidebar still said the old one.
  2. A rebind left a gap with no listener. serve_following_config awaited the old listener's graceful shutdown — i.e. every in-flight request, possibly a multi-minute stream — before binding the new address. In between, neither address accepted connections.
  3. A rebind that could not bind killed the process. Saving a port another program holds returned an error out of the serve loop, the supervisor restarted core with the same config, and it failed again.
  4. "Local network" cut off every client on this machine. Binding en0 bound only its address, and adopted clients are pointed at 127.0.0.1.

What changes

Listening (tw-gateway/src/listen.rs)

  • One serve task per address; the loop binds only the addresses that are new, and only after all of them are bound does it tell the ones no longer wanted to drain. Addresses kept across the change (loopback when switching local ↔ LAN) are never touched.
  • AppState::listening() is the live state: the bound addresses plus, when the last change could not be applied, the reason. /status reads it (gateway_addr, new listen_error); the diagnostics bundle too. Event::ListenChanged { addr, error } is emitted when it changes.
  • A failed rebind keeps the current listener and records gw.listen.port_taken / addr_unavailable / denied / bind_failed / unresolved.
  • relisten uses notify_one, so a change that arrives while a rebind is in progress is not lost.
  • GatewayListen::addrs() replaces socket_addr(): a concrete interface is listened on alongside 127.0.0.1. The allow-list always admits loopback peers (they still need a key).

PUT /listen (tw-control/src/listen.rs)

  • Takes { bind, port, allow_from, base_version }, parses bind with the config file's own rules, resolves the interface, and tries to bind the new addresses before writing. A port in use is a 409 with gw.listen.port_taken and the file is untouched. Defaults are removed from the file rather than written out.

  • An interface that cannot be resolved comes back as gw.listen.no_such_nic (with the interfaces that do exist) or gw.listen.nic_no_addr, so the UI can say it in the user's language.

  • ListenView.allow_from is the list as written. It used to be the effective list, which fills an empty one with the private ranges — a form that loads it and saves it back would turn "nothing set" into six lines of defaults.

Keys

  • GET /keys returns key values in full. /overview keeps its copy masked; upstream credentials are unaffected.

Limits

  • limits.max_concurrent and the global semaphore are removed; per-upstream and per-key limits remain, as do the queue depth and timeout. A config that still contains max_concurrent is rejected as an unknown field, in line with the project's no-compat policy.

CONTROL_API_VERSION → 5.

Tests

  • tw-control/tests/listen.rs: writes and defaults, a taken port refused with nothing written, unknown interface / bad bind / port 0, stale version, the status following the listener after a save, and the overview reporting the allow-list as written.
  • hotreload.rs: the port move now also checks the state and the event; the in-flight test now asserts the new port accepts while the old request is still running; new test for a taken port keeping the old listener and clearing the error once it is free.
  • Unit tests for addrs(), loopback always allowed, and the gate without a global dimension.
  • Checked by hand with the release binary: bind: en0 listens on 127.0.0.1:p and 10.0.3.7:p, both answer; PUT /listen to another port moves /status immediately; a taken port is refused.

cargo fmt --check, clippy -D warnings, cargo test --workspace and scripts/smoke.sh (51 passed) pass locally.

🤖 Generated with Claude Code

@fylorn
fylorn force-pushed the feat/listen-and-keys branch from cb888ee to 7946640 Compare September 22, 2026 03:22
fylorn and others added 3 commits September 22, 2026 11:29
… global cap

Listening
- The status now reports where the gateway is actually listening. It used
  to be the address recorded once at startup, so after a port change the
  UI kept showing the old one while traffic was already on the new one.
  `Event::ListenChanged` says when the listener moved or failed to.
- A rebind binds the new address first and only then lets the old one
  drain. It used to wait for every in-flight request on the old listener
  to finish before binding the new one, leaving no listener at all for
  as long as a stream ran.
- A rebind that cannot bind (port taken, interface without an address)
  keeps the current listener and reports why in `Status::listen_error`.
  It used to return an error that took the whole process down.
- Binding a specific interface also listens on loopback. Adopted clients
  are pointed at 127.0.0.1, so "local network" used to cut every client
  on this machine off. Loopback peers are always let through the source
  allow-list for the same reason.
- `PUT /listen` saves bind, port and allow-list together and tries to
  bind the new address before writing, so a config that cannot take
  effect is never written.

Keys
- `GET /keys` returns the key values in full; the keys page shows them
  with a copy button. `/overview` keeps them masked.

Limits
- `limits.max_concurrent` is gone. On a desktop gateway it only throttled
  the user's own parallel sessions; the per-upstream and per-key limits
  are what guard against a slow upstream or a runaway script.

CONTROL_API_VERSION goes to 5.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…ss, with their own codes

A bind that names an interface can fail two ways that call for different
actions: the name is wrong (fix the setting), or the interface has no
address right now (plug the cable in). Both used to come through as one
code carrying the English sentence, so the Chinese UI could only show it
untranslated.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The overview filled an empty allow-list with the private ranges when the
gateway listened beyond this machine. A settings form that loads that list
and saves it back would rewrite a config that said nothing into six lines
of defaults, and could no longer tell the user that nothing was set.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@fylorn
fylorn force-pushed the feat/listen-and-keys branch from 7946640 to 4850d0c Compare September 22, 2026 03:31
@fylorn
fylorn merged commit b61ade5 into main Sep 22, 2026
1 check passed
@fylorn
fylorn deleted the feat/listen-and-keys branch September 22, 2026 03:40
@fylorn fylorn mentioned this pull request Sep 22, 2026
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