sh2/d2ypp2/scripts/linux/wireguard_watch.sh
eric 0517be0ce2 Harden deploy for WireGuard ShellCrash access and multi-arch installs.
Allow trusted CIDRs (Docker/LAN/10.88.0.0/24) past ShellCrash WAN reject so the dashboard works over WG, unify CPU arch detection for amd64/arm64/armv7, and document install-time LIVE_HTTP_PROXY plus Neko NAT1TO1 fallbacks.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-31 03:22:22 -05:00

119 lines
4.0 KiB
Bash

#!/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 <iface> 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"
elif command -v wg-quick >/dev/null 2>&1 && [ -f "/etc/wireguard/${iface}.conf" ]; then
log "WARN: $iface unit missing; falling back to wg-quick down/up"
wg-quick down "$iface" >/dev/null 2>&1 || true
wg-quick up "$iface"
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