30 lines
1.3 KiB
Bash
Executable file
30 lines
1.3 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# Keeps a free Pinggy tunnel open to the read-only web server and restarts it when it stops.
|
|
#
|
|
# scripts/tunnel.sh [port] (default 3056, the BENCH_READONLY=1 server)
|
|
#
|
|
# Free tunnels last 60 minutes. Each time a tunnel opens, the script appends a line
|
|
# "<time> URL <address>" to data/tunnel.log and writes the address to data/tunnel-url.txt.
|
|
# Run it in tmux: tmux new -d -s bench-tunnel 'scripts/tunnel.sh'
|
|
set -u
|
|
PORT="${1:-3056}"
|
|
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
LOG="$ROOT/data/tunnel.log"
|
|
URLFILE="$ROOT/data/tunnel-url.txt"
|
|
log() { printf '%s %s\n' "$(date '+%F %T')" "$*" | tee -a "$LOG"; }
|
|
|
|
log "starting, forwarding to localhost:$PORT"
|
|
while true; do
|
|
log "opening tunnel"
|
|
# -T and an endless stdin make ssh print the same text as it does outside a terminal, which holds the address.
|
|
tail -f /dev/null | ssh -T -p 443 -o StrictHostKeyChecking=accept-new -o ServerAliveInterval=30 -o ServerAliveCountMax=3 \
|
|
-o ExitOnForwardFailure=yes -R0:localhost:"$PORT" free.pinggy.io 2>&1 |
|
|
while IFS= read -r line; do
|
|
if [[ "$line" =~ (https://[a-z0-9-]+\.free\.pinggy\.net) ]]; then
|
|
echo "${BASH_REMATCH[1]}" > "$URLFILE"
|
|
log "URL ${BASH_REMATCH[1]}"
|
|
fi
|
|
done
|
|
log "tunnel stopped, reopening in 5 s"
|
|
sleep 5
|
|
done
|