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.

163 lines
4.6 KiB

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