#!/bin/bash # 实测设备到 Cloudflare 各边缘 IP 的延迟,挑最快的做优选 IP。 # # 原理: # cloudflared 建隧道时会连 region1.v2.argotunnel.com / region2.v2.argotunnel.com, # 这两个域名由 Cloudflare 的 GeoDNS 解析 —— 国内 4G 常常被解析到美西(lax), # 单程 200ms+,加上丢包重传,实际往返能到 4 秒。 # # 但 Cloudflare 在全球有大量 anycast 边缘 IP,其中不少在亚太(香港/新加坡/ # 东京/首尔)能直连,延迟低得多。做法是: # 1. 测一批候选 IP 的 TCP 7844(隧道端口)握手延迟 # 2. 挑最快的写进 /etc/hosts,覆盖 GeoDNS 的结果 # 3. 重启隧道,实测隧道往返 set -u CANDIDATES=" 104.16.0.0 104.17.0.0 104.18.0.0 104.19.0.0 104.20.0.0 104.21.0.0 104.22.0.0 104.23.0.0 104.24.0.0 104.25.0.0 104.26.0.0 104.27.0.0 104.16.132.229 104.18.2.0 104.18.3.0 104.18.32.0 104.18.33.0 104.18.34.0 104.18.35.0 104.18.36.0 104.18.37.0 104.18.38.0 104.18.39.0 162.159.192.1 162.159.193.1 162.159.195.1 172.64.0.0 172.64.1.0 172.64.2.0 172.64.3.0 172.64.4.0 172.64.5.0 172.64.6.0 172.64.7.0 172.64.8.0 172.64.9.0 172.64.10.0 172.64.11.0 172.64.12.0 172.64.13.0 172.64.14.0 172.64.15.0 172.64.16.0 172.64.17.0 172.64.18.0 172.64.19.0 172.64.20.0 172.64.21.0 172.64.22.0 172.64.23.0 172.64.24.0 172.64.25.0 172.64.26.0 172.64.27.0 172.64.28.0 172.64.29.0 172.64.30.0 172.64.31.0 " echo "===== 当前 cloudflared 连的是哪个边缘 =====" grep -oE 'location=[a-z0-9]+' /root/lanremote-relay/tunnel.log 2>/dev/null | sort | uniq -c | tail -5 | sed 's/^/ /' echo " 当前 region 域名解析到:" getent hosts region1.v2.argotunnel.com 2>/dev/null | head -2 | sed 's/^/ /' getent hosts region2.v2.argotunnel.com 2>/dev/null | head -2 | sed 's/^/ /' echo echo "===== 逐个测 TCP 7844 握手延迟(每个 2 次取最快)=====" : > /root/cf_ip_result.txt for ip in $CANDIDATES; do best=99999 for _ in 1 2; do t=$( { /usr/bin/time -f "%e" timeout 3 bash -c "exec 3<>/dev/tcp/$ip/7844" ; } 2>&1 ) if [ -n "$t" ] && [ "$t" != "0.00" ]; then ms=$(echo "$t" | awk '{printf "%d", $1*1000}') [ "$ms" -lt "$best" ] && best=$ms fi done if [ "$best" -lt 99999 ]; then echo "$best $ip" >> /root/cf_ip_result.txt fi done if [ -s /root/cf_ip_result.txt ]; then echo " 最快的 10 个:" sort -n /root/cf_ip_result.txt | head -10 | while read ms ip; do printf " %6s ms %s\n" "$ms" "$ip" done else echo " 一个都没通 —— 7844 可能被运营商封了" fi