2020-12-13 14:55:26 +00:00
|
|
|
#!/bin/bash
|
2023-07-18 14:00:50 +00:00
|
|
|
set -euo pipefail
|
2016-01-23 16:47:35 +00:00
|
|
|
|
|
|
|
api_host="https://api.digitalocean.com/v2"
|
|
|
|
sleep_interval=${SLEEP_INTERVAL:-300}
|
2021-07-05 00:22:17 +00:00
|
|
|
remove_duplicates=${REMOVE_DUPLICATES:-"false"}
|
2016-01-23 16:47:35 +00:00
|
|
|
|
2023-07-18 11:43:22 +00:00
|
|
|
# Only services with ipv6 supported are listed here.
|
2020-12-10 21:03:27 +00:00
|
|
|
services=(
|
2020-12-13 22:09:34 +00:00
|
|
|
"ifconfig.co"
|
2023-07-18 11:43:22 +00:00
|
|
|
"ifconfig.io"
|
2020-12-10 21:03:27 +00:00
|
|
|
)
|
|
|
|
|
2016-01-23 16:47:35 +00:00
|
|
|
die() {
|
|
|
|
echo "$1"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2023-07-18 13:53:34 +00:00
|
|
|
test -f "${DIGITALOCEAN_TOKEN_FILE:-}" && DIGITALOCEAN_TOKEN="$(cat $DIGITALOCEAN_TOKEN_FILE)"
|
2016-01-23 16:47:35 +00:00
|
|
|
test -z $DIGITALOCEAN_TOKEN && die "DIGITALOCEAN_TOKEN not set!"
|
|
|
|
test -z $DOMAIN && die "DOMAIN not set!"
|
|
|
|
test -z $NAME && die "NAME not set!"
|
|
|
|
|
|
|
|
dns_list="$api_host/domains/$DOMAIN/records"
|
|
|
|
|
2023-07-18 11:43:22 +00:00
|
|
|
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
|
|
|
|
|
2023-07-18 14:01:37 +00:00
|
|
|
data="{\"type\": \"$type\", \"name\": \"$sub\", \"data\": \"$ip\"}"
|
2023-07-18 11:43:22 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-18 13:57:49 +00:00
|
|
|
while ( true )
|
|
|
|
do
|
2017-12-01 02:34:38 +00:00
|
|
|
domain_records=$(curl -s -X GET \
|
|
|
|
-H "Content-Type: application/json" \
|
|
|
|
-H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
|
2020-06-25 18:51:17 +00:00
|
|
|
$dns_list"?per_page=200")
|
2017-12-01 02:34:38 +00:00
|
|
|
|
2023-07-18 13:57:49 +00:00
|
|
|
for service in ${services[@]}
|
|
|
|
do
|
2020-12-10 21:03:27 +00:00
|
|
|
echo "Trying with $service..."
|
|
|
|
|
2023-07-18 13:57:49 +00:00
|
|
|
ipv4="$(curl -4 -s -f $service || echo "")"
|
|
|
|
ipv6="$(curl -6 -s -f $service || echo "")"
|
2023-07-18 11:43:22 +00:00
|
|
|
|
|
|
|
test -n "$ipv4$ipv6" && break
|
2020-12-10 21:03:27 +00:00
|
|
|
done
|
2021-06-17 04:05:34 +00:00
|
|
|
|
2023-07-18 11:43:22 +00:00
|
|
|
echo "Found IPv4 address $ipv4"
|
|
|
|
echo "Found IPv6 address $ipv6"
|
|
|
|
|
2023-07-18 13:57:49 +00:00
|
|
|
if [[ -z $ipv4 ]]
|
|
|
|
then
|
2023-07-18 11:43:22 +00:00
|
|
|
echo "IPv4 wasn't retrieved within allowed interval. Will try $sleep_interval seconds later.."
|
2016-01-23 16:47:35 +00:00
|
|
|
else
|
2023-07-18 11:43:22 +00:00
|
|
|
configure_record $ipv4 "A"
|
2016-01-23 16:47:35 +00:00
|
|
|
fi
|
|
|
|
|
2023-07-18 13:57:49 +00:00
|
|
|
if [[ -z $ipv6 ]]
|
|
|
|
then
|
2023-07-18 11:43:22 +00:00
|
|
|
echo "IPv6 wasn't retrieved within allowed interval. Will try $sleep_interval seconds later.."
|
|
|
|
else
|
|
|
|
configure_record $ipv6 "AAAA"
|
|
|
|
fi
|
|
|
|
|
2016-01-23 16:47:35 +00:00
|
|
|
sleep $sleep_interval
|
|
|
|
done
|