teter-common 26.11.7

Shared types and protocol definitions for teter
Documentation
# teter

Persistent terminal sessions on your own machines, reachable from any
device through your own relay, which cannot read them. A session started at
home survives a dropped connection and is there again from a laptop, and
later from a phone or a browser. It is for people on Linux and macOS who
live in the command line and host their own server.

Home machines run a backend that registers with the relay; a client connects
to the relay to attach to a session. Terminal data is encrypted end to end
between them: the relay forwards bytes it cannot read. The design is in
[docs/architecture.md](docs/architecture.md).

## Status

Early, and usable from the command line. What works today:

- the relay, with TLS, heartbeats, limits, and a grace for a dropped backend
  to come back;
- the backend: shells in PTYs that outlive it, served through the relay, which
  keep each session's screen and draw it for a client that joins;
- the frontend: a raw passthrough client (like `ssh`), attaching by name, with
  the session's history in the terminal's own scrollback, that joins again by
  itself after a dropped connection;
- accounts, machines approved by the account, sessions created, listed and
  ended from any device through signed commands;
- a web client, served by the relay at `/`: log in, the account's machines and
  sessions, a session in a terminal (xterm.js), with the account's keys kept in
  the tab's memory only;
- end-to-end encryption of everything typed and printed.

Not there yet: packages, and the phone client. The plan is the
[roadmap](https://gitlab.com/alteronauta/teter/-/issues/66), and what
"0.1 usable" means is in
[#65](https://gitlab.com/alteronauta/teter/-/issues/65).

Run the relay with its store (accounts, registered machines, sealed session
records) and a TLS certificate and key (PEM):

```sh
cargo run -p teter-relay -- --listen 0.0.0.0:443 --store /var/lib/teter/store.db \
    --tls-cert relay.crt --tls-key relay.key
```

Without `--tls-cert` and `--tls-key` it serves plain `ws://`, for local testing. To run your own
relay on a server, behind a TLS proxy, follow the
[self-hosting guide](docs/self-hosting.md): the package or the container image, Caddy or nginx,
the first invite, backups.
Backends are machines registered with an account (see Accounts below): there is
no shared token.

**The web client.** Build it with `cargo xtask web` (it needs the
`wasm32-unknown-unknown` target: `rustup target add wasm32-unknown-unknown`),
which writes `target/web`, and give the relay that directory with `--web`
(`TETER_WEB`):

```sh
cargo xtask web
cargo run -p teter-relay -- --listen 0.0.0.0:443 --store /var/lib/teter/store.db \
    --tls-cert relay.crt --tls-key relay.key --web target/web
```

The relay then serves the page at `/`, beside its WebSocket endpoints
(`/frontend`, `/backend`, `/account`), with a strict content security policy
(scripts, styles and connections from its own origin only). Behind a proxy that
terminates TLS, such as Caddy, pass every path through to the relay, WebSockets
included; Caddy's `reverse_proxy` does both:

```caddyfile
relay.example.com {
    reverse_proxy 127.0.0.1:8080
}
```

with the relay on `--listen 127.0.0.1:8080` and no `--tls-cert`. The page logs in
to the relay it came from by default; the account's keys stay in the tab's
memory, and closing the tab logs it out.

On the home machine, once registered, run the backend (`teter link`); it prints
the session ID, and in a terminal the frontend token, on its output. The secret, which encrypts
the session end to end (the relay never gets it, and cannot read the terminal), never goes to its
output or logs. Run as a service, the link's output is not a terminal (and the units discard it),
so its journal holds neither the token nor the secret; `RUST_LOG=trace` is the exception, as
the WebSocket library then logs every frame. Ask for all three, in your terminal, to hand to a
frontend:

```sh
teter status
```

The shell runs in a session process that outlives the backend command: stop the
backend and run it again, and it resumes the same session, with the same shell
and the same values. When the shell exits, the session starts a new one. The session
process is detached from the backend command: killing the command, its process
group or, on Linux, its systemd service leaves it running. A session process
that goes away while the backend runs (killed, say) is started again, as a new
session with a new secret. The machine's sessions are registered
(`~/.local/state/teter/sessions`), so after a reboot the backend starts them
all again. End one, and the backend does not start it again (`main` always
exists):

```sh
teter kill build
```

To end every process of a session from the system instead (the backend starts
a registered one again, so stop the backend first):

```sh
# With the backend under your systemd user manager (a user service, or a
# desktop terminal), each session is a scope of its own.
systemctl --user list-units 'teter-session-*'
systemctl --user stop teter-session-main.scope
# Anywhere: find the session process and end it.
pgrep -af 'teter(-backend)? session'   # teter-backend in 26.11.0
kill <pid>
```

After an upgrade, end the old version's sessions the same way before running
the new backend: the two sides of a machine speak a local protocol that can
change between versions, and a backend that finds its default session held by
one it cannot serve says so and stops.

```sh
cargo run -p teter -- link     # the relay the machine registered with
```

`--relay` (or `TETER_RELAY`) names another URL for the same relay; `link` uses
its `/backend` endpoint and `attach` its `/frontend` one, so one `TETER_RELAY`
serves both (a URL ending in either endpoint works too). A relay that asks for
client certificates (`--client-ca`) gets the one `TETER_CLIENT_CERT` and
`TETER_CLIENT_KEY` name, from every command (see the self-hosting guide).

From any machine, attach to a session with the values `teter status` shows for it (session, token, channel and secret); Ctrl-] detaches and
the shell goes on running:

```sh
TETER_TOKEN=... TETER_SECRET=... cargo run -p teter -- attach \
    --relay wss://relay.example.com --session <session ID> --channel <channel>
```

To try it on one machine, in three terminals (plain `ws://` on localhost; the
same steps as the end-to-end test, `teter-frontend/tests/e2e.rs`):

```sh
# terminal 1: the relay, with a store
cargo run -p teter-relay -- --store ./store.db
# terminal 2: an invite, an account, a machine registered with it, the backend
TETER_INVITE=$(cargo run -q -p teter-relay -- invite --store ./store.db) \
    cargo run -p teter -- signup ws://127.0.0.1:8080 --name me
cargo run -p teter -- register --name here \
    --token "$(cargo run -q -p teter -- token create --uses 1 --expires 10m)"
cargo run -p teter -- link
# terminal 3: attach by name
cargo run -p teter -- attach here
```

| crate | what |
| --- | --- |
| `teter-common` | shared protocol: message types and serialization |
| `teter-relay` | the self-hosted relay: authenticates both sides and bridges sessions |
| `teter` | the one binary for hosts and clients: `link`, `status`, `kill`, `attach` |
| `teter-backend` | library: the link and the session processes (the shell in a PTY) |
| `teter-client` | relay connection shared by the backend and the frontend |
| `teter-frontend` | library: the terminal client's loop, attached through the relay |

## Install

On Linux (x86-64 or ARM64), the install script picks the release's tarball for the CPU,
checks it against the release's `SHA256SUMS`, and puts `teter` in `~/.local/bin`:

```sh
curl -sSf https://gitlab.com/alteronauta/teter/-/raw/main/scripts/install.sh | sh
```

`sh -s -- --relay` installs `teter-relay` too, `--version <YY.M.patch>` a given release, and
`--dir <directory>` another place. On macOS, use Homebrew (#102).

On Debian, Ubuntu and Raspberry Pi OS, each release has `.deb` packages for both CPUs:
`teter` (with the link as a systemd user unit: `systemctl --user enable --now teter-link`) and
`teter-relay` (a `teter-relay` system service on `127.0.0.1:8080`, enabled and started on
install and restarted on upgrade, its store in `/var/lib/teter`, its settings in
`/etc/default/teter-relay`, run as a `teter` system user):

```sh
sudo apt install ./teter_<version>_amd64.deb ./teter-relay_<version>_amd64.deb
```

On Arch, the AUR package `teter-bin` installs both binaries and the link's user unit.

With a Rust toolchain, from crates.io: `cargo install teter` (the one binary for machines and
devices) and `cargo install teter-relay` (the relay; built from source, it carries no web client:
give it one with `--web`).

The relay is also a container image, for both CPUs, with the web client in it:
`registry.gitlab.com/alteronauta/teter/relay` (`:latest` or a version). It serves plain `ws://`
on port 8080, for a TLS proxy in front, its store in a volume on `/data`, as a non-root user:

```sh
podman run -d --name teter-relay -p 127.0.0.1:8080:8080 -v teter:/data \
    registry.gitlab.com/alteronauta/teter/relay
podman exec teter-relay /teter-relay invite --store /data/store.db   # the first invite
```

[`packaging/container/compose.yml`](packaging/container/compose.yml) is the same as a compose
file.

Each release (versions are dates, `YY.M.patch`) has static `linux-amd64` and `linux-arm64`
tarballs with `teter` (hosts and clients) and `teter-relay` (the server), and their
`SHA256SUMS`, on the [releases page](https://gitlab.com/alteronauta/teter/-/releases). The
release's `teter-relay` carries the web client and serves it at `/` with no flag (`--web <dir>`
serves another build instead).

## Accounts

The relay keeps accounts in its store (`--store`); the password never leaves your
device, and the relay keeps only what it cannot read. Signup needs an invite
from the relay's admin, made on the server as the user the relay runs as:

```sh
teter-relay invite --store /var/lib/teter/store.db   # prints the code once
teter-relay invite ls --store ...                    # also: invite revoke <id>, account ls, account remove <name>
```

```sh
TETER_INVITE=teter-inv1.... teter signup wss://relay.example.com --name ana   # once
teter login wss://relay.example.com --name ana    # on each other device
teter logout
teter password                                     # other devices log in again
teter rotate-keys                                  # a lost device, a key that may have leaked
```

The device keeps the account's keys and login in the OS keyring (the Secret
Service on Linux, the Keychain on macOS), the rest in
`~/.config/teter/account.json`; with no keyring running, all of it in that file,
mode 0600, which `teter` says. `TETER_KEYRING=file` keeps new keys in the file
(keys already in the keyring are still found there, and removed at logout). A
logout where the keyring does not answer (ssh, say) keeps the file and says so.

Each login is bound to a key this device makes at login (#158): the relay takes
the login's token only in requests that key signs, so a copied token or
`account.json` logs nothing in elsewhere. The key goes in the keyring with the
token; with no keyring, in `~/.config/teter/device.key` (mode 0600, which
`teter` says), and then whoever copies both files holds the login. This guards
against a copied file, not against software running as you: an unlocked
keyring answers any program of your user, and on macOS the key is in the
Keychain, not the Secure Enclave. Logins made by a teter from before this ask
to log in again.

Nothing can recover a forgotten password: the relay holds nothing that opens
without it. Forgotten, it means a new account and registering the machines again;
their sessions go on (the relay keeps none of their data). After a lost device
or a key that may have leaked, `teter rotate-keys` makes new account keys, signed
over by the old ones: the machines follow when they connect, the device approves
them again, every other device logs in again, and registration tokens made before
stop working. What it protects against, and what not: `docs/architecture.md`,
"Password change and key rotation".

To register a machine with the account, make a registration token on a
logged-in device (printed once; the relay keeps only its hash), then use it on
the machine, which from then on links with its own key:

```sh
teter token create --uses 1 --expires 1h      # on a logged-in device
teter register --token <token> --name jupiter # on the machine, once
teter link                                    # on the machine
teter machine ls                              # on a logged-in device
teter machine rm <id>
teter unregister                              # on the machine
```

Then, from any logged-in device, attach by name, with nothing carried by hand
(the machine keeps each session's name and key on the relay, sealed to the
account and signed by the machine; the relay can neither read nor forge them).
The device that made the token approves the machine on its own; another device
approves it by hand, against the fingerprint the machine shows:

```sh
teter machine fingerprint                                 # on the machine
teter machine approve jupiter --fingerprint <it>          # on a logged-in device
```

Then:

```sh
teter ls jupiter                # its sessions: running, clients, size
teter new jupiter:build         # create one and start it
teter attach jupiter            # its default session, main
teter attach jupiter:build
teter kill jupiter:build
```

A session keeps its recent output on disk too, sealed to the account (the machine
cannot read it back): after a reboot, `teter attach` shows what the session printed
before, then `[teter] machine restarted at …`.

## Run as a service

Register the machine first (see Accounts): the link then finds its relay and
key in `~/.config/teter/host.json` (mode 0600), with nothing on the command line
and no shared token. A link that is not registered exits with the status its
service does not restart on; `teter register` starts the installed service
(the unit or agent below, or Homebrew's) once it is done. An optional `~/.config/teter/backend.env` can set
`TETER_RELAY` to another URL of the same relay.

**Linux (systemd).** Install the user unit,
[`packaging/systemd/teter-link.service`](packaging/systemd/teter-link.service),
which runs `~/.local/bin/teter link` (change `ExecStart` if it is elsewhere),
start it, and let it run at boot and after you log out:

```sh
install -Dm644 packaging/systemd/teter-link.service \
    ~/.config/systemd/user/teter-link.service
systemctl --user daemon-reload
systemctl --user enable --now teter-link
loginctl enable-linger "$USER"
journalctl --user -u teter-link -f    # its errors and warnings; no token, no secret
teter status                             # the values for a frontend
```

It restarts the link when it fails. Each session process runs in a scope of
its own (`teter-session-<name>.scope`), outside the unit, so restarting or
stopping the unit leaves the shells running; the next link resumes them.

**macOS (launchd).** Install the agent,
[`packaging/launchd/br.tec.ars.teter-link.plist`](packaging/launchd/br.tec.ars.teter-link.plist),
which runs `teter link` from `~/.local/bin` (or `/opt/homebrew/bin`,
`/usr/local/bin`) with the same environment file, logs its errors to
`~/Library/Logs/teter-link.log`, and leaves the session processes running when its job stops
(`AbandonProcessGroup`). launchd expands no variable in a path, so the install puts your home
directory in the log's:

```sh
sed "s|@HOME@|$HOME|" packaging/launchd/br.tec.ars.teter-link.plist \
    > ~/Library/LaunchAgents/br.tec.ars.teter-link.plist
launchctl bootstrap "gui/$(id -u)" ~/Library/LaunchAgents/br.tec.ars.teter-link.plist
```

**Upgrades.** Installing a new binary restarts nothing: the running link and
session processes keep the old one. Restart the link to run the new one
(`systemctl --user restart teter-link`, or on macOS
`launchctl kickstart -k "gui/$(id -u)/br.tec.ars.teter-link"`); a session
process stays on the old binary until it is ended. When the new version cannot
serve a running session (its local protocol changed), the link says so and
exits with status 3, which the unit does not restart: end the old sessions
(see above), then `systemctl --user start teter-link`. The macOS agent, as
launchd cannot skip a status, runs the link with `--in-use-status 0` (so the agent needs a `teter` that knows the
option: an older one first on its `PATH` fails at once, over and over): it exits 0, which the
agent does not restart either; end the old sessions, then
`launchctl kickstart "gui/$(id -u)/br.tec.ars.teter-link"`.

The relay token stays with the link: session processes and their shells do not
get `TETER_RELAY` in their environment.

## Build

```sh
cargo build --workspace
cargo test --workspace
cargo xtask web    # the web client's files, in target/web
```

The web client's browser test needs Firefox and geckodriver:
`cargo test -p teter-web-test -- --ignored`, after `cargo xtask web`.

How to contribute: [CONTRIBUTING.md](CONTRIBUTING.md). Security reports:
[SECURITY.md](SECURITY.md), not public issues.

## Licence

MIT, see [LICENSE](LICENSE).