#!/bin/bash
# 欢迎界面
function show_welcome() {
echo "###############################################"
echo "# #"
echo "# 欢迎使用Cloudflare官方IP优选工具 #"
echo "# Blog: blog.33375.xyz #"
echo "# Telegram: https://t.me/liutsx #"
echo "# #"
echo "###############################################"
echo
}
# 显示欢迎界面
show_welcome
# 检查是否存在 CloudflareST 文件,如果存在则跳过安装依赖
if [ ! -f "./CloudflareST" ]; then
# 自动安装依赖
ARCH=$(uname -m)
case $ARCH in
x86_64)
echo "正在下载适用于amd64架构的依赖..."
wget -N https://mirror.ghproxy.com/https://github.com/XIU2/CloudflareSpeedTest/releases/download/v2.2.5/CloudflareST_linux_amd64.tar.gz
;;
i386)
echo "正在下载适用于386架构的依赖..."
wget -N https://mirror.ghproxy.com/https://github.com/XIU2/CloudflareSpeedTest/releases/download/v2.2.5/CloudflareST_linux_386.tar.gz
;;
aarch64)
echo "正在下载适用于arm64架构的依赖..."
wget -N https://mirror.ghproxy.com/https://github.com/XIU2/CloudflareSpeedTest/releases/download/v2.2.5/CloudflareST_linux_arm64.tar.gz
;;
*)
echo "不支持此架构的自动安装依赖,请访问 https://github.com/XIU2/CloudflareSpeedTest/releases 下载所需的文件。"
exit 1
;;
esac
# 解压下载的文件
tar -xzvf CloudflareST_linux_*.tar.gz
rm CloudflareST_linux_*.tar.gz ip.txt ipv6.txt cfst_hosts.sh 使用+错误+反馈说明.txt
chmod +x CloudflareST
else
echo "CloudflareST 文件已存在,跳过依赖安装。"
fi
# 检查是否已下载过 Cloudflare IP 列表
if [ ! -f "ip.txt" ]; then
echo "下载 Cloudflare IP 列表..."
curl -s https://www.cloudflare.com/ips-v4 -o ip.txt
else
echo "Cloudflare IP 列表已存在,跳过下载。"
fi
# 配置部分
ZONE_ID="" # 替换为你的Zone ID
API_TOKEN="" # 替换为你的API Token
RECORD_NAME="" # 替换为你想更新的子域名
TTL=60 # DNS TTL (以秒为单位)
PROXIED=false # 是否启用Cloudflare的代理 (true 或 false)
# 步骤1:运行CloudflareST,获取最快的5个IP
echo "运行CloudflareST以找到最佳IP..."
./CloudflareST -url https://spurl.api.030101.xyz/150mb -f ip.txt -tl 200 -dn 100 -n 1000 -allip
# 获取前5个有效的IP
TOP_5_IPS=$(head -n 20 result.csv | awk -F, '{print $1}' | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' | head -n 5)
# 检查是否找到了至少1个有效IP
if [ -z "$TOP_5_IPS" ]; then
echo "在CloudflareST结果中没有找到有效的IP。退出程序..."
exit 1
fi
# 步骤2:获取并删除所有A记录
echo "获取并删除现有的A记录..."
RECORDS=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records?type=A&name=${RECORD_NAME}" \
-H "Authorization: Bearer ${API_TOKEN}" \
-H "Content-Type: application/json" | jq -r '.result[].id')
for RECORD_ID in $RECORDS; do
DELETE_RESPONSE=$(curl -s -X DELETE "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records/${RECORD_ID}" \
-H "Authorization: Bearer ${API_TOKEN}" \
-H "Content-Type: application/json")
if echo "$DELETE_RESPONSE" | grep -q '"success":true'; then
echo "已删除ID为 ${RECORD_ID} 的A记录"
else
echo "删除ID为 ${RECORD_ID} 的A记录失败"
fi
done
# 步骤3:将结果中的5个IP解析到Cloudflare
echo "将前5个IP添加为A记录到Cloudflare..."
for IP in $TOP_5_IPS; do
ADD_RESPONSE=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records" \
-H "Authorization: Bearer ${API_TOKEN}" \
-H "Content-Type: application/json" \
--data "{\"type\":\"A\",\"name\":\"${RECORD_NAME}\",\"content\":\"${IP}\",\"ttl\":${TTL},\"proxied\":${PROXIED}}")
if echo "$ADD_RESPONSE" | grep -q '"success":true'; then
echo "成功添加IP: ${IP} 的A记录"
else
echo "添加IP: ${IP} 的A记录失败"
echo "响应: $ADD_RESPONSE"
fi
done
echo "所有操作已完成。"
CF优选IP并解析到CF域名的脚本,优选官方IP,基于CloudflareST
TG频道原文: Telegram: Contact @liutsx
PS: 是GPT帮我写的