PeerCortex/scripts/tunnel-cleanup.sh
Rene Fichtmueller 2766872aad snapshot: preserve production-only changes (Izzy PDF export, adoption tracker, audit scripts)
Working tree was 3+ months ahead of git in uncommitted local edits, sitting
on a detached HEAD at d3611a8 (2026-04-09), never reconciled with main.
Committing as-is on its own branch, without touching main or the detached
HEAD, to stop these files existing only on this one server:

- public/index.html + server.js: PDF export add-on gated to client Izzy,
  full Adoption Tracker overlay (ASPA + IPv6 tabs) -- neither exists
  anywhere in git history on any branch until now
- audit/: daily/rotating audit scripts + email report sender, previously
  untracked
- scripts/: tunnel-cleanup.sh, refresh-peeringdb.sh, previously untracked
- deploy-from-scp.sh, webhook-subs.json, hijack-alerts.json,
  aspa-adoption-history.json: previously untracked runtime/deploy state
- public/public/: duplicate mirror of the HTML variants found alongside
  the real public/ dir, preserved as found

No reconciliation with main attempted here -- that needs a deliberate pass,
not a side effect of a backup commit.
2026-07-16 06:42:03 +00:00

30 lines
1.0 KiB
Bash
Executable File

#!/bin/bash
# Auto-heal Cloudflare tunnel: clean stale connectors from foreign IPs
TUNNEL=3262c64b-51d5-479f-ad26-a9925b705bd3
LOG=/var/log/peercortex/tunnel-cleanup.log
mkdir -p /var/log/peercortex
# Restart cloudflared if not running
if ! systemctl is-active --quiet cloudflared; then
echo "[$(date +%Y-%m-%dT%H:%M:%S)] cloudflared not running — starting" >> "$LOG"
systemctl start cloudflared
sleep 5
fi
MY_IP=$(curl -s --max-time 5 https://ifconfig.me 2>/dev/null)
[ -z "$MY_IP" ] && exit 0
# Connector rows start with a UUID (36 chars: 8-4-4-4-12)
# Column 5 in connector rows = ORIGIN IP
STALE=$(cloudflared tunnel info "$TUNNEL" 2>/dev/null | \
grep -E '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}' | \
awk -v myip="$MY_IP" '$5 != myip {print $5}')
if [ -n "$STALE" ]; then
echo "[$(date +%Y-%m-%dT%H:%M:%S)] Stale connectors from $STALE — cleaning" >> "$LOG"
cloudflared tunnel cleanup "$TUNNEL" >> "$LOG" 2>&1
systemctl restart cloudflared
echo "[$(date +%Y-%m-%dT%H:%M:%S)] Done" >> "$LOG"
fi