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.

161 lines
4.4 KiB

  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. _zone=$(echo "${_zone}" | sed "s/.*\.\.//")
  27. _debug "dns_arvan_add(): fulldomain ${_fulldomain}"
  28. _debug "dns_arvan_add(): textvalue ${_challenge}"
  29. _debug "dns_arvan_add(): domain ${_record_name}"
  30. _debug "dns_arvan_add(): domain ${_zone}"
  31. _record_add "${_record_name}" "${_zone}" "${_challenge}"
  32. }
  33. #Usage: dns_arvancdn_rm fulldomain txtvalue
  34. dns_arvancdn_rm(){
  35. _fulldomain=$1
  36. _challenge=$2
  37. ARVAN_API_KEY="${ARVAN_API_KEY:-$(_readaccountconf_mutable ARVAN_API_KEY)}"
  38. if [ -z "${ARVAN_API_KEY}" ]; then
  39. ARVAN_API_KEY=""
  40. _err "dns_arvan_rm(): ARVAN_API_KEY has not been defined yet."
  41. _err "dns_arvan_rm(): export ARVAN_API_KEY=\"---YOUR-API-KEY---\""
  42. return 1
  43. fi
  44. _zone=$(_get_root "${_fulldomain}")
  45. if [ $? -ne 0 ]; then
  46. _err "dns_arvan_rm(): Root zone for ${_fulldomain} not found!"
  47. return 1
  48. fi
  49. _record_name=$(echo "${_zone}" | sed "s/\.\..*//")
  50. _zone=$(echo "${_zone}" | sed "s/.*\.\.//")
  51. _record_id=$(_record_get_id "${_zone}" "${_challenge}")
  52. _record_remove "${_zone}" "${_record_id}"
  53. }
  54. ####################
  55. # Private functions
  56. ####################
  57. #Usage: _get_root zone
  58. _get_root(){
  59. _fulldomain=$1
  60. _zone=$_fulldomain
  61. export _H1="Content-Type: application-json"
  62. export _H2="Authorization: apikey ${ARVAN_API_KEY}"
  63. _response=$(_get "${ARVAN_CDN_API}/domains")
  64. _domains_list=( $( echo "${_response}" | grep -Poe '"domain":"[^"]*"' | sed 's/"domain":"//' | sed 's/"//') )
  65. _debug2 "_get_root(): reponse ${_response}"
  66. _debug2 "_get_root(): domains list ${_domains_list}"
  67. #Fibding a matching Zone
  68. while [[ ! -z "${_zone}" ]]; do
  69. for tmp in "${_domains_list[@]}"; do
  70. if [ "${tmp}" = "${_zone}" ]; then
  71. break 2
  72. fi
  73. done
  74. _zone=$(echo "${_zone}" | sed 's/^[^.]*\.\?//')
  75. done
  76. if [ -z "${_zone}" ]; then
  77. _debug2 "_get_root(): Zone not found on provider"
  78. exit 1
  79. fi
  80. _marked_zone=$(echo "${_fulldomain}" | sed "s/^\(.*\)\.\(${_zone}\)$/\1..\2/")
  81. echo "${_marked_zone}"
  82. }
  83. #Usage: _record_add record_name zone challenge
  84. _record_add(){
  85. _record_name=$1
  86. _zone=$2
  87. _challenge=$3
  88. export _H1="Content-Type: application-json"
  89. export _H2="Authorization: apikey ${ARVAN_API_KEY}"
  90. _payload="{\"type\":\"txt\",\"name\":\"${_record_name}\",\"cloud\":false,\"value\":{\"text\":\"${_challenge}\"},\"ttl\":120}"
  91. _response=$(_post "${_payload}" "${ARVAN_CDN_API}/domains/${_zone}/dns-records" "" "POST" "application/json" | _base64)
  92. _debug2 "_record_add(): ${_response}"
  93. _debug2 " Payload: ${_payload}"
  94. }
  95. #Usage: _record_get_id zone challenge
  96. _record_get_id(){
  97. _zone=$1
  98. _challenge=$2
  99. export _H1="Content-Type: application-json"
  100. export _H2="Authorization: apikey ${ARVAN_API_KEY}"
  101. _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/')
  102. _debug2 "_record_get_id(): ${_response}"
  103. echo "${_response}"
  104. }
  105. #Usage: _record_remove zone record_id
  106. _record_remove(){
  107. _zone=$1
  108. _record_id=$2
  109. export _H1="Content-Type: application-json"
  110. export _H2="Authorization: apikey $ARVAN_API_KEY"
  111. _response=$(_post "" "$ARVAN_CDN_API/domains/$_zone/dns-records/$_record_id" "" "DELETE" "application/json")
  112. _debug "_record_remove(): ACME Challenge Removed"
  113. _debug2 " Response: $_response"
  114. }