Skip to content

Commit

Permalink
增加openwrt自动化处理脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
frankiejun committed Apr 16, 2024
1 parent da31210 commit 4c9a3d9
Show file tree
Hide file tree
Showing 5 changed files with 365 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cf_RE.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
##用于openwrt安装jq和timeout,确保CloudflareSpeedTestDDNS运行正常。
opkg update
opkg install jq coreutils-timeout
opkg install yq
opkg list-installed | grep jq
opkg list-installed | grep coreutils-timeout
165 changes: 165 additions & 0 deletions cf_ddns
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#!/bin/bash
##版本:V2.1.1
#新功能,支持更新优选完毕后推送至TG,再也不怕脚本没有成功运行了。
#使用脚本需要安装jq和timeout,新增openwrt专用cf_RE.sh文件,运行cf_RE.sh即可在openwrt安装jq和timeout两个扩展。
#其他linux请自行安装jq和timeout。

ipv4Regex="((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])";
#默认关闭小云朵
proxy="false";
#验证cf账号信息是否正确
res=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${zone_id}" -H "X-Auth-Email:$x_email" -H "X-Auth-Key:$api_key" -H "Content-Type:application/json");
resSuccess=$(echo "$res" | jq -r ".success");
if [[ $resSuccess != "true" ]]; then
pushmessage="登陆错误,检查cloudflare账号信息填写是否正确!"
source cf_push;
exit 1;
fi
echo "Cloudflare账号验证成功";

CFST_P=$CFST_DN;
#判断工作模式
if [ "$IP_ADDR" = "ipv6" ] ; then
if [ ! -f "ipv6.txt" ]; then
echo "当前工作模式为ipv6,但该目录下没有【ipv6.txt】,请配置【ipv6.txt】。下载地址:https://github.com/XIU2/CloudflareSpeedTest/releases";
exit 2;
else
echo "当前工作模式为ipv6";
fi
else
echo "当前工作模式为ipv4";
fi

#读取配置文件中的客户端
if [ "$clien" = "6" ] ; then
CLIEN=bypass;
elif [ "$clien" = "5" ] ; then
CLIEN=openclash;
elif [ "$clien" = "4" ] ; then
CLIEN=clash;
elif [ "$clien" = "3" ] ; then
CLIEN=shadowsocksr;
elif [ "$clien" = "2" ] ; then
CLIEN=passwall2;
else
CLIEN=passwall;
fi

#判断是否停止科学上网服务
if [ "$pause" = "false" ] ; then
echo "按要求未停止科学上网服务";
else
/etc/init.d/$CLIEN stop;
echo "已停止$CLIEN";
fi

#判断是否配置测速地址
if [[ "$CFST_URL" == http* ]] ; then
CFST_URL_R="-url $CFST_URL";
else
CFST_URL_R="";
fi

#判断是否使用国家代码来筛选
if [[ "$CCFLAG" == "true" ]]; then
USECC="-c ";
else
USECC="";
fi

if [ ! -z "$CCODE" ]; then
CCODE_IS="-cc $CCODE ";
else
CCODE_IS="";
fi;

if [ ! -z "$CF_ADDR" ];then
CF_ADDR=" -tp $CF_ADDR";
else
CF_ADDR=" -tp 443";
fi;


if [ "$IP_ADDR" = "ipv6" ]; then
#开始优选IPv6
./CloudflareST $CFST_URL_R -t $CFST_T -n $CFST_N -dn $CFST_DN -tl $CFST_TL -tll $CFST_TLL -sl $CFST_SL -p $CFST_P -f ipv6.txt $USECC $CCODE_IS
echo "./CloudflareST $CFST_URL_R -t $CFST_T -n $CFST_N -dn $CFST_DN -tl $CFST_TL -tll $CFST_TLL -sl $CFST_SL -p $CFST_P -f ipv6.txt $CF_ADDR $USECC $CCODE_IS"
else
#开始优选IPv4
echo "./CloudflareST $CFST_URL_R -t $CFST_T -n $CFST_N -dn $CFST_DN -tl $CFST_TL -tll $CFST_TLL -sl $CFST_SL -p $CFST_P $CF_ADDR $USECC $CCODE_IS"
./CloudflareST $CFST_URL_R -t $CFST_T -n $CFST_N -dn $CFST_DN -tl $CFST_TL -tll $CFST_TLL -sl $CFST_SL -p $CFST_P $CF_ADDR $USECC $CCODE_IS
fi
echo "测速完毕";
if [ "$pause" = "false" ] ; then
echo "按要求未重启科学上网服务";
sleep 3s;
else
/etc/init.d/$CLIEN restart;
echo "已重启$CLIEN";
echo "为保证cloudflareAPI连接正常 将在3秒后开始更新域名解析";
sleep 3s;
fi
#开始循环
echo "正在更新域名,请稍后...";
x=0;
csvfile=""
while [[ ${x} -lt $domain_num ]]; do
CDNhostname=${domains[$x]}
#获取优选后的ip地址

if [ "$CCFLAG" = "true" ]; then
CountryCode=${countryCodes[$x]}
csvfile="$CountryCode"".csv"
echo "csvfile:$csvfile"
else
csvfile="result.csv"
fi

#如果没有生成对应结果文件,跳过
if [ ! -e $csvfile ]; then
continue;
fi

ipAddr=$(sed -n "2,1p" $csvfile | awk -F, '{print $1}');
echo "开始更新第$((x + 1))个---$ipAddr";
#开始DDNS
if [[ $ipAddr =~ $ipv4Regex ]]; then
recordType="A";
else
recordType="AAAA";
fi


listDnsApi="https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records?type=${recordType}&name=${CDNhostname}";
createDnsApi="https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records";

res=$(curl -s -X GET "$listDnsApi" -H "X-Auth-Email:$x_email" -H "X-Auth-Key:$api_key" -H "Content-Type:application/json");
recordId=$(echo "$res" | jq -r ".result[0].id");
recordIp=$(echo "$res" | jq -r ".result[0].content");

echo "recordIp:$recordIp"
if [[ $recordIp = "$ipAddr" ]]; then
echo "更新失败,获取最快的IP与云端相同";
resSuccess=false;
elif [[ $recordId = "null" ]]; then
res=$(curl -s -X POST "$createDnsApi" -H "X-Auth-Email:$x_email" -H "X-Auth-Key:$api_key" -H "Content-Type:application/json" --data "{\"type\":\"$recordType\",\"name\":\"$CDNhostname\",\"content\":\"$ipAddr\",\"proxied\":$proxy}");
resSuccess=$(echo "$res" | jq -r ".success");
else
updateDnsApi="https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records/${recordId}";
res=$(curl -s -X PUT "$updateDnsApi" -H "X-Auth-Email:$x_email" -H "X-Auth-Key:$api_key" -H "Content-Type:application/json" --data "{\"type\":\"$recordType\",\"name\":\"$CDNhostname\",\"content\":\"$ipAddr\",\"proxied\":$proxy}");
resSuccess=$(echo "$res" | jq -r ".success");
fi

if [[ "$resSuccess" = "true" ]]; then
echo "$CDNhostname更新成功";
else
echo "$CDNhostname更新失败";
fi

x=$((x + 1));
sleep 3s;
#会生成一个名为informlog的临时文件作为推送的内容。
done > informlog
pushmessage=$(cat informlog);
source cf_push;
exit 0;
25 changes: 25 additions & 0 deletions cf_push
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash
##用于CloudflareSpeedTestDDNS执行情况推送。
echo $pushmessage
message_text=$pushmessage
#解析模式,可选HTML或Markdown
MODE='HTML'
#api接口
#telegramBotToken=$(yq eval ".telegramBotToken" config.yaml)
#telegramBotUserId=$(yq eval ".telegramBotUserId" config.yaml)
URL="https://api.telegram.org/bot${telegramBotToken}/sendMessage"
if [[ -z ${telegramBotToken} ]]; then
echo "未配置TG推送"
else
res=$(timeout 20s curl -s -X POST $URL -d chat_id=${telegramBotUserId} -d parse_mode=${MODE} -d text="${message_text}")
if [ $? == 124 ];then
echo 'TG_api请求超时,请检查网络是否重启完成并是否能够访问TG'
exit 1
fi
resSuccess=$(echo "$res" | jq -r ".ok")
if [[ $resSuccess = "true" ]]; then
echo "TG推送成功";
else
echo "TG推送失败,请检查TG机器人token和ID";
fi
fi
54 changes: 54 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

##cloudflare配置
#cloudflare账号邮箱
x_email:

#域名(多个用逗号分隔)
hostname:

#空间ID
zone_id:

#Global API Key
api_key:


##openwrt科学上网插件配置
#优选节点时是否自动停止科学上网服务 true: 自动停止 false: 不停止 默认为 true
pause:
true
#填写openwrt使用的是哪个科学上网客户端,填写对应的“数字” 默认为 1 客户端为passwall
# 1: passwall 2: passwall2 3: ShadowSocksR Plus+ 4: clash 5: openclash 6: bypass
clien:
1

#测速地址
CFST_URL:
https://cesu.fkj.pp.ua/20m
#测速线程数量;越多测速越快,性能弱的设备 (如路由器) 请勿太高;(默认 200 最多 1000 )
CFST_N: 200
#延迟测速次数;单个 IP 延迟测速次数,为 1 时将过滤丢包的IP,TCP协议;(默认 4 次 )
CFST_T: 1
#下载测速数量;延迟测速并排序后,从最低延迟起下载测速的数量;(默认 10 个)
CFST_DN: 10
#平均延迟上限;只输出低于指定平均延迟的 IP,可与其他上限/下限搭配;(默认9999 ms 这里推荐配置250 ms)
CFST_TL: 250
#平均延迟下限;只输出高于指定平均延迟的 IP,可与其他上限/下限搭配、过滤假墙 IP;(默认 0 ms 这里推荐配置40)
CFST_TLL: 40
#下载速度下限;只输出高于指定下载速度的 IP,凑够指定数量 [-dn] 才会停止测速;(默认 0.00 MB/s 这里推荐5.00MB/s)
CFST_SL: 5
#是否按国家代码做分组筛选(是: true,否: false)
CCFLAG: true
#需要指定的国家代码,多个用逗号分隔(eg. US,JP)
CCODE: US,JP
#CF服务器对应端口
CF_ADDR: 443

##TG推送设置
#(填写即为开启推送,未填写则为不开启)
#TG机器人token 例如:123456789: ABCDEFG...
telegramBotToken:

telegramBotUserId:


114 changes: 114 additions & 0 deletions start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/bin/bash

ipzipfile="txt.zip"

if [[ -e $ipzipfile ]]; then
rm -rf $ipzipfile;
fi;

echo "0.读取配置文件"
if [[ ! -e config.yaml ]]; then
echo "找不到config.yaml配置文件!"
exit -1
fi

x_email=$(yq eval ".x_email" config.yaml)
hostname=$(yq eval ".hostname" config.yaml)
zone_id=$(yq eval ".zone_id" config.yaml)
api_key=$(yq eval ".api_key" config.yaml)
pause=$(yq eval ".pause" config.yaml)
clien=$(yq eval ".clien" config.yaml)
CFST_URL=$(yq eval ".CFST_URL" config.yaml)
CFST_N=$(yq eval ".CFST_N" config.yaml)
CFST_T=$(yq eval ".CFST_T" config.yaml)
CFST_DN=$(yq eval ".CFST_DN" config.yaml)
CFST_TL=$(yq eval ".CFST_TL" config.yaml)
CFST_TLL=$(yq eval ".CFST_TLL" config.yaml)
CFST_SL=$(yq eval ".CFST_SL" config.yaml)
CCFLAG=$(yq eval ".CCFLAG" config.yaml)
CCODE=$(yq eval ".CCODE" config.yaml)
CF_ADDR=$(yq eval ".CF_ADDR" config.yaml)
telegramBotToken=$(yq eval ".telegramBotToken" config.yaml)
telegramBotUserId=$(yq eval ".telegramBotUserId" config.yaml)

IFS=, read -r -a domains <<< "$hostname";
IFS=, read -r -a countryCodes <<< "$CCODE";

domain_num=${#domains[@]}
countryCode_num=${#countryCodes[@]}

if [ ${#domains[@]} -eq 0 ]; then
echo "hostname must be set in config file!";
exit -1;
fi

#检查域名和国家代码是否一一对应
if [ ! -z $CCFLAG ]; then
echo "domain_num:$domain_num, countryCode_num:$countryCode_num"
if [ $domain_num -ne $countryCode_num ]; then
echo "The name and country code must correspond one to one!";
exit -1;
fi
fi;


handle_err() {
echo "Restore background process."
if [ "$clien" = "6" ] ; then
CLIEN=bypass;
elif [ "$clien" = "5" ] ; then
CLIEN=openclash;
elif [ "$clien" = "4" ] ; then
CLIEN=clash;
elif [ "$clien" = "3" ] ; then
CLIEN=shadowsocksr;
elif [ "$clien" = "2" ] ; then
CLIEN=passwall2;
else
CLIEN=passwall;
fi
/etc/init.d/$CLIEN start
}

trap handle_err ERR

echo "1.Download ip file."
for i in {1..3}
do
wget -O $ipzipfile https://zip.baipiao.eu.org

if [ $? != 0 ]; then
echo "get ip file failed, try again"
sleep 1
continue
else
echo "downloaded."
break
fi
done


if [ -e $ipzipfile ]; then
unzip -o $ipzipfile
fi


echo "2.Select the ip address of the desired port."
port=$( yq eval ".CF_ADDR" config.yaml)
if [ -z $port ];then
port=443
fi;

for file in $(find . -type f -name "*-[0-1]-$port.txt"); do
echo "handling: $file"
cat "$file" >> tmp.txt
done

if [ -e tmp.txt ]; then
cat tmp.txt | sort -u > ip.txt
rm -rf tmp.txt
fi


echo "Run scripts to test speed and update dns records."
source cf_ddns

0 comments on commit 4c9a3d9

Please sign in to comment.