sh2/d2ypp2/scripts/linux/wireguard_watch.sh
eric 3bc2cf6a1e Harden WG+Neko install defaults, Neko proxy egress probe, and YouTube multi-Pro UI.
WireGuard overlay persists remote NAT1TO1 at install, opens WebRTC via ShellCrash trusted CIDRs, and reconfigures Neko after wg-quick restarts. Neko proxy probes Google before proxycheap. Console defaults to multi-channel Pro; docs updated.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-26 06:54:14 -05:00

137 lines
4.7 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 }
'
}
refresh_neko_after_wireguard_restart() {
local root="${LIVE_STACK_ROOT:-/opt/live/d2ypp}"
local ctl="$root/scripts/linux/service_ctl.sh"
local common="$root/scripts/linux/lib/common.sh"
[ -f "$common" ] || return 0
# shellcheck source=/dev/null
. "$common"
STACK_ENV="${LIVE_STACK_ENV:-$root/config/system-stack.env}"
ROOT_DIR="$root"
sync_neko_wireguard_remote_access 1 || return 0
ensure_neko_webrtc_ports_for_trusted_cidrs >/dev/null 2>&1 || true
[ -f "$ctl" ] || return 0
command -v docker >/dev/null 2>&1 || return 0
docker ps --format '{{.Names}}' 2>/dev/null | grep -q '^live-neko-' || return 0
bash "$ctl" neko reconfigure >/dev/null 2>&1 || log "WARN: Neko reconfigure after WireGuard restart failed"
}
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
refresh_neko_after_wireguard_restart || true
}
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