#!/bin/bash # 验证 Cloudflare 快速隧道能不能用。 # 起一个测试 HTTP 服务,用 cloudflared 开隧道,拿到公网地址。 set -u pkill -f 'http.server 8080' 2>/dev/null pkill -f 'cloudflared tunnel' 2>/dev/null sleep 1 echo "===== 1. 起测试 HTTP 服务 =====" cd /root echo "hello-from-4g-wifi" > /root/testpage.txt nohup python3 -m http.server 8080 --bind 127.0.0.1 > /root/httpsrv.log 2>&1 & sleep 2 if curl -s --max-time 5 http://127.0.0.1:8080/testpage.txt | grep -q hello; then echo " 本机 HTTP 正常" else echo " ✗ 本机 HTTP 起不来" cat /root/httpsrv.log exit 1 fi echo echo "===== 2. 开 Cloudflare 快速隧道 =====" rm -f /root/cf.log nohup cloudflared tunnel --no-autoupdate --url http://127.0.0.1:8080 \ > /root/cf.log 2>&1 & echo " cloudflared 已启动,等它拿到地址…" URL="" for i in $(seq 1 40); do sleep 2 URL=$(grep -oE 'https://[a-z0-9-]+\.trycloudflare\.com' /root/cf.log | head -1) if [ -n "$URL" ]; then echo " 第 $((i*2)) 秒拿到地址: $URL" break fi done if [ -z "$URL" ]; then echo " ✗ 40 次都没拿到地址,cloudflared 日志:" tail -30 /root/cf.log exit 1 fi echo echo "===== 3. 从设备自己访问这个公网地址 =====" for i in 1 2 3; do BODY=$(curl -s --max-time 20 "$URL/testpage.txt") if echo "$BODY" | grep -q hello; then echo " 第 $i 次: 成功 -> $BODY" break else echo " 第 $i 次: 还没通(隧道建立中)" sleep 5 fi done echo echo "===== 4. cloudflared 进程状态 =====" ps aux | grep -c '[c]loudflared' echo echo "URL=$URL" > /root/cf_url.txt echo " 地址已存到 /root/cf_url.txt" cat /root/cf_url.txt