#!/bin/bash
update()
{
mkdir cfip 2>/dev/null
cd cfip
seq 10 | xargs -I {} ping -c 1 -W 1 help.1996999.xyz &>/dev/null
curl -k -o cf.sh https://help.1996999.xyz/scancf/scancf_tool/cf.sh
curl -k -o speedlog.txt --user 999:999 https://help.1996999.xyz/scancf/openwrt/cf_speed_result.txt


cat speedlog.txt | awk '$2+0.0 >= 3.0' |  head -n 200 > cfip.txt
rm log.txt 2>/dev/null
chmod 777 cf.sh
./cf.sh -c -s 1>&2
cat log.txt > cfip.txt
rm log.txt 2>/dev/null
./cf.sh -c -m 1 1>&2

iplist=$(cat speedlog.txt | awk 'NR>=2{print $1'}) 
ip="" 
for i in $iplist 
do 
rsp=$(curl --resolve www.baidu.com:80:$i http://www.baidu.com/ --connect-timeout 5 -m 5 2>/dev/null | grep "baidu")
if [ "$rsp"x = x ];then 
ip=$i 
break 
fi 
done

seq 3 | xargs -I {} curl -k -X PUT "https://api.1996999.xyz/client/v4/zones/5dc030dafd76c76901ebf09fbfbd0620/dns_records/655f937869782d46b2ab23dd8b523d82" -H "X-Auth-Email: zengge96@gmail.com " -H "X-Auth-Key:725d1c0a814a6242d56e45715ba075a305213" -H "Content-Type: application/json" --data '{"type":"A","name":"zengge96.eu.org","content":"'$ip'","proxied":false}'

cp speedlog.txt ../
cd ..
rm -r cfip
echo
echo "ipout$ip"
}

ip=$(update | grep ipout | tr -d "ipout")
#clear
cat speedlog.txt
echo "dns resolves to:$ip" 

#{
while :
do
rsp="1"
rsp1="1"
while [ ! "$rsp"x = x ] || [ ! "$rsp1"x = x ]
do
    sleep 30
    rsp=$(curl --resolve valid.scan.cf:80:$ip http://valid.scan.cf/cdn-cgi/trace --connect-timeout 5 -m 5 2>/dev/null | grep 'h=\|colo=' | tr '\n' ' ' | sed "s/^/ip=$ip &/g" | grep 'valid' | grep 'colo=')
    rsp1=$(curl --resolve icook.tw:443:$ip https://icook.tw/cdn-cgi/trace --connect-timeout 5 -m 5 2>/dev/null | grep 'icook')
done
ip=$(update | grep ipout | tr -d "ipout")
done
#}&



