#!/bin/bash # 定稿:HTTP/2 + 4 连接,然后做**长时间**验证(这次不看瞬时值) set -u UNIT=/etc/systemd/system/lanremote-tunnel.service PUB=https://relay.xxmxxs.top RELAY_DIR=/root/lanremote-relay echo "════════ 1. 写回最优配置 ════════" sed -i "s/--protocol [a-z0-9]*/--protocol http2/" "$UNIT" sed -i "s/--ha-connections [0-9]*/--ha-connections 4/" "$UNIT" systemctl daemon-reload systemctl restart lanremote-tunnel echo " $(grep -oE '\-\-protocol [a-z0-9]+|--ha-connections [0-9]+' "$UNIT" | tr '\n' ' ')" echo echo "════════ 2. 等连接全部建立 ════════" for i in $(seq 1 12); do sleep 5 n=$(curl -s --max-time 4 http://127.0.0.1:20241/metrics 2>/dev/null \ | grep '^cloudflared_tunnel_ha_connections' | awk '{print $2}') echo " ${i}0s 内: 连接 $n" [ "$n" = "4" ] && break done echo echo "════════ 3. 公网连测 40 次(约 2 分钟)════════" ok=0; fail=0; worst=0; streak=0; worst_streak=0 for i in $(seq 1 40); do r=$(curl -s -o /dev/null -w '%{http_code} %{time_total}' --max-time 15 \ -H 'User-Agent: LanRemote/1.4.0' "$PUB/v1/health") c=$(echo "$r" | cut -d' ' -f1) t=$(echo "$r" | cut -d' ' -f2) if [ "$c" = "200" ]; then ok=$((ok+1)); streak=0 big=$(echo "$t > $worst" | bc 2>/dev/null || echo 0) [ "$big" = "1" ] && worst=$t else fail=$((fail+1)); streak=$((streak+1)) [ "$streak" -gt "$worst_streak" ] && worst_streak=$streak [ $((i % 5)) -eq 0 ] && echo " 第 $i 次: $c" fi sleep 2.5 done rate=$(echo "scale=1; $ok * 100 / 40" | bc) echo echo " 可用 $ok/40 = ${rate}% 最慢 ${worst}s" echo " 最长连续失败 $worst_streak 次(约 $((worst_streak * 3)) 秒)" echo echo "════════ 4. 这段时间隧道重连了几次 ════════" since=$(date -u -d '4 minutes ago' '+%Y-%m-%dT%H:%M:%S' 2>/dev/null || echo "") if [ -n "$since" ]; then awk -v s="$since" '$0 >= s' "$RELAY_DIR/tunnel.log" 2>/dev/null > /tmp/recent.log for kw in 'Registered tunnel connection' 'Lost connection' 'Retrying connection' 'i/o timeout'; do printf " %-32s %s 次\n" "$kw" "$(grep -c "$kw" /tmp/recent.log)" done fi echo echo "════════ 5. 现在的落点 ════════" grep -E 'Registered tunnel connection' "$RELAY_DIR/tunnel.log" 2>/dev/null | tail -5 \ | grep -oE 'ip=[0-9.]+ location=[a-z0-9]+ protocol=[a-z0-9]+' | sed 's/^/ /' echo echo "════════ 6. watchdog 状态 ════════" tail -6 "$RELAY_DIR/watchdog.log" 2>/dev/null | sed 's/^/ /'