Files
2026-10-07 14:24:06 +08:00

112 lines
4.1 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# 试试能不能把隧道从"美国西海岸"挪到亚洲节点。
#
# 为什么这可能是最大的一招:
# 实测隧道落点全是 sjc / lax(美国圣何塞、洛杉矶),
# 中国 -> 美西 往返就是 ~240ms 起步,加上 4G 的丢包,
# 每个请求固定在隧道回源这一跳吃 ~1100ms。
# 如果能连到 东京 / 香港 / 新加坡,这一跳能砍掉一大半。
#
# cloudflared 理论上支持按区域选 edge,但其实它主要是按
# 运营商的 BGP 出口自动选。这里把能试的都试一遍,
# 用**实测落点**判断有没有用,而不是看参数有没有被接受。
set -u
SVC=/etc/systemd/system/lanremote-tunnel.service
BACKUP=/root/lanremote-tunnel.service.bak.region.$(date +%s)
cp -a "$SVC" "$BACKUP"
UA='LanRemote/1.8.0'
echo "════════ 1. cloudflared 支持的区域参数 ════════"
cloudflared tunnel run --help 2>&1 | grep -iE 'region|edge' -A2 | head -20 | sed 's/^/ /'
echo
echo " cloudflared 版本: $(cloudflared --version 2>&1 | head -1)"
echo
echo "════════ 2. 当前落点(基线)════════"
curl -s --max-time 5 http://127.0.0.1:20241/metrics 2>/dev/null \
| grep 'server_locations.*} 1' | sed 's/^/ /'
echo
echo "════════ 3. 逐个试区域参数 ════════"
try_region() {
local arg="$1" label="$2"
echo " ── $label($arg)──"
# 回滚到干净状态再叠加参数
cp -a "$BACKUP" "$SVC"
if [ -n "$arg" ]; then
sed -i "s|\(ExecStart=.*cloudflared tunnel\)|\1 $arg|" "$SVC"
fi
systemctl daemon-reload
systemctl restart lanremote-tunnel
# 等最多 90 秒看能不能建起连接
local n=0
for i in $(seq 1 18); do
sleep 5
n=$(curl -s --max-time 4 http://127.0.0.1:20241/metrics 2>/dev/null \
| grep '^cloudflared_tunnel_ha_connections' | awk '{print $2}')
[ "${n:-0}" -ge 2 ] && break
done
if [ "${n:-0}" -lt 2 ]; then
echo " 连接数 $n —— **没连上,这个参数不能用**"
return 1
fi
echo " 连接数 $n"
echo " 落点:"
curl -s --max-time 5 http://127.0.0.1:20241/metrics 2>/dev/null \
| grep 'server_locations.*} 1' | sed 's/^/ /'
# 量 6 次公网延迟
local times=()
for i in $(seq 1 6); do
ms=$(curl -s -o /dev/null -w '%{time_total}' --max-time 25 \
-H "User-Agent: $UA" https://relay.xxmxxs.top/v1/health \
| awk '{printf "%d", $1*1000}')
times+=("$ms")
done
local stats=$(printf '%s\n' "${times[@]}" | sort -n | awk '{a[NR]=$1} END {printf "最小 %d 中位 %d 最大 %d", a[1], a[int(NR/2)+1], a[NR]}')
echo " 延迟: $stats ms"
echo "$label|$n|$stats" >> /tmp/region.txt
return 0
}
rm -f /tmp/region.txt
echo "" > /dev/null
# 先看 help 里有没有 region 这个 flag
HAS_REGION=$(cloudflared tunnel run --help 2>&1 | grep -c -- '--region' || true)
echo " --region 参数是否存在: $([ "${HAS_REGION:-0}" -gt 0 ] && echo 是 || echo 否)"
echo
# 逐个试。--region 的取值 cloudflared 没在 help 里列全,
# 常见的是 us / eu / ap 之类;不合法会直接报错退出。
for r in us eu ap; do
try_region "--region $r" "region=$r" || true
echo
done
echo "════════ 4. 汇总 ════════"
if [ -f /tmp/region.txt ]; then
cat /tmp/region.txt | sed 's/^/ /'
else
echo " (没有任何区域参数能用)"
fi
echo
echo "════════ 5. 回滚到备份 ════════"
cp -a "$BACKUP" "$SVC"
systemctl daemon-reload
systemctl restart lanremote-tunnel
sleep 20
echo " 参数: $(grep -oE '--protocol [a-z0-9]+|--ha-connections [0-9]+|--edge-ip-version [0-9]+' "$SVC" | tr '\n' ' ')"
echo " 连接数: $(curl -s --max-time 5 http://127.0.0.1:20241/metrics 2>/dev/null | grep '^cloudflared_tunnel_ha_connections' | awk '{print $2}')"
echo " 落点:"
curl -s --max-time 5 http://127.0.0.1:20241/metrics 2>/dev/null \
| grep 'server_locations.*} 1' | sed 's/^/ /'
echo " 备份: $BACKUP"