Use functions

pull/77/head
Rui Coelho 1 year ago
parent ac2f74f980
commit 703cac8d18
No known key found for this signature in database
GPG Key ID: 6AFF33C32F7DB31B
  1. 56
      cloudflare-template.sh

@ -14,10 +14,17 @@ slackuri="" # URI for Slack WebHook "htt
discorduri="" # URI for Discord WebHook "https://discordapp.com/api/webhooks/xxxxx" discorduri="" # URI for Discord WebHook "https://discordapp.com/api/webhooks/xxxxx"
########################################### validate_variables(){
## Check if we have a public IP # Validate required variables
########################################### if [[ -z $auth_email || -z $auth_key || -z $zone_identifier || -z $record_name ]]; then
echo "Missing required variables. Please provide values for 'auth_email', 'auth_key', 'zone_identifier', and 'record_name'."
exit 1
fi
}
check_ipv4_is_available(){
ipv4_regex='([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])' ipv4_regex='([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])'
ip=$(curl -s -4 https://cloudflare.com/cdn-cgi/trace | grep -E '^ip'); ret=$? ip=$(curl -s -4 https://cloudflare.com/cdn-cgi/trace | grep -E '^ip'); ret=$?
if [[ ! $ret == 0 ]]; then # In the case that cloudflare failed to return an ip. if [[ ! $ret == 0 ]]; then # In the case that cloudflare failed to return an ip.
# Attempt to get the ip from other websites. # Attempt to get the ip from other websites.
@ -32,61 +39,51 @@ if [[ ! $ip =~ ^$ipv4_regex$ ]]; then
logger -s "DDNS Updater: Failed to find a valid IP." logger -s "DDNS Updater: Failed to find a valid IP."
exit 2 exit 2
fi fi
}
########################################### set_auth_headers(){
## Check and set the proper auth header
###########################################
if [[ "${auth_method}" == "global" ]]; then if [[ "${auth_method}" == "global" ]]; then
auth_header="X-Auth-Key:" auth_header="X-Auth-Key:"
else else
auth_header="Authorization: Bearer" auth_header="Authorization: Bearer"
fi fi
}
###########################################
## Seek for the A record
###########################################
check_if_a_record_exists(){
logger "DDNS Updater: Check Initiated" logger "DDNS Updater: Check Initiated"
record=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?type=A&name=$record_name" \ record=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?type=A&name=$record_name" \
-H "X-Auth-Email: $auth_email" \ -H "X-Auth-Email: $auth_email" \
-H "$auth_header $auth_key" \ -H "$auth_header $auth_key" \
-H "Content-Type: application/json") -H "Content-Type: application/json")
###########################################
## Check if the domain has an A record
###########################################
if [[ $record == *"\"count\":0"* ]]; then if [[ $record == *"\"count\":0"* ]]; then
logger -s "DDNS Updater: Record does not exist, perhaps create one first? (${ip} for ${record_name})" logger -s "DDNS Updater: Record does not exist, perhaps create one first? (${ip} for ${record_name})"
exit 1 exit 1
fi fi
}
########################################### get_current_ip_from_cloudflare(){
## Get existing IP
###########################################
old_ip=$(echo "$record" | sed -E 's/.*"content":"(([0-9]{1,3}\.){3}[0-9]{1,3})".*/\1/') old_ip=$(echo "$record" | sed -E 's/.*"content":"(([0-9]{1,3}\.){3}[0-9]{1,3})".*/\1/')
# Compare if they're the same # Compare if they're the same
if [[ $ip == $old_ip ]]; then if [[ $ip == $old_ip ]]; then
logger "DDNS Updater: IP ($ip) for ${record_name} has not changed." logger "DDNS Updater: IP ($ip) for ${record_name} has not changed."
exit 0 exit 0
fi fi
}
########################################### update_ip_on_cloudflare(){
## Set the record identifier from result
###########################################
record_identifier=$(echo "$record" | sed -E 's/.*"id":"([A-Za-z0-9_]+)".*/\1/') record_identifier=$(echo "$record" | sed -E 's/.*"id":"([A-Za-z0-9_]+)".*/\1/')
###########################################
## Change the IP@Cloudflare using the API
###########################################
update=$(curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" \ update=$(curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" \
-H "X-Auth-Email: $auth_email" \ -H "X-Auth-Email: $auth_email" \
-H "$auth_header $auth_key" \ -H "$auth_header $auth_key" \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
--data "{\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\",\"ttl\":\"$ttl\",\"proxied\":${proxy}}") --data "{\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\",\"ttl\":\"$ttl\",\"proxied\":${proxy}}")
}
########################################### send_webhooks(){
## Report the status
###########################################
case "$update" in case "$update" in
*"\"success\":false"*) *"\"success\":false"*)
echo -e "DDNS Updater: $ip $record_name DDNS failed for $record_identifier ($ip). DUMPING RESULTS:\n$update" | logger -s echo -e "DDNS Updater: $ip $record_name DDNS failed for $record_identifier ($ip). DUMPING RESULTS:\n$update" | logger -s
@ -121,3 +118,14 @@ case "$update" in
fi fi
exit 0;; exit 0;;
esac esac
}
main(){
validate_variables
check_ipv4_is_available
set_auth_headers
check_if_a_record_exists
get_current_ip_from_cloudflare
update_ip_on_cloudflare
send_webhooks
}
Loading…
Cancel
Save