From 101344e91f9b3e0ecb4e96b71985ba21212e62ce Mon Sep 17 00:00:00 2001 From: "M.A.Sarbanha" <39798816+sarbanha@users.noreply.github.com> Date: Fri, 24 Jun 2022 17:53:08 +0400 Subject: [PATCH 1/9] Create dns_arvancdn This extension enables acme.sh to issue SSL Certificates using https://www.arvancloud.com DNS service --- dnsapi/dns_arvancdn | 134 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 dnsapi/dns_arvancdn diff --git a/dnsapi/dns_arvancdn b/dnsapi/dns_arvancdn new file mode 100644 index 00000000..52238936 --- /dev/null +++ b/dnsapi/dns_arvancdn @@ -0,0 +1,134 @@ +#!/usr/bin/env #!/bin/sh + +# Author: Mohammad Ali Sarbanha +# Repository: https://github.com/sarbanha/acme.sh-dnsapi-dns_arvancdn + +# export ARVAN_API_KEY="-----------" + +ARVAN_CDN_API="https://napi.arvancloud.com/cdn/4.0" + +#Usage: dns_arvancdn_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" +dns_arvancdn_add() { + + fulldomain=$1 + challenge=$2 + zone=${fulldomain:16} + + _debug "dns_arvan_add(): Started" + + ARVAN_API_KEY="${ARVAN_API_KEY:-$(_readaccountconf_mutable ARVAN_API_KEY)}" + if [ -z $ARVAN_API_KEY ]; then + ARVAN_API_KEY="" + _err "dns_arvan_add(): ARVAN_API_KEY has not been defined yet." + _err "dns_arvan_add(): export ARVAN_API_KEY=\"---YOUR-API-KEY---\"" + return 1 + fi + _saveaccountconf_mutable ARVAN_API_KEY "$ARVAN_API_KEY" + + _debug "dns_arvan_add(): Check domain root zone availability for $zone" + if ! _get_root $zone; then + _err "dns_arvan_add(): Invalid domain $zone" + return 1 + fi + + _debug "dns_arvan_add(): fulldomain $fulldomain" + _debug "dns_arvan_add(): textvalue $challenge" + _debug "dns_arvan_add(): domain $zone" + + _record_add $zone $challenge + +} + +#Usage: dns_arvancdn_rm fulldomain txtvalue +dns_arvancdn_rm(){ + + fulldomain=$1 + challenge=$2 + zone=${fulldomain:16} + + ARVAN_API_KEY="${ARVAN_API_KEY:-$(_readaccountconf_mutable ARVAN_API_KEY)}" + if [ -z $ARVAN_API_KEY ]; then + ARVAN_API_KEY="" + _err "dns_arvan_rm(): ARVAN_API_KEY has not been defined yet." + _err "dns_arvan_rm(): export ARVAN_API_KEY=\"---YOUR-API-KEY---\"" + return 1 + fi + + record_id=$(_record_get_id $zone $challenge) + + _record_remove $zone $record_id + +} + +#################### +# Private functions +#################### + +#Usage: _get_root zone +_get_root(){ + _zone=$1 + + export _H1="Content-Type: application-json" + export _H2="Authorization: apikey $ARVAN_API_KEY" + + _response=$(_get $ARVAN_CDN_API/domains/$_zone) + _debug2 "_get_root()" $_response + _debug2 "_get_root()" $_zone + if _contains "$_response" "$_zone"; then + # is valid + _valid=0 + else + # is not valid + _valid=1 + fi + + return $_valid +} + +#Usage: _record_add zone challenge +_record_add(){ + zone=$1 + challenge=$2 + + export _H1="Content-Type: application-json" + export _H2="Authorization: apikey $ARVAN_API_KEY" + + payload="{\"type\":\"txt\",\"name\":\"_acme-challenge\",\"cloud\":false,\"value\":{\"text\":\"$challenge\"},\"ttl\":120}" + _response=$(_post "$payload" "$ARVAN_CDN_API/domains/$zone/dns-records" "" "POST" "application/json" | _base64) + + _debug2 "_record_add(): " $_response + _debug2 " Payload: " $payload +} + +#Usage: _record_get_id zone challenge +_record_get_id(){ + + zone=$1 + challenge=$2 + + export _H1="Content-Type: application-json" + export _H2="Authorization: apikey $ARVAN_API_KEY" + + _response=$(_get $ARVAN_CDN_API/domains/$zone/dns-records/?type=txt\&search=$challenge | _json_decode | _normalizeJson | grep -Eo '"id":.*?,"value":\{"text":".*?"\}' | sed 's/"id":"\([^"]*\)".*/\1/') + _debug2 "_record_get_id(): " $_response + + + echo "$_response" + +} + +#Usage: _record_remove zone record_id +_record_remove(){ + + zone=$1 + record_id=$2 + + export _H1="Content-Type: application-json" + export _H2="Authorization: apikey $ARVAN_API_KEY" + + _response=$(_post "" $ARVAN_CDN_API/domains/$zone/dns-records/$record_id "" "DELETE" "application/json") + + _debug "_record_remove(): ACME Challenge Removed" + _debug2 " Response: " $_response + +} From 7a0f5348b3ab4e7044af623b96532c80fb45f67e Mon Sep 17 00:00:00 2001 From: "M.A.Sarbanha" <39798816+sarbanha@users.noreply.github.com> Date: Wed, 13 Jul 2022 16:20:40 +0400 Subject: [PATCH 2/9] Updated: looks up for the domain zone --- dnsapi/dns_arvancdn | 134 ---------------------------------- dnsapi/dns_arvancdn.sh | 161 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+), 134 deletions(-) delete mode 100644 dnsapi/dns_arvancdn create mode 100644 dnsapi/dns_arvancdn.sh diff --git a/dnsapi/dns_arvancdn b/dnsapi/dns_arvancdn deleted file mode 100644 index 52238936..00000000 --- a/dnsapi/dns_arvancdn +++ /dev/null @@ -1,134 +0,0 @@ -#!/usr/bin/env #!/bin/sh - -# Author: Mohammad Ali Sarbanha -# Repository: https://github.com/sarbanha/acme.sh-dnsapi-dns_arvancdn - -# export ARVAN_API_KEY="-----------" - -ARVAN_CDN_API="https://napi.arvancloud.com/cdn/4.0" - -#Usage: dns_arvancdn_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" -dns_arvancdn_add() { - - fulldomain=$1 - challenge=$2 - zone=${fulldomain:16} - - _debug "dns_arvan_add(): Started" - - ARVAN_API_KEY="${ARVAN_API_KEY:-$(_readaccountconf_mutable ARVAN_API_KEY)}" - if [ -z $ARVAN_API_KEY ]; then - ARVAN_API_KEY="" - _err "dns_arvan_add(): ARVAN_API_KEY has not been defined yet." - _err "dns_arvan_add(): export ARVAN_API_KEY=\"---YOUR-API-KEY---\"" - return 1 - fi - _saveaccountconf_mutable ARVAN_API_KEY "$ARVAN_API_KEY" - - _debug "dns_arvan_add(): Check domain root zone availability for $zone" - if ! _get_root $zone; then - _err "dns_arvan_add(): Invalid domain $zone" - return 1 - fi - - _debug "dns_arvan_add(): fulldomain $fulldomain" - _debug "dns_arvan_add(): textvalue $challenge" - _debug "dns_arvan_add(): domain $zone" - - _record_add $zone $challenge - -} - -#Usage: dns_arvancdn_rm fulldomain txtvalue -dns_arvancdn_rm(){ - - fulldomain=$1 - challenge=$2 - zone=${fulldomain:16} - - ARVAN_API_KEY="${ARVAN_API_KEY:-$(_readaccountconf_mutable ARVAN_API_KEY)}" - if [ -z $ARVAN_API_KEY ]; then - ARVAN_API_KEY="" - _err "dns_arvan_rm(): ARVAN_API_KEY has not been defined yet." - _err "dns_arvan_rm(): export ARVAN_API_KEY=\"---YOUR-API-KEY---\"" - return 1 - fi - - record_id=$(_record_get_id $zone $challenge) - - _record_remove $zone $record_id - -} - -#################### -# Private functions -#################### - -#Usage: _get_root zone -_get_root(){ - _zone=$1 - - export _H1="Content-Type: application-json" - export _H2="Authorization: apikey $ARVAN_API_KEY" - - _response=$(_get $ARVAN_CDN_API/domains/$_zone) - _debug2 "_get_root()" $_response - _debug2 "_get_root()" $_zone - if _contains "$_response" "$_zone"; then - # is valid - _valid=0 - else - # is not valid - _valid=1 - fi - - return $_valid -} - -#Usage: _record_add zone challenge -_record_add(){ - zone=$1 - challenge=$2 - - export _H1="Content-Type: application-json" - export _H2="Authorization: apikey $ARVAN_API_KEY" - - payload="{\"type\":\"txt\",\"name\":\"_acme-challenge\",\"cloud\":false,\"value\":{\"text\":\"$challenge\"},\"ttl\":120}" - _response=$(_post "$payload" "$ARVAN_CDN_API/domains/$zone/dns-records" "" "POST" "application/json" | _base64) - - _debug2 "_record_add(): " $_response - _debug2 " Payload: " $payload -} - -#Usage: _record_get_id zone challenge -_record_get_id(){ - - zone=$1 - challenge=$2 - - export _H1="Content-Type: application-json" - export _H2="Authorization: apikey $ARVAN_API_KEY" - - _response=$(_get $ARVAN_CDN_API/domains/$zone/dns-records/?type=txt\&search=$challenge | _json_decode | _normalizeJson | grep -Eo '"id":.*?,"value":\{"text":".*?"\}' | sed 's/"id":"\([^"]*\)".*/\1/') - _debug2 "_record_get_id(): " $_response - - - echo "$_response" - -} - -#Usage: _record_remove zone record_id -_record_remove(){ - - zone=$1 - record_id=$2 - - export _H1="Content-Type: application-json" - export _H2="Authorization: apikey $ARVAN_API_KEY" - - _response=$(_post "" $ARVAN_CDN_API/domains/$zone/dns-records/$record_id "" "DELETE" "application/json") - - _debug "_record_remove(): ACME Challenge Removed" - _debug2 " Response: " $_response - -} diff --git a/dnsapi/dns_arvancdn.sh b/dnsapi/dns_arvancdn.sh new file mode 100644 index 00000000..db1dfd13 --- /dev/null +++ b/dnsapi/dns_arvancdn.sh @@ -0,0 +1,161 @@ +#!/usr/bin/env #!/bin/sh + +# Author: Mohammad Ali Sarbanha +# Repository: https://github.com/sarbanha/acme.sh-dnsapi-dns_arvancdn + +# export ARVAN_API_KEY="-----------" + +ARVAN_CDN_API="https://napi.arvancloud.com/cdn/4.0" + +#Usage: dns_arvancdn_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" +dns_arvancdn_add() { + + _fulldomain=$1 + _challenge=$2 + + _debug "dns_arvan_add(): Started" + + ARVAN_API_KEY="${ARVAN_API_KEY:-$(_readaccountconf_mutable ARVAN_API_KEY)}" + if [ -z $ARVAN_API_KEY ]; then + ARVAN_API_KEY="" + _err "dns_arvan_add(): ARVAN_API_KEY has not been defined yet." + _err "dns_arvan_add(): export ARVAN_API_KEY=\"---YOUR-API-KEY---\"" + return 1 + fi + _saveaccountconf_mutable ARVAN_API_KEY "$ARVAN_API_KEY" + + _debug "dns_arvan_add(): Check domain root zone availability for $_fulldomain" + _zone=$(_get_root $_fulldomain) + if [ $? -ne 0 ]; then + _err "dns_arvan_add(): Root zone for $_fulldomain not found!" + return 1 + fi + + _record_name=$(echo $_zone | sed "s/\.\..*//") + _zone=$(echo $_zone | sed "s/.*\.\.//") + + _debug "dns_arvan_add(): fulldomain $_fulldomain" + _debug "dns_arvan_add(): textvalue $_challenge" + _debug "dns_arvan_add(): domain $_record_name" + _debug "dns_arvan_add(): domain $_zone" + + _record_add $_record_name $_zone $_challenge + +} + +#Usage: dns_arvancdn_rm fulldomain txtvalue +dns_arvancdn_rm(){ + + _fulldomain=$1 + _challenge=$2 + + ARVAN_API_KEY="${ARVAN_API_KEY:-$(_readaccountconf_mutable ARVAN_API_KEY)}" + if [ -z $ARVAN_API_KEY ]; then + ARVAN_API_KEY="" + _err "dns_arvan_rm(): ARVAN_API_KEY has not been defined yet." + _err "dns_arvan_rm(): export ARVAN_API_KEY=\"---YOUR-API-KEY---\"" + return 1 + fi + + _zone=$(_get_root $_fulldomain) + if [ $? -ne 0 ]; then + _err "dns_arvan_rm(): Root zone for $_fulldomain not found!" + return 1 + fi + + _record_name=$(echo $_zone | sed "s/\.\..*//") + _zone=$(echo $_zone | sed "s/.*\.\.//") + + + _record_id=$(_record_get_id $_zone $_challenge) + + _record_remove $_zone $_record_id + +} + +#################### +# Private functions +#################### + +#Usage: _get_root zone +_get_root(){ + _fulldomain=$1 + _zone=$_fulldomain + + export _H1="Content-Type: application-json" + export _H2="Authorization: apikey $ARVAN_API_KEY" + + _response=$(_get $ARVAN_CDN_API/domains) + _domains_list=( $( echo $_response | grep -Poe '"domain":"[^"]*"' | sed 's/"domain":"//' | sed 's/"//') ) + + _debug2 "_get_root(): $_response" + _debug2 "_get_root(): $_domains_list" + + #Fibding a matching Zone + while [[ ! -z $_zone ]]; do + for tmp in ${_domains_list[@]}; do + if [ $tmp = $_zone ]; then + break 2 + fi + done + _zone=$(echo $_zone | sed 's/^[^.]*\.\?//') + done + if [ -z $_zone ]; then + _debug2 "_get_root(): Zone not found on provider" + exit 1 + fi + + _marked_zone=$(echo $_fulldomain | sed "s/^\(.*\)\.\(${_zone}\)$/\1..\2/") + echo $_marked_zone + +} + +#Usage: _record_add record_name zone challenge +_record_add(){ + + _record_name=$1 + _zone=$2 + _challenge=$3 + + export _H1="Content-Type: application-json" + export _H2="Authorization: apikey $ARVAN_API_KEY" + + _payload="{\"type\":\"txt\",\"name\":\"$_record_name\",\"cloud\":false,\"value\":{\"text\":\"$_challenge\"},\"ttl\":120}" + _response=$(_post "$_payload" "$ARVAN_CDN_API/domains/$_zone/dns-records" "" "POST" "application/json" | _base64) + + _debug2 "_record_add(): " $_response + _debug2 " Payload: " $_payload +} + +#Usage: _record_get_id zone challenge +_record_get_id(){ + + _zone=$1 + _challenge=$2 + + export _H1="Content-Type: application-json" + export _H2="Authorization: apikey $ARVAN_API_KEY" + + _response=$(_get $ARVAN_CDN_API/domains/$_zone/dns-records/?type=txt\&search=$_challenge | _json_decode | _normalizeJson | grep -Eo '"id":.*?,"value":\{"text":".*?"\}' | sed 's/"id":"\([^"]*\)".*/\1/') + _debug2 "_record_get_id(): " $_response + + + echo "$_response" + +} + +#Usage: _record_remove zone record_id +_record_remove(){ + + _zone=$1 + _record_id=$2 + + export _H1="Content-Type: application-json" + export _H2="Authorization: apikey $ARVAN_API_KEY" + + _response=$(_post "" $ARVAN_CDN_API/domains/$_zone/dns-records/$_record_id "" "DELETE" "application/json") + + _debug "_record_remove(): ACME Challenge Removed" + _debug2 " Response: " $_response + +} From 937e7d973d85c85dd5232a0b51780ebd56e4f08e Mon Sep 17 00:00:00 2001 From: "M.A.Sarbanha" <39798816+sarbanha@users.noreply.github.com> Date: Wed, 13 Jul 2022 18:40:39 +0400 Subject: [PATCH 3/9] Fixed: Shebang typo fixed / + Improvements --- dnsapi/dns_arvancdn.sh | 86 +++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/dnsapi/dns_arvancdn.sh b/dnsapi/dns_arvancdn.sh index db1dfd13..cecc1e0d 100644 --- a/dnsapi/dns_arvancdn.sh +++ b/dnsapi/dns_arvancdn.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env #!/bin/sh +#!/usr/bin/env bash # Author: Mohammad Ali Sarbanha # Repository: https://github.com/sarbanha/acme.sh-dnsapi-dns_arvancdn @@ -16,30 +16,30 @@ dns_arvancdn_add() { _debug "dns_arvan_add(): Started" ARVAN_API_KEY="${ARVAN_API_KEY:-$(_readaccountconf_mutable ARVAN_API_KEY)}" - if [ -z $ARVAN_API_KEY ]; then + if [ -z "${ARVAN_API_KEY}" ]; then ARVAN_API_KEY="" _err "dns_arvan_add(): ARVAN_API_KEY has not been defined yet." _err "dns_arvan_add(): export ARVAN_API_KEY=\"---YOUR-API-KEY---\"" return 1 fi - _saveaccountconf_mutable ARVAN_API_KEY "$ARVAN_API_KEY" + _saveaccountconf_mutable ARVAN_API_KEY "${ARVAN_API_KEY}" - _debug "dns_arvan_add(): Check domain root zone availability for $_fulldomain" - _zone=$(_get_root $_fulldomain) + _debug "dns_arvan_add(): Check domain root zone availability for ${_fulldomain}" + _zone=$(_get_root "${_fulldomain}") if [ $? -ne 0 ]; then - _err "dns_arvan_add(): Root zone for $_fulldomain not found!" + _err "dns_arvan_add(): Root zone for ${_fulldomain} not found!" return 1 fi - _record_name=$(echo $_zone | sed "s/\.\..*//") - _zone=$(echo $_zone | sed "s/.*\.\.//") + _record_name=$(echo "${_zone}" | sed "s/\.\..*//") + _zone=$(echo "${_zone}" | sed "s/.*\.\.//") - _debug "dns_arvan_add(): fulldomain $_fulldomain" - _debug "dns_arvan_add(): textvalue $_challenge" - _debug "dns_arvan_add(): domain $_record_name" - _debug "dns_arvan_add(): domain $_zone" + _debug "dns_arvan_add(): fulldomain ${_fulldomain}" + _debug "dns_arvan_add(): textvalue ${_challenge}" + _debug "dns_arvan_add(): domain ${_record_name}" + _debug "dns_arvan_add(): domain ${_zone}" - _record_add $_record_name $_zone $_challenge + _record_add "${_record_name}" "${_zone}" "${_challenge}" } @@ -50,26 +50,26 @@ dns_arvancdn_rm(){ _challenge=$2 ARVAN_API_KEY="${ARVAN_API_KEY:-$(_readaccountconf_mutable ARVAN_API_KEY)}" - if [ -z $ARVAN_API_KEY ]; then + if [ -z "${ARVAN_API_KEY}" ]; then ARVAN_API_KEY="" _err "dns_arvan_rm(): ARVAN_API_KEY has not been defined yet." _err "dns_arvan_rm(): export ARVAN_API_KEY=\"---YOUR-API-KEY---\"" return 1 fi - _zone=$(_get_root $_fulldomain) + _zone=$(_get_root "${_fulldomain}") if [ $? -ne 0 ]; then - _err "dns_arvan_rm(): Root zone for $_fulldomain not found!" + _err "dns_arvan_rm(): Root zone for ${_fulldomain} not found!" return 1 fi - _record_name=$(echo $_zone | sed "s/\.\..*//") - _zone=$(echo $_zone | sed "s/.*\.\.//") + _record_name=$(echo "${_zone}" | sed "s/\.\..*//") + _zone=$(echo "${_zone}" | sed "s/.*\.\.//") - _record_id=$(_record_get_id $_zone $_challenge) + _record_id=$(_record_get_id "${_zone}" "${_challenge}") - _record_remove $_zone $_record_id + _record_remove "${_zone}" "${_record_id}" } @@ -83,30 +83,30 @@ _get_root(){ _zone=$_fulldomain export _H1="Content-Type: application-json" - export _H2="Authorization: apikey $ARVAN_API_KEY" + export _H2="Authorization: apikey ${ARVAN_API_KEY}" - _response=$(_get $ARVAN_CDN_API/domains) - _domains_list=( $( echo $_response | grep -Poe '"domain":"[^"]*"' | sed 's/"domain":"//' | sed 's/"//') ) + _response=$(_get "${ARVAN_CDN_API}/domains") + _domains_list=( $( echo "${_response}" | grep -Poe '"domain":"[^"]*"' | sed 's/"domain":"//' | sed 's/"//') ) - _debug2 "_get_root(): $_response" - _debug2 "_get_root(): $_domains_list" + _debug2 "_get_root(): reponse ${_response}" + _debug2 "_get_root(): domains list ${_domains_list}" #Fibding a matching Zone - while [[ ! -z $_zone ]]; do - for tmp in ${_domains_list[@]}; do - if [ $tmp = $_zone ]; then + while [[ ! -z "${_zone}" ]]; do + for tmp in "${_domains_list[@]}"; do + if [ "${tmp}" = "${_zone}" ]; then break 2 fi done - _zone=$(echo $_zone | sed 's/^[^.]*\.\?//') + _zone=$(echo "${_zone}" | sed 's/^[^.]*\.\?//') done - if [ -z $_zone ]; then + if [ -z "${_zone}" ]; then _debug2 "_get_root(): Zone not found on provider" exit 1 fi - _marked_zone=$(echo $_fulldomain | sed "s/^\(.*\)\.\(${_zone}\)$/\1..\2/") - echo $_marked_zone + _marked_zone=$(echo "${_fulldomain}" | sed "s/^\(.*\)\.\(${_zone}\)$/\1..\2/") + echo "${_marked_zone}" } @@ -118,13 +118,13 @@ _record_add(){ _challenge=$3 export _H1="Content-Type: application-json" - export _H2="Authorization: apikey $ARVAN_API_KEY" + export _H2="Authorization: apikey ${ARVAN_API_KEY}" - _payload="{\"type\":\"txt\",\"name\":\"$_record_name\",\"cloud\":false,\"value\":{\"text\":\"$_challenge\"},\"ttl\":120}" - _response=$(_post "$_payload" "$ARVAN_CDN_API/domains/$_zone/dns-records" "" "POST" "application/json" | _base64) + _payload="{\"type\":\"txt\",\"name\":\"${_record_name}\",\"cloud\":false,\"value\":{\"text\":\"${_challenge}\"},\"ttl\":120}" + _response=$(_post "${_payload}" "${ARVAN_CDN_API}/domains/${_zone}/dns-records" "" "POST" "application/json" | _base64) - _debug2 "_record_add(): " $_response - _debug2 " Payload: " $_payload + _debug2 "_record_add(): ${_response}" + _debug2 " Payload: ${_payload}" } #Usage: _record_get_id zone challenge @@ -134,13 +134,13 @@ _record_get_id(){ _challenge=$2 export _H1="Content-Type: application-json" - export _H2="Authorization: apikey $ARVAN_API_KEY" + export _H2="Authorization: apikey ${ARVAN_API_KEY}" - _response=$(_get $ARVAN_CDN_API/domains/$_zone/dns-records/?type=txt\&search=$_challenge | _json_decode | _normalizeJson | grep -Eo '"id":.*?,"value":\{"text":".*?"\}' | sed 's/"id":"\([^"]*\)".*/\1/') - _debug2 "_record_get_id(): " $_response + _response=$(_get "${ARVAN_CDN_API}/domains/${_zone}/dns-records/?type=txt\&search=${_challenge}" | _json_decode | _normalizeJson | grep -Eo '"id":.*?,"value":\{"text":".*?"\}' | sed 's/"id":"\([^"]*\)".*/\1/') + _debug2 "_record_get_id(): ${_response}" - echo "$_response" + echo "${_response}" } @@ -153,9 +153,9 @@ _record_remove(){ export _H1="Content-Type: application-json" export _H2="Authorization: apikey $ARVAN_API_KEY" - _response=$(_post "" $ARVAN_CDN_API/domains/$_zone/dns-records/$_record_id "" "DELETE" "application/json") + _response=$(_post "" "$ARVAN_CDN_API/domains/$_zone/dns-records/$_record_id" "" "DELETE" "application/json") _debug "_record_remove(): ACME Challenge Removed" - _debug2 " Response: " $_response + _debug2 " Response: $_response" } From a23ca126bdf8d54cf48ab323f5d302d014349205 Mon Sep 17 00:00:00 2001 From: "M.A.Sarbanha" <39798816+sarbanha@users.noreply.github.com> Date: Thu, 14 Jul 2022 13:34:32 +0400 Subject: [PATCH 4/9] Code Improved --- dnsapi/dns_arvancdn.sh | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/dnsapi/dns_arvancdn.sh b/dnsapi/dns_arvancdn.sh index cecc1e0d..d8a08ec7 100644 --- a/dnsapi/dns_arvancdn.sh +++ b/dnsapi/dns_arvancdn.sh @@ -31,8 +31,10 @@ dns_arvancdn_add() { return 1 fi - _record_name=$(echo "${_zone}" | sed "s/\.\..*//") - _zone=$(echo "${_zone}" | sed "s/.*\.\.//") + #_record_name=$(echo "${_zone}" | sed "s/\.\..*//") + _record_name=${_zone/\.\.*/} + #_zone=$(echo "${_zone}" | sed "s/.*\.\.//") + _zone=${_zone/*\.\./} _debug "dns_arvan_add(): fulldomain ${_fulldomain}" _debug "dns_arvan_add(): textvalue ${_challenge}" @@ -63,8 +65,10 @@ dns_arvancdn_rm(){ return 1 fi - _record_name=$(echo "${_zone}" | sed "s/\.\..*//") - _zone=$(echo "${_zone}" | sed "s/.*\.\.//") + #_record_name=$(echo "${_zone}" | sed "s/\.\..*//") + _record_name=${_zone/\.\.*/} + #_zone=$(echo "${_zone}" | sed "s/.*\.\.//") + _zone=${_zone/*\.\./} _record_id=$(_record_get_id "${_zone}" "${_challenge}") @@ -86,13 +90,14 @@ _get_root(){ export _H2="Authorization: apikey ${ARVAN_API_KEY}" _response=$(_get "${ARVAN_CDN_API}/domains") - _domains_list=( $( echo "${_response}" | grep -Poe '"domain":"[^"]*"' | sed 's/"domain":"//' | sed 's/"//') ) + #_domains_list=( $( echo "${_response}" | grep -Poe '"domain":"[^"]*"' | sed 's/"domain":"//' | sed 's/"//') ) + read -a _domains_list < <( echo "${_response}" | grep -Poe '"domain":"[^"]*"' | sed 's/"domain":"//' | sed 's/"//') _debug2 "_get_root(): reponse ${_response}" - _debug2 "_get_root(): domains list ${_domains_list}" + _debug2 "_get_root(): domains list ${_domains_list[*]}" #Fibding a matching Zone - while [[ ! -z "${_zone}" ]]; do + while [[ -n "${_zone}" ]]; do for tmp in "${_domains_list[@]}"; do if [ "${tmp}" = "${_zone}" ]; then break 2 From 038ccb147620e02550005de3f5ae562b40d713f5 Mon Sep 17 00:00:00 2001 From: "M.A.Sarbanha" <39798816+sarbanha@users.noreply.github.com> Date: Sat, 16 Jul 2022 10:21:00 +0400 Subject: [PATCH 5/9] Code Improved #2 --- dnsapi/dns_arvancdn.sh | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/dnsapi/dns_arvancdn.sh b/dnsapi/dns_arvancdn.sh index d8a08ec7..a8e9aaa0 100644 --- a/dnsapi/dns_arvancdn.sh +++ b/dnsapi/dns_arvancdn.sh @@ -25,8 +25,8 @@ dns_arvancdn_add() { _saveaccountconf_mutable ARVAN_API_KEY "${ARVAN_API_KEY}" _debug "dns_arvan_add(): Check domain root zone availability for ${_fulldomain}" - _zone=$(_get_root "${_fulldomain}") - if [ $? -ne 0 ]; then + + if ! _zone=$(_get_root "${_fulldomain}"); then _err "dns_arvan_add(): Root zone for ${_fulldomain} not found!" return 1 fi @@ -59,8 +59,7 @@ dns_arvancdn_rm(){ return 1 fi - _zone=$(_get_root "${_fulldomain}") - if [ $? -ne 0 ]; then + if ! _zone=$(_get_root "${_fulldomain}"); then _err "dns_arvan_rm(): Root zone for ${_fulldomain} not found!" return 1 fi @@ -91,7 +90,7 @@ _get_root(){ _response=$(_get "${ARVAN_CDN_API}/domains") #_domains_list=( $( echo "${_response}" | grep -Poe '"domain":"[^"]*"' | sed 's/"domain":"//' | sed 's/"//') ) - read -a _domains_list < <( echo "${_response}" | grep -Poe '"domain":"[^"]*"' | sed 's/"domain":"//' | sed 's/"//') + read -r -a _domains_list < <( echo "${_response}" | grep -Poe '"domain":"[^"]*"' | sed 's/"domain":"//' | sed 's/"//') _debug2 "_get_root(): reponse ${_response}" _debug2 "_get_root(): domains list ${_domains_list[*]}" From d0d4926e2ca1eef495462f981daefc71aa93d01e Mon Sep 17 00:00:00 2001 From: "M.A.Sarbanha" <39798816+sarbanha@users.noreply.github.com> Date: Sat, 16 Jul 2022 10:42:07 +0400 Subject: [PATCH 6/9] All ShellCheck Alarms fixed --- dnsapi/dns_arvancdn.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dnsapi/dns_arvancdn.sh b/dnsapi/dns_arvancdn.sh index a8e9aaa0..37d8753b 100644 --- a/dnsapi/dns_arvancdn.sh +++ b/dnsapi/dns_arvancdn.sh @@ -102,14 +102,14 @@ _get_root(){ break 2 fi done - _zone=$(echo "${_zone}" | sed 's/^[^.]*\.\?//') + _zone=$(sed 's/^[^.]*\.\?//' <(echo "${_zone}")) done if [ -z "${_zone}" ]; then _debug2 "_get_root(): Zone not found on provider" exit 1 fi - _marked_zone=$(echo "${_fulldomain}" | sed "s/^\(.*\)\.\(${_zone}\)$/\1..\2/") + _marked_zone=$(sed "s/^\(.*\)\.\(${_zone}\)$/\1..\2/" <(echo "${_fulldomain}")) echo "${_marked_zone}" } From 37778b6ef27786936b809cf216bfd591d83d7832 Mon Sep 17 00:00:00 2001 From: "M.A.Sarbanha" <39798816+sarbanha@users.noreply.github.com> Date: Sat, 16 Jul 2022 10:46:44 +0400 Subject: [PATCH 7/9] Code format fixed --- dnsapi/dns_arvancdn.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dnsapi/dns_arvancdn.sh b/dnsapi/dns_arvancdn.sh index 37d8753b..1662a40e 100644 --- a/dnsapi/dns_arvancdn.sh +++ b/dnsapi/dns_arvancdn.sh @@ -46,7 +46,7 @@ dns_arvancdn_add() { } #Usage: dns_arvancdn_rm fulldomain txtvalue -dns_arvancdn_rm(){ +dns_arvancdn_rm() { _fulldomain=$1 _challenge=$2 @@ -81,7 +81,7 @@ dns_arvancdn_rm(){ #################### #Usage: _get_root zone -_get_root(){ +_get_root() { _fulldomain=$1 _zone=$_fulldomain @@ -115,7 +115,7 @@ _get_root(){ } #Usage: _record_add record_name zone challenge -_record_add(){ +_record_add() { _record_name=$1 _zone=$2 @@ -132,7 +132,7 @@ _record_add(){ } #Usage: _record_get_id zone challenge -_record_get_id(){ +_record_get_id() { _zone=$1 _challenge=$2 @@ -149,7 +149,7 @@ _record_get_id(){ } #Usage: _record_remove zone record_id -_record_remove(){ +_record_remove() { _zone=$1 _record_id=$2 From 058ef52c51037977217b6d2dd5bf202f7a10ade3 Mon Sep 17 00:00:00 2001 From: "M.A.Sarbanha" <39798816+sarbanha@users.noreply.github.com> Date: Sat, 16 Jul 2022 11:00:40 +0400 Subject: [PATCH 8/9] Formatted --- dnsapi/dns_arvancdn.sh | 194 ++++++++++++++++++++--------------------- 1 file changed, 96 insertions(+), 98 deletions(-) diff --git a/dnsapi/dns_arvancdn.sh b/dnsapi/dns_arvancdn.sh index 1662a40e..c93ef1c2 100644 --- a/dnsapi/dns_arvancdn.sh +++ b/dnsapi/dns_arvancdn.sh @@ -10,69 +10,68 @@ ARVAN_CDN_API="https://napi.arvancloud.com/cdn/4.0" #Usage: dns_arvancdn_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" dns_arvancdn_add() { - _fulldomain=$1 - _challenge=$2 + _fulldomain=$1 + _challenge=$2 - _debug "dns_arvan_add(): Started" + _debug "dns_arvan_add(): Started" - ARVAN_API_KEY="${ARVAN_API_KEY:-$(_readaccountconf_mutable ARVAN_API_KEY)}" - if [ -z "${ARVAN_API_KEY}" ]; then - ARVAN_API_KEY="" - _err "dns_arvan_add(): ARVAN_API_KEY has not been defined yet." - _err "dns_arvan_add(): export ARVAN_API_KEY=\"---YOUR-API-KEY---\"" - return 1 - fi - _saveaccountconf_mutable ARVAN_API_KEY "${ARVAN_API_KEY}" + ARVAN_API_KEY="${ARVAN_API_KEY:-$(_readaccountconf_mutable ARVAN_API_KEY)}" + if [ -z "${ARVAN_API_KEY}" ]; then + ARVAN_API_KEY="" + _err "dns_arvan_add(): ARVAN_API_KEY has not been defined yet." + _err "dns_arvan_add(): export ARVAN_API_KEY=\"---YOUR-API-KEY---\"" + return 1 + fi + _saveaccountconf_mutable ARVAN_API_KEY "${ARVAN_API_KEY}" - _debug "dns_arvan_add(): Check domain root zone availability for ${_fulldomain}" + _debug "dns_arvan_add(): Check domain root zone availability for ${_fulldomain}" - if ! _zone=$(_get_root "${_fulldomain}"); then - _err "dns_arvan_add(): Root zone for ${_fulldomain} not found!" - return 1 - fi + if ! _zone=$(_get_root "${_fulldomain}"); then + _err "dns_arvan_add(): Root zone for ${_fulldomain} not found!" + return 1 + fi - #_record_name=$(echo "${_zone}" | sed "s/\.\..*//") - _record_name=${_zone/\.\.*/} - #_zone=$(echo "${_zone}" | sed "s/.*\.\.//") - _zone=${_zone/*\.\./} + #_record_name=$(echo "${_zone}" | sed "s/\.\..*//") + _record_name=${_zone/\.\.*/} + #_zone=$(echo "${_zone}" | sed "s/.*\.\.//") + _zone=${_zone/*\.\./} - _debug "dns_arvan_add(): fulldomain ${_fulldomain}" - _debug "dns_arvan_add(): textvalue ${_challenge}" - _debug "dns_arvan_add(): domain ${_record_name}" - _debug "dns_arvan_add(): domain ${_zone}" + _debug "dns_arvan_add(): fulldomain ${_fulldomain}" + _debug "dns_arvan_add(): textvalue ${_challenge}" + _debug "dns_arvan_add(): domain ${_record_name}" + _debug "dns_arvan_add(): domain ${_zone}" - _record_add "${_record_name}" "${_zone}" "${_challenge}" + _record_add "${_record_name}" "${_zone}" "${_challenge}" } #Usage: dns_arvancdn_rm fulldomain txtvalue dns_arvancdn_rm() { - _fulldomain=$1 - _challenge=$2 + _fulldomain=$1 + _challenge=$2 - ARVAN_API_KEY="${ARVAN_API_KEY:-$(_readaccountconf_mutable ARVAN_API_KEY)}" - if [ -z "${ARVAN_API_KEY}" ]; then - ARVAN_API_KEY="" - _err "dns_arvan_rm(): ARVAN_API_KEY has not been defined yet." - _err "dns_arvan_rm(): export ARVAN_API_KEY=\"---YOUR-API-KEY---\"" - return 1 - fi + ARVAN_API_KEY="${ARVAN_API_KEY:-$(_readaccountconf_mutable ARVAN_API_KEY)}" + if [ -z "${ARVAN_API_KEY}" ]; then + ARVAN_API_KEY="" + _err "dns_arvan_rm(): ARVAN_API_KEY has not been defined yet." + _err "dns_arvan_rm(): export ARVAN_API_KEY=\"---YOUR-API-KEY---\"" + return 1 + fi - if ! _zone=$(_get_root "${_fulldomain}"); then - _err "dns_arvan_rm(): Root zone for ${_fulldomain} not found!" - return 1 - fi + if ! _zone=$(_get_root "${_fulldomain}"); then + _err "dns_arvan_rm(): Root zone for ${_fulldomain} not found!" + return 1 + fi - #_record_name=$(echo "${_zone}" | sed "s/\.\..*//") - _record_name=${_zone/\.\.*/} - #_zone=$(echo "${_zone}" | sed "s/.*\.\.//") - _zone=${_zone/*\.\./} + #_record_name=$(echo "${_zone}" | sed "s/\.\..*//") + _record_name=${_zone/\.\.*/} + #_zone=$(echo "${_zone}" | sed "s/.*\.\.//") + _zone=${_zone/*\.\./} + _record_id=$(_record_get_id "${_zone}" "${_challenge}") - _record_id=$(_record_get_id "${_zone}" "${_challenge}") - - _record_remove "${_zone}" "${_record_id}" + _record_remove "${_zone}" "${_record_id}" } @@ -82,84 +81,83 @@ dns_arvancdn_rm() { #Usage: _get_root zone _get_root() { - _fulldomain=$1 - _zone=$_fulldomain - - export _H1="Content-Type: application-json" - export _H2="Authorization: apikey ${ARVAN_API_KEY}" - - _response=$(_get "${ARVAN_CDN_API}/domains") - #_domains_list=( $( echo "${_response}" | grep -Poe '"domain":"[^"]*"' | sed 's/"domain":"//' | sed 's/"//') ) - read -r -a _domains_list < <( echo "${_response}" | grep -Poe '"domain":"[^"]*"' | sed 's/"domain":"//' | sed 's/"//') - - _debug2 "_get_root(): reponse ${_response}" - _debug2 "_get_root(): domains list ${_domains_list[*]}" - - #Fibding a matching Zone - while [[ -n "${_zone}" ]]; do - for tmp in "${_domains_list[@]}"; do - if [ "${tmp}" = "${_zone}" ]; then - break 2 - fi - done - _zone=$(sed 's/^[^.]*\.\?//' <(echo "${_zone}")) - done - if [ -z "${_zone}" ]; then - _debug2 "_get_root(): Zone not found on provider" - exit 1 - fi - - _marked_zone=$(sed "s/^\(.*\)\.\(${_zone}\)$/\1..\2/" <(echo "${_fulldomain}")) - echo "${_marked_zone}" + _fulldomain=$1 + _zone=$_fulldomain + + export _H1="Content-Type: application-json" + export _H2="Authorization: apikey ${ARVAN_API_KEY}" + + _response=$(_get "${ARVAN_CDN_API}/domains") + #_domains_list=( $( echo "${_response}" | grep -Poe '"domain":"[^"]*"' | sed 's/"domain":"//' | sed 's/"//') ) + read -r -a _domains_list < <(echo "${_response}" | grep -Poe '"domain":"[^"]*"' | sed 's/"domain":"//' | sed 's/"//') + + _debug2 "_get_root(): reponse ${_response}" + _debug2 "_get_root(): domains list ${_domains_list[*]}" + + #Fibding a matching Zone + while [[ -n "${_zone}" ]]; do + for tmp in "${_domains_list[@]}"; do + if [ "${tmp}" = "${_zone}" ]; then + break 2 + fi + done + _zone=$(sed 's/^[^.]*\.\?//' <(echo "${_zone}")) + done + if [ -z "${_zone}" ]; then + _debug2 "_get_root(): Zone not found on provider" + exit 1 + fi + + _marked_zone=$(sed "s/^\(.*\)\.\(${_zone}\)$/\1..\2/" <(echo "${_fulldomain}")) + echo "${_marked_zone}" } #Usage: _record_add record_name zone challenge _record_add() { - _record_name=$1 - _zone=$2 - _challenge=$3 + _record_name=$1 + _zone=$2 + _challenge=$3 - export _H1="Content-Type: application-json" - export _H2="Authorization: apikey ${ARVAN_API_KEY}" + export _H1="Content-Type: application-json" + export _H2="Authorization: apikey ${ARVAN_API_KEY}" - _payload="{\"type\":\"txt\",\"name\":\"${_record_name}\",\"cloud\":false,\"value\":{\"text\":\"${_challenge}\"},\"ttl\":120}" - _response=$(_post "${_payload}" "${ARVAN_CDN_API}/domains/${_zone}/dns-records" "" "POST" "application/json" | _base64) + _payload="{\"type\":\"txt\",\"name\":\"${_record_name}\",\"cloud\":false,\"value\":{\"text\":\"${_challenge}\"},\"ttl\":120}" + _response=$(_post "${_payload}" "${ARVAN_CDN_API}/domains/${_zone}/dns-records" "" "POST" "application/json" | _base64) - _debug2 "_record_add(): ${_response}" - _debug2 " Payload: ${_payload}" + _debug2 "_record_add(): ${_response}" + _debug2 " Payload: ${_payload}" } #Usage: _record_get_id zone challenge _record_get_id() { - _zone=$1 - _challenge=$2 - - export _H1="Content-Type: application-json" - export _H2="Authorization: apikey ${ARVAN_API_KEY}" + _zone=$1 + _challenge=$2 - _response=$(_get "${ARVAN_CDN_API}/domains/${_zone}/dns-records/?type=txt\&search=${_challenge}" | _json_decode | _normalizeJson | grep -Eo '"id":.*?,"value":\{"text":".*?"\}' | sed 's/"id":"\([^"]*\)".*/\1/') - _debug2 "_record_get_id(): ${_response}" + export _H1="Content-Type: application-json" + export _H2="Authorization: apikey ${ARVAN_API_KEY}" + _response=$(_get "${ARVAN_CDN_API}/domains/${_zone}/dns-records/?type=txt\&search=${_challenge}" | _json_decode | _normalizeJson | grep -Eo '"id":.*?,"value":\{"text":".*?"\}' | sed 's/"id":"\([^"]*\)".*/\1/') + _debug2 "_record_get_id(): ${_response}" - echo "${_response}" + echo "${_response}" } #Usage: _record_remove zone record_id _record_remove() { - _zone=$1 - _record_id=$2 + _zone=$1 + _record_id=$2 - export _H1="Content-Type: application-json" - export _H2="Authorization: apikey $ARVAN_API_KEY" + export _H1="Content-Type: application-json" + export _H2="Authorization: apikey $ARVAN_API_KEY" - _response=$(_post "" "$ARVAN_CDN_API/domains/$_zone/dns-records/$_record_id" "" "DELETE" "application/json") + _response=$(_post "" "$ARVAN_CDN_API/domains/$_zone/dns-records/$_record_id" "" "DELETE" "application/json") - _debug "_record_remove(): ACME Challenge Removed" - _debug2 " Response: $_response" + _debug "_record_remove(): ACME Challenge Removed" + _debug2 " Response: $_response" } From 1a0ef3318563c64ec9da4855ae2f98bd7b8d59e0 Mon Sep 17 00:00:00 2001 From: "M.A.Sarbanha" <39798816+sarbanha@users.noreply.github.com> Date: Sat, 16 Jul 2022 11:06:23 +0400 Subject: [PATCH 9/9] Formatted #2 --- dnsapi/dns_arvancdn.sh | 192 ++++++++++++++++++++--------------------- 1 file changed, 96 insertions(+), 96 deletions(-) diff --git a/dnsapi/dns_arvancdn.sh b/dnsapi/dns_arvancdn.sh index c93ef1c2..4fedec7f 100644 --- a/dnsapi/dns_arvancdn.sh +++ b/dnsapi/dns_arvancdn.sh @@ -10,68 +10,68 @@ ARVAN_CDN_API="https://napi.arvancloud.com/cdn/4.0" #Usage: dns_arvancdn_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" dns_arvancdn_add() { - _fulldomain=$1 - _challenge=$2 + _fulldomain=$1 + _challenge=$2 - _debug "dns_arvan_add(): Started" + _debug "dns_arvan_add(): Started" - ARVAN_API_KEY="${ARVAN_API_KEY:-$(_readaccountconf_mutable ARVAN_API_KEY)}" - if [ -z "${ARVAN_API_KEY}" ]; then - ARVAN_API_KEY="" - _err "dns_arvan_add(): ARVAN_API_KEY has not been defined yet." - _err "dns_arvan_add(): export ARVAN_API_KEY=\"---YOUR-API-KEY---\"" - return 1 - fi - _saveaccountconf_mutable ARVAN_API_KEY "${ARVAN_API_KEY}" + ARVAN_API_KEY="${ARVAN_API_KEY:-$(_readaccountconf_mutable ARVAN_API_KEY)}" + if [ -z "${ARVAN_API_KEY}" ]; then + ARVAN_API_KEY="" + _err "dns_arvan_add(): ARVAN_API_KEY has not been defined yet." + _err "dns_arvan_add(): export ARVAN_API_KEY=\"---YOUR-API-KEY---\"" + return 1 + fi + _saveaccountconf_mutable ARVAN_API_KEY "${ARVAN_API_KEY}" - _debug "dns_arvan_add(): Check domain root zone availability for ${_fulldomain}" + _debug "dns_arvan_add(): Check domain root zone availability for ${_fulldomain}" - if ! _zone=$(_get_root "${_fulldomain}"); then - _err "dns_arvan_add(): Root zone for ${_fulldomain} not found!" - return 1 - fi + if ! _zone=$(_get_root "${_fulldomain}"); then + _err "dns_arvan_add(): Root zone for ${_fulldomain} not found!" + return 1 + fi - #_record_name=$(echo "${_zone}" | sed "s/\.\..*//") - _record_name=${_zone/\.\.*/} - #_zone=$(echo "${_zone}" | sed "s/.*\.\.//") - _zone=${_zone/*\.\./} + #_record_name=$(echo "${_zone}" | sed "s/\.\..*//") + _record_name=${_zone/\.\.*/} + #_zone=$(echo "${_zone}" | sed "s/.*\.\.//") + _zone=${_zone/*\.\./} - _debug "dns_arvan_add(): fulldomain ${_fulldomain}" - _debug "dns_arvan_add(): textvalue ${_challenge}" - _debug "dns_arvan_add(): domain ${_record_name}" - _debug "dns_arvan_add(): domain ${_zone}" + _debug "dns_arvan_add(): fulldomain ${_fulldomain}" + _debug "dns_arvan_add(): textvalue ${_challenge}" + _debug "dns_arvan_add(): domain ${_record_name}" + _debug "dns_arvan_add(): domain ${_zone}" - _record_add "${_record_name}" "${_zone}" "${_challenge}" + _record_add "${_record_name}" "${_zone}" "${_challenge}" } #Usage: dns_arvancdn_rm fulldomain txtvalue dns_arvancdn_rm() { - _fulldomain=$1 - _challenge=$2 + _fulldomain=$1 + _challenge=$2 - ARVAN_API_KEY="${ARVAN_API_KEY:-$(_readaccountconf_mutable ARVAN_API_KEY)}" - if [ -z "${ARVAN_API_KEY}" ]; then - ARVAN_API_KEY="" - _err "dns_arvan_rm(): ARVAN_API_KEY has not been defined yet." - _err "dns_arvan_rm(): export ARVAN_API_KEY=\"---YOUR-API-KEY---\"" - return 1 - fi + ARVAN_API_KEY="${ARVAN_API_KEY:-$(_readaccountconf_mutable ARVAN_API_KEY)}" + if [ -z "${ARVAN_API_KEY}" ]; then + ARVAN_API_KEY="" + _err "dns_arvan_rm(): ARVAN_API_KEY has not been defined yet." + _err "dns_arvan_rm(): export ARVAN_API_KEY=\"---YOUR-API-KEY---\"" + return 1 + fi - if ! _zone=$(_get_root "${_fulldomain}"); then - _err "dns_arvan_rm(): Root zone for ${_fulldomain} not found!" - return 1 - fi + if ! _zone=$(_get_root "${_fulldomain}"); then + _err "dns_arvan_rm(): Root zone for ${_fulldomain} not found!" + return 1 + fi - #_record_name=$(echo "${_zone}" | sed "s/\.\..*//") - _record_name=${_zone/\.\.*/} - #_zone=$(echo "${_zone}" | sed "s/.*\.\.//") - _zone=${_zone/*\.\./} + #_record_name=$(echo "${_zone}" | sed "s/\.\..*//") + _record_name=${_zone/\.\.*/} + #_zone=$(echo "${_zone}" | sed "s/.*\.\.//") + _zone=${_zone/*\.\./} - _record_id=$(_record_get_id "${_zone}" "${_challenge}") + _record_id=$(_record_get_id "${_zone}" "${_challenge}") - _record_remove "${_zone}" "${_record_id}" + _record_remove "${_zone}" "${_record_id}" } @@ -81,83 +81,83 @@ dns_arvancdn_rm() { #Usage: _get_root zone _get_root() { - _fulldomain=$1 - _zone=$_fulldomain - - export _H1="Content-Type: application-json" - export _H2="Authorization: apikey ${ARVAN_API_KEY}" - - _response=$(_get "${ARVAN_CDN_API}/domains") - #_domains_list=( $( echo "${_response}" | grep -Poe '"domain":"[^"]*"' | sed 's/"domain":"//' | sed 's/"//') ) - read -r -a _domains_list < <(echo "${_response}" | grep -Poe '"domain":"[^"]*"' | sed 's/"domain":"//' | sed 's/"//') - - _debug2 "_get_root(): reponse ${_response}" - _debug2 "_get_root(): domains list ${_domains_list[*]}" - - #Fibding a matching Zone - while [[ -n "${_zone}" ]]; do - for tmp in "${_domains_list[@]}"; do - if [ "${tmp}" = "${_zone}" ]; then - break 2 - fi - done - _zone=$(sed 's/^[^.]*\.\?//' <(echo "${_zone}")) - done - if [ -z "${_zone}" ]; then - _debug2 "_get_root(): Zone not found on provider" - exit 1 - fi - - _marked_zone=$(sed "s/^\(.*\)\.\(${_zone}\)$/\1..\2/" <(echo "${_fulldomain}")) - echo "${_marked_zone}" + _fulldomain=$1 + _zone=$_fulldomain + + export _H1="Content-Type: application-json" + export _H2="Authorization: apikey ${ARVAN_API_KEY}" + + _response=$(_get "${ARVAN_CDN_API}/domains") + #_domains_list=( $( echo "${_response}" | grep -Poe '"domain":"[^"]*"' | sed 's/"domain":"//' | sed 's/"//') ) + read -r -a _domains_list < <(echo "${_response}" | grep -Poe '"domain":"[^"]*"' | sed 's/"domain":"//' | sed 's/"//') + + _debug2 "_get_root(): reponse ${_response}" + _debug2 "_get_root(): domains list ${_domains_list[*]}" + + #Fibding a matching Zone + while [[ -n "${_zone}" ]]; do + for tmp in "${_domains_list[@]}"; do + if [ "${tmp}" = "${_zone}" ]; then + break 2 + fi + done + _zone=$(sed 's/^[^.]*\.\?//' <(echo "${_zone}")) + done + if [ -z "${_zone}" ]; then + _debug2 "_get_root(): Zone not found on provider" + exit 1 + fi + + _marked_zone=$(sed "s/^\(.*\)\.\(${_zone}\)$/\1..\2/" <(echo "${_fulldomain}")) + echo "${_marked_zone}" } #Usage: _record_add record_name zone challenge _record_add() { - _record_name=$1 - _zone=$2 - _challenge=$3 + _record_name=$1 + _zone=$2 + _challenge=$3 - export _H1="Content-Type: application-json" - export _H2="Authorization: apikey ${ARVAN_API_KEY}" + export _H1="Content-Type: application-json" + export _H2="Authorization: apikey ${ARVAN_API_KEY}" - _payload="{\"type\":\"txt\",\"name\":\"${_record_name}\",\"cloud\":false,\"value\":{\"text\":\"${_challenge}\"},\"ttl\":120}" - _response=$(_post "${_payload}" "${ARVAN_CDN_API}/domains/${_zone}/dns-records" "" "POST" "application/json" | _base64) + _payload="{\"type\":\"txt\",\"name\":\"${_record_name}\",\"cloud\":false,\"value\":{\"text\":\"${_challenge}\"},\"ttl\":120}" + _response=$(_post "${_payload}" "${ARVAN_CDN_API}/domains/${_zone}/dns-records" "" "POST" "application/json" | _base64) - _debug2 "_record_add(): ${_response}" - _debug2 " Payload: ${_payload}" + _debug2 "_record_add(): ${_response}" + _debug2 " Payload: ${_payload}" } #Usage: _record_get_id zone challenge _record_get_id() { - _zone=$1 - _challenge=$2 + _zone=$1 + _challenge=$2 - export _H1="Content-Type: application-json" - export _H2="Authorization: apikey ${ARVAN_API_KEY}" + export _H1="Content-Type: application-json" + export _H2="Authorization: apikey ${ARVAN_API_KEY}" - _response=$(_get "${ARVAN_CDN_API}/domains/${_zone}/dns-records/?type=txt\&search=${_challenge}" | _json_decode | _normalizeJson | grep -Eo '"id":.*?,"value":\{"text":".*?"\}' | sed 's/"id":"\([^"]*\)".*/\1/') - _debug2 "_record_get_id(): ${_response}" + _response=$(_get "${ARVAN_CDN_API}/domains/${_zone}/dns-records/?type=txt\&search=${_challenge}" | _json_decode | _normalizeJson | grep -Eo '"id":.*?,"value":\{"text":".*?"\}' | sed 's/"id":"\([^"]*\)".*/\1/') + _debug2 "_record_get_id(): ${_response}" - echo "${_response}" + echo "${_response}" } #Usage: _record_remove zone record_id _record_remove() { - _zone=$1 - _record_id=$2 + _zone=$1 + _record_id=$2 - export _H1="Content-Type: application-json" - export _H2="Authorization: apikey $ARVAN_API_KEY" + export _H1="Content-Type: application-json" + export _H2="Authorization: apikey $ARVAN_API_KEY" - _response=$(_post "" "$ARVAN_CDN_API/domains/$_zone/dns-records/$_record_id" "" "DELETE" "application/json") + _response=$(_post "" "$ARVAN_CDN_API/domains/$_zone/dns-records/$_record_id" "" "DELETE" "application/json") - _debug "_record_remove(): ACME Challenge Removed" - _debug2 " Response: $_response" + _debug "_record_remove(): ACME Challenge Removed" + _debug2 " Response: $_response" }