# Space Ports The Spaces sidebar, plus one line under each space listing the TCP ports that space's programs are listening on, like the old herdr-gpui status line. Click a port to open it in the browser: on this computer it opens `localhost:`, on another machine it first starts an `ssh -L` forward to this computer. ![Space Ports](preview.png) ## What you see - The shipped Spaces list, unchanged, except that a space with listening ports is 18px taller. - Under its name row: a globe (`U+1F310` text form), then `:` for each port in ascending order, 11.5px, `theme().dim`, 6px apart, indented 27px (22px for a worktree row) so it lines up with the name. - A port forwarded right now is drawn in `theme().accent`. If it was forwarded to a different local port (the same number was taken here), it reads `:โ†’`. ## How it behaves - Every 5 s, for each machine that has a space with a live terminal, one `sh` runs there (through `exec(..., { on: machine })`). It lists listening sockets with their pids (`ss -ltnpH` on Linux, `lsof -iTCP -sTCP:LISTEN` on macOS) and the `pid ppid` table. A port belongs to a space when its process descends from one of that space's terminals' shell pid (label `herdr.space`). The row redraws only when the set changes. - Hover a port: a tooltip says what a click does (`Open localhost:N`, `Forward glod:N and open`, or `Forwarded to localhost:M`). - Click a local space's port: `open http://localhost:/`. - Click a remote space's port: if a forward for it is live, open its local port. Otherwise pick a local port (the same number if free, else a random one from 20000โ€“59999), start `hc run --bg --no-parent --no-notify --rm --label ports.forward=:= -- ssh -N -o ControlMaster=no -o ControlPath=none -o ExitOnForwardFailure=yes -o ServerAliveInterval=15 -L 127.0.0.1::localhost: `, wait (up to ~10 s) for it to listen, show a notification, and open it. `` is the machine's `target` from `hc machine ls --json`. - Right-click a forwarded port: `hc kill` its ssh terminal, which ends the forward. - Forwards are herdr-core terminals, so they survive closing the window. They are found again by their `ports.forward` label. - Clicking a port does not select the space (the event stops at the port). ## Built from - A clone of the shipped `spaces` mod (`mod.clone {name: "spaces", as: "spaces-ports"}`). The layout's `spaces` block points at `{ "mod": "spaces-ports" }`. - `ports.js`: `PortScanner` (`scan()` โ†’ `bySpace: { [spaceId]: number[] }`), `forwards()`, `openPort(machine, port, target)`, `stopForward(machine, port)`. Uses `terminals`, `exec`, `hc`, `notify` from `herdr`. - `main.js` edits to the clone: import `ports.js`; in `init`, create the scanner and scan every 5000 ms with `cx.timer.every` (not once from init); in `row()`, add 18px when there are ports and append `portsLine(w, tree, t)`; add `portList(w)`, `otherPorts(w)`, `hasPorts(w)` and `portsLine(...)`. - Other ports: every listener the scan saw on a machine that no space owns, below 32768, whose process or an ancestor has a tty (`ps -o tty=` not `?`/`??`), minus the local ends of this mod's forwards, goes to `byMachine[machine ?? ""]`. `portsLine` draws the space's own ports, then a `ยท`, then those at opacity 0.6; they click like the others. - Overflow: the root reports its width (`on_size_change`); `portsLine` estimates each chip at `(digits + 1) * 5.8 + 10` px and, if the line doesn't fit, keeps every own port and as many others as fit, then a `+N` chip. Its click opens this same mod as a popup: `openOverlay({ mod: "spaces-ports", kind: "popup", props: { ports: { machine, target, items } } })`. `init` sees `props().ports`, starts nothing (no scanner, no timers), and `render` draws only `popupView()`: one row a port, same chip, closes after a click. - Space ids: a local space is its `herdr.space` label. A remote one is `/