#!/bin/bash # 彻底清空中转(设备 + 孤儿绑定)。 set -u BASE=http://127.0.0.1:8080 KEY=$(cat /root/lanremote-relay/data/admin_key.txt 2>/dev/null) STATE=/root/lanremote-relay/data/relay_state.json echo "===== 清空之前 =====" python3 - "$STATE" <<'PY' import json, sys try: d = json.load(open(sys.argv[1])) except Exception: print(" 读不到状态文件") raise SystemExit print(f" 绑定 {len(d.get('codes', []))} 条") PY curl -s --max-time 8 "$BASE/v1/health" > /tmp/pre.json 2>/dev/null python3 -c " import json d=json.load(open('/tmp/pre.json')) print(f\" 设备 {len(d.get('devices',[]))} 台,会话 {d.get('sessions')}\") " 2>/dev/null echo echo "===== 调 delete_all(管理钥匙走 X-Admin 头)=====" RESULT=$(curl -s --max-time 15 -X POST "$BASE/v1/admin/delete_all" \ -H "X-Admin: $KEY" -H 'Content-Type: application/json' -d '{}') echo " $RESULT" echo echo "===== 清空之后 =====" python3 - "$STATE" <<'PY' import json, sys d = json.load(open(sys.argv[1])) codes = d.get("codes", []) print(f" 绑定 {len(codes)} 条" + ("" if not codes else f" 残留: {codes}")) PY curl -s --max-time 8 "$BASE/v1/health" > /tmp/post.json 2>/dev/null python3 -c " import json d=json.load(open('/tmp/post.json')) print(f\" 设备 {len(d.get('devices',[]))} 台,会话 {d.get('sessions')},内存 {d.get('memoryKB',0)//1024} MB\") " 2>/dev/null echo echo "===== 789113 能不能用 =====" R=$(curl -s --max-time 8 -X POST "$BASE/v1/agent/register" \ -H 'Content-Type: application/json' \ -d '{"deviceId":"probe-final-check","code":"789113","name":"探测","version":"1.4.0"}') echo " $R" | head -c 140 echo if echo "$R" | grep -q '"ok": *true'; then echo " ✓ 789113 空闲可用" curl -s -o /dev/null -X POST "$BASE/v1/admin/forget" \ -H "X-Admin: $KEY" -H 'Content-Type: application/json' \ -d '{"code":"789113"}' echo " (探测记录已清掉,恢复空闲)" else echo " ✗ 还被占" fi echo echo "===== 最终 =====" printf " %-30s %-10s %s\n" "服务" "运行" "自启" for s in lanremote-relay lanremote-tunnel lanremote-watchdog.timer; do printf " %-30s %-10s %s\n" "$s" \ "$(systemctl is-active $s 2>/dev/null)" \ "$(systemctl is-enabled $s 2>/dev/null)" done echo echo " 公网地址: $(cat /root/lanremote-relay/public_url.txt)" echo " 可达性: HTTP $(timeout 20 curl -4 -s -o /dev/null -w '%{http_code}' -H 'User-Agent: LanRemote/1.4.0' https://relay.xxmxxs.top/v1/health 2>/dev/null)" echo " 连接数: $(curl -s --max-time 5 http://127.0.0.1:20241/metrics 2>/dev/null | grep '^cloudflared_tunnel_ha_connections' | awk '{print $2}')" echo " 内存: $(free -m | awk 'NR==2{printf "已用 %s MB / 可用 %s MB", $3, $7}')"