Files
collector/tunnel.sh
2026-10-06 23:43:30 -07:00

31 lines
825 B
Bash

#!/bin/bash
# Start a Cloudflare quick tunnel for The Collector, extract the trycloudflare
# URL, and self-register it with the app (/api/config). Blocks while the tunnel
# runs (intended for a systemd service).
LOG=/opt/collector/tunnel.log
APP=http://127.0.0.1:8766
: > "$LOG"
/usr/local/bin/cloudflared tunnel --url http://localhost:8766 --no-autoupdate >> "$LOG" 2>&1 &
CF_PID=$!
trap 'kill $CF_PID 2>/dev/null' EXIT
URL=""
for _ in $(seq 1 45); do
URL=$(grep -oE 'https://[a-z0-9-]+\.trycloudflare\.com' "$LOG" | head -1)
[ -n "$URL" ] && break
sleep 2
done
if [ -n "$URL" ]; then
echo "TUNNEL_URL=$URL"
curl -s -X POST "$APP/api/config" -H 'Content-Type: application/json' \
-d "{\"public_base_url\":\"$URL\"}" >/dev/null 2>&1
echo "registered with app"
else
echo "TUNNEL_FAILED"
fi
wait $CF_PID