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.3 KiB

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