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

View file

@ -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,28 +23,16 @@ test -z $NAME && die "NAME not set!"
dns_list="$api_host/domains/$DOMAIN/records"
while ( true ); do
domain_records=$(curl -s -X GET \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
$dns_list"?per_page=200")
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
done
echo "Found IP address $ip"
if [[ -n $ip ]]; then
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 == \"A\" and .name == \"$sub\") | .id")
record_data=$(echo $domain_records| jq -r ".domain_records[] | select(.type == \"A\" and .name == \"$sub\") | .data")
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 :
@ -94,8 +83,36 @@ while ( true ); do
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" \
-H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
$dns_list"?per_page=200")
for service in ${services[@]}; do
echo "Trying with $service..."
ipv4="$(curl -4 -s -f $service)"
ipv6="$(curl -6 -s -f $service)"
test -n "$ipv4$ipv6" && break
done
echo "Found IPv4 address $ipv4"
echo "Found IPv6 address $ipv6"
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