Files
airc/docs/development.md
Tommi Kyntola a111823bfc Give every tunnel its own switch, and put ngrok first
Tailscale Funnel has been the flaky one here: the public *.ts.net path goes
dead while every local tailscale check stays green, and it takes the advertised
URL down with it. ngrok has never done that. But `publicUrl` — one string,
outranking ngrok by construction — left no way to lean on ngrok without
deleting the Funnel URL, and no place to park a third tunnel while trying it
out.

So tunnels become a group, one entry each with its own `enabled` flag:

  "tunnels": {
    "prefer": ["ngrok", "tailscale", "external"],
    "ngrok":     { "enabled": false, "domain": ..., "binary": ..., "apiUrl": ... },
    "tailscale": { "enabled": false, "url": "" },
    "external":  { "enabled": false, "url": "" }
  }

`prefer` is precedence, not exclusivity. Every enabled tunnel still reaches the
server; the first one that can name a URL is merely what gets baked into
bookmarks, the pairing QR and /api/config. Swapping the advertised route is a
reorder of that list plus a restart, with the losing tunnel left configured and
live as a spare. `external` is the slot for whatever comes next — Cloudflare, a
reverse proxy — without another schema change. A partial `prefer` ranks the
rest last rather than hiding them; an unknown name in it is a load error, since
a typo there would otherwise silently re-point the advertised URL.

Only ngrok is supervised, as before: `tailscale` and `external` are URLs airc
advertises for tunnels somebody else keeps up, which is all `publicUrl` ever
was. Because a reserved ngrok domain is knowable before the agent is up, it is
now advertised from the first request instead of after the agent reports; the
live URL replaces it when it lands (the two differ only without a reserved
domain), and both paths log through one announce().

`--no-ngrok` now means only what it says — skip the agent, let the next tunnel
down the list take over. `--no-tunnels` is new and means LAN/loopback only;
`tools/airc local` and `npm run dev` use it, which is what keeps local mode
preferring a LAN URL in pairing payloads now that disabling ngrok no longer
implies disabling everything. /healthz and /api/status gained a `tunnels` block
listing the routes in preference order with the advertised one named, and
`tools/airc status` prints that name next to the URL.

Old configs load untouched: a top-level `ngrok` block becomes `tunnels.ngrok`,
a top-level `publicUrl` becomes `tunnels.external.url`, and a file still in
that shape keeps its old precedence — publicUrl over ngrok — so pulling this
cannot silently re-point an existing pairing at a different tunnel. Only a
config that opts into `tunnels` gets the new ngrok-first default.

test/tunnels.test.js covers the ordering, both migration paths and the two
flags. Checked live against both orderings on a scratch port before restarting
the real server; with ngrok advertised, both it and the still-enabled Funnel
answer 200. Worth naming the cost: ngrok's free-tier interstitial does not
render in the Tesla browser, so the car keeps its ts.net bookmark, which works
precisely because a demoted tunnel stays up.
2026-08-26 13:09:57 +03:00

209 lines
6.1 KiB
Markdown

# Development
## Architecture
- `src/server.js`: HTTP routes, lifecycle, health, pairing
- `src/tmux.js`: tmux capture, pane listing, resize, input forwarding
- `src/ansi.js`: ANSI SGR to HTML conversion
- `src/auth.js`: token extraction and `authLevel`
- `src/config.js`: config defaults, CLI args, pairing payloads
- `src/ngrok.js`: supervised ngrok child process
- `src/dais.js`: ndjson client for the dais voice daemon (mic button; optional)
- `public/`: browser viewer, probe page, terminal assets
- `tools/airc`: background server wrapper
- `android-app/`: native Kotlin Android client
The browser viewer is the canonical terminal UI. The Android app renders
terminal frames in a WebView and keeps controls native, while sharing the same
server frame/input APIs.
Runtime paths:
```text
Android app -> LAN URL or tunnel URL -> Node server -> tmux
Browser/Tesla -> tunnel URL -> Node server -> tmux
```
Tunnels are configured under `tunnels` in `config.json` — `ngrok`, `tailscale`
and `external`, each with its own `enabled` flag — and `tunnels.prefer` orders
which one is advertised. The server supervises the ngrok agent when
`tunnels.ngrok.enabled` is true; the other two are URLs it only advertises.
`tools/airc` itself is not systemd-backed; it starts a detached Node process and tracks pid/state/log
files.
## Ports And Pairing
Airc defaults to `8080`.
Local mode:
```sh
tools/airc local
```
This prepends:
```sh
--host 0.0.0.0 --no-tunnels
```
Pairing payload behavior:
- tunnel mode: `baseUrl` is the advertised tunnel's URL.
- local mode: `baseUrl` prefers the first LAN URL.
- `lanUrls` and `publicUrl` are included so Android can try LAN first and fall
back to the tunnel.
- Browser pairing commands print QR/URL/token values instead of Android JSON.
- `pair-app` uses `controlToken`.
- `pair-web` uses `viewToken`.
- `pair-web-control` uses `controlToken`.
## Server API
- `GET /api/config`
- `GET /api/pairing`
- `GET /api/tmux/frame`
- `GET /api/tmux/panes`
- `POST /api/tmux/input`
- `GET /api/attention` (panes whose agent needs interaction; see [attention.md](attention.md))
- `POST /api/agent/event` (agent hook; control token)
- `POST /api/dais/dictate` (voice dictation latch; control token; 404 unless `dais.enabled`)
- `GET /api/dais/status` (mic/daemon state for the button; control token; see [dais.md](dais.md))
- `GET /api/status`
- `GET /probe`
- `GET /api/probe/poll`
- `GET /healthz`
- `WebSocket /api/tmux/ws`
Unauthenticated static assets:
- `/app.js`
- `/app.css`
- `/fonts/FiraCode-Regular.ttf`
Auth uses generated tokens in `config.json`. `viewToken` can view only;
`controlToken` can view and send input. Requests can use:
- `?k=<token>`
- `X-Airc-Auth: <token>`
- `X-Swyd-Auth: <token>` during migration
- `Authorization: Bearer <token>`
- `airc_auth` cookie
- `swyd_auth` cookie during migration
`GET /api/config` returns `canControl` based on the token presented. Browser UI
controls are shown only when `canControl` is true. `/api/tmux/input` and
`/api/pairing` require the control token and return `404` otherwise.
Unauthorized protected routes return `404`, not `401`, to avoid advertising the
private surface.
`GET /api/config` also returns non-secret URL metadata (`publicUrl`, `lanUrls`)
for clients.
`GET /api/status` returns live tunnel status and local battery summary for the
browser dashboard: `ngrok` is the supervised agent's own state, and `tunnels`
lists every configured route in preference order with the advertised one named
(`/healthz` carries the same `tunnels` block).
Input payloads:
```json
{ "target": "active", "text": "hello" }
```
```json
{ "target": "pane", "paneId": "%5", "key": "Enter" }
```
Text uses `tmux send-keys -l`; named keys use `tmux send-keys`.
## Frame Flow
1. Client polls `/api/tmux/frame`.
2. Server resolves the active pane for `config.session`, unless `?pane=%N`
pins a concrete pane.
3. Server runs `tmux capture-pane -p -e -t <pane-id>`.
4. ANSI SGR is converted to HTML.
5. Response includes pane metadata, cursor position, HTML, and `ETag`.
6. Client sends `If-None-Match`; unchanged frames get `304`.
See [Implementation Notes](implementation-notes.md) for tmux edge cases and
sizing details.
The browser prefers `WebSocket /api/tmux/ws` for frame updates and falls back to
HTTP polling if the socket is unavailable or closes.
## Build And Test
The repo-root `Makefile` wraps the common flow (`make help` lists targets).
It models the APK as a file target whose prerequisites are the Kotlin / res /
manifest / gradle sources, so `make push` only rebuilds when something changed:
```sh
make check # npm run check
make build # cd android-app && ./gradlew assembleDebug
make push # rebuild if needed, then adb install -r
make deploy # push and launch on the device
```
Equivalent raw commands:
Node/server check:
```sh
npm run check
```
Android build:
```sh
cd android-app
./gradlew --no-daemon assembleDebug
```
Install:
```sh
~/android/platform-tools/adb install -r android-app/app/build/outputs/apk/debug/app-debug.apk
```
SDK/tooling assumptions:
- Android SDK root: `~/android`
- Gradle install: `~/android/gradle-9.4.1`
- JDK 17: `~/android/jdk-17`
- `android-app/local.properties` is machine-local and ignored
### Release APK signing
`make release-apk` (run by the `/release` flow) assembles a signed
`airc-<version>.apk` from `./gradlew assembleRelease`. Signing reads a
git-ignored `android-app/keystore.properties`:
```properties
storeFile=airc-release.jks
storePassword=…
keyAlias=airc
keyPassword=…
```
The keystore (`android-app/airc-release.jks`) and this file live only on the
release machine and are **never committed** — `*.jks` and `keystore.properties`
are git-ignored. Without them, `assembleRelease` falls back to the debug key
(so a plain build still works), but `make release-apk` hard-fails rather than
ship a debug-signed artifact. Keep a secure backup of the keystore: losing it
means future APKs can't upgrade an installed copy in place.
## Agent Notes
For fresh coding-agent contexts, also read `AGENTS.md`.
Do not commit:
- `config.json`
- `.airc-server.*`
- `node_modules/`
- `android-app/local.properties`
- Android/Gradle build outputs