#!/bin/bash # 把中转的状态文件清空 —— 让用户的电脑能干净地认领 789113。 # # 为什么设备列表抹干净了 789113 还被占: # codes(deviceId -> 配对码)是持久化在 relay_state.json 里的, # 里面堆积了我早期所有测试留下的绑定。有些绑定的设备早就没了, # 但码还占着。而 /v1/admin/forget 只能按"当前在线设备列表"里的码来抹, # 抹不到这些孤儿绑定。 # # 做法:停服务 -> 清空 codes -> 起服务。 set -u STATE=/root/lanremote-relay/data/relay_state.json echo "===== 1. 清空之前有多少绑定 =====" python3 - <<'PY' import json try: d = json.load(open('/root/lanremote-relay/data/relay_state.json')) except Exception as exc: print(f" 读不到: {exc}") raise SystemExit codes = d.get("codes", []) print(f" {len(codes)} 条绑定") has = [c for c in codes if str(c.get("code")) == "789113"] if has: print(f" 789113 绑在: {has[0].get('deviceId')}") PY echo echo "===== 2. 备份 + 停服务 =====" cp -f "$STATE" "${STATE}.bak" 2>/dev/null || true ls -la "${STATE}.bak" | sed 's/^/ /' systemctl stop lanremote-relay sleep 2 echo " 服务: $(systemctl is-active lanremote-relay)" echo echo "===== 3. 清空绑定(保留其他设置字段)=====" python3 - <<'PY' import json path = "/root/lanremote-relay/data/relay_state.json" try: d = json.load(open(path)) except Exception: d = {} before = len(d.get("codes", [])) d["codes"] = [] # 顺带清掉退役会话记录 if "retired" in d: d["retired"] = {} json.dump(d, open(path, "w")) print(f" 清掉 {before} 条绑定,现在是 {len(d.get('codes', []))} 条") print(f" 保留的字段: {sorted(d.keys())}") PY echo echo "===== 4. 起服务 =====" systemctl start lanremote-relay sleep 4 echo " 服务: $(systemctl is-active lanremote-relay)" echo echo "===== 5. 验证 789113 空闲 =====" R=$(curl -s --max-time 8 -X POST http://127.0.0.1:8080/v1/agent/register \ -H 'Content-Type: application/json' \ -d '{"deviceId":"probe-clean","code":"789113","name":"探测","version":"1.4.0"}') echo " $R" | head -c 180 echo if echo "$R" | grep -q '"ok": *true'; then echo " ✓ 789113 空闲可用" KEY=$(cat /root/lanremote-relay/data/admin_key.txt) curl -s -o /dev/null -X POST http://127.0.0.1:8080/v1/admin/forget \ -H "X-Admin: $KEY" -H 'Content-Type: application/json' \ -d '{"code":"789113"}' echo " (探测记录已清掉,789113 恢复空闲)" else echo " ✗ 还被占" fi echo echo "===== 6. 最终状态 =====" python3 - <<'PY' import json d = json.load(open('/root/lanremote-relay/data/relay_state.json')) print(f" 绑定数: {len(d.get('codes', []))}") PY curl -s --max-time 8 http://127.0.0.1:8080/v1/health > /tmp/f.json 2>/dev/null python3 - <<'PY' import json try: d = json.load(open('/tmp/f.json')) except Exception: raise SystemExit print(f" 在线设备 {len(d.get('devices', []))} 台 会话 {d.get('sessions')} " f"内存 {d.get('memoryKB', 0) // 1024} MB") PY echo for s in lanremote-relay lanremote-tunnel lanremote-watchdog.timer; do printf " %-30s %-9s %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 2>/dev/null)" 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)"