# Run a Cloud Relay and Tor/Tails guidance

Peers behind residential NATs or cellular carriers reach each other through a lightweight **Circuit Relay v2** node (`--mode infrastructure`) and upgrade to direct connections via DCUtR hole punching whenever possible. A relay only forwards Noise-encrypted bytes and never sees plaintext prompts or responses.

## Roaming reliability and private-network discovery

Laptops and GPU helpers move between home Wi-Fi, office networks, VPNs, and cellular hotspots without needing manual restarts:

- **Dual QUIC + TCP listeners:** Nodes listen on both QUIC v1 (UDP/TLS 1.3) and TCP (Noise XX) so fast direct links establish cleanly wherever UDP is allowed while TCP relays keep working through restrictive firewalls.
- **Fast dead-link eviction (`15s` ping keepalives):** `phase-net` runs libp2p `ping` every `15s` (`15s` timeout) and immediately closes unresponsive sockets after sleep, Wi-Fi switches, or carrier NAT drops instead of waiting minutes for a stale TCP timeout.
- **Self-healing relay reservations:** When a relay listener closes or its reservation address expires, background maintenance automatically re-dials the relay and re-establishes the circuit reservation.
- **Automatic direct-path preference:** As soon as a direct or DCUtR hole-punched connection comes up to a peer, `phase-net` automatically prunes redundant relayed circuits to that peer so inference and `/phase/shard-rpc/1.0.0` tunnels never accidentally ride a slower relay.
- **Private-network-friendly discovery:** Local mDNS and explicit LAN `--bootstrap-peer` routes keep their provenance and remain routable in Kademlia even when mixed with public bootstrap seeds—so machines on the same Wi-Fi or studio LAN discover and talk directly without hairpinning through the internet.

## One-command Linux Cloud Relay setup

Any small Linux VPS with a public IPv4 address (~24 MiB RSS in practice) can serve as a private or community relay using `scripts/setup-cloud-relay.sh`:

```bash
curl -fsSL https://raw.githubusercontent.com/msitarzewski/phase/main/scripts/setup-cloud-relay.sh -o setup-cloud-relay.sh
sudo bash setup-cloud-relay.sh --ip 203.0.113.10 --port 443
```

What the script sets up (fail-closed):

- Rejects loopback, RFC 1918, link-local, and CGNAT addresses for `--ip`.
- Downloads the verified `lucidd` release bundle (`SHA256SUMS.txt` checked).
- Creates an unprivileged system user `lucid` (`/usr/sbin/nologin`).
- Installs a hardened systemd unit (`/etc/systemd/system/lucidd-relay.service`) with `NoNewPrivileges=yes`, `ProtectSystem=strict`, `MemoryDenyWriteExecute=yes`, and—only when `--port 443` is selected—`AmbientCapabilities=CAP_NET_BIND_SERVICE` so restrictive corporate/hotel firewalls that block port `4001` still pass traffic.
- Opens the chosen TCP port in `ufw` (unless `--no-firewall` is passed).
- Prints the node's exact PeerID-pinned multiaddr and DNS TXT record value.

> [!NOTE]
> Running without `sudo` installs a user systemd unit with `loginctl enable-linger` on port `4001` (port `443` requires root for `CAP_NET_BIND_SERVICE`). Public rendezvous serving stays disabled in `lucidd` infrastructure mode until upstream registration-count quotas are available.

### Hardened relay limits in `phase-net`

Circuit Relay v2 server mode is strictly bounded against abuse:

- Default reservations: `128` total, `2` per peer, `1 hour` duration (hard ceiling `1024` / `8` / `1h`).
- Default circuits: `256` total, `4` per peer, `2 minute` duration, `64 MiB` per circuit (hard ceiling `2048` / `16` / `1h` / `1 GiB`).

## PeerID-pinned DNS TXT bootstrap

By default, `lucidd` resolves TXT records on `bootstrap.phasebased.net` via your system DNS resolver (`--bootstrap-dns`, up to 64 addresses per domain). Every TXT record must end with `/p2p/<PeerId>`, which Noise authenticates on dial.

To run your own private relay without touching the default bootstrap domain:

```bash
lucidd \
  --no-default-bootstrap \
  --bootstrap-peer /ip4/203.0.113.10/tcp/443/p2p/12D3KooWYourRelayPeerId \
  --relay-peer /ip4/203.0.113.10/tcp/443/p2p/12D3KooWYourRelayPeerId
```

Or publish a TXT record on your own domain (`_dns` value `/ip4/203.0.113.10/tcp/443/p2p/12D3KooW...`) and pass `--no-default-bootstrap --bootstrap-dns bootstrap.example.org`.

> [!WARNING]
> System DNS TXT lookups do not enforce DNSSEC today; `--dns-fallback` (off by default) falls back to `1.1.1.1` only if you opt in. For maximum bootstrap certainty, pin `--no-default-bootstrap --relay-peer <multiaddr>` directly.

## Running over Tor and TailsOS

`phase-net` and `lucidd` include **native, fail-closed `--tor-socks5` & v3 `.onion` transport** (zero-`unsafe` RFC 1928/1929 SOCKS5h with remote DNS resolution through Tor). You can run LUCID over Tor on macOS, Linux, Windows, or **TailsOS** in Join-Only mode:

```bash
lucidd \
  --mode consume-only \
  --tor-socks5 127.0.0.1:9050 \
  --no-default-bootstrap \
  --bootstrap-peer /onion3/<your-relay-56-char-v3-address>:443/p2p/12D3KooWYourRelayPeerId \
  --relay-peer /onion3/<your-relay-56-char-v3-address>:443/p2p/12D3KooWYourRelayPeerId
```

In LUCID Desktop, launch with `LUCID_TOR_SOCKS5=127.0.0.1:9050` (on TailsOS, the Tor daemon listens on `127.0.0.1:9050` by default).

What `--tor-socks5` enforces automatically (fail-closed):

- Zero direct TCP/QUIC listen sockets, zero mDNS LAN discovery, zero DCUtR hole punching, and zero AutoNAT probes
- Zero clearnet model/runtime downloads and local providing disabled (Join-Only Consumer UI)
- **RAM-only chat history:** Local disk chat history is hard-locked off so nothing is written to disk
- **Tor-native DuckDuckGo `.onion` Search & `.onion` Web Fetch:** Built-in Chat Web Search and Web Fetch route strictly through `socks5h://` (remote DNS via Tor) targeting DuckDuckGo's official `.onion` service
- **External MCP subprocess lockdown:** Third-party stdio MCP binaries are automatically disabled so an external subprocess cannot bypass the SOCKS5h proxy

### Honest Tor & TailsOS caveats

1. **Serving peers still see plaintext prompts:** Tor hides your IP address from the relay and serving peer; it does *not* hide your prompt text from the serving peer executing inference.
2. **Avoid private LAN multiaddrs:** Even when TailsOS's kernel firewall blocks non-Tor internet traffic, only dial Tor `.onion` or public relay addresses when you require strict Tor-only routing.
3. **Use TCP port `443` or v3 `.onion` relays:** Tor only carries TCP (never UDP/QUIC), and `.onion` relays keep traffic end-to-end inside Tor without using an exit node.
4. **Use an ephemeral identity** (`--identity-path /tmp/ephemeral.key`) if you do not want sessions linked across reboots by `PeerId`.
