# ws.api.littlebearz.com — Vegas WebSocket API

A WebSocket API on littlebear-vegas that replicates SSH command execution.
Built 2026-10-04.

## Why

SSH from restricted networks (e.g. Hatch VM behind an egress proxy with a
port whitelist) is fragile. This API provides an alternative command channel
over `wss://` (port 443), which passes through most firewalls and proxies.

## Architecture

```
Client ──wss://ws.api.littlebearz.com:443──> nginx (vegas)
                                                │ proxy_pass + Upgrade headers
                                                ▼
                                          ws://127.0.0.1:8223
                                          (ws_api_server.py, --no-tls)
```

Direct Tailscale path (no nginx):

```
Tailnet client ──wss://100.64.0.22:8222──> ws_api_server.py (TLS)
```

## Endpoints

| Endpoint | Network | TLS | Auth |
|---|---|---|---|
| `wss://ws.api.littlebearz.com` | Public internet | Let's Encrypt | API key |
| `wss://100.64.0.22:8222` | Tailscale only | Self-signed | API key |

## Protocol (JSON over WebSocket, RFC 6455)

```
C -> S: {"auth": "<api_key>"}
S -> C: {"ok": true}  |  {"error": "auth failed"}  (then connection closed)

C -> S: {"cmd": "<shell command>", "timeout": 60}
S -> C: {"stdout": "...", "stderr": "...", "exit": 0}
```

- Auth is mandatory as the first message. Wrong/empty key → `auth failed` + close.
- Commands run via `/bin/bash -c`. Timeout 1–600s (default 60).
- Non-WebSocket HTTP requests (e.g. `curl -sI`) get 502 — expected.

## Server (vegas)

- **Script:** `~/bin/ws_api_server.py` — pure Python 3 stdlib, zero dependencies
- **Instances:**
  - `wss://100.64.0.22:8222` — Tailscale, self-signed cert (`~/.ws_api_cert.pem`)
  - `ws://127.0.0.1:8223` — localhost plain, nginx backend (`--no-tls`)
- **API key:** `~/.ws_api_key` (mode 600, auto-generated `wsak-` + 256-bit)
- **Log:** `~/.ws_api.log`
- **Nginx vhost:** `/etc/nginx/sites-enabled/ws.api.littlebearz.com.conf`
  (proxies `/` → `127.0.0.1:8223` with `Upgrade`/`Connection` headers)
- **Boot persistence:** cron `@reboot` × 2 (no sudo for systemd linger)

### Manage

```bash
# restart both instances
pkill -f ws_api_server.py
sleep 20 && python3 ~/bin/ws_api_server.py --bind 100.64.0.22 --port 8222 >> ~/.ws_api.log 2>&1 &
sleep 20 && python3 ~/bin/ws_api_server.py --bind 127.0.0.1 --port 8223 --no-tls >> ~/.ws_api.log 2>&1 &

# logs
tail -f ~/.ws_api.log

# rotate API key (redistribute to all clients afterwards)
rm ~/.ws_api_key && pkill -f ws_api_server.py  # then restart manually
```

## Client

### `vegas-ws` — direct WebSocket client

Pure Python 3 stdlib. Usage mirrors ssh:

```bash
vegas-ws "df -h"                          # run command, exit code = remote exit
echo "uptime" | vegas-ws                  # stdin command
vegas-ws --timeout 120 "long-job.sh"      # custom timeout
```

Exit codes: remote command's exit code on success; `255` on transport/auth
failure (ssh convention); `2` on usage error.

Env vars:

| Var | Default | Purpose |
|---|---|---|
| `VEGAS_WS_URL` | `wss://ws.api.littlebearz.com` | Endpoint |
| `VEGAS_WS_KEY` | — | API key (or key file) |
| `VEGAS_WS_KEY_FILE` | `~/.ws_api_key_vegas` | API key file |
| `VEGAS_WS_CA` | — | Pin a specific server cert (self-signed) |
| `VEGAS_WS_INSECURE` | — | Set `1` to skip TLS verification (not recommended) |
| `HTTPS_PROXY` | — | Proxy CONNECT tunneling |

### `vegas` — wrapper (ws-api primary, SSH fallback)

```bash
vegas "ls -la /var/www/html"
```

Tries `vegas-ws` first. On transport failure (exit 255) falls back to
`ssh littlebear-vegas` automatically. A non-zero exit from the remote command
itself is passed through — no fallback.

This is the default way to access vegas from Hatch VM.

## Security

- **API key is the sole gate.** No IP whitelist at the nginx layer (yet).
  Whoever holds the key has full shell as `littlebear` on vegas.
- Key holders (2026-10-04): vegas `~/.ws_api_key`, Hatch VM
  `~/.ws_api_key_vegas`. Both mode 600.
- Audit 2026-10-04: 4/4 auth tests pass (no-auth blocked, wrong/empty key
  rejected, correct key works). Scanners probed the new subdomain within
  minutes of DNS going live — got 502, no access.
- Recommendations: rotate key if ever exposed; add nginx `allow`/`deny`
  IP whitelist before granting key to third parties.

## Limitations vs SSH

- No interactive PTY (`vim`, `top` won't work — use batch commands)
- No file transfer (use `scp`/`sftp` over SSH for that)
- No port forwarding / tunneling
- Long-running commands: use `nohup`/`setsid` server-side, or increase `--timeout`

## Files in this directory

- `README.md` — this file
- `ws_api_server.py` — server (deploy to vegas `~/bin/`)
- `vegas-ws.py` — direct client
- `vegas` — wrapper script (ws-api → SSH fallback)
- `ws.api.littlebearz.com.conf` — nginx vhost template

## Web landing page & online installer

Visiting `https://ws.api.littlebearz.com/` in a browser now shows an HTML
landing page (instead of a 502) explaining the service and how AI agents
can integrate with it.

| Path | Content |
|---|---|
| `/` | HTML landing page: overview, install, AI-agent integration guide |
| `/install.sh` | Online installer: `curl -sSL https://ws.api.littlebearz.com/install.sh \| bash` |
| `/vegas-ws` | Python client (zero dependencies) |
| `/README.md` | This documentation |

The server serves these for plain HTTP GET requests; WebSocket upgrades
(`Upgrade: websocket` + `Sec-WebSocket-Key`) still go through the JSON
command protocol. Static files live in `static/` next to `ws_api_server.py`.

### Installer usage

```bash
# Interactive (prompts for API key):
curl -sSL https://ws.api.littlebearz.com/install.sh | bash

# Non-interactive:
curl -sSL https://ws.api.littlebearz.com/install.sh | VEGAS_WS_KEY=wsak-... bash
```

Installs `vegas-ws` to `~/.local/bin`, saves the key to
`~/.ws_api_key_vegas` (600), and verifies the connection.
