Files
xmz7891 8c4ee6df08 修真凶:4G 链路静默断连导致隧道"假活"(用户撞上 Error 1033)
现象:用户早上打开 relay.xxmxxs.top 看到 Cloudflare Error 1033。
      watchdog 日志把过程记下来了:
        公网不通(HTTP 000,ha_connections=2)  <- 隧道显示连着,公网不通
        公网不通(HTTP 502,ha_connections=2)
        公网恢复(HTTP 200)                     <- 自己又好了
      也就是说:**间歇性假活**,短样本测不出来。
      我之前只测 15~25 次就说"可用率 100%",太草率了。

根因:4G 链路会**静默断开** TCP 连接 —— 不发 FIN/RST,双方都以为还连着。
      Cloudflare 把请求发到这条僵尸连接上,就回 502/530/1033。
      Linux 默认 tcp_keepalive_time = **7200 秒(2 小时)**才发现连接死了,
      所以中间这段时间服务一直"看着在线、实际打不开"。

      tunnel.log 最近 3000 行的佐证:
        i/o timeout              212 次
        Retrying connection      127 次
        Lost connection           22 次

修法:
  1. TCP keepalive 调到 30 秒 + 每 10 秒探测 3 次
     => 最多约 60 秒发现僵尸连接,cloudflared 立刻重连
     (/etc/sysctl.d/97-ykxmz-keepalive.conf)
  2. 隧道连接数 2 -> 4,多几条冗余
  3. 修 relay 从不清理设备记录的问题:
     self.devices 只增不减,被控端每换端口/重装就多一条,
     实测一天攒了 75 条僵尸记录,/v1/health 从几 KB 涨到 20KB。
     加 DEVICE_TTL(6 小时)在 snapshot() 里清掉。

实测(修完立刻验证):
    设备侧  公网 15/15 HTTP 200
    Windows 公网 25/25,中位 520 ms,最快 463 ms
    health  20931 字节 / 75 台  ->  152 字节 / 0 台
2026-10-07 09:15:27 +08:00

62 lines
2.8 KiB
Bash

#!/bin/bash
# 查隧道反复重连的根因:内存限制?OOM?DNS 差异?
set -u
echo "════════ 1. 内存总览 ════════"
free -m | sed 's/^/ /'
echo
echo "════════ 2. 各进程内存占用 ════════"
ps aux --sort=-rss 2>/dev/null | head -8 | awk '{printf " %-8s %6s MB %s\n", $1, int($6/1024), $11}'
echo
echo "════════ 3. 隧道 unit 里的内存限制 ════════"
grep -E 'MemoryMax|MemoryHigh|Restart' /etc/systemd/system/lanremote-tunnel.service | sed 's/^/ /'
echo " 当前实际用量:"
systemctl show lanremote-tunnel -p MemoryCurrent -p MemoryMax --value 2>/dev/null | sed 's/^/ /'
echo
echo "════════ 4. 有没有被 OOM 杀过 ════════"
journalctl -u lanremote-tunnel --since '6 hours ago' --no-pager 2>/dev/null \
| grep -iE 'oom|killed|memory|out of memory' | tail -8 | sed 's/^/ /'
echo " dmesg 里的 OOM:"
dmesg 2>/dev/null | grep -iE 'oom|killed process' | tail -6 | sed 's/^/ /'
echo " (没输出=没被 OOM 杀)"
echo
echo "════════ 5. 隧道服务重启过几次 ════════"
journalctl -u lanremote-tunnel --since '6 hours ago' --no-pager 2>/dev/null \
| grep -cE 'Started|Stopped|Deactivated' | sed 's/^/ 系统层面事件数: /'
echo " 最近的启动/停止:"
journalctl -u lanremote-tunnel --since '6 hours ago' --no-pager 2>/dev/null \
| grep -E 'Started|Stopped|Deactivated|killed' | tail -8 | sed 's/^/ /'
echo
echo "════════ 6. 隧道日志里重连的频率 ════════"
echo " 最近 6 小时的日志行数: $(wc -l < /root/lanremote-relay/tunnel.log)"
for kw in 'Registered tunnel connection' 'Lost connection' 'Retrying connection' 'i/o timeout'; do
n=$(tail -3000 /root/lanremote-relay/tunnel.log 2>/dev/null | grep -c "$kw")
printf " %-32s %s 次\n" "$kw" "$n"
done
echo
echo "════════ 7. DNS:设备这边解析到哪 ════════"
getent hosts relay.xxmxxs.top | sed 's/^/ /'
echo " 直接问 1.1.1.1:"
nslookup relay.xxmxxs.top 1.1.1.1 2>/dev/null | grep -A2 'Name:' | sed 's/^/ /'
echo
echo "════════ 8. relay.log 里的错误(最后 15 条)════════"
tail -400 /root/lanremote-relay/relay.log 2>/dev/null \
| grep -iE 'error|exception|traceback|broken|reset' | tail -15 | sed 's/^/ /'
echo " (没输出=没有明显错误)"
echo
echo "════════ 9. relay.log 最后 12 行(看它在干什么)════════"
tail -12 /root/lanremote-relay/relay.log 2>/dev/null | sed 's/^/ /'
echo
echo "════════ 10. watchdog 在干什么 ════════"
tail -12 /root/lanremote-relay/watchdog.log 2>/dev/null | sed 's/^/ /'
echo " watchdog 服务: $(systemctl is-active lanremote-watchdog 2>&1)"