initial commit: wirecon WireGuard stack

Rootless-Docker server (wireguard-go + Caddy sharing netns) and
unprivileged-LXC client (kernel WG + systemd watchdog/monitor/MTU-probe)
with curl-pipeable bootstrap. Tuned for Telekom DSL/LTE Hybrid uplinks.
This commit is contained in:
d4nn13 committed 2026-04-19 18:20:21 +02:00
commit 49b01dfcd3
19 files changed
+770

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
# Real WireGuard configs contain private keys — never commit.
docker/wg/config/wg0.conf
lxc/etc/wireguard/wg0.conf
*.key
*.psk
# Editor / OS junk
.DS_Store
*.swp
*~
# Claude Code local state
.claude/
+72
View File
@@ -0,0 +1,72 @@
# 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:
+23
View File
@@ -0,0 +1,23 @@
# 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
# }
+20
View File
@@ -0,0 +1,20 @@
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"]
+32
View File
@@ -0,0 +1,32 @@
# /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.
+36
View File
@@ -0,0 +1,36 @@
#!/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
+16
View File
@@ -0,0 +1,16 @@
#!/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
+56
View File
@@ -0,0 +1,56 @@
#!/bin/bash
# Bootstrap the wirecon LXC side from a fresh unprivileged LXC.
#
# One-liner:
# curl -fsSL https://git.d4nn13.de/d4nn13/wirecon/raw/branch/main/lxc/bootstrap.sh | sudo bash
# or with a specific ref / fork:
# curl -fsSL .../bootstrap.sh | sudo WIRECON_REPO=https://git.d4nn13.de/me/wirecon.git WIRECON_REF=main bash
#
# What it does:
# 1. installs git if missing
# 2. clones (or fast-forwards) the repo into /opt/wirecon
# 3. runs lxc/install.sh from that checkout
set -euo pipefail
REPO="${WIRECON_REPO:-https://git.d4nn13.de/d4nn13/wirecon.git}"
REF="${WIRECON_REF:-main}"
DEST="${WIRECON_DEST:-/opt/wirecon}"
log() { printf '[bootstrap] %s\n' "$*"; }
if [[ $EUID -ne 0 ]]; then
echo "must run as root (sudo)" >&2
exit 1
fi
# --- prereqs ---------------------------------------------------------------
if ! command -v git >/dev/null; then
log "installing git"
if command -v apt-get >/dev/null; then
DEBIAN_FRONTEND=noninteractive apt-get update -q
DEBIAN_FRONTEND=noninteractive apt-get install -yq git ca-certificates
elif command -v dnf >/dev/null; then
dnf install -y git ca-certificates
else
echo "unsupported distro: need apt-get or dnf" >&2
exit 1
fi
fi
# --- clone or update -------------------------------------------------------
if [[ -d "$DEST/.git" ]]; then
log "updating existing checkout at $DEST"
git -C "$DEST" fetch --depth 1 origin "$REF"
git -C "$DEST" checkout -f "$REF"
git -C "$DEST" reset --hard "origin/$REF"
else
log "cloning $REPO@$REF → $DEST"
install -d -m 0755 "$(dirname "$DEST")"
git clone --depth 1 --branch "$REF" "$REPO" "$DEST"
fi
# --- run installer ---------------------------------------------------------
log "running lxc/install.sh"
cd "$DEST"
exec bash lxc/install.sh
@@ -0,0 +1,14 @@
[Unit]
# Make sure we come up after the LAN is ready.
After=network-online.target
Wants=network-online.target
[Service]
# wg-quick@ is Type=oneshot with RemainAfterExit=yes by default.
# If `wg-quick up` itself fails (e.g. transient DNS blip resolving the
# server endpoint at boot), retry a few times before giving up — the
# watchdog takes over from there.
Restart=on-failure
RestartSec=10s
# oneshot needs this for Restart to apply:
# (systemd accepts Restart on oneshot in practice)
@@ -0,0 +1,17 @@
[Unit]
Description=wirecon tunnel continuous monitor
After=wg-quick@wg0.service network-online.target
Wants=wg-quick@wg0.service
[Service]
Type=simple
EnvironmentFile=-/etc/wirecon/wirecon.conf
ExecStart=/usr/local/bin/wirecon-monitor.sh
Restart=always
RestartSec=5s
SyslogIdentifier=wirecon-monitor
# Low priority; observation only.
Nice=5
[Install]
WantedBy=multi-user.target
@@ -0,0 +1,11 @@
[Unit]
Description=wirecon tunnel watchdog (one tick)
After=wg-quick@wg0.service network-online.target
Wants=wg-quick@wg0.service
[Service]
Type=oneshot
EnvironmentFile=-/etc/wirecon/wirecon.conf
ExecStart=/usr/local/bin/wirecon-watchdog.sh
SyslogIdentifier=wirecon-watchdog
Nice=-5
@@ -0,0 +1,14 @@
[Unit]
Description=wirecon watchdog — run check every 20s
After=wg-quick@wg0.service
[Timer]
# Kick off soon after boot, then every 20s.
OnBootSec=30s
OnUnitActiveSec=20s
AccuracySec=1s
# Prevents timer drift if a check takes a while (e.g. during a reconnect).
Unit=wirecon-watchdog.service
[Install]
WantedBy=timers.target
+34
View File
@@ -0,0 +1,34 @@
# /etc/wirecon/wirecon.conf — shared config for wirecon scripts.
# Sourced by bash; keep it simple KEY=VALUE.
# WireGuard interface name
WG_INTERFACE=wg0
# Peer's in-tunnel IP (the server side of the tunnel)
PEER_IP=10.42.0.1
# Reconnect if no handshake has completed within this many seconds.
# WireGuard handshakes every ~120s when traffic flows, so >180s is firmly stale.
HANDSHAKE_MAX_AGE=180
# In-tunnel ping probe
PING_COUNT=3
PING_TIMEOUT=2 # seconds per ping
PING_LOSS_THRESHOLD=3 # total lost-pings across PING_COUNT that trips reconnect
# How many consecutive watchdog ticks must fail before forcing a down/up cycle.
# Guards against single transient LTE hiccups triggering thrash.
FAIL_STREAK_THRESHOLD=2
# MTU probe bounds (for wirecon-mtu-probe.sh)
MTU_MIN=1200
MTU_MAX=1420
# Monitor cadence (seconds between log emissions)
MONITOR_INTERVAL=10
# Log tag (shows up as SYSLOG_IDENTIFIER in journald)
LOG_TAG=wirecon
# Runtime state dir
STATE_DIR=/run/wirecon
+32
View File
@@ -0,0 +1,32 @@
# /etc/wireguard/wg0.conf on the LXC (client) side.
# Rename to wg0.conf and fill in the marked fields.
[Interface]
Address = 10.42.0.2/24
PrivateKey = REPLACE_WITH_CLIENT_PRIVATE_KEY
# Default 1280 for Telekom DSL/LTE Hybrid (GRE overlay eats headroom).
# Run wirecon-mtu-probe.sh to find a better value and update here +
# on the server side.
MTU = 1280
# MASQ traffic from the WG tunnel onto the LAN so hosts on 192.168.1.0/24
# see it as coming from the LXC's LAN IP and reply without needing a
# return route to 10.42.0.0/24.
# Replace eth0 with the LXC's LAN interface name.
PostUp = sysctl -w net.ipv4.ip_forward=1
PostUp = iptables -t nat -A POSTROUTING -s 10.42.0.0/24 -o eth0 -j MASQUERADE
PostUp = iptables -A FORWARD -i %i -o eth0 -j ACCEPT
PostUp = iptables -A FORWARD -i eth0 -o %i -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
PostDown = iptables -t nat -D POSTROUTING -s 10.42.0.0/24 -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -o eth0 -j ACCEPT
PostDown = iptables -D FORWARD -i eth0 -o %i -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
[Peer]
PublicKey = REPLACE_WITH_SERVER_PUBLIC_KEY
Endpoint = REPLACE_WITH_SERVER_PUBLIC_HOST:51820
# Only route the server's tunnel IP through wg0. We do NOT set 0.0.0.0/0
# here because the LXC's default gateway stays on the LAN.
AllowedIPs = 10.42.0.1/32
# Client drives handshakes through the NAT; 25s is standard.
PersistentKeepalive = 25
Executable
+100
View File
@@ -0,0 +1,100 @@
#!/bin/bash
# Installer for the LXC side. Run as root inside the LXC.
#
# sudo ./install.sh
#
# Idempotent: re-running only updates files that changed. Does NOT touch
# wg0.conf if it already exists (so real keys aren't clobbered).
set -euo pipefail
if [[ $EUID -ne 0 ]]; then
echo "must run as root" >&2
exit 1
fi
SRC="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
log() { printf '[install] %s\n' "$*"; }
# --- packages ---------------------------------------------------------------
log "installing packages"
if command -v apt-get >/dev/null; then
DEBIAN_FRONTEND=noninteractive apt-get update -q
DEBIAN_FRONTEND=noninteractive apt-get install -yq \
wireguard wireguard-tools iptables iproute2 iputils-ping conntrack
elif command -v dnf >/dev/null; then
dnf install -y wireguard-tools iptables iproute iputils conntrack-tools
else
echo "unsupported distro: need apt-get or dnf" >&2
exit 1
fi
# --- /dev/net/tun sanity ----------------------------------------------------
if [[ ! -e /dev/net/tun ]]; then
cat >&2 <<'EOF'
ERROR: /dev/net/tun is not present in this unprivileged LXC.
On the Proxmox host, append proxmox/lxc.conf.snippet to
/etc/pve/lxc/<VMID>.conf
and restart the container.
EOF
exit 1
fi
# --- files ------------------------------------------------------------------
install -d -m 0755 /etc/wirecon /etc/wireguard /usr/local/bin \
/etc/systemd/system/wg-quick@wg0.service.d
log "deploying scripts"
install -m 0755 "$SRC/usr/local/bin/wirecon-watchdog.sh" /usr/local/bin/
install -m 0755 "$SRC/usr/local/bin/wirecon-monitor.sh" /usr/local/bin/
install -m 0755 "$SRC/usr/local/bin/wirecon-mtu-probe.sh" /usr/local/bin/
log "deploying systemd units"
install -m 0644 "$SRC/etc/systemd/system/wirecon-watchdog.service" /etc/systemd/system/
install -m 0644 "$SRC/etc/systemd/system/wirecon-watchdog.timer" /etc/systemd/system/
install -m 0644 "$SRC/etc/systemd/system/wirecon-monitor.service" /etc/systemd/system/
install -m 0644 "$SRC/etc/systemd/system/wg-quick@wg0.service.d/override.conf" \
/etc/systemd/system/wg-quick@wg0.service.d/override.conf
if [[ ! -e /etc/wirecon/wirecon.conf ]]; then
log "installing default /etc/wirecon/wirecon.conf"
install -m 0644 "$SRC/etc/wirecon/wirecon.conf" /etc/wirecon/wirecon.conf
else
log "keeping existing /etc/wirecon/wirecon.conf"
fi
if [[ ! -e /etc/wireguard/wg0.conf ]]; then
log "installing wg0.conf.example → /etc/wireguard/wg0.conf (EDIT BEFORE STARTING)"
install -m 0600 "$SRC/etc/wireguard/wg0.conf.example" /etc/wireguard/wg0.conf
EDIT_REQUIRED=1
else
log "keeping existing /etc/wireguard/wg0.conf"
EDIT_REQUIRED=0
fi
# --- systemd wiring ---------------------------------------------------------
systemctl daemon-reload
if [[ "$EDIT_REQUIRED" == "1" ]]; then
cat <<'EOF'
NEXT STEPS:
1. Generate keys, edit /etc/wireguard/wg0.conf (server pubkey, endpoint,
private key, LAN iface name in PostUp rules).
2. Tune /etc/wirecon/wirecon.conf if defaults don't fit.
3. Start everything:
systemctl enable --now wg-quick@wg0
systemctl enable --now wirecon-watchdog.timer
systemctl enable --now wirecon-monitor.service
4. Watch it:
journalctl -f -t wirecon-watchdog -t wirecon-monitor
EOF
else
log "enabling + (re)starting services"
systemctl enable --now wg-quick@wg0
systemctl enable --now wirecon-watchdog.timer
systemctl enable --now wirecon-monitor.service
systemctl restart wirecon-monitor.service
log "done. tail logs with: journalctl -f -t wirecon-watchdog -t wirecon-monitor"
fi
+92
View File
@@ -0,0 +1,92 @@
#!/bin/bash
# wirecon-monitor.sh — long-running observer. Emits one structured log line
# every MONITOR_INTERVAL seconds with handshake age, throughput deltas,
# endpoint, and in-tunnel RTT. Designed to make silent-death diagnosable
# after the fact via journalctl.
set -uo pipefail
CONF="${WIRECON_CONF:-/etc/wirecon/wirecon.conf}"
# shellcheck source=/dev/null
[[ -r "$CONF" ]] && source "$CONF"
: "${WG_INTERFACE:=wg0}"
: "${PEER_IP:=10.42.0.1}"
: "${MONITOR_INTERVAL:=10}"
prev_rx=""
prev_tx=""
prev_hs=""
prev_ep=""
prev_mtu=""
log() {
local level="$1"; shift
printf 'level=%s ts=%s %s\n' "$level" "$(date -u +%FT%TZ)" "$*"
}
rtt_probe() {
# single ping, short timeout, returns ms or "-"
local out rtt
out=$(ping -I "$WG_INTERFACE" -c 1 -W 2 -n "$PEER_IP" 2>/dev/null) || { echo "-"; return; }
rtt=$(echo "$out" | awk -F'=' '/time=/{split($NF,a," "); print a[1]; exit}')
[[ -z "$rtt" ]] && rtt="-"
echo "$rtt"
}
log info "event=monitor_start iface=$WG_INTERFACE peer_ip=$PEER_IP interval=${MONITOR_INTERVAL}s"
while :; do
if ! ip link show "$WG_INTERFACE" >/dev/null 2>&1; then
log warn "event=iface_missing iface=$WG_INTERFACE"
sleep "$MONITOR_INTERVAL"
continue
fi
# wg show <iface> dump has one header line then one per peer:
# iface: private pub listen_port fwmark
# peer: pub psk endpoint allowed_ips last_handshake rx_bytes tx_bytes keepalive
dump=$(wg show "$WG_INTERFACE" dump 2>/dev/null | awk 'NR==2')
if [[ -z "$dump" ]]; then
log warn "event=no_peer_dump iface=$WG_INTERFACE"
sleep "$MONITOR_INTERVAL"
continue
fi
endpoint=$(awk '{print $3}' <<<"$dump")
hs_ts=$(awk '{print $5}' <<<"$dump")
rx=$(awk '{print $6}' <<<"$dump")
tx=$(awk '{print $7}' <<<"$dump")
mtu=$(ip -o link show "$WG_INTERFACE" | awk '{for(i=1;i<=NF;i++) if($i=="mtu"){print $(i+1); exit}}')
now=$(date +%s)
hs_age=-1
[[ "$hs_ts" != "0" && -n "$hs_ts" ]] && hs_age=$((now - hs_ts))
# byte deltas (bps averaged over interval)
rx_bps="-"; tx_bps="-"
if [[ -n "$prev_rx" && -n "$prev_tx" ]]; then
rx_bps=$(( (rx - prev_rx) * 8 / MONITOR_INTERVAL ))
tx_bps=$(( (tx - prev_tx) * 8 / MONITOR_INTERVAL ))
fi
rtt=$(rtt_probe)
# Highlight state changes at warn level so they're easy to grep.
level=info
notes=""
if [[ -n "$prev_ep" && "$prev_ep" != "$endpoint" ]]; then
level=warn; notes+=" endpoint_changed_from=$prev_ep"
fi
if [[ -n "$prev_mtu" && "$prev_mtu" != "$mtu" ]]; then
level=warn; notes+=" mtu_changed_from=$prev_mtu"
fi
if [[ -n "$prev_hs" && "$prev_hs" != "0" && "$hs_ts" == "$prev_hs" && "$hs_age" -gt 180 ]]; then
level=warn; notes+=" handshake_stuck"
fi
log "$level" "event=tick iface=$WG_INTERFACE endpoint=$endpoint handshake_age=${hs_age}s mtu=$mtu rtt_ms=$rtt rx_bps=$rx_bps tx_bps=$tx_bps rx_total=$rx tx_total=$tx${notes}"
prev_rx="$rx"; prev_tx="$tx"; prev_hs="$hs_ts"; prev_ep="$endpoint"; prev_mtu="$mtu"
sleep "$MONITOR_INTERVAL"
done
+65
View File
@@ -0,0 +1,65 @@
#!/bin/bash
# wirecon-mtu-probe.sh — find the largest payload that survives PMTUD toward
# the WG peer's public endpoint, then print the recommended WG MTU
# (payload - 80 bytes of WG/UDP/IPv4 overhead).
#
# Usage: wirecon-mtu-probe.sh [<target_host>]
# If <target_host> is omitted, reads the Endpoint from /etc/wireguard/wg0.conf.
set -euo pipefail
CONF="${WIRECON_CONF:-/etc/wirecon/wirecon.conf}"
# shellcheck source=/dev/null
[[ -r "$CONF" ]] && source "$CONF"
: "${MTU_MIN:=1200}"
: "${MTU_MAX:=1420}"
target="${1:-}"
if [[ -z "$target" ]]; then
# extract Endpoint host from wg0.conf
target=$(awk -F'= *' '/^Endpoint/ {gsub(/:[0-9]+$/,"",$2); print $2; exit}' /etc/wireguard/wg0.conf 2>/dev/null || true)
fi
if [[ -z "$target" ]]; then
echo "usage: $0 <target_host>" >&2
exit 2
fi
echo "probing PMTU toward $target (range $MTU_MIN..$MTU_MAX)" >&2
# ICMP payload = mtu - 28 (IP+ICMP headers)
probe_ok() {
local mtu=$1 payload=$((mtu - 28))
ping -M do -s "$payload" -c 1 -W 2 -n "$target" >/dev/null 2>&1
}
lo=$MTU_MIN
hi=$MTU_MAX
best=0
if ! probe_ok "$lo"; then
echo "FAIL: even $lo does not pass DF — path is broken or host unreachable" >&2
exit 1
fi
best=$lo
while [[ $lo -le $hi ]]; do
mid=$(( (lo + hi) / 2 ))
if probe_ok "$mid"; then
best=$mid
lo=$(( mid + 1 ))
else
hi=$(( mid - 1 ))
fi
done
# WireGuard-over-IPv4-over-UDP overhead: 20 (IPv4) + 8 (UDP) + 32 (WG) = 60.
# Round down to 80 for safety margin (covers IPv6, extra hops).
wg_mtu=$(( best - 80 ))
echo "path MTU: $best"
echo "recommended WG MTU: $wg_mtu"
echo
echo "apply on both peers:"
echo " sudo sed -i 's/^MTU = .*/MTU = ${wg_mtu}/' /etc/wireguard/wg0.conf"
echo " sudo systemctl restart wg-quick@wg0"
+108
View File
@@ -0,0 +1,108 @@
#!/bin/bash
# wirecon-watchdog.sh — invoked periodically by wirecon-watchdog.timer.
#
# Checks handshake age and in-tunnel reachability. If either fails for
# FAIL_STREAK_THRESHOLD consecutive ticks, bounces the WG interface and
# logs the reason.
#
# All output goes to stdout/stderr; systemd routes it to journald under
# SYSLOG_IDENTIFIER=wirecon-watchdog.
set -uo pipefail
CONF="${WIRECON_CONF:-/etc/wirecon/wirecon.conf}"
# shellcheck source=/dev/null
[[ -r "$CONF" ]] && source "$CONF"
: "${WG_INTERFACE:=wg0}"
: "${PEER_IP:=10.42.0.1}"
: "${HANDSHAKE_MAX_AGE:=180}"
: "${PING_COUNT:=3}"
: "${PING_TIMEOUT:=2}"
: "${PING_LOSS_THRESHOLD:=3}"
: "${FAIL_STREAK_THRESHOLD:=2}"
: "${STATE_DIR:=/run/wirecon}"
mkdir -p "$STATE_DIR"
STREAK_FILE="$STATE_DIR/fail_streak"
LAST_RECONNECT_FILE="$STATE_DIR/last_reconnect"
# Structured single-line log. kv pairs => easy to grep / jq via journalctl.
log() {
local level="$1"; shift
printf 'level=%s ts=%s %s\n' "$level" "$(date -u +%FT%TZ)" "$*"
}
read_streak() {
[[ -r "$STREAK_FILE" ]] && cat "$STREAK_FILE" || echo 0
}
write_streak() { echo "$1" > "$STREAK_FILE"; }
handshake_age() {
# `wg show <iface> latest-handshakes` → "<peer_pubkey>\t<unix_ts>"
local ts now
ts=$(wg show "$WG_INTERFACE" latest-handshakes 2>/dev/null | awk 'NR==1{print $2}')
[[ -z "$ts" || "$ts" == "0" ]] && { echo -1; return; }
now=$(date +%s)
echo $((now - ts))
}
in_tunnel_ping() {
# returns number of lost pings (0..PING_COUNT)
local out loss
out=$(ping -I "$WG_INTERFACE" -c "$PING_COUNT" -W "$PING_TIMEOUT" -n "$PEER_IP" 2>&1) || true
loss=$(echo "$out" | awk -F',' '/packet loss/ {gsub(/[^0-9]/,"",$3); print $3}')
# loss is percent; convert to absolute count
[[ -z "$loss" ]] && { echo "$PING_COUNT"; return; }
echo $(( PING_COUNT * loss / 100 ))
}
reconnect() {
local reason="$1"
log warn "event=reconnect reason=\"$reason\" iface=$WG_INTERFACE"
date -u +%FT%TZ > "$LAST_RECONNECT_FILE"
# `wg-quick down` can fail if the iface is already gone — that's fine.
wg-quick down "$WG_INTERFACE" 2>&1 | sed 's/^/ down: /' || true
sleep 1
if wg-quick up "$WG_INTERFACE" 2>&1 | sed 's/^/ up: /'; then
log info "event=reconnect_ok iface=$WG_INTERFACE"
write_streak 0
else
log error "event=reconnect_failed iface=$WG_INTERFACE"
fi
}
# --- main check --------------------------------------------------------------
# 1. Is the interface even there?
if ! ip link show "$WG_INTERFACE" >/dev/null 2>&1; then
log error "event=iface_missing iface=$WG_INTERFACE"
reconnect "interface missing"
exit 0
fi
age=$(handshake_age)
lost=$(in_tunnel_ping)
streak=$(read_streak)
reason=""
if [[ "$age" -lt 0 ]]; then
reason="no handshake ever"
elif [[ "$age" -gt "$HANDSHAKE_MAX_AGE" ]]; then
reason="handshake stale age=${age}s max=${HANDSHAKE_MAX_AGE}s"
elif [[ "$lost" -ge "$PING_LOSS_THRESHOLD" ]]; then
reason="ping loss=${lost}/${PING_COUNT}"
fi
if [[ -n "$reason" ]]; then
streak=$((streak + 1))
write_streak "$streak"
log warn "event=check_fail streak=$streak/$FAIL_STREAK_THRESHOLD handshake_age=${age}s ping_lost=${lost}/${PING_COUNT} reason=\"$reason\""
if [[ "$streak" -ge "$FAIL_STREAK_THRESHOLD" ]]; then
reconnect "$reason (streak=$streak)"
fi
else
[[ "$streak" -ne 0 ]] && log info "event=recovered streak_was=$streak handshake_age=${age}s"
write_streak 0
log info "event=ok handshake_age=${age}s ping_lost=${lost}/${PING_COUNT}"
fi
+15
View File
@@ -0,0 +1,15 @@
# Append these lines to /etc/pve/lxc/<VMID>.conf on the Proxmox host to
# give an UNPRIVILEGED LXC access to /dev/net/tun (required for kernel
# WireGuard inside the container).
#
# After editing, restart the container:
# pct stop <VMID>
# pct start <VMID>
#
# Verify inside the LXC: ls -l /dev/net/tun → should exist, crw-rw-rw-
lxc.cgroup2.devices.allow: c 10:200 rwm
lxc.mount.entry: /dev/net/tun dev/net/tun none bind,create=file 0 0
# (Optional) Proxmox features line you may want in the same config file:
# features: keyctl=1,nesting=1