Update cloudflare-template.sh

Proposed fix for double sending webhooks even if IP Address did not change. What it does is it saves the ip address on a file ".cf-wan_ip_<name of record>.txt" and fetches this text file every update making sure that it is comparing a valid IP address.
This commit is contained in:
psycho-neon 2023-01-06 22:07:43 +03:00 committed by GitHub
parent 88c73e30f8
commit 31989c5298
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 10 deletions

View File

@ -33,6 +33,25 @@ if [[ ! $ip =~ ^$ipv4_regex$ ]]; then
exit 2 exit 2
fi fi
###########################################
## Check for the old IP record
###########################################
wan_ip_record=$HOME/.cf-wan_ip_$record_name.txt
if [ -f $wan_ip_record ]; then
old_ip=`cat $wan_ip_record`
else
logger "DDNS Updater: No old IP record found."
old_ip=""
fi
###########################################
## Compare existing IP record to new IP
###########################################
if [[ $ip == $old_ip ]]; then
logger "DDNS Updater: IP ($ip) for ${record_name} has not changed."
exit 0
fi
########################################### ###########################################
## Check and set the proper auth header ## Check and set the proper auth header
########################################### ###########################################
@ -60,16 +79,6 @@ if [[ $record == *"\"count\":0"* ]]; then
exit 1 exit 1
fi fi
###########################################
## Get existing IP
###########################################
old_ip=$(echo "$record" | sed -E 's/.*"content":"(([0-9]{1,3}\.){3}[0-9]{1,3})".*/\1/')
# Compare if they're the same
if [[ $ip == $old_ip ]]; then
logger "DDNS Updater: IP ($ip) for ${record_name} has not changed."
exit 0
fi
########################################### ###########################################
## Set the record identifier from result ## Set the record identifier from result
########################################### ###########################################
@ -106,6 +115,10 @@ case "$update" in
exit 1;; exit 1;;
*) *)
logger "DDNS Updater: $ip $record_name DDNS updated." logger "DDNS Updater: $ip $record_name DDNS updated."
# echo our new ip to the record
echo $ip > $wan_ip_record
if [[ $slackuri != "" ]]; then if [[ $slackuri != "" ]]; then
curl -L -X POST $slackuri \ curl -L -X POST $slackuri \
--data-raw '{ --data-raw '{