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.

156 lines
4.1 KiB

2 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_arvan_info='ArvanCloud.ir
  4. Site: ArvanCloud.ir
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_arvan
  6. Options:
  7. Arvan_Token API Token
  8. Issues: github.com/acmesh-official/acme.sh/issues/2796
  9. Author: Vahid Fardi
  10. '
  11. ARVAN_API_URL="https://napi.arvancloud.ir/cdn/4.0/domains"
  12. ######## Public functions #####################
  13. #Usage: dns_arvan_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_arvan_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. _info "Using Arvan"
  18. Arvan_Token="${Arvan_Token:-$(_readaccountconf_mutable Arvan_Token)}"
  19. if [ -z "$Arvan_Token" ]; then
  20. _err "You didn't specify \"Arvan_Token\" token yet."
  21. _err "You can get yours from here https://npanel.arvancloud.ir/profile/api-keys"
  22. return 1
  23. fi
  24. #save the api token to the account conf file.
  25. _saveaccountconf_mutable Arvan_Token "$Arvan_Token"
  26. _debug "First detect the root zone"
  27. if ! _get_root "$fulldomain"; then
  28. _err "invalid domain"
  29. return 1
  30. fi
  31. _debug _domain_id "$_domain_id"
  32. _debug _sub_domain "$_sub_domain"
  33. _debug _domain "$_domain"
  34. _info "Adding record"
  35. if _arvan_rest POST "$_domain/dns-records" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"value\":{\"text\":\"$txtvalue\"},\"ttl\":120}"; then
  36. if _contains "$response" "$txtvalue"; then
  37. _info "response id is $response"
  38. _info "Added, OK"
  39. return 0
  40. elif _contains "$response" "Record Data is duplicate"; then
  41. _info "Already exists, OK"
  42. return 0
  43. else
  44. _err "Add txt record error."
  45. return 1
  46. fi
  47. fi
  48. _err "Add txt record error."
  49. return 0
  50. }
  51. #Usage: fulldomain txtvalue
  52. #Remove the txt record after validation.
  53. dns_arvan_rm() {
  54. fulldomain=$1
  55. txtvalue=$2
  56. _info "Using Arvan"
  57. _debug fulldomain "$fulldomain"
  58. _debug txtvalue "$txtvalue"
  59. Arvan_Token="${Arvan_Token:-$(_readaccountconf_mutable Arvan_Token)}"
  60. _debug "First detect the root zone"
  61. if ! _get_root "$fulldomain"; then
  62. _err "invalid domain"
  63. return 1
  64. fi
  65. _debug _domain_id "$_domain_id"
  66. _debug _sub_domain "$_sub_domain"
  67. _debug _domain "$_domain"
  68. _debug "Getting txt records"
  69. _arvan_rest GET "${_domain}/dns-records"
  70. if ! printf "%s" "$response" | grep \"current_page\":1 >/dev/null; then
  71. _err "Error on Arvan Api"
  72. _err "Please create a github issue with debbug log"
  73. return 1
  74. fi
  75. _record_id=$(echo "$response" | _egrep_o ".\"id\":\"[^\"]*\",\"type\":\"txt\",\"name\":\"_acme-challenge\",\"value\":{\"text\":\"$txtvalue\"}" | cut -d : -f 2 | cut -d , -f 1 | tr -d \")
  76. if ! _arvan_rest "DELETE" "${_domain}/dns-records/${_record_id}"; then
  77. _err "Error on Arvan Api"
  78. return 1
  79. fi
  80. _debug "$response"
  81. _contains "$response" 'dns record deleted'
  82. return 0
  83. }
  84. #################### Private functions below ##################################
  85. #_acme-challenge.www.domain.com
  86. #returns
  87. # _sub_domain=_acme-challenge.www
  88. # _domain=domain.com
  89. # _domain_id=sdjkglgdfewsdfg
  90. _get_root() {
  91. domain=$1
  92. i=2
  93. p=1
  94. while true; do
  95. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  96. _debug h "$h"
  97. if [ -z "$h" ]; then
  98. #not valid
  99. return 1
  100. fi
  101. if ! _arvan_rest GET "$h"; then
  102. return 1
  103. fi
  104. if _contains "$response" "\"domain\":\"$h\""; then
  105. _domain_id=$(echo "$response" | cut -d : -f 3 | cut -d , -f 1 | tr -d \")
  106. if [ "$_domain_id" ]; then
  107. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  108. _domain=$h
  109. return 0
  110. fi
  111. return 1
  112. fi
  113. p=$i
  114. i=$(_math "$i" + 1)
  115. done
  116. return 1
  117. }
  118. _arvan_rest() {
  119. mtd="$1"
  120. ep="$2"
  121. data="$3"
  122. token_trimmed=$(echo "$Arvan_Token" | tr -d '"')
  123. export _H1="Authorization: $token_trimmed"
  124. if [ "$mtd" = "DELETE" ]; then
  125. #DELETE Request shouldn't have Content-Type
  126. _debug data "$data"
  127. response="$(_post "$data" "$ARVAN_API_URL/$ep" "" "$mtd")"
  128. elif [ "$mtd" = "POST" ]; then
  129. export _H2="Content-Type: application/json"
  130. export _H3="Accept: application/json"
  131. _debug data "$data"
  132. response="$(_post "$data" "$ARVAN_API_URL/$ep" "" "$mtd")"
  133. else
  134. response="$(_get "$ARVAN_API_URL/$ep$data")"
  135. fi
  136. return 0
  137. }