joy-cli/scripts/manhuang/check-tor-scout.sh
feibisi 2c6281d041 feat(manhuang): supply-chain dashboard, probes, and side-cache data
Expand manhuang collect/UI (tabs, markets, guishi/quote probes), add
configs and launchd helpers, ops handbooks, and scripts for channel
validation and price/threat enrichment. Keys stay in keys.env.example only.
2026-07-13 02:08:29 +08:00

68 lines
2.4 KiB
Bash
Executable file
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# scout Tor 电路快检(不改配置;可选 --restart
# 用法:
# bash scripts/manhuang/check-tor-scout.sh
# bash scripts/manhuang/check-tor-scout.sh --restart
set -euo pipefail
RESTART=0
[[ "${1:-}" == "--restart" ]] && RESTART=1
log() { echo "[tor-scout] $*"; }
if ! command -v ssh >/dev/null 2>&1; then
log "no ssh"
exit 1
fi
_check() {
ssh -o BatchMode=yes -o ConnectTimeout=12 scout \
'SOCKS=0; CIRCUIT=0; IP=""; ss -lntp 2>/dev/null | grep -q 9050 && SOCKS=1; if [ "$SOCKS" = 1 ]; then body=$(curl -sS --max-time 20 --socks5-hostname 127.0.0.1:9050 https://check.torproject.org/api/ip 2>/dev/null || true); echo "$body" | grep -q "\"IsTor\":true" && CIRCUIT=1; IP=$(echo "$body" | python3 -c "import sys,json;print(json.load(sys.stdin).get(\"IP\",\"\"))" 2>/dev/null || true); fi; ACTIVE=$(systemctl is-active tor 2>/dev/null || echo unknown); echo "SOCKS=$SOCKS CIRCUIT=$CIRCUIT ACTIVE=$ACTIVE IP=$IP"' \
2>&1 || true
}
out=$(_check)
log "$out"
SOCKS=$(echo "$out" | grep -oE 'SOCKS=[01]' | tail -1 | cut -d= -f2)
CIRCUIT=$(echo "$out" | grep -oE 'CIRCUIT=[01]' | tail -1 | cut -d= -f2)
_write_hint() {
local hint="$1"
python3 -c "
import json, time
from pathlib import Path
p = Path.home() / '.cache/manhuang/pipeline-last.json'
d = json.loads(p.read_text()) if p.is_file() else {}
d['tor_hint'] = '$hint'
d['tor_checked_ts'] = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(json.dumps(d, ensure_ascii=False, indent=2) + '\n')
" 2>/dev/null || true
}
if [[ "${CIRCUIT:-0}" == "1" ]]; then
log "ok: circuit up"
_write_hint circuit_ok
exit 0
fi
log "fail: circuit down (socks=${SOCKS:-?})"
if [[ "$RESTART" == "1" ]]; then
log "restarting tor on scout…"
ssh -o BatchMode=yes -o ConnectTimeout=15 scout \
'sudo -n systemctl restart tor 2>/dev/null || sudo -n service tor restart 2>/dev/null || systemctl --user restart tor 2>/dev/null; sleep 4; curl -sS --max-time 25 --socks5-hostname 127.0.0.1:9050 https://check.torproject.org/api/ip || true' \
2>&1 | tail -8
log "re-check…"
out=$(_check)
log "$out"
CIRCUIT=$(echo "$out" | grep -oE 'CIRCUIT=[01]' | tail -1 | cut -d= -f2)
if [[ "${CIRCUIT:-0}" == "1" ]]; then
log "ok after restart"
_write_hint circuit_ok
exit 0
fi
fi
log "hint: bash $0 --restart # or scout: Bridge/Snowflake"
_write_hint tor_down
exit 1