CHANGELOG 加 1.8.1

This commit is contained in:
xmz7891
2026-10-07 14:24:06 +08:00
parent b1119d0da6
commit 46196c3618
5 changed files with 226 additions and 148 deletions
+95 -80
View File
@@ -1,96 +1,111 @@
#!/bin/bash
# 测试 --region:能不能让 cloudflared 连亚洲边缘而不是美西?
# 试试能不能把隧道从"美国西海岸"挪到亚洲节点。
#
# 现状:连的是 198.41.x.x,落地 lax01/sjc07(美西),ICMP 199~243ms,
# 丢包 40%,握手经常超时。
# cloudflared 有个 --region 参数("Cloudflare Edge region to connect to")。
# 如果能把落地换到香港/新加坡/东京,延迟和丢包都会好很多。
# 为什么这可能是最大的一招:
# 实测隧道落点全是 sjc / lax(美国圣何塞、洛杉矶),
# 中国 -> 美西 往返就是 ~240ms 起步,加上 4G 的丢包,
# 每个请求固定在隧道回源这一跳吃 ~1100ms。
# 如果能连到 东京 / 香港 / 新加坡,这一跳能砍掉一大半。
#
# cloudflared 理论上支持按区域选 edge,但其实它主要是按
# 运营商的 BGP 出口自动选。这里把能试的都试一遍,
# 用**实测落点**判断有没有用,而不是看参数有没有被接受。
set -u
URL=https://relay.xxmxxs.top
RELAY_DIR=/root/lanremote-relay
BASE="--no-autoupdate --edge-ip-version 4 --retries 5 --config /root/.cloudflared/config.yml run lanremote"
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 region="$1"
local label="$2"
local arg="$1" label="$2"
echo " ── $label($arg)──"
systemctl stop lanremote-tunnel 2>/dev/null || true
pkill -f cloudflared 2>/dev/null || true
sleep 2
: > "$RELAY_DIR/tunnel.log"
if [ -n "$region" ]; then
nohup cloudflared tunnel --region "$region" $BASE \
>> "$RELAY_DIR/tunnel.log" 2>&1 &
else
nohup cloudflared tunnel $BASE >> "$RELAY_DIR/tunnel.log" 2>&1 &
# 回滚到干净状态再叠加参数
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
sleep 30
local conns loc proto
conns=$(grep -c 'Registered tunnel connection' "$RELAY_DIR/tunnel.log" 2>/dev/null || echo 0)
loc=$(grep -o 'location=[a-z0-9]*' "$RELAY_DIR/tunnel.log" 2>/dev/null | tail -1)
proto=$(grep -o 'protocol=[a-z0-9]*' "$RELAY_DIR/tunnel.log" 2>/dev/null | tail -1)
printf " %-14s 注册 %-3s %-14s %s\n" "$label" "$conns" "${loc:-?}" "${proto:-?}"
if [ "$conns" = "0" ]; then
echo " 报错:"
grep -iE 'error|fatal|invalid' "$RELAY_DIR/tunnel.log" 2>/dev/null \
| tail -2 | sed 's/^/ /'
fi
# 快速探 10 次
local ok=0
for i in $(seq 1 10); do
c=$(timeout 12 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))
sleep 2
# 等最多 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
echo " 公网 $ok/10"
echo "$label|$ok|${loc:-?}|$conns" >> /root/region_result.txt
pkill -f cloudflared 2>/dev/null || true
sleep 2
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
}
: > /root/region_result.txt
rm -f /tmp/region.txt
echo "" > /dev/null
echo "===== 各 region 的表现 ====="
try_region "" "默认(global)"
try_region "ap" "ap"
try_region "us" "us"
# 先看 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 "===== 汇总 ====="
while IFS='|' read -r label ok loc conns; do
printf " %-16s 公网 %s/10 落地=%s 注册=%s\n" "$label" "$ok" "$loc" "$conns"
done < /root/region_result.txt
echo
echo "===== 恢复默认配置 ====="
cat > /etc/systemd/system/lanremote-tunnel.service <<EOF
[Unit]
Description=LanRemote Cloudflare 具名隧道
After=network-online.target lanremote-relay.service
Wants=network-online.target lanremote-relay.service
[Service]
Type=simple
ExecStart=/usr/local/bin/cloudflared tunnel --protocol http2 --ha-connections 2 $BASE
Restart=always
RestartSec=5
MemoryMax=160M
StandardOutput=append:/root/lanremote-relay/tunnel.log
StandardError=append:/root/lanremote-relay/tunnel.log
[Install]
WantedBy=multi-user.target
EOF
echo "════════ 5. 回滚到备份 ════════"
cp -a "$BACKUP" "$SVC"
systemctl daemon-reload
systemctl start lanremote-tunnel
sleep 30
echo " 隧道: $(systemctl is-active lanremote-tunnel)"
echo " 连接数: $(curl -s --max-time 5 http://127.0.0.1:20241/metrics | grep '^cloudflared_tunnel_ha_connections' | awk '{print $2}')"
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"