#!/bin/bash # 诊断隧道当前状态(e2e 连续失败)。 set -u RELAY_DIR=/root/lanremote-relay URL=https://relay.xxmxxs.top echo "===== 1. 三个服务 =====" for s in lanremote-relay lanremote-tunnel lanremote-watchdog.timer; do printf " %-30s %-10s %s\n" "$s" \ "$(systemctl is-active $s 2>/dev/null)" \ "$(systemctl is-enabled $s 2>/dev/null)" done echo echo "===== 2. cloudflared 连接数 =====" echo -n " ha_connections = " curl -s --max-time 5 http://127.0.0.1:20241/metrics 2>/dev/null \ | grep '^cloudflared_tunnel_ha_connections' | awk '{print $2}' echo -n " TCP 会话 = " curl -s --max-time 5 http://127.0.0.1:20241/metrics 2>/dev/null \ | grep '^cloudflared_tcp_active_sessions' | awk '{print $2}' echo -n " 请求总数 = " curl -s --max-time 5 http://127.0.0.1:20241/metrics 2>/dev/null \ | grep '^cloudflared_tunnel_total_requests' | awk '{print $2}' echo echo "===== 3. 隧道进程 =====" pgrep -a -f 'cloudflared tunnel' | head -3 | sed 's/^/ /' || echo " 没有进程" echo echo "===== 4. 日志统计 =====" echo " 注册成功: $(grep -c 'Registered tunnel connection' $RELAY_DIR/tunnel.log 2>/dev/null || echo 0)" echo " 连接断开: $(grep -c 'Connection terminated' $RELAY_DIR/tunnel.log 2>/dev/null || echo 0)" echo " quic 错误: $(grep -c 'Failed to dial a quic' $RELAY_DIR/tunnel.log 2>/dev/null || echo 0)" echo echo " 日志最近 8 行:" tail -8 "$RELAY_DIR/tunnel.log" 2>/dev/null | sed 's/^/ /' echo echo "===== 5. 看门狗最近动作 =====" tail -8 "$RELAY_DIR/watchdog.log" 2>/dev/null | sed 's/^/ /' || echo " (没日志)" echo -n " 看门狗状态: " cat "$RELAY_DIR/watchdog.state" 2>/dev/null | tr '\n' ' ' echo echo echo "===== 6. 源站(本地)=====" echo " /v1/health: HTTP $(timeout 8 curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:8080/v1/health)" echo " / (管理页): HTTP $(timeout 8 curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:8080/)" echo -n " 中转内存: " curl -s --max-time 5 http://127.0.0.1:8080/v1/health | python3 -c 'import json,sys;print(str(json.load(sys.stdin)["memoryKB"]//1024)+" MB")' 2>/dev/null || echo "?" echo echo "===== 7. 公网可达性(连打 10 次)=====" OK=0 for i in $(seq 1 10); do C=$(timeout 20 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)); M="✓"; } || M="✗" printf " %2d: %s %s\n" "$i" "$M" "${C:-超时}" sleep 2 done echo " 公网 $OK / 10" echo echo "===== 8. 诊断结论 =====" CONNS=$(curl -s --max-time 5 http://127.0.0.1:20241/metrics 2>/dev/null | grep '^cloudflared_tunnel_ha_connections' | awk '{print $2}') if [ "${CONNS:-0}" = "0" ]; then echo " 0 条连接 —— cloudflared 到边缘的链路断了,正在退避重连" echo " 看门狗应该会在 30-60 秒内把它重启" else echo " 有 $CONNS 条连接,但公网只有 $OK/10 —— 链路质量差" fi