VPS利用CloudFlare API来设置DDNS
第一步:做VPS的基础安装脚本
先升级一下版本
apt update apt upgrade
再安装常用命令
apt install -y cron apt install -y socat apt install -y vim apt install -y curl
打开默认的BBR
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf sysctl -p
看看解锁情况
bash <(curl -L -s check.unlock.media)
装个X-UI
bash <(curl -Ls https://raw.githubusercontent.com/FranzKafkaYu/x-ui/master/install.sh)
安装SSL证书
curl https://get.acme.sh | sh ~/.acme.sh/acme.sh --register-account -m 你的邮箱 ~/.acme.sh/acme.sh --issue -d 你的域名 --standalone ~/.acme.sh/acme.sh --installcert -d 你的域名 --key-file /root/private.key --fullchain-file /root/cert.crt
再设置证书自动更新
~/.acme.sh/acme.sh --upgrade --auto-upgrade
自动运行重启X-UI
0 4 * * * systemctl restart x-ui
第二步:获取CloudFlare Global API
https://dash.cloudflare.com/profile/api-tokens
设置DDNS域名,比如tw.xxx.xxx,然后把tw的A记录指向1.1.1.1,并关闭CloudFlare代理开关
下载cf-ddns脚本
wget -N --no-check-certificate https://raw.githubusercontent.com/yulewang/cloudflare-api-v4-ddns/master/cf-v4-ddns.sh
编辑cf-ddns脚本
vi cf-v4-ddns.sh
# API key, see https://www.cloudflare.com/a/account/my-account, # incorrect api-key results in E_UNAUTH error CFKEY=您的Global API Key # Username, eg: user@example.com CFUSER=您Cloudflare的帐户名称 # Zone name, eg: example.com CFZONE_NAME=您的域名 # Hostname to update, eg: homeserver.example.com CFRECORD_NAME=你的二级域名
修改cf-ddns脚本权限
chmod +x cf-v4-ddns.sh
并运行脚本
./cf-v4-ddns.sh
设定定时任务自动更新ddns的IP
crontab -e
代表每分钟更新ddns
*/1 * * * * /root/cf-v4-ddns.sh >/dev/null 2>&1
第三步:监测Netflix掉解锁自动更换IP
下载ipc.sh,这个脚本主要用来监测是否被墙,如果监测到被墙,那么就会自动换IP
wget -N --no-check-certificate https://raw.githubusercontent.com/kaze-kaze/ipchange/refs/heads/main/ipc.sh
把更换IP的API以及telegram机器人信息填写进去
#!/bin/bash # 获取IP地址 ip_address=$(curl -s ifconfig.me) # 执行ping命令,并检查结果 if ping -c 5 -W 2 -i 0.2 www.itdog.cn | grep "100% packet loss" > /dev/null then echo "当前IP已经被封锁,正在尝试换IP..." # 向Telegram Bot发送消息 curl -s "https://api.telegram.org/bot7568792273:AAFz7WNMB78-_5_f6XXXXXn0ktQaasX64Y/sendMessage" \ -d "chat_id=1208188888" \ -d "text=当前IP已经被封锁,正在尝试换IP..." # 执行换IP的命令 curl https://api.pqs.pw/ipch/xy9999nmus echo "IP已经更换完成。" else echo "当前IP未被封锁" fi
注:curl命令后面可以加“”也可以不加,加的话代表这是一个整体的命令行(因为有些有特殊字符)
更改权限,并运行此脚本
chmod +x ipc.sh ./ipc.sh
接下来下载nf.sh,这个脚本主要用来监测IP是否被Netflix封锁,如果监测到被封,那就进行自动更换IP的操作
wget -N --no-check-certificate https://raw.githubusercontent.com/kaze-kaze/ipchange/refs/heads/main/nf.sh
#!/bin/bash # 启动: nohup bash ./nf.sh > /dev/null 2>&1 & # 停止: ps aux | grep nf.sh | grep -v grep | awk '{print $2}' | xargs kill -9 IPv="4" # 使用 IPv4 还是 IPv6 检测 UA_Browser="Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0" url="https://www.netflix.com/title/70143836" # Netflix 自制剧URL (絕命毒師) log="/root/ip.txt" # 日志文件 # 设置检测间隔(单位: 分钟) interval=1 check_nf() { curl -${IPv}fsL -A "${UA_Browser}" -w %{http_code} -o /dev/null -m 10 "${url}" 2>&1 } # check_nf; # 200: unblock, 404: block while true; do code=$(check_nf) # echo $code if [ "$code" = "404" ]; then date +"%Y-%m-%d %H:%M:%S" >> "$log" # 不解锁时, 调用API自动更换IP curl "https://api.pqs.pw/ipch/xy99999mus" >> "$log" curl -s -X POST \ https://api.telegram.org/bot7568799999:AAFz7WNMB78-_5_f6tRs99999ktQaasX64Y/sendMessage \ -d chat_id=1208199999 \ -d text="Netflix正在尝试为您更换IP,请这位爷稍等片刻,最多五分钟……" fi # 休眠 interval 分钟 sleep $((60 * interval)) done
填入更换IP的API,以及telegram的机器人信息,然后按照下面的命令行进行更改权限并运行
chmod +x nf.sh nohup bash ./nf.sh > /dev/null 2>&1 &
然后把ipc.sh和nf.sh加入crontab自动运行
*/5 * * * * bash /root/ipc.sh >/dev/null 2>&1 */1 * * * * /root/nf.sh > /dev/null 2>&1
注:ipc.sh设置为每5分钟执行一次,nf.sh设置为每1分钟执行一次
参考文章: