Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/interplane-forge/blob/main/infra/spark/forged-tunnel.sh # interplane-forge INTERPLANE Forge Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `interplane-forge / infra / spark / forged-tunnel.sh` 63 lines ยท 3367 bytes Raw Blame History 1 #!/usr/bin/env bash 2 # spark -> hub reverse tunnel for forged: 3 # ssh -N -R 127.0.0.1:18480:127.0.0.1:18480 hub 4 # In production it runs as the systemd user unit interplane-forged-tunnel.service 5 # (infra/spark/interplane-forged-tunnel.service, Restart=always); when that unit is loaded, start, 6 # stop and status act through systemctl --user on it. Without the unit: tmux session `forged-tunnel`. 7 # Same mechanism and ssh options as the existing `rad-hub-tunnel` session (which carries 8 # 18776 -> radicle-node 8776 for the hub's radicle-node). It is a separate session on purpose: 9 # adding a -R to rad-hub-tunnel would mean restarting it and dropping the hub<->spark Radicle 10 # link; this one starts, stops and rolls back on its own. The hub end binds loopback only. 11 # 12 # Usage: infra/spark/forged-tunnel.sh start|stop|status 13 set -u 14 SESSION=forged-tunnel 15 PORT=18480 16 CPUS=${FORGED_CPUS:-4-5} 17 SELF=$(cd "$(dirname "$0")" && pwd)/$(basename "$0") 18 UNIT=${FORGED_TUNNEL_UNIT:-interplane-forged-tunnel.service} 19 running() { tmux has-session -t "=$SESSION" 2>/dev/null; } 20 under_unit() { 21 command -v systemctl >/dev/null 2>&1 && 22 [ "$(systemctl --user show -p LoadState --value "$UNIT" 2>/dev/null)" = loaded ] 23 } 24 hub_up() { ssh -o BatchMode=yes -o ConnectTimeout=5 hub "ss -ltnH 'sport = :$PORT' | grep -q 127.0.0.1:$PORT"; } 25 26 case "${1:-}" in 27 _loop) 28 while true; do 29 taskset -c "$CPUS" nice -n 10 ssh -N -o BatchMode=yes -o ExitOnForwardFailure=yes \ 30 -o ServerAliveInterval=30 -o ServerAliveCountMax=3 \ 31 -R 127.0.0.1:$PORT:127.0.0.1:$PORT hub 32 echo "tunnel exited $? at $(date -u +%FT%TZ); retry in 10s"; sleep 10 33 done ;; 34 start) 35 if under_unit; then 36 running && { echo "tmux session $SESSION is running too; stop it first" >&2; exit 1; } 37 systemctl --user start "$UNIT" || exit 1 38 for i in $(seq 1 20); do hub_up && { echo "hub 127.0.0.1:$PORT is up ($UNIT)"; exit 0; }; sleep 1; done 39 echo "hub 127.0.0.1:$PORT not listening after 20s; journal:" >&2 40 journalctl --user -u "$UNIT" -n 5 --no-pager >&2; exit 1 41 fi 42 running && { echo "tmux session $SESSION already running"; exit 0; } 43 tmux new-session -d -s "$SESSION" "$(printf '%q ' env FORGED_CPUS="$CPUS" bash "$SELF" _loop)" # tmux does not inherit our env 44 for i in $(seq 1 20); do 45 hub_up && { echo "hub 127.0.0.1:$PORT is up"; exit 0; } 46 sleep 1 47 done 48 echo "hub 127.0.0.1:$PORT not listening after 20s; pane:" >&2 49 tmux capture-pane -pt "=$SESSION" | grep -v '^$' | tail -5 >&2; exit 1 ;; 50 stop) 51 under_unit && systemctl --user stop "$UNIT" 52 running && tmux kill-session -t "=$SESSION" 53 pkill -u "$(id -u)" -f -- "-R 127.0.0.1:$PORT:127.0.0.1:$PORT hub" 2>/dev/null 54 echo "forged-tunnel stopped" ;; 55 status) 56 if under_unit; then 57 echo "systemd $UNIT: $(systemctl --user is-active "$UNIT") ($(systemctl --user is-enabled "$UNIT")), ssh $(systemctl --user show -p MainPID --value "$UNIT"), restarts $(systemctl --user show -p NRestarts --value "$UNIT")" 58 fi 59 running && echo "tmux $SESSION: up" || echo "tmux $SESSION: down" 60 ssh -o BatchMode=yes -o ConnectTimeout=5 hub "ss -ltnH 'sport = :$PORT'" | grep -q "127.0.0.1:$PORT" \ 61 && echo "hub 127.0.0.1:$PORT: listening" || { echo "hub 127.0.0.1:$PORT: closed"; exit 1; } ;; 62 *) echo "usage: $0 start|stop|status" >&2; exit 2 ;; 63 esac