#!/usr/bin/env bash # Restart stale WireGuard client interfaces so NAT keepalive does not silently die. set -euo pipefail ROOT_DIR="${LIVE_STACK_ROOT:-/opt/live/d2ypp}" STACK_ENV="${LIVE_STACK_ENV:-$ROOT_DIR/config/system-stack.env}" STATE_DIR="${WIREGUARD_WATCH_STATE_DIR:-/run}" log() { printf '[wireguard-watch] %s\n' "$*" } positive_int() { case "${1:-}" in ''|*[!0-9]*) return 1 ;; *) [ "$1" -gt 0 ] ;; esac } reload_env() { if [ -f "$STACK_ENV" ]; then set -a # shellcheck source=/dev/null . "$STACK_ENV" || true set +a fi WIREGUARD_WATCH_INTERVAL="${WIREGUARD_WATCH_INTERVAL:-30}" WIREGUARD_STALE_SECONDS="${WIREGUARD_STALE_SECONDS:-300}" WIREGUARD_RESTART_COOLDOWN="${WIREGUARD_RESTART_COOLDOWN:-300}" positive_int "$WIREGUARD_WATCH_INTERVAL" || WIREGUARD_WATCH_INTERVAL=30 positive_int "$WIREGUARD_STALE_SECONDS" || WIREGUARD_STALE_SECONDS=300 positive_int "$WIREGUARD_RESTART_COOLDOWN" || WIREGUARD_RESTART_COOLDOWN=300 [ "$WIREGUARD_WATCH_INTERVAL" -lt 10 ] && WIREGUARD_WATCH_INTERVAL=10 [ "$WIREGUARD_STALE_SECONDS" -lt 60 ] && WIREGUARD_STALE_SECONDS=60 [ "$WIREGUARD_RESTART_COOLDOWN" -lt "$WIREGUARD_STALE_SECONDS" ] && WIREGUARD_RESTART_COOLDOWN="$WIREGUARD_STALE_SECONDS" return 0 } interface_has_keepalive_peer() { local iface="$1" wg show "$iface" dump 2>/dev/null | awk ' NR > 1 { # "wg show dump" omits the interface column; "wg show all dump" # includes it. Accept either shape so this stays correct if the command # changes during maintenance. ka = (NF >= 9) ? $9 : $8 if (ka != "" && ka != "off" && ka != "0") found = 1 } END { exit found ? 0 : 1 } ' } interface_is_stale() { local iface="$1" now="$2" stale="$3" wg show "$iface" dump 2>/dev/null | awk -v now="$now" -v stale="$stale" ' NR > 1 { latest = (NF >= 9) ? $6 : $5 ka = (NF >= 9) ? $9 : $8 if (ka != "" && ka != "off" && ka != "0" && (latest == 0 || now - latest > stale)) bad = 1 } END { exit bad ? 0 : 1 } ' } restart_interface() { local iface="$1" if systemctl list-unit-files "wg-quick@${iface}.service" --no-legend 2>/dev/null | grep -q .; then systemctl restart "wg-quick@${iface}.service" elif systemctl list-units "wg-quick@${iface}.service" --no-legend 2>/dev/null | grep -q .; then systemctl restart "wg-quick@${iface}.service" else log "WARN: $iface is stale but wg-quick@${iface}.service is not installed" return 1 fi } watch_once() { local now iface last last_restart_var last_restart command -v wg >/dev/null 2>&1 || return 0 command -v systemctl >/dev/null 2>&1 || return 0 now="$(date +%s)" for iface in $(wg show interfaces 2>/dev/null || true); do [ -n "$iface" ] || continue interface_has_keepalive_peer "$iface" || continue interface_is_stale "$iface" "$now" "$WIREGUARD_STALE_SECONDS" || continue last_restart_var="$STATE_DIR/live-wireguard-watch-${iface}.last" last_restart="$(cat "$last_restart_var" 2>/dev/null || echo 0)" positive_int "$last_restart" || last_restart=0 if [ $((now - last_restart)) -lt "$WIREGUARD_RESTART_COOLDOWN" ]; then continue fi last="$(wg show "$iface" latest-handshakes 2>/dev/null | awk 'NR == 1 {print $2}')" log "Restarting stale $iface; latest_handshake=${last:-0}; stale_seconds=$WIREGUARD_STALE_SECONDS" if restart_interface "$iface"; then mkdir -p "$STATE_DIR" 2>/dev/null || true date +%s >"$last_restart_var" fi done } main() { log "Watching WireGuard keepalive health" while true; do reload_env if [ "${ENABLE_WIREGUARD_WATCH:-1}" = "1" ]; then watch_once || true fi sleep "$WIREGUARD_WATCH_INTERVAL" done } if [ "${WIREGUARD_WATCH_RUN_FOREVER:-1}" != "0" ]; then main "$@" fi