sh2/d2ypp2/scripts/linux/neko_ip_watch.sh
eric 278b48497e Add YouTube relay log tooling and optional hub service controls.
This captures the current live stack updates across the API, Linux service scripts, tests, diagnostics, and exported console assets so the Ubuntu deployment can pull a consistent test3 build.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-30 05:10:09 -05:00

100 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_ACCESS_MODE NEKO_WEBRTC_NAT1TO1 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
docker ps --format '{{.Names}}' 2>/dev/null | grep -E '^live-neko-[0-9]+$' | sort -V | while read -r name; do
[ -n "$name" ] || continue
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 target 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-[0-9]+$'; then
return 0
fi
target="$(resolve_neko_nat1to1 || true)"
if [ -z "$target" ]; then
return 0
fi
active=""
for active_value in $(running_neko_nat1to1_values || true); do
[ -n "$active_value" ] || continue
if [ "$active_value" != "$target" ]; 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 NAT1TO1 drift: container=${active} target=${target}; reconfiguring Neko"
if bash "$SERVICE_CTL" neko reconfigure; then
log "Neko reconfigure finished; refreshed NAT1TO1=${target}"
else
log "WARN: Neko reconfigure failed after NAT1TO1 drift"
fi
sleep "$cooldown"
}
main() {
load_env
reload_watch_env
log "Watching Neko access IP changes"
while true; do
reload_watch_env
maybe_refresh_neko_for_ip_change
sleep "$(normalize_interval "${NEKO_IP_WATCH_INTERVAL:-15}")"
done
}
main "$@"