2
0
Fork 0
Mobile-first PWA to make tmux portable
  • Rust 53.8%
  • TypeScript 42.5%
  • JavaScript 2.5%
  • CSS 0.7%
  • HTML 0.5%
Find a file
2026-11-29 16:09:41 +00:00
adr feat: use a one-line textarea for the swipe-type field 2026-11-29 16:09:41 +00:00
deploy feat: add bell notifications 2026-11-04 12:01:11 +00:00
mux_portal feat: show the session age and mark the last opened window 2026-11-24 00:15:36 +00:00
mux_portal_ui feat: use a one-line textarea for the swipe-type field 2026-11-29 16:09:41 +00:00
.gitignore feat: add bell notifications 2026-11-04 12:01:11 +00:00
AGENTS.md feat: add crates 2026-10-31 20:03:48 +00:00
Cargo.lock feat: add bell notifications 2026-11-04 12:01:11 +00:00
Cargo.toml feat: add bell notifications 2026-11-04 12:01:11 +00:00
config.example.json feat: add bell notifications 2026-11-04 12:01:11 +00:00
dprint.json feat: add crates 2026-10-31 20:03:48 +00:00
README.md feat: use a one-line textarea for the swipe-type field 2026-11-29 16:09:41 +00:00
rustfmt.toml feat: add crates 2026-10-31 20:03:48 +00:00

Mux Portal

A web portal for terminal multiplexer sessions, with a Rust backend and a React interface, shipped as one binary. The first target is tmux.

The backend serves a JSON API under /api and the web interface under a configurable base path. The interface is a Vite/React single-page application; its bundle is built by the mux_portal_ui package and embedded into the release binary. The decisions live in adr/; adr/README.md is the index.

Status: configuration, password login, cookie sessions, tmux inventory, session/window management, the xterm.js terminal, bell notifications, the mobile/PWA layer and the systemd deployment are implemented.

Layout

  • adr/ — architecture decision records, the source of truth for the design;
  • mux_portal — the backend: the axum application, configuration, authentication, API routes and the binary;
  • mux_portal_ui — the web UI: the Vite project at mux_portal_ui/ui/, its build integration and the static host mounted by the backend;
  • deploy/ — the systemd unit and the deployment runbook.

See adr/package.md, adr/config.md, adr/auth.md, adr/tmux.md, adr/terminal.md, adr/ui.md, adr/notifications.md and adr/deploy.md.

Requirements

  • a Rust toolchain with Cargo;
  • Node.js 20.19+ (or 22.12+) and npm for the UI;
  • tmux 3.1 or newer on the target host; the integration tests use it when it is installed and skip otherwise. tmux 3.9 also has the side status line, which the portal turns off inside its viewer sessions;
  • systemd and an external TLS terminator for deployment; the backend itself speaks HTTP;
  • OpenSSL development headers to build and libssl at runtime for Web Push (ece and native-tls).

Configuration

The portal is configured by one JSON file, passed with --config:

cp config.example.json config.json
cargo run -p mux_portal -- hash-password

hash-password reads the password twice without echo and prints an Argon2id PHC string. Put that string into passwordHash; the example contains a placeholder and refuses to start until it is replaced.

The optional push block may carry the REPLACE_WITH_THE_OUTPUT_OF_vapid-keys placeholder. On the first start the server generates a VAPID key pair, writes it back to config.json with mode 0600 and logs only that it did so. The service user must be able to write the file; the systemd unit grants /etc/mux-portal. mux_portal vapid-keys remains for manual generation. push.subject is required and must be a real contact URI; Apple rejects a localhost subject. Replace the example address with your own. Omitting the push block disables server push. The password hash still has to be generated with hash-password; the example refuses to start until it is replaced.

{
  "host": "127.0.0.1",
  "port": 34080,
  "passwordHash": "$argon2id$v=19$m=19456,t=2,p=1$...",
  "muxType": "TMUX",
  "muxBinPath": "/usr/bin/tmux",
  "muxSocketPath": null,
  "basePath": "/",
  "trustProxy": true,
  "secureCookies": true,
  "sessionTtlHours": 24,
  "push": {
    "vapidPrivateKey": "REPLACE_WITH_THE_OUTPUT_OF_vapid-keys",
    "subject": "mailto:admin@example.com"
  }
}
Field Required Default Meaning
host yes — Listen host
port yes — Listen port; the example uses 34080
passwordHash yes — Argon2id PHC string
muxType yes — TMUX
muxBinPath yes — Absolute path to the multiplexer binary
muxSocketPath no null Absolute socket path, or the mux default
basePath no / / or a prefix such as /portal/
trustProxy no false Trust X-Forwarded-* from nginx
secureCookies no true Add Secure to the session cookie
sessionTtlHours no 24 Idle timeout
push no absent VAPID private key and subject; optional

For local development over plain HTTP set secureCookies: false; otherwise the browser will not send the cookie. The target deployment terminates TLS at nginx and keeps it true.

sessionTtlHours is an idle timeout: every authenticated request refreshes it, and the browser cookie is persistent, so an actively used portal stays signed in past the configured TTL. A session that is idle for longer than the TTL is removed on the next request.

The portal does not start tmux. It attaches to an existing server under the same user; if the server is absent the UI will show an error and a Reconnect button. See adr/config.md.

config.json is ignored by git. A file readable by group or others is accepted, but startup logs a warning because it contains the password hash and the VAPID private key.

Sessions and Windows

After login the home page is the session list: every tmux session and its windows. The list is capped at 500px and centered on desktop. Settings, New session and the back-to-terminal close button are in a sticky bottom toolbar under the list; its top border appears only when the list scrolls. Creating, renaming and killing works for both sessions and windows; the list is refreshed every two seconds and after every action. Each session is a section heading with its windows as separate bordered cards; clicking the session name collapses the cards, a chevron shows the state, and the collapsed session ids are kept in localStorage; an empty list leaves every session open. A leading NN: prefix is drawn dimmed, matching the tmux side status. The heading ends with the session age from tmux session_activity (now, 7m, 2h, 3d), right-aligned with the bell dots of the window cards. The window the session used last, read from tmux session_stack, keeps the normal title color while every other window name is dimmed to the border color.

The always-visible ellipsis on a session or window expands its actions inline instead of opening a popup menu. On desktop the window header's top-right button opens the session list; on mobile the terminal shortcut bar's sessions button does. The third toolbar button returns to the window that was open, or is disabled when no window was recorded. Logout is on the Settings page; Settings uses the same sticky bottom bar with a single back arrow and the same 500px centered content column. Clicking a window opens /session/$N/window/@M and attaches its terminal. A window with an unread tmux bell shows a blue dot next to its name. The session header does not aggregate unread windows; opening the window clears the flag. Opening the list from a window draws that window's card on the active-row background and scrolls it into view, so the list shows where the user came from.

If no tmux server is running, the home page shows the error and a Reconnect button. The portal never starts tmux itself. See adr/tmux.md.

The inventory API is:

GET    /api/mux
GET    /api/tree
POST   /api/sessions
PATCH  /api/sessions/{id}
DELETE /api/sessions/{id}
POST   /api/sessions/{id}/windows
PATCH  /api/windows/{id}
DELETE /api/windows/{id}
GET    /api/ws/terminal/{session}/{window}
GET    /api/push
POST   /api/push/subscribe
DELETE /api/push/subscribe
POST   /api/push/test

Every window in /api/tree carries a bell boolean and an alternate boolean for the active pane. The terminal endpoint is a WebSocket upgrade. All of these require the session cookie; /api/health and /api/login are public.

Terminal

The window page runs xterm.js and connects to the terminal WebSocket. Each connection attaches directly to the user's tmux session, selects the requested window, temporarily turns off the horizontal status line and the tmux 3.9 side status line, enables mouse input and sets window-size latest on the window. The previous session options are saved as @mux_portal_saved_* and restored after the last viewer closes; startup cleanup restores them after a crash. No mux-portal-* session is created. The tradeoff is that the current window is shared by all clients of the session.

Binary WebSocket frames carry terminal bytes; text frames carry the resize control. A lost connection is retried with backoff and a Reconnect button. A reconnect attaches to the same session and window; tmux redraws the screen but xterm's local scrollback is not preserved. Closing the page kills the tmux client and restores the session options. See adr/terminal.md.

Notifications

A program in a pane rings a bell, and tmux records it as #{window_bell_flag} when monitor-bell is on (the default). The portal reads the flag from the same list-windows call that builds the tree; it never attaches a client to a window. Several bells collapse into one unread state. The window open in the portal is the exception: the terminal PTY scans its output for BEL and reports every bell, so a chat in that window notifies on each message. A bell in a window viewed by another tmux client is not reported.

The Settings page has a Notifications card. Enabling asks for the browser permission, subscribes with the VAPID key from GET /api/push, and registers the subscription. Web Push delivers a notification with the session and window names even when the PWA is closed. The card also has a Send test button and a per-browser "Notify while the app is in the foreground" switch; when it is off, the service worker suppresses notifications only while the app is in the foreground. A backgrounded or closed app still gets them, and the list icon remains. iOS requires an installed PWA on 16.4 or newer; the card explains the Home Screen step.

Subscriptions are stored in push-subscriptions.json with mode 0600. The file lives in $STATE_DIRECTORY (/var/lib/mux-portal under the systemd unit) or next to config.json when the variable is absent. They survive a portal restart, so the client does not need to re-register; the authenticated page still re-registers idempotently as a safety net. Logout removes the device subscription. The portal does not check or change monitor-bell; keep it on. See adr/notifications.md.

Mobile, PWA and Theme

The UI is dark only, using the One Dark palette. Roboto is self-hosted for the interface and RobotoMono Nerd Font Mono for the terminal; the terminal view is lazily loaded, so xterm.js is fetched only when a window is opened. The terminal uses xterm's WebGL renderer with a DOM fallback, so box-drawing characters such as nvim separators are drawn as exact-color lines instead of antialiased font glyphs. Interface and terminal text both default to 16px; Settings offers 10–24px for each and saves the choice in the browser's local storage.

The home page is the session list and does not move an open terminal while it is not visible. Page headers reserve the iOS safe area, so a standalone PWA does not draw them under the notch or status bar. The app height also includes the iOS status-bar inset in a home-screen launch. A key bar above the mobile keyboard is a fixed nine-slot grid: Esc, Ctrl, Tab, Backspace, Enter, a drag-and-hold arrow pad, a keyboard toggle, a compose field and the sessions button. It never scrolls horizontally. The arrow pad sends one arrow when the finger crosses a direction and repeats it while held; its centre does nothing. Tapping the terminal no longer opens the keyboard; the keyboard slot toggles it. Taps still reach xterm's mouse reports, so mouse-tracking TUI buttons can be pressed without opening the keyboard. The compose slot opens a one-line textarea above the bar with autocorrect and spellcheck enabled, so swipe typing works; it never grows past its single row and does not wrap. Enter sends the text plus a newline to the PTY. Keys preserve the current focus, so tapping them does not open or close the keyboard. Alternate-screen TUIs (pi, nvim, lazygit) use the browser's native scroll model: the shortcut grid is the scroll target. A normal shell instead pins the page to the visualViewport while the keyboard is open, so the terminal resizes above the keyboard rather than the whole document scrolling up. The key bar drops its bottom safe-area padding while the keyboard is open. A vertical swipe over the terminal is forwarded to tmux as a wheel event, scaled slightly above the finger travel so the scroll feels responsive, and the swipe keeps moving with a short momentum phase after the finger lifts. A long press starts a text selection instead: xterm draws to a canvas, so the browser has no native selectable text. The touched word is selected, dragging extends the range and a floating Copy/Cancel bar appears above the key bar. The key bar respects the device safe area.

npm run build also generates manifest.webmanifest, sw.js and the PWA icons. The app is installable as a standalone PWA and the service worker precaches the shell, fonts and hashed assets; /api/ and the terminal WebSocket stay network-only. A new deployment shows a "new version available" bar instead of reloading an active terminal. On a phone the bar spans the viewport minus 1rem and uses a small shadow, so the message and Reload button stay on one line. The worker also imports push-handler.js for the push, notificationclick and pushsubscriptionchange handlers; all of its URLs are resolved against the service worker scope, so a base path works. The service worker is disabled in the Vite dev server, so the PWA shell and push must be tested with the built bundle.

The font licences are in mux_portal_ui/ui/public/fonts/. See adr/ui.md.

Build and Run

mux_portal_ui/build.rs builds the UI when its bundle is missing or stale, so a fresh checkout needs one command:

cargo run -p mux_portal -- --config config.json

The server listens on the configured address, for example http://127.0.0.1:34080/. The build needs OpenSSL headers (libssl-dev on Debian/Ubuntu). The health check is public at /api/health; every other API route requires a session. Open the root URL, enter the password and the interface appears.

A release build embeds the UI into the binary:

cargo build --release -p mux_portal
./target/release/mux_portal --config config.json

The UI build step can be skipped when the bundle already exists, which keeps a Rust-only check free of npm:

MUX_PORTAL_UI_SKIP_BUILD=1 cargo check -p mux_portal

MUX_PORTAL_UI_NPM selects the npm executable when it is not npm. See adr/ui.md.

Base path

Set basePath to mount the portal behind a prefix:

"basePath": "/portal/"

Both the UI and the API move under /portal/. Vite emits relative asset URLs and the backend injects <base href="/portal/"> and a base meta tag into index.html; API calls and the React Router basename read that meta tag. An nginx configuration can pass the prefix through unchanged. See adr/ui.md.

UI Development

Run the backend and Vite in two terminals:

cargo run -p mux_portal -- --config config.json
cd mux_portal_ui/ui && npm run dev

Vite serves the interface on http://127.0.0.1:5173 and proxies /api to the backend on port 34080, so development uses one origin and needs no CORS setup. npm run build type-checks the project and writes the bundle to mux_portal_ui/ui/dist/; in a debug build the running backend reads that directory from disk, so a UI rebuild does not require a Rust rebuild. The PWA service worker is generated only by the production build; test it against the Rust server or npm run preview, not the Vite dev server.

Deployment

The target deployment is a systemd system service running as the user who owns the tmux server, behind an existing TLS terminator. The backend binds 127.0.0.1 and speaks HTTP; the proxy configuration itself is maintained outside this repository. Web Push also needs outbound HTTPS to the push services, and the binary links OpenSSL, so the host needs libssl. The unit sets StateDirectory=mux-portal for the subscription state and ReadWritePaths=/etc/mux-portal for the first-start config rewrite. Sessions are in memory, so a restart logs everyone out, but push subscriptions survive and do not require the client to register again.

install -Dm0755 target/release/mux_portal /usr/local/bin/mux_portal
install -Dm0644 deploy/mux-portal.service /etc/systemd/system/mux-portal.service
systemctl daemon-reload
systemctl enable --now mux-portal

The unit deliberately keeps PrivateTmp=false: a private /tmp hides the tmux socket. The complete runbook — user, /etc/mux-portal/config.json, proxy requirements, upgrade and troubleshooting — is in deploy/README.md. The decision is recorded in adr/deploy.md.

On stop, the binary cancels live terminal handlers, restores the session options they changed and cleans old mux-portal-* sessions before exiting.

Tests and Formatting

cargo test --workspace
cargo fmt --all
cargo clippy --workspace --all-targets
cd mux_portal_ui/ui && npm run typecheck

dprint fmt

cargo test --workspace runs the inline unit tests of both packages, the tmux adapter tests in mux_portal/tests/tmux.rs, the terminal WebSocket test in mux_portal/tests/terminal.rs, the push and bell-watcher tests, the config key generation and state persistence tests, and the API tests for the push routes. The tmux tests also cover a bell flag and monitor-bell off. The fixture starts a private tmux server on a temporary socket and kills it afterwards, so the developer's tmux is not touched. If tmux is not installed those tests return early. Set MUX_PORTAL_TEST_TMUX to use a specific tmux binary. See adr/test.md.

dprint fmt formats Rust, TypeScript, JSON and Markdown through dprint.json; cargo fmt remains the reference for a Rust-only pass. Logging is controlled by RUST_LOG, for example RUST_LOG=mux_portal=debug,tower_http=debug. See adr/log.md.