You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.6 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #!/usr/bin/env bash
  2. # Author: Mohammad Ali Sarbanha <sarbanha at yahoo dot com>
  3. # Repository: https://github.com/sarbanha/acme.sh-dnsapi-dns_arvancdn
  4. # export ARVAN_API_KEY="-----------"
  5. ARVAN_CDN_API="https://napi.arvancloud.com/cdn/4.0"
  6. #Usage: dns_arvancdn_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  7. dns_arvancdn_add() {
  8. _fulldomain=$1
  9. _challenge=$2
  10. _debug "dns_arvan_add(): Started"
  11. ARVAN_API_KEY="${ARVAN_API_KEY:-$(_readaccountconf_mutable ARVAN_API_KEY)}"
  12. if [ -z "${ARVAN_API_KEY}" ]; then
  13. ARVAN_API_KEY=""
  14. _err "dns_arvan_add(): ARVAN_API_KEY has not been defined yet."
  15. _err "dns_arvan_add(): export ARVAN_API_KEY=\"---YOUR-API-KEY---\""
  16. return 1
  17. fi
  18. _saveaccountconf_mutable ARVAN_API_KEY "${ARVAN_API_KEY}"
  19. _debug "dns_arvan_add(): Check domain root zone availability for ${_fulldomain}"
  20. _zone=$(_get_root "${_fulldomain}")
  21. if [ $? -ne 0 ]; then
  22. _err "dns_arvan_add(): Root zone for ${_fulldomain} not found!"
  23. return 1
  24. fi
  25. #_record_name=$(echo "${_zone}" | sed "s/\.\..*//")
  26. _record_name=${_zone/\.\.*/}
  27. #_zone=$(echo "${_zone}" | sed "s/.*\.\.//")
  28. _zone=${_zone/*\.\./}
  29. _debug "dns_arvan_add(): fulldomain ${_fulldomain}"
  30. _debug "dns_arvan_add(): textvalue ${_challenge}"
  31. _debug "dns_arvan_add(): domain ${_record_name}"
  32. _debug "dns_arvan_add(): domain ${_zone}"
  33. _record_add "${_record_name}" "${_zone}" "${_challenge}"
  34. }
  35. #Usage: dns_arvancdn_rm fulldomain txtvalue
  36. dns_arvancdn_rm(){
  37. _fulldomain=$1
  38. _challenge=$2
  39. ARVAN_API_KEY="${ARVAN_API_KEY:-$(_readaccountconf_mutable ARVAN_API_KEY)}"
  40. if [ -z "${ARVAN_API_KEY}" ]; then
  41. ARVAN_API_KEY=""
  42. _err "dns_arvan_rm(): ARVAN_API_KEY has not been defined yet."
  43. _err "dns_arvan_rm(): export ARVAN_API_KEY=\"---YOUR-API-KEY---\""
  44. return 1
  45. fi
  46. _zone=$(_get_root "${_fulldomain}")
  47. if [ $? -ne 0 ]; then
  48. _err "dns_arvan_rm(): Root zone for ${_fulldomain} not found!"
  49. return 1
  50. fi
  51. #_record_name=$(echo "${_zone}" | sed "s/\.\..*//")
  52. _record_name=${_zone/\.\.*/}
  53. #_zone=$(echo "${_zone}" | sed "s/.*\.\.//")
  54. _zone=${_zone/*\.\./}
  55. _record_id=$(_record_get_id "${_zone}" "${_challenge}")
  56. _record_remove "${_zone}" "${_record_id}"
  57. }
  58. ####################
  59. # Private functions
  60. ####################
  61. #Usage: _get_root zone
  62. _get_root(){
  63. _fulldomain=$1
  64. _zone=$_fulldomain
  65. export _H1="Content-Type: application-json"
  66. export _H2="Authorization: apikey ${ARVAN_API_KEY}"
  67. _response=$(_get "${ARVAN_CDN_API}/domains")
  68. #_domains_list=( $( echo "${_response}" | grep -Poe '"domain":"[^"]*"' | sed 's/"domain":"//' | sed 's/"//') )
  69. read -a _domains_list < <( echo "${_response}" | grep -Poe '"domain":"[^"]*"' | sed 's/"domain":"//' | sed 's/"//')
  70. _debug2 "_get_root(): reponse ${_response}"
  71. _debug2 "_get_root(): domains list ${_domains_list[*]}"
  72. #Fibding a matching Zone
  73. while [[ -n "${_zone}" ]]; do
  74. for tmp in "${_domains_list[@]}"; do
  75. if [ "${tmp}" = "${_zone}" ]; then
  76. break 2
  77. fi
  78. done
  79. _zone=$(echo "${_zone}" | sed 's/^[^.]*\.\?//')
  80. done
  81. if [ -z "${_zone}" ]; then
  82. _debug2 "_get_root(): Zone not found on provider"
  83. exit 1
  84. fi
  85. _marked_zone=$(echo "${_fulldomain}" | sed "s/^\(.*\)\.\(${_zone}\)$/\1..\2/")
  86. echo "${_marked_zone}"
  87. }
  88. #Usage: _record_add record_name zone challenge
  89. _record_add(){
  90. _record_name=$1
  91. _zone=$2
  92. _challenge=$3
  93. export _H1="Content-Type: application-json"
  94. export _H2="Authorization: apikey ${ARVAN_API_KEY}"
  95. _payload="{\"type\":\"txt\",\"name\":\"${_record_name}\",\"cloud\":false,\"value\":{\"text\":\"${_challenge}\"},\"ttl\":120}"
  96. _response=$(_post "${_payload}" "${ARVAN_CDN_API}/domains/${_zone}/dns-records" "" "POST" "application/json" | _base64)
  97. _debug2 "_record_add(): ${_response}"
  98. _debug2 " Payload: ${_payload}"
  99. }
  100. #Usage: _record_get_id zone challenge
  101. _record_get_id(){
  102. _zone=$1
  103. _challenge=$2
  104. export _H1="Content-Type: application-json"
  105. export _H2="Authorization: apikey ${ARVAN_API_KEY}"
  106. _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/')
  107. _debug2 "_record_get_id(): ${_response}"
  108. echo "${_response}"
  109. }
  110. #Usage: _record_remove zone record_id
  111. _record_remove(){
  112. _zone=$1
  113. _record_id=$2
  114. export _H1="Content-Type: application-json"
  115. export _H2="Authorization: apikey $ARVAN_API_KEY"
  116. _response=$(_post "" "$ARVAN_CDN_API/domains/$_zone/dns-records/$_record_id" "" "DELETE" "application/json")
  117. _debug "_record_remove(): ACME Challenge Removed"
  118. _debug2 " Response: $_response"
  119. }