132 lines
3.8 KiB
Bash
Executable file
132 lines
3.8 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# vim: set filetype=sh
|
|
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.
|
|
# And are not using cloudflare or similar services
|
|
# that may block requests from this script.
|
|
services=(
|
|
"ifconfig.io"
|
|
)
|
|
|
|
info() {
|
|
echo "$1" >/dev/stdout
|
|
}
|
|
|
|
warn() {
|
|
echo "$1" >/dev/stderr
|
|
}
|
|
|
|
error() {
|
|
warn "$1"
|
|
exit 1
|
|
}
|
|
|
|
test -f "${DIGITALOCEAN_TOKEN_FILE:-}" && DIGITALOCEAN_TOKEN="$(cat "$DIGITALOCEAN_TOKEN_FILE")"
|
|
test -z "$DIGITALOCEAN_TOKEN" && error "DIGITALOCEAN_TOKEN not set!"
|
|
test -z "${DOMAIN}" && error "DOMAIN not set!"
|
|
test -z "${NAME}" && error "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
|
|
warn "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\": \"$type\", \"name\": \"$sub\", \"data\": \"$ip\"}"
|
|
url="$dns_list/$record_id"
|
|
|
|
if [[ -z $record_id ]]; then
|
|
info "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
|
|
info "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
|
|
info "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
|
|
info "Trying with $service..."
|
|
|
|
ipv4="$(curl -4 -s -f --connect-timeout 2 "$service" || echo "")"
|
|
ipv6="$(curl -6 -s -f --connect-timeout 2 "$service" || echo "")"
|
|
|
|
if [[ -n "$ipv4$ipv6" ]]; then
|
|
break
|
|
else
|
|
warn "Failed to retrieve IP from $service"
|
|
fi
|
|
done
|
|
|
|
if [[ -z $ipv4 ]]; then
|
|
warn "IPv4 wasn't retrieved within allowed interval. Will try $sleep_interval seconds later.."
|
|
else
|
|
info "Found IPv4 address $ipv4"
|
|
configure_record "$ipv4" "A"
|
|
fi
|
|
|
|
if [[ -z $ipv6 ]]; then
|
|
warn "IPv6 wasn't retrieved within allowed interval. Will try $sleep_interval seconds later.."
|
|
else
|
|
info "Found IPv6 address $ipv6"
|
|
configure_record "$ipv6" "AAAA"
|
|
fi
|
|
|
|
sleep "$sleep_interval"
|
|
done
|