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.

150 lines
3.9 KiB

4 years ago
  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 "response id is $response"
  33. _info "Added, OK"
  34. return 0
  35. elif _contains "$response" "Record Data is Duplicated"; then
  36. _info "Already exists, OK"
  37. return 0
  38. else
  39. _err "Add txt record error."
  40. return 1
  41. fi
  42. fi
  43. _err "Add txt record error."
  44. return 0
  45. }
  46. #Usage: fulldomain txtvalue
  47. #Remove the txt record after validation.
  48. dns_arvan_rm() {
  49. fulldomain=$1
  50. txtvalue=$2
  51. _info "Using Arvan"
  52. _debug fulldomain "$fulldomain"
  53. _debug txtvalue "$txtvalue"
  54. Arvan_Token="${Arvan_Token:-$(_readaccountconf_mutable Arvan_Token)}"
  55. _debug "First detect the root zone"
  56. if ! _get_root "$fulldomain"; then
  57. _err "invalid domain"
  58. return 1
  59. fi
  60. _debug _domain_id "$_domain_id"
  61. _debug _sub_domain "$_sub_domain"
  62. _debug _domain "$_domain"
  63. _debug "Getting txt records"
  64. _arvan_rest GET "${_domain}/dns-records"
  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. _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 \")
  71. if ! _arvan_rest "DELETE" "${_domain}/dns-records/${_record_id}"; then
  72. _err "Error on Arvan Api"
  73. return 1
  74. fi
  75. _debug "$response"
  76. _contains "$response" 'dns record deleted'
  77. return 0
  78. }
  79. #################### Private functions below ##################################
  80. #_acme-challenge.www.domain.com
  81. #returns
  82. # _sub_domain=_acme-challenge.www
  83. # _domain=domain.com
  84. # _domain_id=sdjkglgdfewsdfg
  85. _get_root() {
  86. domain=$1
  87. i=2
  88. p=1
  89. while true; do
  90. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  91. _debug h "$h"
  92. if [ -z "$h" ]; then
  93. #not valid
  94. return 1
  95. fi
  96. if ! _arvan_rest GET "$h"; then
  97. return 1
  98. fi
  99. if _contains "$response" "\"domain\":\"$h\""; then
  100. _domain_id=$(echo "$response" | cut -d : -f 3 | cut -d , -f 1 | tr -d \")
  101. if [ "$_domain_id" ]; then
  102. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  103. _domain=$h
  104. return 0
  105. fi
  106. return 1
  107. fi
  108. p=$i
  109. i=$(_math "$i" + 1)
  110. done
  111. return 1
  112. }
  113. _arvan_rest() {
  114. mtd="$1"
  115. ep="$2"
  116. data="$3"
  117. token_trimmed=$(echo "$Arvan_Token" | tr -d '"')
  118. export _H1="Authorization: $token_trimmed"
  119. if [ "$mtd" = "DELETE" ]; then
  120. #DELETE Request shouldn't have Content-Type
  121. _debug data "$data"
  122. response="$(_post "$data" "$ARVAN_API_URL/$ep" "" "$mtd")"
  123. elif [ "$mtd" = "POST" ]; then
  124. export _H2="Content-Type: application/json"
  125. _debug data "$data"
  126. response="$(_post "$data" "$ARVAN_API_URL/$ep" "" "$mtd")"
  127. else
  128. response="$(_get "$ARVAN_API_URL/$ep$data")"
  129. fi
  130. return 0
  131. }