# Deployment Guide<no value>

# Deploying the Syncthing Relay on `relay.btoven.net`

> Complements `strategy.md` with the **infrastructure details**.

---

## 1. The fundamental problem

> **The Syncthing relay protocol is NOT HTTP. It is raw TCP over TLS with a self-signed certificate whose fingerprint IS the relay's device ID. That is why it cannot sit behind an HTTP reverse proxy that "terminates" TLS (Traefik, Nginx Proxy Manager, Caddy).**

Evidence (source `cmd/strelaysrv/main.go`):
```go
tlsCfg := &tls.Config{
    Certificates: []tls.Certificate{cert},   // own self-signed cert
    ClientAuth:   tls.RequestClientCert,
    InsecureSkipVerify: true,
}
id := syncthingprotocol.NewDeviceID(cert.Certificate[0]) // device ID = cert fingerprint
```

Consequences:
1. **Do not terminate TLS with a CA cert (Let's Encrypt).** The relay's "SSL" is its own self-signed cert + device ID.
2. **An L7 reverse proxy would break compatibility.**
3. There is plaintext chatter before the TLS handshake in session mode, and the SNI is not a normal hostname.
4. `strelaysrv` **does not support the PROXY protocol**.

**Conclusion:** the relay must be exposed as **TCP passthrough (L4)** or via **direct port publishing**, with no intermediate TLS termination.

---

## 2. Decoupling "web" vs "relay protocol"

| Resource | Protocol | Ports | Reverse proxy? |
|----------|----------|-------|----------------|
| **Relay v1 protocol** | raw TCP/self-signed TLS | `22067` (or `443`) | **NO** — direct exposure or TCP passthrough |
| **Status/stats** | HTTP | `22070` | **YES** — can go through Traefik/NPM |
| **Site/docs** | HTTP/HTTPS | `80/443` | **YES** — Traefik/NPM (as you already do) |

**The port conflict only exists if you try to put the relay protocol on the web proxy's `443`.** Solution: **do not use `443` for the relay** while `443` belongs to the proxy.

---

## 3. Architecture decisions

### Scenario 1 — PRIVATE (start here)
- The `strelaysrv` container publishes **directly** `22067` and `22070`.
- No proxy for the protocol. No Let's Encrypt needed.
- `relay.btoven.net` for the web/docs stays on Coolify (`80/443`).
- Clients: `relay://relay.btoven.net:22067/?id=<DEVICE_ID>`
- **Same VPS as Coolify?** Yes, as long as Coolify does not occupy `22067`.

### Scenario 2 — PUBLIC (pool, maximum reach)
- Syncthing recommends **443** for maximum compatibility with corporate firewalls.
- **`443` must belong to the relay**, not Traefik:
  - **a) Dedicated server** for the relay (`443` owned by the relay).
  - **b) Traefik TCP passthrough by SNI** on the same `443` (advanced; Coolify does not expose this easily).
- Clients: `relay://relay.btoven.net:443/?id=<DEVICE_ID>`

---

## 4. Dockerfile / image

**Option A — Official image directly (recommended):**
```yaml
services:
  syncthing-relay:
    image: syncthing/relaysrv:latest
    container_name: syncthing-relay
    restart: unless-stopped
    ports:
      - "22067:22067"
      - "22070:22070"
    command:
      - -listen=:22067
      - -status-srv=:22070
      - -pools=
      - -provided-by=BTOVEN NETWORK
      - -global-rate=52428800
      - -per-session-rate=10485760
      - -network-timeout=2m
      - -message-timeout=1m
      - -ping-interval=1m
    environment:
      - PUID=1000
      - PGID=1000
    volumes:
      - syncthing-relay-data:/var/strelaysrv
    init: true
    healthcheck:
      test: ["CMD", "nc", "-z", "localhost", "22067"]
      interval: 60s
      timeout: 10s
      retries: 3
      start_period: 10s
volumes:
  syncthing-relay-data:
```

**Option B — Own Dockerfile (for pipeline/registry):**
```dockerfile
FROM golang:1.26-alpine AS builder
RUN apk add --no-cache git
RUN git clone --depth 1 --branch v2.1.3 https://github.com/syncthing/syncthing /src \
 && cd /src \
 && go run build.go -no-upgrade build strelaysrv

FROM alpine:3.20
RUN apk add --no-cache ca-certificates su-exec
COPY --from=builder /src/strelaysrv /bin/strelaysrv
RUN addgroup -g 1000 relay && adduser -u 1000 -G relay -s /bin/sh -D relay \
 && mkdir -p /var/strelaysrv && chown -R relay /var/strelaysrv
USER relay
WORKDIR /var/strelaysrv
EXPOSE 22067 22070
ENTRYPOINT ["/bin/strelaysrv"]
CMD ["-listen=:22067", "-status-srv=:22070", "-pools="]
```

---

## 5. Ports and firewall

| Port | Protocol | Expose | Notes |
|------|----------|--------|-------|
| `22067` | TCP/TLS relay | **YES** | always required |
| `22070` | HTTP status | Optional | keep internal if not using the pool |
| `80/443` | HTTP/HTTPS web | Traefik/NPM | do NOT touch for the relay (Sc. 1) |

---

## 6. Coolify vs NPM vs direct

| Tool | Relay protocol? | Web? | Verdict |
|------|-----------------|------|---------|
| Coolify/Traefik | NO (L7) | YES | use for the web |
| Nginx Proxy Manager | NO | YES | web only |
| **Direct exposure (docker)** | **YES** | — | **for the relay container** |

**Recommendation:** deploy `strelaysrv` as a normal Docker container with `ports: "22067:22067"` (direct, no proxy). Coolify/Traefik manages the web on `80/443`. This avoids the port conflict entirely.

---

## 7. References
- https://docs.syncthing.net/users/strelaysrv.html
- https://github.com/syncthing/syncthing/blob/main/cmd/strelaysrv/main.go
- https://forum.syncthing.net/t/strelaysrv-behind-reverse-proxy/23590
- https://forum.syncthing.net/t/stdiscosrv-and-strelaysrv-behind-openbsd-relayd/21381
- https://relays.syncthing.net/
