#!/bin/bash # 修:重启中转会连带把隧道停掉,而且不会自己起来。 # # 原因: # 隧道的 unit 里写了 # Requires=lanremote-relay.service # Requires 是**双向**的 —— 被依赖的服务一停,依赖它的也会被停。 # 于是重启中转时隧道被停,而没有任何东西会再把它拉起来, # 公网入口就一直 530。 # # 修法: # 改成 Wants= + After= # Wants 只表示"希望它也起来",单向,不会因为对方停了而停 # After 只表示启动顺序 # 这样中转重启时隧道继续跑(中间那一两秒会 502,随后自动恢复), # 隧道的 Restart=always 也能管住自己的进程。 set -u UNIT=/etc/systemd/system/lanremote-tunnel.service echo "===== 1. 改之前 =====" grep -E '^(Requires|Wants|After|Description)' "$UNIT" | sed 's/^/ /' echo echo "===== 2. 改成单向依赖 =====" python3 - <<'PY' from pathlib import Path path = Path("/etc/systemd/system/lanremote-tunnel.service") text = path.read_text() text = text.replace("Requires=lanremote-relay.service\n", "") if "Wants=network-online.target lanremote-relay.service" not in text: text = text.replace("Wants=network-online.target", "Wants=network-online.target lanremote-relay.service") if "After=network-online.target lanremote-relay.service" not in text: text = text.replace( "After=network-online.target", "After=network-online.target lanremote-relay.service") # 补一句说明,免得以后有人又改回去 if "# 注意:这里用 Wants 不用 Requires" not in text: text = text.replace( "[Unit]\n", "[Unit]\n# 注意:这里用 Wants 不用 Requires。\n" "# Requires 是双向的 —— 重启中转会把隧道一起停掉,\n" "# 而且没有东西会再把它拉起来,公网入口就一直 530。\n", 1) path.write_text(text) print(" 已改") PY echo echo "===== 3. 改之后 =====" grep -E '^(Requires|Wants|After)' "$UNIT" | sed 's/^/ /' echo echo "===== 4. 重新加载并启动 =====" systemctl daemon-reload systemctl restart lanremote-tunnel sleep 35 echo " 隧道: $(systemctl is-active lanremote-tunnel)" echo " 中转: $(systemctl is-active lanremote-relay)" echo echo " 已注册连接: $(grep -c 'Registered tunnel connection' /root/lanremote-relay/tunnel.log 2>/dev/null || echo 0)" echo echo "===== 5. 验证:重启中转,隧道应该活着 =====" BEFORE=$(systemctl is-active lanremote-tunnel) systemctl restart lanremote-relay sleep 8 AFTER=$(systemctl is-active lanremote-tunnel) echo " 重启中转前隧道: $BEFORE" echo " 重启中转后隧道: $AFTER" if [ "$AFTER" = "active" ]; then echo " ✓ 修好了 —— 中转重启不再连带停掉隧道" else echo " ✗ 还是被连带停掉了" fi echo echo "===== 6. 公网可达性(连打 10 次)=====" sleep 20 OK=0 for i in $(seq 1 10); do C=$(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) [ "$C" = "200" ] && { OK=$((OK + 1)); M="✓"; } || M="✗" printf " %2d: %s HTTP %s\n" "$i" "$M" "${C:-超时}" sleep 2 done echo " 成功 $OK / 10" echo echo "===== 7. 最终状态 =====" 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 " 绑定数: $(python3 -c 'import json;print(len(json.load(open(\"/root/lanremote-relay/data/relay_state.json\")).get(\"codes\",[])))' 2>/dev/null)"