From ae1afb952f89779385b32d3bfb1f0ebf394915c0 Mon Sep 17 00:00:00 2001 From: Tine Jozelj Date: Tue, 18 Jul 2023 13:43:22 +0200 Subject: [PATCH] feat: ipv6 support added --- dyndns.sh | 143 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 80 insertions(+), 63 deletions(-) diff --git a/dyndns.sh b/dyndns.sh index 0d9709c..2d34f8f 100755 --- a/dyndns.sh +++ b/dyndns.sh @@ -1,13 +1,14 @@ #!/bin/bash +set -euo pipefail api_host="https://api.digitalocean.com/v2" sleep_interval=${SLEEP_INTERVAL:-300} remove_duplicates=${REMOVE_DUPLICATES:-"false"} +# Only services with ipv6 supported are listed here. services=( "ifconfig.co" - "ipinfo.io/ip" - "ifconfig.me" + "ifconfig.io" ) die() { @@ -22,6 +23,68 @@ test -z $NAME && die "NAME not set!" dns_list="$api_host/domains/$DOMAIN/records" +configure_record() { + # disable glob expansion + set -f + + ip=$1 + type=$2 + + for sub in ${NAME//;/ }; do + record_id=$(echo $domain_records| jq ".domain_records[] | select(.type == \"$type\" and .name == \"$sub\") | .id") + record_data=$(echo $domain_records| jq -r ".domain_records[] | select(.type == \"$type\" and .name == \"$sub\") | .data") + + if [ $(echo "$record_id" | wc -l) -ge 2 ]; then : + if [[ "${remove_duplicates}" == "true" ]]; then : + echo "'$sub' domain name has duplicate DNS records, removing duplicates" + record_id_to_delete=$(echo "$record_id"| tail -n +2) + record_id=$(echo "$record_id"| head -1) + record_data=$(echo "$record_data"| head -1) + + while IFS= read -r line; do + curl -s -X DELETE \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ + "$dns_list/$line" &> /dev/null + done <<< "$record_id_to_delete" + else : + echo "Unable to update '$sub' domain name as it has duplicate DNS records. Set REMOVE_DUPLICATES='true' to remove them." + continue + fi + fi + + # re-enable glob expansion + set +f + + data="{\"type\": \"A\", \"name\": \"$sub\", \"data\": \"$ip\"}" + url="$dns_list/$record_id" + + if [[ -z $record_id ]]; then + echo "No record found with '$sub' domain name. Creating record, sending data=$data to url=$url" + + new_record=$(curl -s -X POST \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ + -d "$data" \ + "$url") + + record_data=$(echo $new_record| jq -r ".data") + fi + + if [[ "$ip" != "$record_data" ]]; then + echo "existing DNS record address ($record_data) doesn't match current IP ($ip), sending data=$data to url=$url" + + curl -s -X PUT \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ + -d "$data" \ + "$url" &> /dev/null + else + echo "existing DNS record address ($record_data) did not need updating" + fi + done +} + while ( true ); do domain_records=$(curl -s -X GET \ -H "Content-Type: application/json" \ @@ -31,72 +94,26 @@ while ( true ); do for service in ${services[@]}; do echo "Trying with $service..." - ip="$(curl -s $service | grep '[0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}')" - test -n "$ip" && break + ipv4="$(curl -4 -s -f $service)" + ipv6="$(curl -6 -s -f $service)" + + test -n "$ipv4$ipv6" && break done - echo "Found IP address $ip" + echo "Found IPv4 address $ipv4" + echo "Found IPv6 address $ipv6" - if [[ -n $ip ]]; then - # disable glob expansion - set -f - - for sub in ${NAME//;/ }; do - record_id=$(echo $domain_records| jq ".domain_records[] | select(.type == \"A\" and .name == \"$sub\") | .id") - record_data=$(echo $domain_records| jq -r ".domain_records[] | select(.type == \"A\" and .name == \"$sub\") | .data") - - if [ $(echo "$record_id" | wc -l) -ge 2 ]; then : - if [[ "${remove_duplicates}" == "true" ]]; then : - echo "'$sub' domain name has duplicate DNS records, removing duplicates" - record_id_to_delete=$(echo "$record_id"| tail -n +2) - record_id=$(echo "$record_id"| head -1) - record_data=$(echo "$record_data"| head -1) - - while IFS= read -r line; do - curl -s -X DELETE \ - -H "Content-Type: application/json" \ - -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ - "$dns_list/$line" &> /dev/null - done <<< "$record_id_to_delete" - else : - echo "Unable to update '$sub' domain name as it has duplicate DNS records. Set REMOVE_DUPLICATES='true' to remove them." - continue - fi - fi - - # re-enable glob expansion - set +f - - data="{\"type\": \"A\", \"name\": \"$sub\", \"data\": \"$ip\"}" - url="$dns_list/$record_id" - - if [[ -z $record_id ]]; then - echo "No record found with '$sub' domain name. Creating record, sending data=$data to url=$url" - - new_record=$(curl -s -X POST \ - -H "Content-Type: application/json" \ - -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ - -d "$data" \ - "$url") - - record_data=$(echo $new_record| jq -r ".data") - fi - - if [[ "$ip" != "$record_data" ]]; then - echo "existing DNS record address ($record_data) doesn't match current IP ($ip), sending data=$data to url=$url" - - curl -s -X PUT \ - -H "Content-Type: application/json" \ - -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ - -d "$data" \ - "$url" &> /dev/null - else - echo "existing DNS record address ($record_data) did not need updating" - fi - done + if [[ -z $ipv4 ]]; then + echo "IPv4 wasn't retrieved within allowed interval. Will try $sleep_interval seconds later.." else - echo "IP wasn't retrieved within allowed interval. Will try $sleep_interval seconds later.." + configure_record $ipv4 "A" fi + if [[ -z $ipv6 ]]; then + echo "IPv6 wasn't retrieved within allowed interval. Will try $sleep_interval seconds later.." + else + configure_record $ipv6 "AAAA" + fi + sleep $sleep_interval done