#! /bin/bash

#本机公网IP
ip=$(curl -s "ifconfig.me")

#CloudFlare API 参数
auth_email="cf_email"
auth_key="cf_key" # 在cloudflare帐户设置中找到
zone_name="example.com"
record_name="www.example.com"

#Oracle日志文件路径
oracle_log_file="/root/oracle_Replace/oracle_Replace_ip.log"

# cf相关文件路径
cf_ip_file="/root/oracle_Replace/cf_ip.txt"
id_file="/root/oracle_Replace/cloudflare.ids"
log_file="/root/oracle_Replace/cloudflare.log"

# CF记录
log() {
    if [ "$1" ]; then
        echo -e "[$(date)] - $1" >> $log_file
    fi
}

#CF DDNS主程序
CloudFlare_DDNS(){
    log "检查启动"

if [ -f $cf_ip_file ]; then
    old_ip=$(cat $cf_ip_file)
    if [ $ip == $old_ip ]; then
        echo "IP has not changed."
        exit 0
    fi
fi

if [ -f $id_file ] && [ $(wc -l $id_file | cut -d " " -f 1) == 2 ]; then
    zone_identifier=$(head -1 $id_file)
    record_identifier=$(tail -1 $id_file)
else
    zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )
    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 "X-Auth-Key: $auth_key" -H "Content-Type: application/json"  | grep -Po '(?<="id":")[^"]*')
    echo "$zone_identifier" > $id_file
    echo "$record_identifier" >> $id_file
fi

update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\"}")

if [[ $update == *"\"success\":false"* ]]; then
    message="API UPDATE FAILED. DUMPING RESULTS:\n$update"
    log "$message"
    echo -e "$message"
    exit 1 
else
    message="IP changed to: $ip"
    echo "$ip" > $cf_ip_file
    log "$message"
    echo "$message"
fi
}

#甲骨文更换ip
Replace_IP(){
#my_ip="150.230.125.187"
my_ip=$(curl -s "ifconfig.me")
private_ip_id=$(oci network public-ip get --public-ip-address $my_ip | grep "private-ip-id" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')
public_ip_id=$(oci network public-ip get --public-ip-address $my_ip | grep '"id"' | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')
compartment_id=$(oci network public-ip get --public-ip-address $my_ip | grep "compartment-id" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')
echo -e "删除临时公共IP...."
oci network public-ip delete --public-ip-id $public_ip_id --force
echo "分配临时公共ip..."
echo -e "新公共IP为：$(oci network public-ip create --compartment-id $compartment_id --lifetime EPHEMERAL --private-ip-id $private_ip_id | grep "ip-address" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')"
}

#检测是否可以访问Netflix

MediaUnlockTest_Netflix(){
    local result1=$(curl --user-agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36" -fsL --write-out %{http_code} --output /dev/null --max-time 10 "https://www.netflix.com/title/81215567" 2>&1)
        
    if [[ "$result1" == "404" ]];then
        Netflix="N"
        return;
                
        elif  [[ "$result1" == "403" ]];then
        Netflix="N"
        return;
                
        elif [[ "$result1" == "200" ]];then
        Netflix="Y"
        return;
                
        elif  [[ "$result1" == "000" ]];then
                Netflix="N"
        return;
    fi   
}

#脚本开始
count=`ps -ef | grep brook | grep -v "grep" | wc -l`
time=`date +"%Y-%m-%d %H:%M:%S"`
MediaUnlockTest_Netflix
if [ "$Netflix" == "N" ]; then
Replace_IP
echo -e "${time}：IP已更换为：$(oci network public-ip get --private-ip-id $private_ip_id | grep "ip-address" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')" >> $oracle_log_file
CloudFlare_DDNS
else
echo -e "${time}：IP(${ip})解锁NetFlix" >> $oracle_log_file
fi