# RelayLink · AI Agent Installation Guide

> **For the executing AI agent:** Follow these instructions step by step. Never guess target hosts, never invent credentials or private keys, and ask the user for confirmation before modifying firewall rules, creating system service accounts, or overwriting existing RelayLink configurations.

---

## 0. Safety & Integrity Rules (Mandatory)

1. **Confirm Scope First**: Before running privileged or state-changing commands, ask the user:
   - Are we installing **RelayLink Server** or **RelayLink Agent** (or both)?
   - What is the target OS and architecture (`Linux x64`, `Windows x64`, `macOS arm64`, `macOS x64`)?
   - For **Server**: what DNS name or IP (`AgentServerHost`) will Agents use to reach the Server?
   - For **Agent**: does the user already have the single-server JSON configuration downloaded from the Server Admin UI?
2. **Verify Release Checksums**: Download binaries and `SHA256SUMS` only from `https://github.com/LazyWorkshopCreate/RelayLink/releases` and verify the SHA-256 hash before extracting or running installers.
3. **Unsigned Binaries Notice**: Inform the user transparently that Windows `.exe` installers/archives and macOS `.tar.gz` archives are currently **unsigned and unnotarized**—rely strictly on `SHA256SUMS` verification.
4. **Protect Secrets**: Never echo, log, commit, or write admin password hashes, client registration secrets, or TLS private keys to shared summaries or temporary repo files.

---

## 1. Installing RelayLink Server

### Option A: Linux Docker Compose (Fastest)

Requires **Linux Docker Engine**, **Docker Compose v2**, and **PowerShell 7 (`pwsh`)**. (Docker Desktop / non-Linux containers are not supported for host-network dynamic port binding.)

1. Clone the repository and enter it:
   ```bash
   git clone https://github.com/LazyWorkshopCreate/RelayLink.git
   cd RelayLink
   ```
2. Run the bootstrap script (replace `tunnel.example.com` with the DNS name or IP reachable by Agents):
   ```powershell
   pwsh ./scripts/start-server-compose.ps1 -AgentServerHost tunnel.example.com
   ```
   The script prompts interactively for an admin password (storing only a PBKDF2-SHA256 hash), generates `.local/docker-server/config/server.json` (`0600`), builds the image, runs `--check-config`, and starts `relaylink-server`.
3. Verify container health and endpoints:
   ```bash
   docker compose -f deploy/docker/compose.yaml ps
   ```
   - **Admin UI**: `http://127.0.0.1:18080/` (loopback-only by default; access via SSH tunnel or authorized Agent-to-Agent channel—never expose plaintext HTTP admin UI directly to the public internet)
   - **Control Port**: `7443/tcp` (Agents only)
   - **Data Port**: `7444/tcp` (Agents only)
4. **Production TLS Reminder**: The starter Compose config disables tunnel TLS for local smoke testing. Before production or Agent-to-Agent peer access, place TLS certificates in `.local/docker-server/config/tls/` and enable `"tlsEnabled": true` in `.local/docker-server/config/server.json` (see `deploy/docker/README.md`).

### Option B: Linux Native (`systemd`)

1. Download `RelayLink-Server-linux-x64-<ver>.tar.gz` and `SHA256SUMS` from GitHub Releases and verify:
   ```bash
   sha256sum -c SHA256SUMS --ignore-missing
   ```
2. Create dedicated service user `relaylink` and protected directories (`0700`), extract binaries to `/opt/relaylink/server`, and copy `server.json` (`0600`) to `/etc/relaylink/server.json` (reference `config/examples/server.example.json` and `deploy/linux/README.md`):
   ```bash
   sudo install -d -o relaylink -g relaylink -m 0700 /etc/relaylink /etc/relaylink/clients /var/lib/relaylink /var/lib/relaylink/logs
   sudo install -o relaylink -g relaylink -m 0600 ./server.json /etc/relaylink/server.json
   ```
3. Validate configuration without starting the service, then install the `systemd` unit:
   ```bash
   sudo -u relaylink /opt/relaylink/server/RelayLink.Server --config /etc/relaylink/server.json --check-config
   sudo install -m 0644 deploy/linux/relaylink-server.service /etc/systemd/system/relaylink-server.service
   sudo systemctl daemon-reload
   sudo systemctl enable --now relaylink-server
   ```

### Option C: Windows Native (Windows Service)

1. Download `RelayLink-Server-win-x64-<ver>.zip` and `SHA256SUMS`, verify SHA-256 (`Get-FileHash -Algorithm SHA256`), and extract to `C:\Program Files\RelayLink\Server`.
2. Prepare `C:\ProgramData\RelayLink\server.json` (reference `config/examples/server.windows.example.json`), `clients`, and TLS keys restricted to the service account (`NT AUTHORITY\LocalService`), `SYSTEM`, and `Administrators`.
3. In an elevated PowerShell session, run `install-server-service.ps1` from `deploy/windows/`:
   ```powershell
   .\install-server-service.ps1 -ExecutablePath 'C:\Program Files\RelayLink\Server\RelayLink.Server.exe' -ConfigurationPath 'C:\ProgramData\RelayLink\server.json'
   ```

---

## 2. Installing RelayLink Agent

> **Prerequisite:** Sign in to the Server Admin UI (`http://127.0.0.1:18080/`), create a dedicated client for this machine, and download its single-server JSON file (it contains the registration secret and embedded CA trust).

### Option A: Linux Agent (`systemd`)

1. Download `RelayLink-Agent-linux-x64-<ver>.tar.gz` and `SHA256SUMS`; verify SHA-256.
2. Create the unprivileged service user and directories:
   ```bash
   sudo useradd --system --home-dir /var/lib/relaylink-agent --shell /usr/sbin/nologin relaylink-agent
   sudo install -d -o root -g root -m 0755 /opt/relaylink/agent
   sudo tar -xzf RelayLink-Agent-linux-x64-<ver>.tar.gz -C /opt/relaylink/agent
   sudo chown -R root:root /opt/relaylink/agent
   sudo chmod 0755 /opt/relaylink/agent/RelayLink.Agent /opt/relaylink/agent/RelayLink.Agent.ConfigMigrator
   sudo install -d -o relaylink-agent -g relaylink-agent -m 0700 /var/lib/relaylink-agent
   sudo install -o relaylink-agent -g relaylink-agent -m 0600 ./relaylink-agent-<clientId>.json /var/lib/relaylink-agent/agent.json
   ```
3. Run the pre-start config migrator and `--check-config`, then enable `systemd`:
   ```bash
   sudo -u relaylink-agent /opt/relaylink/agent/RelayLink.Agent.ConfigMigrator --config /var/lib/relaylink-agent/agent.json
   sudo -u relaylink-agent /opt/relaylink/agent/RelayLink.Agent --config /var/lib/relaylink-agent/agent.json --check-config
   sudo install -m 0644 deploy/linux/relaylink-agent.service /etc/systemd/system/relaylink-agent.service
   sudo systemctl daemon-reload
   sudo systemctl enable --now relaylink-agent
   ```

### Option B: Windows Agent (Installer or Archive)

- Minimum OS: **Windows Server 2012 R2 x64** with **Microsoft Visual C++ 2015–2022 Redistributable x64** (`framework-dependent` also requires `.NET 10 ASP.NET Core Runtime x64`; `self-contained` bundles the runtime).
- Download `RelayLink-Agent-win-x64-self-contained-<ver>.exe` and `SHA256SUMS`, verify SHA-256, and run as Administrator (or silently with `/CONFIG`):
  ```powershell
  .\RelayLink-Agent-win-x64-self-contained-<ver>.exe /VERYSILENT /SUPPRESSMSGBOXES '/CONFIG=C:\path\agent.json'
  ```
  (For existing installs, use `/MODE=update` or `/MODE=reconfigure /CONFIG=C:\path\new-agent.json`. Legacy Server 2012/2012 R2 ZIP deployments can use `deploy/windows/register-agent-service-legacy.ps1`.)

### Option C: macOS Agent (`launchd`)

1. Detect architecture with `uname -m` (`arm64` → `osx-arm64`, `x86_64` → `osx-x64`), download `RelayLink-Agent-osx-<arch>-<ver>.tar.gz` and `SHA256SUMS`, and verify with `shasum -a 256 -c SHA256SUMS --ignore-missing`.
2. Extract binaries to `/Library/RelayLink/Agent` (`root:wheel`, `0755`), place the downloaded JSON config at `/Library/Application Support/RelayLink/Agent/agent.json` (`_relaylink-agent:_relaylink-agent`, dir `0700`, file `0600`), and create `/Library/Logs/RelayLink` (`0750`).
3. Validate and load the `launchd` service (`deploy/macos/relaylink-agent.plist`):
   ```sh
   sudo -u _relaylink-agent /Library/RelayLink/Agent/RelayLink.Agent.ConfigMigrator --config '/Library/Application Support/RelayLink/Agent/agent.json'
   sudo -u _relaylink-agent /Library/RelayLink/Agent/RelayLink.Agent --config '/Library/Application Support/RelayLink/Agent/agent.json' --check-config
   sudo plutil -lint /Library/LaunchDaemons/com.relaylink.agent.plist
   sudo launchctl bootstrap system /Library/LaunchDaemons/com.relaylink.agent.plist
   sudo launchctl enable system/com.relaylink.agent
   sudo launchctl kickstart -k system/com.relaylink.agent
   ```

---

## 3. Post-Install Verification & Channel Setup

1. Confirm the Agent shows **Online** in the Server Admin UI (`http://127.0.0.1:18080/`).
2. Confirm the local Agent UI responds on `http://127.0.0.1:18081/` (to connect additional Servers, import another single-server JSON directly on this page).
3. Create your first channel (**TCP**, **unicast UDP**, or **HTTP/HTTPS v5**) pointing to a host and port reachable from the Agent's private network, or add an **authorized peer mapping** on a caller Agent.
4. **Before Production**:
   - Enable control TLS (`7443/tcp`).
   - Restrict firewall exposure on `7443/tcp` and `7444/tcp` to Agent source IPs.
   - Standard (non-peer) TCP/UDP tunnels forward raw bytes—enable application TLS (e.g., SQL Server TLS) or use peer channels with inner TLS.
   - Back up `server.json`, `clients/`, and the SQLite database including `-wal` / `-shm` files.
