#!/bin/bash # 强制隧道走 IPv4 + HTTP2。 # # 根因:这条 4G 线路**分配了 IPv6 地址但实际不通**。 # Cloudflare 边缘默认优先 IPv6(日志里是 2606:4700:a8::10), # 于是每次建隧道都在等一个永远连不上的地址 -> context deadline exceeded。 # # 修法: # --edge-ip-version 4 只走 IPv4 # --protocol http2 用 TCP 而不是 QUIC(有些 4G 会拦 UDP) set -u RELAY_DIR=/root/lanremote-relay echo "===== 确认 IPv4 / IPv6 =====" timeout 6 curl -4 -s -o /dev/null -w ' IPv4 -> %{http_code}\n' https://www.cloudflare.com || echo ' IPv4 不通' timeout 6 curl -6 -s -o /dev/null -w ' IPv6 -> %{http_code}\n' https://www.cloudflare.com || echo ' IPv6 不通(这就是根因)' echo echo "===== 更新 systemd 服务,加 IPv4 参数 =====" systemctl stop lanremote-tunnel 2>/dev/null || true pkill -f 'cloudflared tunnel' 2>/dev/null || true sleep 2 cat > /etc/systemd/system/lanremote-tunnel.service <<'UNIT' [Unit] Description=LanRemote Cloudflare 隧道 After=network-online.target lanremote-relay.service Wants=network-online.target Requires=lanremote-relay.service [Service] Type=simple # 这条 4G 线路的 IPv6 有地址但不通,必须强制 IPv4; # 同时用 http2 而不是 QUIC,避开某些 4G 对 UDP 的拦截。 ExecStart=/usr/local/bin/cloudflared tunnel --no-autoupdate \ --edge-ip-version 4 --protocol http2 \ --url http://127.0.0.1:8080 Restart=always RestartSec=5 MemoryMax=128M StandardOutput=append:/root/lanremote-relay/tunnel.log StandardError=append:/root/lanremote-relay/tunnel.log [Install] WantedBy=multi-user.target UNIT systemctl daemon-reload systemctl enable lanremote-tunnel >/dev/null 2>&1 rm -f "$RELAY_DIR/tunnel.log" systemctl start lanremote-tunnel echo " 服务已更新并启动" echo echo "===== 等公网地址(最多 100 秒)=====" URL="" for i in $(seq 1 50); do sleep 2 URL=$(grep -oE 'https://[a-z0-9]+(-[a-z0-9]+)+\.trycloudflare\.com' \ "$RELAY_DIR/tunnel.log" 2>/dev/null | tail -1) if [ -n "$URL" ]; then echo " 第 $((i*2)) 秒拿到: $URL" break fi done if [ -z "$URL" ]; then echo " 还是没拿到,日志尾部:" tail -12 "$RELAY_DIR/tunnel.log" exit 1 fi echo "$URL" > "$RELAY_DIR/public_url.txt" echo echo "===== 公网可达性 =====" for i in 1 2 3 4 5; do if curl -s --max-time 20 "$URL/v1/health" | grep -q '"ok"'; then echo " 第 $i 次: 通了" break fi echo " 第 $i 次: 还没通" sleep 4 done echo echo "===== 走的是 IPv4 吗 =====" curl -s --max-time 15 -o /dev/null -w ' 连接对端 IP: %{remote_ip}\n' "$URL/v1/health" echo echo "===== 服务状态 =====" systemctl is-active lanremote-relay lanremote-tunnel echo free -m | head -2 echo echo "PUBLIC_URL=$URL"