cloudflare-ddns-updater/cloudflare-template.sh

108 lines
3.8 KiB
Bash
Raw Normal View History

2020-11-09 12:54:09 +00:00
#!/bin/bash
set -u
2020-11-09 12:54:09 +00:00
auth_email="${DDNS_AUTH_EMAIL:-}" # The email used to login 'https://dash.cloudflare.com'
auth_method="${DDNS_AUTH_METHOD:-token}" # Set to "global" for Global API Key or "token" for Scoped API Token
auth_key="${DDNS_AUTH_KEY:-}" # Your API Token or Global API Key
zone_identifier="${DDNS_ZONE_IDENTIFIER:-}" # Can be found in the "Overview" tab of your domain
record_name="${DDNS_RECORD_NAME:-}" # Which record you want to be synced
proxy="${DDNS_PROXY:-false}" # Set the proxy to true or false
2020-11-09 12:54:09 +00:00
###########################################
## Validate variables have been set
###########################################
if [ -z "${auth_email}" ]; then
logger "DDNS Updater: DDNS_AUTH_EMAIL was unset"
exit 1
fi
if [ -z "${auth_method}" ]; then
logger "DDNS Updater: DDNS_AUTH_METHOD was unset"
exit 1
fi
if [ -z "${auth_key}" ]; then
logger "DDNS Updater: DDNS_AUTH_KEY was unset"
exit 1
fi
if [ -z "${zone_identifier}" ]; then
logger "DDNS Updater: DDNS_ZONE_IDENTIFIER was unset"
exit 1
fi
if [ -z "${record_name}" ]; then
logger "DDNS Updater: DDNS_RECORD_NAME was unset"
exit 1
fi
if [ -z "${proxy}" ]; then
logger "DDNS Updater: DDNS_PROXY was unset"
exit 1
fi
2021-07-19 00:49:53 +00:00
2020-11-09 12:54:09 +00:00
###########################################
## Check if we have a public IP
2020-11-09 12:54:09 +00:00
###########################################
ip=$(curl -s https://api.ipify.org || curl -s https://ipv4.icanhazip.com/)
2021-07-19 12:57:52 +00:00
2020-11-09 12:54:09 +00:00
if [ "${ip}" == "" ]; then
2021-07-21 13:51:41 +00:00
logger -s "DDNS Updater: No public IP found"
2020-11-09 12:54:09 +00:00
exit 1
fi
###########################################
## Check and set the proper auth header
###########################################
if [ "${auth_method}" == "global" ]; then
2021-07-19 12:18:24 +00:00
auth_header="X-Auth-Key:"
else
auth_header="Authorization: Bearer"
fi
2020-11-09 12:54:09 +00:00
###########################################
## Seek for the A record
###########################################
logger "DDNS Updater: Check Initiated"
2021-07-19 18:52:27 +00:00
record=$(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 "$auth_header $auth_key" -H "Content-Type: application/json")
2020-11-09 12:54:09 +00:00
###########################################
## Check if the domain has an A record
2020-11-09 12:54:09 +00:00
###########################################
if [[ $record == *"\"count\":0"* ]]; then
2021-07-21 13:51:41 +00:00
logger -s "DDNS Updater: Record does not exist, perhaps create one first? (${ip} for ${record_name})"
2020-11-09 12:54:09 +00:00
exit 1
fi
###########################################
## Get existing IP
2020-11-09 12:54:09 +00:00
###########################################
old_ip=$(echo "$record" | grep -Po '(?<="content":")[^"]*' | head -1)
# Compare if they're the same
if [[ $ip == "${old_ip}" ]]; then
logger "DDNS Updater: IP ($ip) for ${record_name} has not changed."
2020-11-09 12:54:09 +00:00
exit 0
fi
###########################################
## Set the record identifier from result
###########################################
record_identifier=$(echo "$record" | grep -Po '(?<="id":")[^"]*' | head -1)
###########################################
## Change the IP@Cloudflare using the API
###########################################
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 "$auth_header $auth_key" \
2020-11-09 12:54:09 +00:00
-H "Content-Type: application/json" \
--data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"proxied\":${proxy},\"name\":\"$record_name\",\"content\":\"$ip\"}")
###########################################
## Report the status
###########################################
case "$update" in
*"\"success\":false"*)
2021-07-21 13:51:41 +00:00
logger -s "DDNS Updater: $ip $record_name DDNS failed for $record_identifier ($ip). DUMPING RESULTS:\n$update"
2020-11-09 12:54:09 +00:00
exit 1;;
*)
logger "DDNS Updater: $ip $record_name DDNS updated."
2020-11-09 12:54:09 +00:00
exit 0;;
esac