#!/bin/bash # 当前链路严重退化(18 次断开 / 20 次 quic 错误 / 公网 0/10)。 # 试试 ha-connections=1:少一条并发 QUIC 就少一份争抢。 set -u RELAY_DIR=/root/lanremote-relay UNIT=/etc/systemd/system/lanremote-tunnel.service URL=https://relay.xxmxxs.top echo "===== 1. 改成 ha=1 =====" cat > "$UNIT" <<'EOF' [Unit] Description=LanRemote Cloudflare 具名隧道 After=network-online.target lanremote-relay.service Wants=network-online.target lanremote-relay.service [Service] Type=simple # 协议:必须 QUIC(这条线路挡了 HTTP/2 的 TCP 7844) # 连接数:1 # 实测 4 条并发 QUIC 握手互相挤(只成功 1 条); # 链路退化时 2 条也会有一条一直失败重试、拖累另一条。 # 1 条最稳。丢冗余换稳定 —— 看门狗会在断的时候重启。 ExecStart=/usr/local/bin/cloudflared tunnel --no-autoupdate --edge-ip-version 4 --ha-connections 1 --retries 5 --config /root/.cloudflared/config.yml run lanremote Restart=always RestartSec=5 MemoryMax=128M StandardOutput=append:/root/lanremote-relay/tunnel.log StandardError=append:/root/lanremote-relay/tunnel.log [Install] WantedBy=multi-user.target EOF systemctl stop lanremote-tunnel 2>/dev/null || true pkill -f cloudflared 2>/dev/null || true sleep 3 rm -f "$RELAY_DIR/tunnel.log" systemctl daemon-reload systemctl start lanremote-tunnel echo " 已启动,等 40 秒建连接…" sleep 40 echo echo "===== 2. 连接情况 =====" echo -n " ha_connections = " curl -s --max-time 5 http://127.0.0.1:20241/metrics 2>/dev/null \ | grep '^cloudflared_tunnel_ha_connections' | awk '{print $2}' echo " 注册: $(grep -c 'Registered tunnel connection' $RELAY_DIR/tunnel.log 2>/dev/null || echo 0)" echo " 断开: $(grep -c 'Connection terminated' $RELAY_DIR/tunnel.log 2>/dev/null || echo 0)" echo echo "===== 3. 公网连打 15 次 =====" OK=0 for i in $(seq 1 15); do C=$(timeout 20 curl -4 -s -o /dev/null -w '%{http_code}' \ -H 'User-Agent: LanRemote/1.4.0' "$URL/v1/health" 2>/dev/null) [ "$C" = "200" ] && { OK=$((OK+1)); M="✓"; } || M="✗" printf " %2d: %s %s\n" "$i" "$M" "${C:-超时}" sleep 2 done PCT=$((OK * 100 / 15)) echo " 公网 $OK / 15 ($PCT%)" echo echo "===== 4. 再等 60 秒看长稳不稳 =====" sleep 60 OK2=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' "$URL/v1/health" 2>/dev/null) [ "$C" = "200" ] && OK2=$((OK2+1)) sleep 3 done echo " 第二轮 $OK2 / 10" echo -n " ha_connections = " curl -s --max-time 5 http://127.0.0.1:20241/metrics 2>/dev/null \ | grep '^cloudflared_tunnel_ha_connections' | awk '{print $2}' echo echo "===== 5. 结论 =====" echo " ha=1: 第一轮 $OK/15,第二轮 $OK2/10" if [ "$OK" -ge 12 ] || [ "$OK2" -ge 8 ]; then echo " ✓ 比 ha=2 好,定 ha=1" else echo " ✗ 还是不行 —— 这是链路本身的问题(运营商/信号)," echo " 不是配置能救的。看门狗会持续重启。" fi