From b145569be06f60b422df2fb83cf782261b8b01ae Mon Sep 17 00:00:00 2001 From: lebaned Date: Wed, 15 Aug 2018 23:28:19 +0200 Subject: [PATCH 1/3] add versio API (dns_versio.sh) --- README.md | 19 ++++ dnsapi/README.md | 1 - dnsapi/dns_versio.sh | 211 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 230 insertions(+), 1 deletion(-) create mode 100755 dnsapi/dns_versio.sh diff --git a/README.md b/README.md index 30e6e554..f021a2f9 100644 --- a/README.md +++ b/README.md @@ -329,6 +329,25 @@ See: https://github.com/acmesh-official/acme.sh/wiki/dns-manual-mode first. If your dns provider doesn't support any api access, you can add the txt record by hand. +And: + +**lexicon DNS API: https://github.com/Neilpang/acme.sh/wiki/How-to-use-lexicon-dns-api + (DigitalOcean, DNSimple, DNSMadeEasy, DNSPark, EasyDNS, Namesilo, NS1, PointHQ, Rage4 and Vultr etc.)** + + +**More APIs coming soon...** + +If your DNS provider is not on the supported list above, you can write your own DNS API script easily. If you do, please consider submitting a [Pull Request](https://github.com/Neilpang/acme.sh/pulls) and contribute it to the project. + +For more details: [How to use DNS API](dnsapi) + +# 8. Use DNS manual mode: + +See: https://github.com/Neilpang/acme.sh/wiki/dns-manual-mode first. + +If your dns provider doesn't support any api access, you can add the txt record by your hand. +>>>>>>> 10a24dc... add versio API (dns_versio.sh) + ```bash acme.sh --issue --dns -d example.com -d www.example.com -d cp.example.com ``` diff --git a/dnsapi/README.md b/dnsapi/README.md index e81f7916..0002b706 100644 --- a/dnsapi/README.md +++ b/dnsapi/README.md @@ -3,4 +3,3 @@ DNS api usage: https://github.com/acmesh-official/acme.sh/wiki/dnsapi - diff --git a/dnsapi/dns_versio.sh b/dnsapi/dns_versio.sh new file mode 100755 index 00000000..82056e3b --- /dev/null +++ b/dnsapi/dns_versio.sh @@ -0,0 +1,211 @@ +#!/usr/bin/env sh +# +# DNS API for Versio.nl +# Author: lebaned +# Report Bugs here: https://github.com/lebaned/acme.sh +# +######## Public functions ##################### + +#Usage: dns_versio_add _acme-challenge.www.domain.com "[txtvalue]" +dns_versio_add() { + fulldomain=$1 + txtvalue=$2 + _info "Using Versio" + _debug fulldomain "$fulldomain" + _debug txtvalue "$txtvalue" + + if ! _get_credentials; then + return 1 + fi + + #save the credentials to the account conf file. + _saveaccountconf_mutable Versio_Username "$Versio_Username" + _saveaccountconf_mutable Versio_Password "$Versio_Password" + + _debug "First detect the root zone" + if ! _get_root "$fulldomain"; then + _err "invalid domain" + return 1 + fi + + _info fulldomain "$fulldomain" + _info _domain "$_domain" + _info _sub_domain "$_sub_domain" + + if ! _get_dns_records "$_domain"; then + _err "invalid domain" + return 1 + fi + + _debug "orginal dnsrecords" "$_dns_records" + _delete_dns_record "TXT" "$fulldomain." + _debug "dnsrecords after deleted old record" "$_dns_records" + _add_dns_record "TXT" "$fulldomain" "\\\"$txtvalue\\\"" 0 300 + _debug "dnsrecords after add record" "{\"dns_records\":[$_dns_records]}" + + if _versio_rest POST "domains/$_domain/update" "{\"dns_records\":[$_dns_records]}"; then + _debug "rest update response" "$response" + return 0 + fi + + _err "Error!" + return 1 +} + +#Usage: fulldomain txtvalue +#Remove the txt record after validation. +dns_versio_rm() { + fulldomain=$1 + txtvalue=$2 + _info "Using Versio" + _debug fulldomain "$fulldomain" + _debug txtvalue "$txtvalue" + + if ! _get_credentials; then + return 1 + fi + + _debug "First detect the root zone" + if ! _get_root "$fulldomain"; then + _err "invalid domain" + return 1 + fi + + _debug fulldomain "$fulldomain" + _debug _domain "$_domain" + _debug _sub_domain "$_sub_domain" + + if ! _get_dns_records "$_domain"; then + _err "invalid domain" + return 1 + fi + + _debug "orginal dnsrecords" "$_dns_records" + _delete_dns_record "TXT" "$fulldomain." + _debug "dnsrecords after deleted old record" "$_dns_records" + + if _versio_rest POST "domains/$_domain/update" "{\"dns_records\":[$_dns_records]}"; then + _debug "rest update response" "$response" + return 0 + fi + + _err "Error!" + return 1 + +} + +#################### Private functions below ################################## + +#_acme-challenge.www.domain.com +#returns +# _sub_domain=_acme-challenge.www +# _domain=domain.com +_get_root() { + domain=$1 + i=2 + p=1 + + if _versio_rest GET "domains?status=OK"; then + response="$(echo "$response" | tr -d "\n" | sed 's/{/\n&/g')" + while true; do + h=$(printf "%s" "$domain" | cut -d . -f $i-100) + _info h "$h" + _debug h "$h" + if [ -z "$h" ]; then + #not valid + return 1 + fi + + hostedzone="$(echo "$response" | _egrep_o "{.*\"domain\":\s*\"$h\".*}")" + if [ "$hostedzone" ]; then + _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) + _domain=$h + return 0 + fi + p=$i + i=$(_math "$i" + 1) + done + fi + return 1 +} + +#parameters: [record type] [record name] +_delete_dns_record() { + _dns_records=$(echo "$_dns_records" | sed 's/{"type":"'"$1"'","name":"'"$2"'"[^}]*}[,]\?//' | sed 's/,$//') +} + +#parameters: [type] [name] [value] [prio] [ttl] +_add_dns_record() { + _dns_records="$_dns_records,{\"type\":\"$1\",\"name\":\"$2\",\"value\":\"$3\",\"prio\":$4,\"ttl\":$5}" +} + +#parameters: [root domain] +#returns +# _dns_records +_get_dns_records() { + + if _versio_rest GET "domains/$1?show_dns_records=true"; then + _dns_records=$(echo "$response" | grep -oP '(?<="dns_records":\[)[^\]]*') + return 0 + fi + return 1 +} + +#method uri qstr data +_versio_rest() { + mtd="$1" + ep="$2" + data="$3" + + _debug mtd "$mtd" + _debug ep "$ep" + + VERSIO_API_URL="https://www.versio.nl/api/v1" + VERSIO_CREDENTIALS_BASE64=$(printf "%s:%s" "$Versio_Username" "$Versio_Password" | openssl enc -base64) + + export _H1="Accept: application/json" + export _H2="Content-Type: application/json" + export _H3="Authorization: Basic $VERSIO_CREDENTIALS_BASE64" + + if [ "$mtd" != "GET" ]; then + # both POST and DELETE. + _debug data "$data" + response="$(_post "$data" "$VERSIO_API_URL/$ep" "" "$mtd")" + else + response="$(_get "$VERSIO_API_URL/$ep")" + fi + + case $? in + 0) + _debug response "$response" + return 0 + ;; + 6) + _err "Authentication failure. Check your Versio email address and password" + return 1 + ;; + *) + _err "Unknown error" + return 1 + ;; + esac +} + +#parameters: [] +#returns: +# Versio_Username +# Versio_Password +_get_credentials() { + Versio_Username="${Versio_Username:-$(_readaccountconf_mutable Versio_Username)}" + Versio_Password="${Versio_Password:-$(_readaccountconf_mutable Versio_Password)}" + if [ -z "$Versio_Username" ] || [ -z "$Versio_Password" ]; then + Versio_Username="" + Versio_Password="" + _err "You don't specify Versio email address and/or password yet." + _err "Example:" + _err "export Versio_Username=[email address]" + _err "export Versio_Password=[password]" + return 1 + fi + return 0 +} From bd3f98daa693483255bfd971918da846469b7255 Mon Sep 17 00:00:00 2001 From: Tom Blauwendraat Date: Wed, 12 Sep 2018 03:03:57 +0200 Subject: [PATCH 2/3] [FIX] Remove grep -oP and sed newline usage, other fixes --- README.md | 19 --------- dnsapi/README.md | 1 + dnsapi/dns_versio.sh | 91 ++++++++++++++++++++++++++++---------------- 3 files changed, 59 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index f021a2f9..30e6e554 100644 --- a/README.md +++ b/README.md @@ -329,25 +329,6 @@ See: https://github.com/acmesh-official/acme.sh/wiki/dns-manual-mode first. If your dns provider doesn't support any api access, you can add the txt record by hand. -And: - -**lexicon DNS API: https://github.com/Neilpang/acme.sh/wiki/How-to-use-lexicon-dns-api - (DigitalOcean, DNSimple, DNSMadeEasy, DNSPark, EasyDNS, Namesilo, NS1, PointHQ, Rage4 and Vultr etc.)** - - -**More APIs coming soon...** - -If your DNS provider is not on the supported list above, you can write your own DNS API script easily. If you do, please consider submitting a [Pull Request](https://github.com/Neilpang/acme.sh/pulls) and contribute it to the project. - -For more details: [How to use DNS API](dnsapi) - -# 8. Use DNS manual mode: - -See: https://github.com/Neilpang/acme.sh/wiki/dns-manual-mode first. - -If your dns provider doesn't support any api access, you can add the txt record by your hand. ->>>>>>> 10a24dc... add versio API (dns_versio.sh) - ```bash acme.sh --issue --dns -d example.com -d www.example.com -d cp.example.com ``` diff --git a/dnsapi/README.md b/dnsapi/README.md index 0002b706..e81f7916 100644 --- a/dnsapi/README.md +++ b/dnsapi/README.md @@ -3,3 +3,4 @@ DNS api usage: https://github.com/acmesh-official/acme.sh/wiki/dnsapi + diff --git a/dnsapi/dns_versio.sh b/dnsapi/dns_versio.sh index 82056e3b..ed452687 100755 --- a/dnsapi/dns_versio.sh +++ b/dnsapi/dns_versio.sh @@ -1,8 +1,8 @@ #!/usr/bin/env sh # -# DNS API for Versio.nl +# DNS API for Versio.nl/Versio.eu/Versio.uk # Author: lebaned -# Report Bugs here: https://github.com/lebaned/acme.sh +# Author: Tom Blauwendraat # ######## Public functions ##################### @@ -14,14 +14,10 @@ dns_versio_add() { _debug fulldomain "$fulldomain" _debug txtvalue "$txtvalue" - if ! _get_credentials; then + if ! _get_configuration; then return 1 fi - #save the credentials to the account conf file. - _saveaccountconf_mutable Versio_Username "$Versio_Username" - _saveaccountconf_mutable Versio_Password "$Versio_Password" - _debug "First detect the root zone" if ! _get_root "$fulldomain"; then _err "invalid domain" @@ -37,14 +33,13 @@ dns_versio_add() { return 1 fi - _debug "orginal dnsrecords" "$_dns_records" - _delete_dns_record "TXT" "$fulldomain." - _debug "dnsrecords after deleted old record" "$_dns_records" - _add_dns_record "TXT" "$fulldomain" "\\\"$txtvalue\\\"" 0 300 + _debug "original dnsrecords" "$_dns_records" + _add_dns_record "TXT" "$fulldomain." "\\\"$txtvalue\\\"" 0 300 _debug "dnsrecords after add record" "{\"dns_records\":[$_dns_records]}" if _versio_rest POST "domains/$_domain/update" "{\"dns_records\":[$_dns_records]}"; then _debug "rest update response" "$response" + _debug "changed dnsrecords" "$_dns_records" return 0 fi @@ -61,7 +56,7 @@ dns_versio_rm() { _debug fulldomain "$fulldomain" _debug txtvalue "$txtvalue" - if ! _get_credentials; then + if ! _get_configuration; then return 1 fi @@ -80,12 +75,13 @@ dns_versio_rm() { return 1 fi - _debug "orginal dnsrecords" "$_dns_records" + _debug "original dnsrecords" "$_dns_records" _delete_dns_record "TXT" "$fulldomain." _debug "dnsrecords after deleted old record" "$_dns_records" if _versio_rest POST "domains/$_domain/update" "{\"dns_records\":[$_dns_records]}"; then _debug "rest update response" "$response" + _debug "changed dnsrecords" "$_dns_records" return 0 fi @@ -102,11 +98,11 @@ dns_versio_rm() { # _domain=domain.com _get_root() { domain=$1 - i=2 + i=1 p=1 if _versio_rest GET "domains?status=OK"; then - response="$(echo "$response" | tr -d "\n" | sed 's/{/\n&/g')" + response="$(echo "$response" | tr -d "\n")" while true; do h=$(printf "%s" "$domain" | cut -d . -f $i-100) _info h "$h" @@ -115,8 +111,7 @@ _get_root() { #not valid return 1 fi - - hostedzone="$(echo "$response" | _egrep_o "{.*\"domain\":\s*\"$h\".*}")" + hostedzone="$(echo "$response" | _egrep_o "{.*\"domain\":\s*\"$h\"")" if [ "$hostedzone" ]; then _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) _domain=$h @@ -143,9 +138,8 @@ _add_dns_record() { #returns # _dns_records _get_dns_records() { - if _versio_rest GET "domains/$1?show_dns_records=true"; then - _dns_records=$(echo "$response" | grep -oP '(?<="dns_records":\[)[^\]]*') + _dns_records="$(echo "$response" | sed -n 's/.*\"dns\_records\":\[\([^][]*\).*/\1/p')" return 0 fi return 1 @@ -161,22 +155,44 @@ _versio_rest() { _debug ep "$ep" VERSIO_API_URL="https://www.versio.nl/api/v1" - VERSIO_CREDENTIALS_BASE64=$(printf "%s:%s" "$Versio_Username" "$Versio_Password" | openssl enc -base64) + VERSIO_CREDENTIALS_BASE64=$(printf "%s:%s" "$VERSIO_Username" "$VERSIO_Password" | _base64) export _H1="Accept: application/json" - export _H2="Content-Type: application/json" - export _H3="Authorization: Basic $VERSIO_CREDENTIALS_BASE64" + export _H2="Authorization: Basic $VERSIO_CREDENTIALS_BASE64" + export _H3="" + export _H4="" + export _H5="" if [ "$mtd" != "GET" ]; then # both POST and DELETE. _debug data "$data" - response="$(_post "$data" "$VERSIO_API_URL/$ep" "" "$mtd")" + response="$(_post "$data" "$VERSIO_API_URL/$ep" "" "$mtd" "application/json")" else response="$(_get "$VERSIO_API_URL/$ep")" fi + # sleeping in order not to exceed rate limit + if [ -n "$VERSIO_Slow_rate" ]; then + _info "Sleeping $VERSIO_Slow_rate seconds to slow down hit rate on API" + _sleep "$VERSIO_Slow_rate" + fi + case $? in 0) + if [ "$response" = "Rate limit exceeded" ]; then + _err "Rate limit exceeded. Try again later." + return 1 + fi + case $response in + "<"*) + _err "Invalid non-JSON response! $response" + return 1 + ;; + "{\"error\":"*) + _err "Error response! $response" + return 1 + ;; + esac _debug response "$response" return 0 ;; @@ -193,19 +209,28 @@ _versio_rest() { #parameters: [] #returns: -# Versio_Username -# Versio_Password -_get_credentials() { - Versio_Username="${Versio_Username:-$(_readaccountconf_mutable Versio_Username)}" - Versio_Password="${Versio_Password:-$(_readaccountconf_mutable Versio_Password)}" - if [ -z "$Versio_Username" ] || [ -z "$Versio_Password" ]; then - Versio_Username="" - Versio_Password="" +# VERSIO_Username +# VERSIO_Password +# VERSIO_Slow_rate +_get_configuration() { + VERSIO_Username="${VERSIO_Username:-$(_readaccountconf_mutable VERSIO_Username)}" + VERSIO_Password="${VERSIO_Password:-$(_readaccountconf_mutable VERSIO_Password)}" + if [ -z "$VERSIO_Username" ] || [ -z "$VERSIO_Password" ]; then + VERSIO_Username="" + VERSIO_Password="" _err "You don't specify Versio email address and/or password yet." _err "Example:" - _err "export Versio_Username=[email address]" - _err "export Versio_Password=[password]" + _err "export VERSIO_Username=[email address]" + _err "export VERSIO_Password=[password]" return 1 fi + VERSIO_Slow_rate="${VERSIO_Slow_rate:-$(_readaccountconf_mutable VERSIO_Slow_rate)}" + _info "Using slowdown rate: $VERSIO_Slow_rate seconds" + if [ -z "$VERSIO_Slow_rate" ]; then + VERSIO_Slow_rate="" + fi + _saveaccountconf_mutable VERSIO_Username "$VERSIO_Username" + _saveaccountconf_mutable VERSIO_Password "$VERSIO_Password" + _saveaccountconf_mutable VERSIO_Slow_rate "$VERSIO_Slow_rate" return 0 } From 0ea6d632baec7e450778f00a34953f2ae2567b17 Mon Sep 17 00:00:00 2001 From: Tom Blauwendraat Date: Tue, 23 Aug 2022 00:31:49 +0200 Subject: [PATCH 3/3] fixup! [FIX] Remove grep -oP and sed newline usage, other fixes --- dnsapi/dns_versio.sh | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/dnsapi/dns_versio.sh b/dnsapi/dns_versio.sh index ed452687..93179e6f 100755 --- a/dnsapi/dns_versio.sh +++ b/dnsapi/dns_versio.sh @@ -37,11 +37,18 @@ dns_versio_add() { _add_dns_record "TXT" "$fulldomain." "\\\"$txtvalue\\\"" 0 300 _debug "dnsrecords after add record" "{\"dns_records\":[$_dns_records]}" - if _versio_rest POST "domains/$_domain/update" "{\"dns_records\":[$_dns_records]}"; then - _debug "rest update response" "$response" - _debug "changed dnsrecords" "$_dns_records" - return 0 - fi + while [ "$j" -le 3 ]; do + _versio_rest POST "domains/$_domain/update" "{\"dns_records\":[$_dns_records]}" + if [ "$?" = "0" ]; then + _debug "rest update response" "$response" + _debug "changed dnsrecords" "$_dns_records" + return 0 + fi + if [ "$?" != "2" ]; then + break + fi + j=$((j + 1)) + done _err "Error!" return 1 @@ -186,7 +193,7 @@ _versio_rest() { case $response in "<"*) _err "Invalid non-JSON response! $response" - return 1 + return 2 # retryable ;; "{\"error\":"*) _err "Error response! $response"