#!/bin/bash # 决定性对比:找出最能扛的隧道配置。 # # 现状:http2 + ha=2 大约 60~75% 可用。剩下 25% 失败的原因是 # 到 Cloudflare 美西边缘(sjc/lax)的国际链路丢包。 # # 单协议单通道扛不住,那就试三套: # A) http2 + ha=2 现状 # B) http2 + ha=4 TCP 下多连接不像 UDP 那样互相挤 # C) http2 ha=1 + quic ha=1 两个进程、两种协议同时挂着 # —— Cloudflare 会在所有连接间分发请求, # 哪个协议当下能通就走哪个 set -u URL=https://relay.xxmxxs.top RELAY_DIR=/root/lanremote-relay UNIT=/etc/systemd/system/lanremote-tunnel.service stop_all() { systemctl stop lanremote-tunnel 2>/dev/null || true systemctl stop lanremote-tunnel-quic 2>/dev/null || true pkill -f cloudflared 2>/dev/null || true sleep 3 } write_unit() { local name="$1" exec_args="$2" cat > "/etc/systemd/system/$name.service" </dev/null) code=${code:-000} [ "$code" = "200" ] && ok=$((ok + 1)) || bad=$((bad + 1)) sleep 2 done local conns conns=$(curl -s --max-time 5 http://127.0.0.1:20241/metrics 2>/dev/null \ | grep '^cloudflared_tunnel_ha_connections' | awk '{print $2}') local pct=$((ok * 100 / (ok + bad))) printf " >>> %-18s 可用 %2d/%2d (%d%%) 连接数=%s\n" \ "$label" "$ok" "$((ok + bad))" "$pct" "${conns:-?}" echo "$label|$pct|${conns:-0}" >> /root/net_ab.txt return 0 } : > /root/net_ab.txt CFG="--no-autoupdate --edge-ip-version 4 --retries 5 --config /root/.cloudflared/config.yml run lanremote" echo "===== A) http2 + ha=2(现状)=====" stop_all : > "$RELAY_DIR/tunnel.log" write_unit lanremote-tunnel "$CFG --protocol http2 --ha-connections 2" systemctl daemon-reload systemctl start lanremote-tunnel echo " 等 40 秒…"; sleep 40 measure "http2 ha=2" echo echo "===== B) http2 + ha=4 =====" stop_all : > "$RELAY_DIR/tunnel.log" write_unit lanremote-tunnel "$CFG --protocol http2 --ha-connections 4" systemctl daemon-reload systemctl start lanremote-tunnel echo " 等 40 秒…"; sleep 40 measure "http2 ha=4" echo echo "===== C) http2 ha=1 + quic ha=1(双进程双协议)=====" stop_all : > "$RELAY_DIR/tunnel.log" write_unit lanremote-tunnel "$CFG --protocol http2 --ha-connections 1" write_unit lanremote-tunnel-quic "$CFG --protocol quic --ha-connections 1" systemctl daemon-reload systemctl start lanremote-tunnel systemctl start lanremote-tunnel-quic echo " 等 50 秒…"; sleep 50 measure "http2+quic 双协议" echo echo "===== 汇总 =====" printf " %-20s %s\n" "配置" "可用率" while IFS='|' read -r label pct conns; do printf " %-20s %s%% (连接 %s)\n" "$label" "$pct" "$conns" done < /root/net_ab.txt echo best=$(sort -t'|' -k2 -rn /root/net_ab.txt | head -1 | cut -d'|' -f1) echo " 最优:$best"