Files
gitlab-instance-0a899031_sh/d2ypp2/scripts/linux/neko_ip_watch.sh
root fa422fb7b8 s
2026-05-18 13:49:53 +00:00

102 lines
2.3 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
SELF_DIR="$(cd "$(dirname "$0")" && pwd)"
INSTALL_TAG="${INSTALL_TAG:-neko-ip-watch}"
. "$SELF_DIR/lib/common.sh"
reload_watch_env() {
unset ENABLE_NEKO NEKO_IP_WATCH_INTERVAL
if [ -f "$STACK_ENV" ]; then
set -a
. "$STACK_ENV"
set +a
fi
ENABLE_NEKO="${ENABLE_NEKO:-1}"
NEKO_IP_WATCH_INTERVAL="${NEKO_IP_WATCH_INTERVAL:-15}"
}
normalize_interval() {
local value="${1:-15}"
case "$value" in
''|*[!0-9]*)
value=15
;;
esac
if [ "$value" -lt 5 ]; then
value=5
fi
printf '%s\n' "$value"
}
running_neko_nat1to1_values() {
local name value
for name in live-neko-1 live-neko-2 live-neko-3; do
if ! docker ps --format '{{.Names}}' 2>/dev/null | grep -qx "$name"; then
continue
fi
value="$(
docker inspect "$name" --format '{{range .Config.Env}}{{println .}}{{end}}' 2>/dev/null |
awk -F= '/^NEKO_WEBRTC_NAT1TO1=/{print $2; exit}'
)"
if [ -n "$value" ]; then
printf '%s\n' "$value"
fi
done
}
maybe_refresh_neko_for_ip_change() {
local preferred active interval cooldown active_value
if [ "${ENABLE_NEKO:-1}" != "1" ] || ! have docker; then
return 0
fi
if ! docker ps --format '{{.Names}}' 2>/dev/null | grep -Eq '^live-neko-[123]$'; then
return 0
fi
preferred="$(detect_preferred_lan_ip || true)"
if [ -z "$preferred" ]; then
return 0
fi
active=""
for active_value in $(running_neko_nat1to1_values || true); do
[ -n "$active_value" ] || continue
if [ "$active_value" != "$preferred" ]; then
active="$active_value"
break
fi
done
if [ -z "$active" ]; then
return 0
fi
interval="$(normalize_interval "${NEKO_IP_WATCH_INTERVAL:-15}")"
cooldown="$interval"
if [ "$cooldown" -lt 15 ]; then
cooldown=15
fi
log "Detected Neko LAN IP drift: container=${active} current=${preferred}; restarting Neko"
if bash "$SERVICE_CTL" neko restart; then
log "Neko restart finished; refreshed NAT1TO1=${preferred}"
else
log "WARN: Neko restart failed after LAN IP drift"
fi
sleep "$cooldown"
}
main() {
load_env
reload_watch_env
log "Watching Neko LAN IP changes"
while true; do
reload_watch_env
maybe_refresh_neko_for_ip_change
sleep "$(normalize_interval "${NEKO_IP_WATCH_INTERVAL:-15}")"
done
}
main "$@"