feat: ipv6 support added
All checks were successful
ci / docker (push) Successful in 24s

This commit is contained in:
Tine Jozelj 2023-07-18 13:43:22 +02:00
parent b1348e7133
commit ae1afb952f
Signed by: mentos1386
SSH key fingerprint: SHA256:MNtTsLbihYaWF8j1fkOHfkKNlnN1JQfxEU/rBU8nCGw

143
dyndns.sh
View file

@ -1,13 +1,14 @@
#!/bin/bash #!/bin/bash
set -euo pipefail
api_host="https://api.digitalocean.com/v2" api_host="https://api.digitalocean.com/v2"
sleep_interval=${SLEEP_INTERVAL:-300} sleep_interval=${SLEEP_INTERVAL:-300}
remove_duplicates=${REMOVE_DUPLICATES:-"false"} remove_duplicates=${REMOVE_DUPLICATES:-"false"}
# Only services with ipv6 supported are listed here.
services=( services=(
"ifconfig.co" "ifconfig.co"
"ipinfo.io/ip" "ifconfig.io"
"ifconfig.me"
) )
die() { die() {
@ -22,6 +23,68 @@ test -z $NAME && die "NAME not set!"
dns_list="$api_host/domains/$DOMAIN/records" 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 while ( true ); do
domain_records=$(curl -s -X GET \ domain_records=$(curl -s -X GET \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
@ -31,72 +94,26 @@ while ( true ); do
for service in ${services[@]}; do for service in ${services[@]}; do
echo "Trying with $service..." echo "Trying with $service..."
ip="$(curl -s $service | grep '[0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}')" ipv4="$(curl -4 -s -f $service)"
test -n "$ip" && break ipv6="$(curl -6 -s -f $service)"
test -n "$ipv4$ipv6" && break
done done
echo "Found IP address $ip" echo "Found IPv4 address $ipv4"
echo "Found IPv6 address $ipv6"
if [[ -n $ip ]]; then if [[ -z $ipv4 ]]; then
# disable glob expansion echo "IPv4 wasn't retrieved within allowed interval. Will try $sleep_interval seconds later.."
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
else else
echo "IP wasn't retrieved within allowed interval. Will try $sleep_interval seconds later.." configure_record $ipv4 "A"
fi 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 sleep $sleep_interval
done done