脚本功能介绍

  • 添加域名解析
  • 修改域名解析
  • 删除域名解析

申请 CloudFlare API

前往 https://dash.cloudflare.com/profile/api-tokens 登陆后,右上角选择个人资料,点击创建令牌

为了安全,用自定义令牌,没有用全局令牌。如你非要用全局令牌,请修改脚本 Authorization 全部替换为 X-Auth-Key

获取脚本

wget https://blog.wxlost.com/wp-content/uploads/2022/05/cloudFlare.sh && chmod +x cloudFlare.sh

编辑 cloudFlare.sh 内的参数(API与Email)后.运行脚本 添加/更新 域名的解析

bash cloudFlare.sh 域名

如需删除域名解析,使用参数1即可

bash cloudFlare.sh 域名 1

其他问题

仅在IPV4的机器上测试,没有IPV4+IPV6的环境,故此IPV6环境请注意使用。

官方API文档

脚本开源

#!/usr/bin/env bash
#https://blog.wxlost.com/cloudflare-domain.html

#颜色
Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m"

#核心! CloudFlare
Authorization="你的秘钥-如果全局记得替换参数文本" #api
auth_email="你的账号"

echo -e "--- 欢迎使用-脚本初始化中-请稍后 ----"
echo -e "https://blog.wxlost.com/cloudflare-domain.html"
ip=$(curl -s https://api.myip.la)
record_name="$1"
record_del="$2"
zone_name="${record_name#*.}"

if [ "${record_name}" = '' ]; then
    echo -e "error: 域名为空!!"
    exit 1
fi
if [ "${record_del}" = '' ]; then
    record_del="0"
fi

function int(){
#获取域ID
zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" -H "X-Auth-Email: $auth_email" -H "Authorization: $Authorization" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )
if [ "${zone_identifier}" = '' ]; then
    echo && echo -e "${Red_font_prefix}域ID 获取失败!${Font_color_suffix}" && exit 0;
fi
#获取域名ID
record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $auth_email" -H "Authorization: $Authorization" -H "Content-Type: application/json"  | grep -Po '(?<="id":")[^"]*')
#获取域名IP
Domain_name_status=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $auth_email" -H "Authorization: $Authorization" -H "Content-Type: application/json"  | grep -Po '(?<="content":")[^"]*')
}

function Domain_name_status(){
int
if [ "${record_del}" = '0' ]; then
        if [ "${ip}" == "${Domain_name_status}" ]; then
        echo && echo -e "${Red_font_prefix}域名已存在且IP$1 与本机IP$ip一致,无需操作!${Font_color_suffix}"&& echo -e "${Red_font_prefix}如需强制删除: bash xxx.sh xxx.xxx.com 1${Font_color_suffix}" && exit 0;
        fi
    else
    Domain_delete
fi
}

function Domain_go(){
if [ "${record_identifier}" = '' ]; then
    echo -e "${Red_font_prefix}info: 域名首次解析,开始新增解析${Font_color_suffix}"
    Domain_add
    exit 0
    else
    echo -e "${Red_font_prefix}info: 域名存在解析,开始更新解析${Font_color_suffix}"
    Domain_update
    exit 0
fi
}


function Domain_add(){
#添加
result=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records" \
     -H "X-Auth-Email: $auth_email" \
     -H "Authorization: $Authorization" \
     -H "Content-Type: application/json" \
     --data '{"type":"A","name":"'"$record_name"'","content":"'"$ip"'","ttl":1,"priority":10,"proxied":false}')
int
if [[ $result == *"\"success\":false"* ]]; then
    message="API ADD FAILED. DUMPING RESULTS:\n$result"
    echo -e "$message"
    #exit 1 
else
    message="${Green_font_prefix}域名: ${Font_color_suffix} $record_name 解析为:$Domain_name_status 完成!"
    echo -e "$message"
    exit 0
fi
}

function Domain_update(){
#更新
result=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" \
     -H "X-Auth-Email: $auth_email" \
     -H "Authorization: $Authorization" \
     -H "Content-Type: application/json" \
     --data '{"type":"A","name":"'"$record_name"'","content":"'"$ip"'","ttl":1,"proxied":false}')
int
if [[ $result == *"\"success\":false"* ]]; then
    message="API UPDATE FAILED. DUMPING RESULTS:\n$result"
    echo -e "$message"
    #exit 1 
else
    message="${Green_font_prefix}域名: ${Font_color_suffix} $record_name 更新解析为:$Domain_name_status 完成!"
    echo -e "$message"
    exit 0
fi
}
function Domain_delete(){
#删除
result=$(curl -s -X DELETE "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" \
     -H "X-Auth-Email: $auth_email" \
     -H "Authorization: $Authorization" \
     -H "Content-Type: application/json")

if [[ $result == *"\"success\":false"* ]]; then
    message="API DELETE FAILED. DUMPING RESULTS:\n$result"
    echo -e "$message"
    #exit 1 
else
    message="${Green_font_prefix}域名: ${Font_color_suffix} $record_name $Domain_name_status 删除完成!"
    echo -e "$message"
    exit 0
fi
}

function Domain_list(){
#查询-备用
result=$(curl -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" \
     -H "X-Auth-Email: $auth_email" \
     -H "Authorization: $Authorization" \
     -H "Content-Type: application/json")
}

#判断式菜单
Domain_name_status
#clear
echo -e "${Green_font_prefix}CloudFlare api${Font_color_suffix} ${Red_font_prefix}[v1.0]${Font_color_suffix} 
域名:${Red_font_prefix}$1${Font_color_suffix} 本机IP:${Red_font_prefix}$ip${Font_color_suffix}
--- 欢迎使用 ----
"
Domain_go

# echo -e "待解析域名为${Green_font_prefix} $1 ${Font_color_suffix},本机IP为${Green_font_prefix} $ip ${Font_color_suffix},继续吗?${Green_font_prefix}[Y/n]${Font_color_suffix}"
# read -p "(默认: y):" yn
# [[ -z "${yn}" ]] && yn="y"
# if [[ ${yn} == [Yy] ]]; then
# echo -e "开始执行解析操作!"
# Domain_go
# else
# echo && echo "已取消本次操作..." && echo && exit 0;
# fi