#!/bin/bash # 定稿:ha-connections=2,并做长时间稳定性验证。 # # 实测(见 /root/ha_result.txt): # ha=4 -> 9/10,而且只成功注册 1 条连接(其余一直在握手超时) # ha=2 -> 10/10,2 条都注册上 # ha=1 -> 10/10,1 条 # # 原因很可能是这条 4G 线路对并发 UDP 有节制: # 4 条 QUIC 同时握手会互相挤,反而条条失败。 # ha=2 是甜点:够稳,又还有冗余。 set -u RELAY_DIR=/root/lanremote-relay UNIT=/etc/systemd/system/lanremote-tunnel.service echo "===== 1. 写入正式配置(ha=2)=====" cat > "$UNIT" <<'EOF' [Unit] Description=LanRemote Cloudflare 具名隧道 After=network-online.target lanremote-relay.service # 用 Wants 不用 Requires:Requires 是双向的, # 重启中转会把隧道一起停掉,而且没东西会再拉起来。 Wants=network-online.target lanremote-relay.service [Service] Type=simple # 协议:必须 QUIC(默认)。 # 实测这条线路把 HTTP/2 用的 TCP 7844 挡了(TLS 握手超时), # 而 QUIC 走 UDP 7844 是通的。 # 连接数:ha-connections=2。 # 实测 4 条并发 QUIC 握手会互相挤,只成功 1 条、可用率 9/10; # 降到 2 条后 10/10,且仍有冗余(连两个不同边缘节点)。 # IPv4:这条线路有 IPv6 地址但实际不通,必须 --edge-ip-version 4。 ExecStart=/usr/local/bin/cloudflared tunnel --no-autoupdate --edge-ip-version 4 --ha-connections 2 --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 enable lanremote-tunnel >/dev/null 2>&1 systemctl start lanremote-tunnel echo echo "===== 2. 等它建齐连接 =====" for i in 1 2 3 4 5; do sleep 15 N=$(grep -c 'Registered tunnel connection' "$RELAY_DIR/tunnel.log" 2>/dev/null || echo 0) echo " $((i*15)) 秒:已注册 $N 条" [ "$N" -ge 2 ] && break done echo echo "===== 3. 长时间稳定性:连打 30 次,每次隔 3 秒 =====" OK=0 FAIL=0 FAILED_CODES="" for i in $(seq 1 30); 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) if [ "$C" = "200" ]; then OK=$((OK + 1)); M="✓" else FAIL=$((FAIL + 1)); M="✗"; FAILED_CODES="$FAILED_CODES ${C:-超时}" fi printf " %2d: %s %s\n" "$i" "$M" "${C:-超时}" sleep 3 done PCT=$((OK * 100 / 30)) echo echo " 成功 $OK / 30 ($PCT%)" [ -n "$FAILED_CODES" ] && echo " 失败的返回码:$FAILED_CODES" echo echo "===== 4. 对照:中转本地(应该 100%)=====" LOK=0 for i in $(seq 1 10); do C=$(timeout 8 curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:8080/v1/health) [ "$C" = "200" ] && LOK=$((LOK + 1)) done echo " 局域网直连 $LOK / 10" echo echo "===== 5. 隧道日志统计 =====" 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 " quic 错误: $(grep -c 'Failed to dial a quic' "$RELAY_DIR/tunnel.log" 2>/dev/null || echo 0)" echo echo "===== 6. 最终服务清单 =====" for s in lanremote-relay lanremote-tunnel lanremote-watchdog.timer; do printf " %-32s %-10s %s\n" "$s" \ "$(systemctl is-active $s 2>/dev/null)" \ "$(systemctl is-enabled $s 2>/dev/null)" done echo echo " 公网地址: $(cat $RELAY_DIR/public_url.txt)" echo " 内存: $(free -m | awk 'NR==2{printf "已用 %s MB / 可用 %s MB", $3, $7}')" if [ "$PCT" -ge 90 ]; then echo echo " ✓ 稳定了($PCT%)—— ha-connections=2 定稿" else echo echo " ~ 还有抖动($PCT%),看门狗会兜底" fi