#!/bin/bash # 诊断中转为什么 502 / 延时 1426ms set -u echo "════════ 1. 服务状态 ════════" for s in lanremote-relay lanremote-tunnel lanremote-nettune lanremote-watchdog.timer; do printf " %-26s %s\n" "$s" "$(systemctl is-active $s 2>&1)" done echo echo "════════ 2. 隧道连接数 · 落点 ════════" curl -s --max-time 5 http://127.0.0.1:20241/metrics 2>/dev/null \ | grep -E '^cloudflared_tunnel_ha_connections|server_locations' | sed 's/^/ /' echo echo "════════ 3. 本机中转响应(排除公网因素)════════" for i in 1 2 3; do printf " %d: HTTP %s %sms\n" "$i" \ "$(curl -s -o /dev/null -w '%{http_code}' --max-time 8 http://127.0.0.1:8080/v1/health)" \ "$(curl -s -o /dev/null -w '%{time_total}' --max-time 8 http://127.0.0.1:8080/v1/health | awk '{printf "%d", $1*1000}')" done echo echo "════════ 4. 公网延时拆解(关键)════════" echo " a) 到 Cloudflare 边缘的裸 RTT:" for i in 1 2 3; do t=$(curl -s -o /dev/null -w '%{time_connect}' --max-time 15 https://relay.xxmxxs.top/v1/health 2>/dev/null) echo " 连接耗时 $(echo "$t" | awk '{printf "%.0f", $1*1000}') ms" done echo echo " b) 完整请求耗时(含隧道回源):" for i in 1 2 3 4 5; do out=$(curl -s -o /dev/null -w '%{http_code} %{time_connect} %{time_starttransfer} %{time_total}' \ --max-time 25 -H 'User-Agent: LanRemote/1.8.0' https://relay.xxmxxs.top/v1/health 2>/dev/null) echo " 码/连接/首字节/总计: $(echo "$out" | awk '{printf "%s %.0fms %.0fms %.0fms", $1, $2*1000, $3*1000, $4*1000}')" sleep 1 done echo echo "════════ 5. 本机到 Cloudflare 的网络质量 ════════" ping -c 10 -W 2 1.1.1.1 2>/dev/null | tail -2 | sed 's/^/ /' echo " ---" ping -c 10 -W 2 104.21.54.45 2>/dev/null | tail -2 | sed 's/^/ /' echo " ---" ping -c 10 -W 2 198.41.200.43 2>/dev/null | tail -2 | sed 's/^/ /' echo echo "════════ 6. 隧道日志最近的重连/错误 ════════" tail -400 /root/lanremote-relay/tunnel.log 2>/dev/null \ | grep -E 'Registered|Lost connection|Retrying|i/o timeout|context deadline' \ | tail -12 | sed 's/^/ /' echo echo "════════ 7. 隧道连接数变化趋势(连打 30 次)════════" for i in $(seq 1 30); do c=$(curl -s --max-time 4 http://127.0.0.1:20241/metrics 2>/dev/null \ | grep '^cloudflared_tunnel_ha_connections' | awk '{print $2}') h=$(curl -s -o /dev/null -w '%{http_code}' --max-time 12 \ -H 'User-Agent: LanRemote/1.8.0' https://relay.xxmxxs.top/v1/health) printf " %2d: 连接 %-3s 公网 %s\n" "$i" "${c:-?}" "$h" sleep 2 done echo echo "════════ 8. watchdog 最近记录 ════════" tail -10 /root/lanremote-relay/watchdog.log 2>/dev/null | sed 's/^/ /' echo echo "════════ 9. 资源占用 ════════" free -m | sed 's/^/ /' echo " 负载: $(cat /proc/loadavg | cut -d' ' -f1-3)"