diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3b9a3e6..d227574 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,7 +10,7 @@ jobs: strategy: fail-fast: false matrix: - repository: [rsync-server, sshd, webhook, gandi, http-tunnel, piwigo-souvenirs, restic-auto, restic-rest, shairport-sync, telegraf] + repository: [dnsupdater, rsync-server, sshd, webhook, gandi, http-tunnel, piwigo-souvenirs, restic-auto, restic-rest, shairport-sync, telegraf] steps: - name: Checkout diff --git a/dnsupdater/Dockerfile b/dnsupdater/Dockerfile new file mode 100644 index 0000000..72f755d --- /dev/null +++ b/dnsupdater/Dockerfile @@ -0,0 +1,8 @@ +FROM alpine:latest + +ADD dnsupdater.sh /usr/bin/dnsupdater.sh + +RUN apk add --no-cache bash curl jq bind-tools \ + && chmod +x /usr/bin/dnsupdater.sh + +CMD /usr/bin/dnsupdater.sh diff --git a/dnsupdater/dnsupdater.sh b/dnsupdater/dnsupdater.sh new file mode 100755 index 0000000..546da99 --- /dev/null +++ b/dnsupdater/dnsupdater.sh @@ -0,0 +1,58 @@ +#!/bin/bash + +while true ; do + # Get my current IP + my_ip=$(curl -s https://api.ipify.org) + + # Get my currently registered IP + current_ip= + if [[ "$GANDI_API_KEY" != "" ]] ; then + current_ip=$(curl -s -H"X-Api-Key: $GANDI_API_KEY" https://dns.api.gandi.net/api/v5/domains/$DNS_DOMAIN/records | jq -r '.[] | select(.rrset_name == "'$DNS_HOST'") | select(.rrset_type == "A") | .rrset_values[0]') + elif [[ "$OVH_USERNAME" != "" ]] ; then + current_ip=$(dig +short @dns13.ovh.net $DNS_HOST.$DNS_DOMAIN) + fi + + # Check if both IP addresses are correct + if [[ "$my_ip" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ && "$current_ip" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] ; then + # If they do not match, change it (and keep the TTL and TYPE) + if [[ "$my_ip" != "$current_ip" ]]; then + result=1 + echo "Updating $DNS_HOST.$DNS_DOMAIN record with IP $my_ip" + if [[ "$GANDI_API_KEY" != "" ]] ; then + current_record=$(curl -s -H"X-Api-Key: $GANDI_API_KEY" https://dns.api.gandi.net/api/v5/domains/$DNS_DOMAIN/records | jq -c '.[] | select(.rrset_name == "'$DNS_HOST'") | select(.rrset_type == "A")') + current_ttl=$(echo $current_record | jq -r '.rrset_ttl') + curl -s -X PUT -H "Content-Type: application/json" -H "X-Api-Key: $GANDI_API_KEY" -d '{"rrset_ttl": '$current_ttl', "rrset_values":["'$my_ip'"]}' https://dns.api.gandi.net/api/v5/domains/$DNS_DOMAIN/records/$DNS_HOST/A + result=$? + elif [[ "$OVH_USERNAME" != "" ]] ; then + curl --user "$OVH_USERNAME:$OVH_PASSWORD" "http://www.ovh.com/nic/update?system=dyndns&hostname=$DNS_HOST.$DNS_DOMAIN&myip=$my_ip" + result=$? + fi + + # If the update was OK + if [[ $result == 0 ]] ; then + # Send a notification to Slack + if [[ "$SLACK_URL" != "" ]] ; then + curl -o /dev/null -s -m 10 --retry 5 -X POST -d "payload={\"username\": \"gandi\", \"icon_emoji\": \":dart:\", \"text\": \"New IP $my_ip for host $DNS_HOST.$DNS_DOMAIN\"}" $SLACK_URL + fi + + # Send a notification to Gotify + if [[ "$GOTIFY_URL" != "" ]] ; then + curl -o /dev/null -s -m 10 --retry 5 -X POST -H "accept: application/json" -H "Content-Type: application/json" -d "{\"priority\": 5, \"title\": \"New IP for host $DNS_HOST.$DNS_DOMAIN\", \"message\": \"New IP $my_ip for host $DNS_HOST.$DNS_DOMAIN, the old IP was $current_ip\"}" $GOTIFY_URL + fi + + # Send a notification to Healthchecks + if [[ "$HEALTHCHECKS_URL" != "" ]] ; then + curl -o /dev/null -s -m 10 --retry 5 $HEALTHCHECKS_URL + fi + fi + else + # Send a notification to Healthchecks + if [[ "$HEALTHCHECKS_URL" != "" ]] ; then + curl -o /dev/null -s -m 10 --retry 5 $HEALTHCHECKS_URL + fi + fi + fi + + # Wait 5 minutes + sleep 300 +done