diff --git a/.gitignore b/.gitignore index ab19bd9..e060cd1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ # Real WireGuard configs contain private keys — never commit. -docker/wg/config/wg0.conf lxc/etc/wireguard/wg0.conf *.key *.psk diff --git a/README.md b/README.md index 23e916a..3df348d 100644 --- a/README.md +++ b/README.md @@ -1,69 +1,99 @@ # wirecon -WireGuard tunnel between a **rootless-Docker** internet host and an **unprivileged Alpine LXC** on Proxmox, with Caddy reverse-proxying inbound traffic into the LXC's LAN. Tuned for flaky uplinks (Telekom DSL/LTE Hybrid) with a fast-reconnect watchdog and structured logging. +WireGuard **client** tooling for an unprivileged Alpine LXC on Proxmox. Connects the LXC to an existing WireGuard server over a flaky uplink (built for Telekom DSL/LTE Hybrid, where MTU pitfalls cause silent stalls) with: -``` - internet Telekom Hybrid (DSL+LTE) - clients ──► Docker host ══════ wg0 (UDP 51820) ══════ LXC ──► 192.168.1.0/24 - (Caddy + (Alpine + kernel WG - wireguard-go, + OpenRC watchdog - rootless) & monitor) -``` +- a Proxmox-host one-liner that creates the LXC from scratch +- an OpenRC **watchdog** that detects stale handshakes / ping loss and re-dials fast +- a continuous **monitor** that emits structured logs for post-mortem analysis +- an **MTU probe** that finds the real path MTU via DF-ping binary search + +The server side is intentionally out of scope — point the LXC at any existing WireGuard endpoint. ## Install -### Proxmox side — one-liner (creates the LXC from scratch) - Run on the **Proxmox host** as root: ``` curl -fsSL https://git.d4nn13.de/d4nn13/wirecon/raw/branch/main/proxmox/create-lxc.sh | bash ``` -Defaults: Alpine template (latest), hostname `concad`, 1 core, 256 MiB RAM, 2 GiB disk, DHCP on `vmbr0`, auto-picked VMID. The script creates the container, applies the `/dev/net/tun` passthrough, starts it, and chains into the in-LXC bootstrap which clones the repo and runs `lxc/install.sh`. Override defaults via env vars: +That script: + +1. picks the next free VMID (`pvesh get /cluster/nextid`) +2. downloads the latest Alpine template if missing +3. creates an unprivileged LXC (`concad`, 1 core, 256 MiB RAM, 2 GiB disk, DHCP on `vmbr0`, nesting+keyctl features) +4. appends the `/dev/net/tun` passthrough to `/etc/pve/lxc/.conf` +5. starts the container, waits for DHCP +6. chains into `lxc/bootstrap.sh` inside the LXC, which clones this repo to `/opt/wirecon` and runs `lxc/install.sh` + +Override defaults via env vars: ``` HOSTNAME=tunbox MEMORY=512 STORAGE=fast-nvme \ bash -c "$(curl -fsSL https://git.d4nn13.de/d4nn13/wirecon/raw/branch/main/proxmox/create-lxc.sh)" ``` -### Finishing up +Available overrides: `HOSTNAME`, `CORES`, `MEMORY`, `DISK`, `BRIDGE`, `STORAGE`, `TEMPLATE_STORAGE`, `WIRECON_REPO`, `WIRECON_REF`. -Edit `/etc/wireguard/wg0.conf` (keys, endpoint, LAN iface in `PostUp`), optionally tune `/etc/wirecon/wirecon.conf`, then: +## Finishing up (inside the LXC) ``` +pct enter + +vi /etc/wireguard/wg0.conf # PrivateKey, Peer PublicKey, Endpoint, MTU, LAN iface in PostUp +vi /etc/wirecon/wirecon.conf # PEER_IP + watchdog thresholds (optional) + rc-update add wg-quick.wg0 default && rc-service wg-quick.wg0 start rc-update add wirecon-watchdog default && rc-service wirecon-watchdog start rc-update add wirecon-monitor default && rc-service wirecon-monitor start + tail -F /var/log/wirecon-watchdog.log /var/log/wirecon-monitor.log ``` -### Docker side (internet host) - -Rootless Docker daemon already running: - -``` -git clone https://git.d4nn13.de/d4nn13/wirecon.git -cd wirecon/docker -cp wg/config/wg0.conf.example wg/config/wg0.conf # fill in keys -# edit proxy/Caddyfile for your sites -docker compose up -d --build -``` - -Host prereqs: -- `/dev/net/tun` readable by the rootless user -- `sysctl net.ipv4.ip_unprivileged_port_start=80` if you want 80/443 bound directly - ## Layout ``` -docker/ rootless-Docker stack: wg (wireguard-go) + Caddy sharing netns -lxc/ Alpine installer, OpenRC services, watchdog/monitor/MTU-probe -proxmox/ host-side LXC creator + tun-passthrough snippet +lxc/ + install.sh idempotent apk + OpenRC installer (runs inside the LXC) + bootstrap.sh clones repo into /opt/wirecon then calls install.sh + etc/init.d/ OpenRC services: wirecon-watchdog, wirecon-monitor + etc/wirecon/wirecon.conf tunable thresholds (peer IP, handshake age, MTU bounds…) + etc/wireguard/wg0.conf.example + usr/local/bin/ + wirecon-watchdog.sh one tick: handshake age + in-tunnel ping → reconnect if needed + wirecon-watchdog-loop.sh supervise-daemon target that re-runs the tick every WATCHDOG_INTERVAL + wirecon-monitor.sh long-running structured-log emitter + wirecon-mtu-probe.sh DF-ping binary search → recommended WG MTU + +proxmox/ + create-lxc.sh host-side LXC provisioner (the one-liner above) + lxc.conf.snippet tun passthrough lines (also appended by create-lxc.sh) ``` +## How the watchdog decides to reconnect + +Every `WATCHDOG_INTERVAL` seconds (default 20 s), one tick runs: + +1. **Interface check** — `ip link show wg0`; if missing → reconnect immediately. +2. **Handshake age** — `wg show wg0 latest-handshakes`; if older than `HANDSHAKE_MAX_AGE` (default 180 s), mark failure. +3. **In-tunnel ping** — `ping -I wg0 -c 3 -W 2 $PEER_IP`; if loss ≥ `PING_LOSS_THRESHOLD`, mark failure. +4. **Failure streak** — only after `FAIL_STREAK_THRESHOLD` consecutive failed ticks (default 2 = 40 s) does it actually `wg-quick down && up`. This prevents a single LTE hiccup from thrashing the tunnel. + +Every decision — pass, fail, streak bump, reconnect — is logged as a single `key=value` line with a reason. + ## Tuning -- **MTU** — default `1280`. Run `/usr/local/bin/wirecon-mtu-probe.sh` inside the LXC to find the real path MTU; update `MTU =` on both peers. -- **Watchdog** (`/etc/wirecon/wirecon.conf`): `HANDSHAKE_MAX_AGE`, `PING_LOSS_THRESHOLD`, `FAIL_STREAK_THRESHOLD`, `WATCHDOG_INTERVAL`. -- **Logs** — structured `key=value` lines in `/var/log/wirecon-*.log`; grep-friendly. +| File | What to tune | +|---|---| +| `/etc/wireguard/wg0.conf` | `MTU` (run `wirecon-mtu-probe.sh` first), LAN iface in the `PostUp` iptables MASQ rules | +| `/etc/wirecon/wirecon.conf` | `PEER_IP`, `HANDSHAKE_MAX_AGE`, `PING_LOSS_THRESHOLD`, `FAIL_STREAK_THRESHOLD`, `WATCHDOG_INTERVAL`, `MONITOR_INTERVAL` | + +## Logs + +Both services write `key=value` structured lines; grep and `awk` friendly: + +``` +tail -F /var/log/wirecon-watchdog.log /var/log/wirecon-monitor.log +grep event=reconnect /var/log/wirecon-watchdog.log +awk -F'rtt_ms=' '/rtt_ms=/{split($2,a," "); print $0}' /var/log/wirecon-monitor.log +``` diff --git a/docker/compose.yaml b/docker/compose.yaml deleted file mode 100644 index 0a589fd..0000000 --- a/docker/compose.yaml +++ /dev/null @@ -1,72 +0,0 @@ -# Runs on the internet host under rootless Docker. -# -# Prereqs on the rootless-Docker user: -# - /dev/net/tun readable (e.g. udev rule or `sudo chmod 0666 /dev/net/tun`) -# - `sysctl net.ipv4.ip_unprivileged_port_start=80` on the host if you want -# Caddy to bind 80/443 from a rootless daemon; otherwise remap to 8080/8443. -# -# Bring up: docker compose up -d --build -# Logs: docker compose logs -f wg proxy - -name: wirecon - -services: - wg: - build: ./wg - container_name: wirecon-wg - restart: unless-stopped - # wireguard-go is userspace, but still needs NET_ADMIN within its own netns - # to create wg0 and program routes/iptables. In rootless Docker this - # capability is confined to the container's user namespace. - cap_add: - - NET_ADMIN - devices: - - /dev/net/tun:/dev/net/tun - sysctls: - net.ipv4.ip_forward: "1" - ports: - # WireGuard handshake/data - - "51820:51820/udp" - # Reverse-proxy ingress. Published here (not on `proxy`) because proxy - # shares wg's network namespace. - - "80:80/tcp" - - "443:443/tcp" - - "443:443/udp" # HTTP/3 - volumes: - - ./wg/config:/etc/wireguard:ro - healthcheck: - test: ["CMD", "/usr/local/bin/healthcheck.sh"] - interval: 30s - timeout: 5s - retries: 3 - start_period: 15s - logging: - driver: json-file - options: - max-size: "10m" - max-file: "5" - - proxy: - image: caddy:2-alpine - container_name: wirecon-proxy - restart: unless-stopped - depends_on: - wg: - condition: service_healthy - # Share wg's network namespace so Caddy can reach 192.168.1.0/24 via wg0 - # without host-level routing. All port publishing must live on the wg - # service above. - network_mode: "service:wg" - volumes: - - ./proxy/Caddyfile:/etc/caddy/Caddyfile:ro - - caddy_data:/data - - caddy_config:/config - logging: - driver: json-file - options: - max-size: "10m" - max-file: "5" - -volumes: - caddy_data: - caddy_config: diff --git a/docker/proxy/Caddyfile b/docker/proxy/Caddyfile deleted file mode 100644 index 34765e3..0000000 --- a/docker/proxy/Caddyfile +++ /dev/null @@ -1,23 +0,0 @@ -# Caddy runs inside the wg container's netns, so it can dial 192.168.1.x -# directly through wg0. -# -# Example: publish an internal service at https://service.example.com. -# Replace the hostnames + upstream IPs with real ones. - -{ - # Uncomment and fill to use ACME for real certs. - # email you@example.com -} - -service.example.com { - reverse_proxy 192.168.1.10:8080 - log { - output stdout - format console - } -} - -# Add more sites as needed: -# other.example.com { -# reverse_proxy 192.168.1.20:3000 -# } diff --git a/docker/wg/Dockerfile b/docker/wg/Dockerfile deleted file mode 100644 index 0d1b579..0000000 --- a/docker/wg/Dockerfile +++ /dev/null @@ -1,20 +0,0 @@ -FROM alpine:3.20 - -RUN apk add --no-cache \ - wireguard-tools \ - wireguard-go \ - iptables \ - ip6tables \ - iproute2 \ - bash \ - curl \ - tini - -ENV WG_QUICK_USERSPACE_IMPLEMENTATION=wireguard-go \ - WG_SUDO=0 - -COPY entrypoint.sh /usr/local/bin/entrypoint.sh -COPY healthcheck.sh /usr/local/bin/healthcheck.sh -RUN chmod +x /usr/local/bin/entrypoint.sh /usr/local/bin/healthcheck.sh - -ENTRYPOINT ["/sbin/tini", "--", "/usr/local/bin/entrypoint.sh"] diff --git a/docker/wg/config/wg0.conf.example b/docker/wg/config/wg0.conf.example deleted file mode 100644 index eb1eef2..0000000 --- a/docker/wg/config/wg0.conf.example +++ /dev/null @@ -1,32 +0,0 @@ -# /etc/wireguard/wg0.conf on the Docker (server) side. -# Rename to wg0.conf and fill in the marked fields. -# -# Generate keys: -# wg genkey | tee server.key | wg pubkey > server.pub -# wg genkey | tee client.key | wg pubkey > client.pub - -[Interface] -# Server's tunnel address. Pick a subnet that doesn't clash with 192.168.1.0/24. -Address = 10.42.0.1/24 -ListenPort = 51820 -PrivateKey = REPLACE_WITH_SERVER_PRIVATE_KEY - -# Match MTU on both ends. 1280 is safe default behind Telekom Hybrid. -MTU = 1280 - -# Enable forwarding + MASQ inside the container's own netns so traffic from -# the proxy container (sharing this netns) can flow into wg0 and back. -# No host iptables are touched. -PostUp = sysctl -w net.ipv4.ip_forward=1 -PostUp = iptables -A FORWARD -i %i -j ACCEPT -PostUp = iptables -A FORWARD -o %i -j ACCEPT -PostDown = iptables -D FORWARD -i %i -j ACCEPT -PostDown = iptables -D FORWARD -o %i -j ACCEPT - -[Peer] -# The LXC client. -PublicKey = REPLACE_WITH_CLIENT_PUBLIC_KEY -# Route both the client's tunnel IP and its LAN subnet back through wg0. -AllowedIPs = 10.42.0.2/32, 192.168.1.0/24 -# Server should not set PersistentKeepalive — the client drives this, since -# it's the one behind NAT / hybrid uplink. diff --git a/docker/wg/entrypoint.sh b/docker/wg/entrypoint.sh deleted file mode 100755 index 7b42531..0000000 --- a/docker/wg/entrypoint.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/bash -set -euo pipefail - -IFACE="${WG_INTERFACE:-wg0}" -CONF="/etc/wireguard/${IFACE}.conf" - -log() { printf '%s wirecon-wg[%s] %s\n' "$(date -u +%FT%TZ)" "$$" "$*"; } - -if [[ ! -r "$CONF" ]]; then - log "FATAL: $CONF missing or unreadable" - exit 1 -fi - -if [[ ! -e /dev/net/tun ]]; then - log "FATAL: /dev/net/tun not present; mount it from the host" - exit 1 -fi - -shutdown() { - log "shutting down ${IFACE}" - wg-quick down "$IFACE" || true - exit 0 -} -trap shutdown SIGINT SIGTERM - -log "bringing up ${IFACE} (userspace impl: ${WG_QUICK_USERSPACE_IMPLEMENTATION})" -wg-quick up "$IFACE" - -log "interface up; endpoint info:" -wg show "$IFACE" | while IFS= read -r line; do log " $line"; done - -# Park foreground on a loop so signals reach us. -while :; do - sleep 60 & - wait $! -done diff --git a/docker/wg/healthcheck.sh b/docker/wg/healthcheck.sh deleted file mode 100755 index a9dd808..0000000 --- a/docker/wg/healthcheck.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash -# Healthcheck: the interface must exist and have at least one peer. -# A stale handshake is not failed here — that's the watchdog's job on the -# LXC (client) side; the server is passive and only responds. -set -euo pipefail - -IFACE="${WG_INTERFACE:-wg0}" - -ip link show "$IFACE" >/dev/null 2>&1 || exit 1 -wg show "$IFACE" >/dev/null 2>&1 || exit 1 - -# Must have at least one peer configured. -peers=$(wg show "$IFACE" peers | wc -l) -[[ "$peers" -ge 1 ]] || exit 1 - -exit 0