#!/bin/bash # 对照实验:验证"被控端一提交配对码,隧道就掉"这个说法。 # # 做法: # 前 20 秒 什么都不做,只看隧道连接数(基线) # 第 20 秒 模拟一次注册(把配对码提交到中转) # 后 90 秒 继续看 # 对比两段的掉线频率。 set -u URL=https://relay.xxmxxs.top PORT=8080 conns() { curl -s --max-time 4 http://127.0.0.1:20241/metrics 2>/dev/null \ | grep '^cloudflared_tunnel_ha_connections' | awk '{print $2}' } pub() { timeout 12 curl -4 -s -o /dev/null -w '%{http_code}' \ -H 'User-Agent: LanRemote/1.4.0' "$URL/v1/health" 2>/dev/null } probe() { local label="$1" seconds="$2" local i c p drops=0 samples=0 for i in $(seq 1 "$seconds"); do c=$(conns); c=${c:-?} p=$(pub); p=${p:-000} samples=$((samples + 1)) [ "$p" != "200" ] && drops=$((drops + 1)) printf " [%s] %2ds ha=%-3s 公网=%s\n" "$label" "$i" "$c" "$p" sleep 1 done echo " >>> $label 段:$drops / $samples 次不通" } echo "===== A 段:什么都不做(基线)=====" probe "基线" 20 echo echo "===== B 段:提交一次配对码(模拟被控端注册)=====" CODE=$((RANDOM % 900000 + 100000)) DEV="probe-$(date +%s)" echo " 注册设备 $DEV,配对码 $CODE" BODY=$(printf '{"deviceId":"%s","code":"%s","name":"实验机","version":"1.4.0"}' "$DEV" "$CODE") echo -n " 本地注册: " curl -s --max-time 10 -H 'User-Agent: LanRemote/1.4.0' \ -H 'Content-Type: application/json' \ -d "$BODY" "http://127.0.0.1:$PORT/v1/agent/register" | head -c 160 echo echo -n " 经公网注册: " curl -s --max-time 20 -H 'User-Agent: LanRemote/1.4.0' \ -H 'Content-Type: application/json' \ -d "$BODY" "$URL/v1/agent/register" | head -c 160 echo echo probe "注册后" 40 echo echo "===== 清理实验设备 =====" KEY=$(cat /root/lanremote-relay/data/admin_key.txt 2>/dev/null) curl -s --max-time 15 -H 'User-Agent: LanRemote/1.4.0' \ -H "X-Admin: $KEY" -H 'Content-Type: application/json' \ -d "{\"deviceId\":\"$DEV\"}" \ "http://127.0.0.1:$PORT/v1/admin/delete" | head -c 200 echo echo echo "===== 隧道日志里的断开记录(最近 20 条)=====" grep -E 'Connection terminated|Failed to dial|Registered tunnel' \ /root/lanremote-relay/tunnel.log 2>/dev/null | tail -20 | sed 's/^/ /'