From 59e6cef27cf17ba5e3df8211fa2358e3a4f3bdef Mon Sep 17 00:00:00 2001 From: starry <115192496+sky22333@users.noreply.github.com> Date: Tue, 23 Sep 2025 15:45:56 +0800 Subject: [PATCH] Update cf-tunnel.sh --- dev/cf-tunnel.sh | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/dev/cf-tunnel.sh b/dev/cf-tunnel.sh index 582a886..f39380a 100644 --- a/dev/cf-tunnel.sh +++ b/dev/cf-tunnel.sh @@ -1,8 +1,6 @@ #!/bin/bash # https://github.com/sky22333/shell - set -e - # === 颜色定义 === RED='\033[0;31m' GREEN='\033[0;32m' @@ -10,11 +8,17 @@ YELLOW='\033[1;33m' BLUE='\033[1;34m' NC='\033[0m' # 清除颜色 -CLOUDFLARED_URL="https://github.com/cloudflare/cloudflared/releases/download/2025.8.0/cloudflared-linux-amd64" +LOC=$(curl -s https://www.cloudflare.com/cdn-cgi/trace | grep 'loc=' | cut -d= -f2) + +if [[ "$LOC" == "CN" ]]; then + CLOUDFLARED_URL="https://gh-proxy.com/https://github.com/cloudflare/cloudflared/releases/download/2025.9.1/cloudflared-linux-amd64" +else + CLOUDFLARED_URL="https://github.com/cloudflare/cloudflared/releases/download/2025.9.1/cloudflared-linux-amd64" +fi + CLOUDFLARED_BIN="/usr/local/bin/cloudflared" SERVICE_PATH="/etc/systemd/system/cloudflared-tunnel.service" LOG_PATH="/var/log/cloudflared.log" - # 检查 cloudflared 是否已存在 if [[ -f "$CLOUDFLARED_BIN" ]]; then echo -e "${GREEN}已存在文件,跳过下载。${NC}" @@ -26,7 +30,6 @@ else fi chmod +x "$CLOUDFLARED_BIN" fi - # 检查服务是否存在 SERVICE_EXISTS=false if sudo systemctl list-units --full --all | grep -q 'cloudflared-tunnel.service'; then @@ -46,26 +49,20 @@ if sudo systemctl list-units --full --all | grep -q 'cloudflared-tunnel.service' echo -e "${YELLOW}将保留旧服务配置,仅修改穿透地址${NC}" fi fi - # 用户选择运行模式 echo "" echo -e "${YELLOW}请选择运行模式:${NC}" echo "1) 临时运行(前台运行并显示临时访问域名)" echo "2) 后台运行(自动配置后台服务并显示访问域名)" read -p "请输入 1 或 2: " MODE - # 输入内网地址 read -p "请输入要穿透的本地地址(例如 127.0.0.1:8080): " LOCAL_ADDR - if [[ "$MODE" == "1" ]]; then echo -e "${BLUE}正在前台运行 cloudflared...${NC}" - LOGFILE=$(mktemp) stdbuf -oL "$CLOUDFLARED_BIN" tunnel --url "$LOCAL_ADDR" 2>&1 | tee "$LOGFILE" & PID=$! - echo -e "${YELLOW}等待 cloudflared 输出访问域名...${NC}" - for i in {1..60}; do DOMAIN=$(grep -oP 'https://[a-zA-Z0-9-]+\.trycloudflare\.com' "$LOGFILE" | head -n1) if [[ -n "$DOMAIN" ]]; then @@ -77,26 +74,21 @@ if [[ "$MODE" == "1" ]]; then fi sleep 1 done - echo -e "${RED}超时未能获取临时域名,日志保存在:$LOGFILE${NC}" kill $PID 2>/dev/null || true exit 1 - elif [[ "$MODE" == "2" ]]; then echo -e "${BLUE}正在配置 systemd 服务...${NC}" - if [[ "$SERVICE_EXISTS" == false ]]; then sudo bash -c "cat > $SERVICE_PATH" <