#!/bin/bash # 试不同的 cloudflared 连接数,找出这条线路上最稳的配置。 # # 现状:4 条 ha-connection 的 QUIC 全在超时 # "failed to dial to edge with quic: timeout: handshake did not complete" # 怀疑:这条 4G 线路对 UDP 有节制 —— 同时建 4 条 QUIC 连接反而更容易被限。 # # 试:ha=4 / ha=2 / ha=1,各观察一轮。 set -u RELAY_DIR=/root/lanremote-relay UNIT=/etc/systemd/system/lanremote-tunnel.service URL=https://relay.xxmxxs.top restart_with() { local ha="$1" echo "===== 试 ha-connections=$ha =====" systemctl stop lanremote-tunnel 2>/dev/null || true pkill -f cloudflared 2>/dev/null || true sleep 3 cat > "$UNIT" </dev/null || echo 0) local drops drops=$(grep -c 'Connection terminated' "$RELAY_DIR/tunnel.log" 2>/dev/null || echo 0) echo " 已注册 $conns 条,断开 $drops 次" local 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' "$URL/v1/health" 2>/dev/null) [ "$C" = "200" ] && ok=$((ok + 1)) printf " %2d: HTTP %s\n" "$i" "${C:-超时}" sleep 2 done echo " >>> ha=$ha 可用率 $ok/10" echo "$ha $ok $conns $drops" >> /root/ha_result.txt echo } : > /root/ha_result.txt restart_with 4 restart_with 2 restart_with 1 echo "===== 汇总 =====" printf " %-8s %-10s %-10s %s\n" "ha" "可用/10" "已注册" "断开" while read -r ha ok conns drops; do printf " %-8s %-10s %-10s %s\n" "$ha" "$ok/10" "$conns" "$drops" done < /root/ha_result.txt BEST=$(sort -k2 -nr /root/ha_result.txt | head -1 | awk '{print $1}') BEST_OK=$(sort -k2 -nr /root/ha_result.txt | head -1 | awk '{print $2}') echo echo " 最稳的是 ha-connections=$BEST($BEST_OK/10)" echo "$BEST" > /root/best_ha.txt