#!/bin/bash # 重测协议:QUIC(UDP)vs HTTP/2(TCP)。 # # 背景:这条 4G 线路 14 小时里抖了 81 次断开 / 150 次 quic 错误, # 平均每小时 10 次。每次抖动期间公网都是 1033。 # # 之前测过 HTTP/2 失败(TCP 7844 握手超时),但那是晚上高峰。 # 现在早上重测一次,两边各跑 3 分钟对比。 set -u RELAY_DIR=/root/lanremote-relay URL=https://relay.xxmxxs.top probe() { local label="$1"; shift local extra="$*" echo "===== 试 $label =====" echo " 参数: $extra" systemctl stop lanremote-tunnel 2>/dev/null || true pkill -f cloudflared 2>/dev/null || true sleep 3 : > "$RELAY_DIR/tunnel.log" # shellcheck disable=SC2086 nohup cloudflared tunnel --no-autoupdate --edge-ip-version 4 $extra \ --config /root/.cloudflared/config.yml run lanremote \ >> "$RELAY_DIR/tunnel.log" 2>&1 & local pid=$! echo " 等 40 秒建连接…" sleep 40 local conns conns=$(grep -c 'Registered tunnel connection' "$RELAY_DIR/tunnel.log" 2>/dev/null || echo 0) echo " 已注册 $conns 条" if [ "$conns" = "0" ]; then echo " 日志里的报错:" grep -iE 'ERR|failed' "$RELAY_DIR/tunnel.log" | tail -3 | sed 's/^/ /' fi local ok=0 for i in $(seq 1 12); do C=$(timeout 15 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)) sleep 5 done local drops drops=$(grep -c 'Connection terminated' "$RELAY_DIR/tunnel.log" 2>/dev/null || echo 0) local errs errs=$(grep -c 'Failed to dial' "$RELAY_DIR/tunnel.log" 2>/dev/null || echo 0) echo " >>> $label: 公网 $ok/12,断开 $drops,重连失败 $errs" echo "$label|$ok|$conns|$drops|$errs" >> /root/proto_result.txt kill "$pid" 2>/dev/null || true sleep 2 echo } : > /root/proto_result.txt probe "QUIC(UDP)" "--ha-connections 1 --retries 5" probe "HTTP2(TCP)" "--protocol http2 --ha-connections 1 --retries 5" echo "===== 对比 =====" printf " %-14s %-8s %-8s %-8s %s\n" "协议" "公网" "注册" "断开" "重试失败" while IFS='|' read -r label ok conns drops errs; do printf " %-14s %-8s %-8s %-8s %s\n" "$label" "$ok/12" "$conns" "$drops" "$errs" done < /root/proto_result.txt echo echo "===== 恢复正式配置(QUIC + ha=1)=====" cat > /etc/systemd/system/lanremote-tunnel.service <<'EOF' [Unit] Description=LanRemote Cloudflare 具名隧道 After=network-online.target lanremote-relay.service Wants=network-online.target lanremote-relay.service [Service] Type=simple 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 daemon-reload systemctl restart lanremote-tunnel sleep 30 echo " 隧道: $(systemctl is-active lanremote-tunnel)" echo " 公网: HTTP $(timeout 15 curl -4 -s -o /dev/null -w '%{http_code}' -H 'User-Agent: LanRemote/1.4.0' $URL/v1/health 2>/dev/null)"