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.

134 lines
3.4 KiB

  1. #!/usr/bin/env #!/bin/sh
  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. zone=${fulldomain:16}
  11. _debug "dns_arvan_add(): Started"
  12. ARVAN_API_KEY="${ARVAN_API_KEY:-$(_readaccountconf_mutable ARVAN_API_KEY)}"
  13. if [ -z $ARVAN_API_KEY ]; then
  14. ARVAN_API_KEY=""
  15. _err "dns_arvan_add(): ARVAN_API_KEY has not been defined yet."
  16. _err "dns_arvan_add(): export ARVAN_API_KEY=\"---YOUR-API-KEY---\""
  17. return 1
  18. fi
  19. _saveaccountconf_mutable ARVAN_API_KEY "$ARVAN_API_KEY"
  20. _debug "dns_arvan_add(): Check domain root zone availability for $zone"
  21. if ! _get_root $zone; then
  22. _err "dns_arvan_add(): Invalid domain $zone"
  23. return 1
  24. fi
  25. _debug "dns_arvan_add(): fulldomain $fulldomain"
  26. _debug "dns_arvan_add(): textvalue $challenge"
  27. _debug "dns_arvan_add(): domain $zone"
  28. _record_add $zone $challenge
  29. }
  30. #Usage: dns_arvancdn_rm fulldomain txtvalue
  31. dns_arvancdn_rm(){
  32. fulldomain=$1
  33. challenge=$2
  34. zone=${fulldomain:16}
  35. ARVAN_API_KEY="${ARVAN_API_KEY:-$(_readaccountconf_mutable ARVAN_API_KEY)}"
  36. if [ -z $ARVAN_API_KEY ]; then
  37. ARVAN_API_KEY=""
  38. _err "dns_arvan_rm(): ARVAN_API_KEY has not been defined yet."
  39. _err "dns_arvan_rm(): export ARVAN_API_KEY=\"---YOUR-API-KEY---\""
  40. return 1
  41. fi
  42. record_id=$(_record_get_id $zone $challenge)
  43. _record_remove $zone $record_id
  44. }
  45. ####################
  46. # Private functions
  47. ####################
  48. #Usage: _get_root zone
  49. _get_root(){
  50. _zone=$1
  51. export _H1="Content-Type: application-json"
  52. export _H2="Authorization: apikey $ARVAN_API_KEY"
  53. _response=$(_get $ARVAN_CDN_API/domains/$_zone)
  54. _debug2 "_get_root()" $_response
  55. _debug2 "_get_root()" $_zone
  56. if _contains "$_response" "$_zone"; then
  57. # is valid
  58. _valid=0
  59. else
  60. # is not valid
  61. _valid=1
  62. fi
  63. return $_valid
  64. }
  65. #Usage: _record_add zone challenge
  66. _record_add(){
  67. zone=$1
  68. challenge=$2
  69. export _H1="Content-Type: application-json"
  70. export _H2="Authorization: apikey $ARVAN_API_KEY"
  71. payload="{\"type\":\"txt\",\"name\":\"_acme-challenge\",\"cloud\":false,\"value\":{\"text\":\"$challenge\"},\"ttl\":120}"
  72. _response=$(_post "$payload" "$ARVAN_CDN_API/domains/$zone/dns-records" "" "POST" "application/json" | _base64)
  73. _debug2 "_record_add(): " $_response
  74. _debug2 " Payload: " $payload
  75. }
  76. #Usage: _record_get_id zone challenge
  77. _record_get_id(){
  78. zone=$1
  79. challenge=$2
  80. export _H1="Content-Type: application-json"
  81. export _H2="Authorization: apikey $ARVAN_API_KEY"
  82. _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/')
  83. _debug2 "_record_get_id(): " $_response
  84. echo "$_response"
  85. }
  86. #Usage: _record_remove zone record_id
  87. _record_remove(){
  88. zone=$1
  89. record_id=$2
  90. export _H1="Content-Type: application-json"
  91. export _H2="Authorization: apikey $ARVAN_API_KEY"
  92. _response=$(_post "" $ARVAN_CDN_API/domains/$zone/dns-records/$record_id "" "DELETE" "application/json")
  93. _debug "_record_remove(): ACME Challenge Removed"
  94. _debug2 " Response: " $_response
  95. }