From a258c8a946e1bd7d976b37d9460001e01016cbf9 Mon Sep 17 00:00:00 2001 From: Marcio Cruz <40072316+CruzMarcio@users.noreply.github.com> Date: Wed, 8 Mar 2023 14:16:23 -0300 Subject: [PATCH] Create dns_googledomains.sh --- dnsapi/dns_googledomains.sh | 72 +++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 dnsapi/dns_googledomains.sh diff --git a/dnsapi/dns_googledomains.sh b/dnsapi/dns_googledomains.sh new file mode 100644 index 00000000..9437673c --- /dev/null +++ b/dnsapi/dns_googledomains.sh @@ -0,0 +1,72 @@ +#!/usr/bin/env sh +GOOGLEDOMAINS_API="https://acmedns.googleapis.com/v1/acmeChallengeSets" +dns_googledomains_add() { + fulldomain=$1 + txtvalue=$2 + _info "Using Google Domains api" + _debug fulldomain "$fulldomain" + _debug txtvalue "$txtvalue" + + GOOGLEDOMAINS_TOKEN="${GOOGLEDOMAINS_TOKEN:-$(_readaccountconf_mutable GOOGLEDOMAINS_TOKEN)}" + + if [ -z "$GOOGLEDOMAINS_TOKEN" ]; then + GOOGLEDOMAINS_TOKEN="" + _err "You did not specify GOOGLEDOMAINS_TOKEN yet." + _err "Please create your key and try again." + _err "e.g." + _err "export GOOGLEDOMAINS_TOKEN=d41d8cd98f00b204e9800998ecf8427e" + return 1 + fi + #save the api token to the account conf file. + _saveaccountconf_mutable GOOGLEDOMAINS_TOKEN "$GOOGLEDOMAINS_TOKEN" + + _debug "First detect the root zone" + i=0 + while [ $i -le $(echo "$fulldomain" | grep -o '\.' | wc -l) ]; do + # join the domain parts from the current index to the end + current_domain=$(echo "$fulldomain" | cut -d "." -f $(($i+1))-) + + # make a curl request to the URL and break the loop if the HTTP response code is 200 + response="$(_get "$GOOGLEDOMAINS_API/$current_domain")" + + if _contains "$response" "INVALID_ARGUMENT"; then + _info "Invalid domain: $current_domain" + else + _info "Found valid domain: $current_domain" + break + fi + i=$((i+1)) + done + export _H1="Content-Type: application/json" + _post "{\"accessToken\":\"$GOOGLEDOMAINS_TOKEN\",\"keepExpiredRecords\":true,\"recordsToAdd\":[{\"digest\":\"$txtvalue\",\"fqdn\":\"$fulldomain\"}]}" "$GOOGLEDOMAINS_API/$current_domain:rotateChallenges" "" "" +} + +#fulldomain txtvalue +dns_googledomains_rm() { + fulldomain=$1 + txtvalue=$2 + + GOOGLEDOMAINS_TOKEN="${GOOGLEDOMAINS_TOKEN:-$(_readaccountconf_mutable GOOGLEDOMAINS_TOKEN)}" + _info "Using Google Domains api" + _debug fulldomain "$fulldomain" + _debug txtvalue "$txtvalue" + i=0 + while [ $i -le $(echo "$fulldomain" | grep -o '\.' | wc -l) ]; do + # join the domain parts from the current index to the end + current_domain=$(echo "$fulldomain" | cut -d "." -f $(($i+1))-) + echo $current_domain + + # make a curl request to the URL and break the loop if the HTTP response code is 200 + response="$(_get "$GOOGLEDOMAINS_API/$current_domain")" + echo $response + if _contains "$response" "INVALID_ARGUMENT"; then + echo "Invalid domain: $current_domain" + else + echo "Found valid domain: $current_domain" + break + fi + i=$((i+1)) + done + export _H1="Content-Type: application/json" + _post "{\"accessToken\":\"$GOOGLEDOMAINS_TOKEN\",\"keepExpiredRecords\":true,\"recordsToRemove\":[{\"digest\":\"$txtvalue\",\"fqdn\":\"$fulldomain\"}]}" "$GOOGLEDOMAINS_API/$current_domain:rotateChallenges" "" "" +} \ No newline at end of file