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.

185 lines
4.5 KiB

8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_alviy_info='Alviy.com
  4. Site: Alviy.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_alviy
  6. Options:
  7. Alviy_token API token. Get it from the https://cloud.alviy.com/token
  8. Issues: github.com/acmesh-official/acme.sh/issues/5115
  9. '
  10. Alviy_Api="https://cloud.alviy.com/api/v1"
  11. ######## Public functions #####################
  12. #Usage: dns_alviy_add _acme-challenge.www.domain.com "content"
  13. dns_alviy_add() {
  14. fulldomain=$1
  15. txtvalue=$2
  16. Alviy_token="${Alviy_token:-$(_readaccountconf_mutable Alviy_token)}"
  17. if [ -z "$Alviy_token" ]; then
  18. Alviy_token=""
  19. _err "Please specify Alviy token."
  20. return 1
  21. fi
  22. #save the api key and email to the account conf file.
  23. _saveaccountconf_mutable Alviy_token "$Alviy_token"
  24. _debug "First detect the root zone"
  25. if ! _get_root "$fulldomain"; then
  26. _err "invalid domain"
  27. return 1
  28. fi
  29. _debug _sub_domain "$_sub_domain"
  30. _debug _domain "$_domain"
  31. _debug "Getting existing records"
  32. if _alviy_txt_exists "$_domain" "$fulldomain" "$txtvalue"; then
  33. _info "This record already exists, skipping"
  34. return 0
  35. fi
  36. _add_data="{\"content\":\"$txtvalue\",\"type\":\"TXT\"}"
  37. _debug2 _add_data "$_add_data"
  38. _info "Adding record"
  39. if _alviy_rest POST "zone/$_domain/domain/$fulldomain/" "$_add_data"; then
  40. _debug "Checking updated records of '${fulldomain}'"
  41. if ! _alviy_txt_exists "$_domain" "$fulldomain" "$txtvalue"; then
  42. _err "TXT record '${txtvalue}' for '${fulldomain}', value wasn't set!"
  43. return 1
  44. fi
  45. else
  46. _err "Add txt record error, value '${txtvalue}' for '${fulldomain}' was not set."
  47. return 1
  48. fi
  49. _sleep 10
  50. _info "Added TXT record '${txtvalue}' for '${fulldomain}'."
  51. return 0
  52. }
  53. #fulldomain
  54. dns_alviy_rm() {
  55. fulldomain=$1
  56. txtvalue=$2
  57. Alviy_token="${Alviy_token:-$(_readaccountconf_mutable Alviy_token)}"
  58. _debug "First detect the root zone"
  59. if ! _get_root "$fulldomain"; then
  60. _err "invalid domain"
  61. return 1
  62. fi
  63. _debug _sub_domain "$_sub_domain"
  64. _debug _domain "$_domain"
  65. if ! _alviy_txt_exists "$_domain" "$fulldomain" "$txtvalue"; then
  66. _info "The record does not exist, skip"
  67. return 0
  68. fi
  69. _add_data=""
  70. uuid=$(echo "$response" | tr "{" "\n" | grep "$txtvalue" | tr "," "\n" | grep uuid | cut -d \" -f4)
  71. # delete record
  72. _debug "Delete TXT record for '${fulldomain}'"
  73. if ! _alviy_rest DELETE "zone/$_domain/record/$uuid" "{\"confirm\":1}"; then
  74. _err "Cannot delete empty TXT record for '$fulldomain'"
  75. return 1
  76. fi
  77. _info "The record '$fulldomain'='$txtvalue' deleted"
  78. }
  79. #################### Private functions below ##################################
  80. #_acme-challenge.www.domain.com
  81. #returns
  82. # _sub_domain=_acme-challenge.www
  83. # _domain=domain.com
  84. _get_root() {
  85. domain=$1
  86. i=3
  87. a="init"
  88. while [ -n "$a" ]; do
  89. a=$(printf "%s" "$domain" | cut -d . -f $i-)
  90. i=$((i + 1))
  91. done
  92. n=$((i - 3))
  93. h=$(printf "%s" "$domain" | cut -d . -f $n-)
  94. if [ -z "$h" ]; then
  95. #not valid
  96. _alviy_rest GET "zone/$domain/"
  97. _debug "can't get host from $domain"
  98. return 1
  99. fi
  100. if ! _alviy_rest GET "zone/$h/"; then
  101. return 1
  102. fi
  103. if _contains "$response" '"code":"NOT_FOUND"'; then
  104. _debug "$h not found"
  105. else
  106. s=$((n - 1))
  107. _sub_domain=$(printf "%s" "$domain" | cut -d . -f -$s)
  108. _domain="$h"
  109. return 0
  110. fi
  111. return 1
  112. }
  113. _alviy_txt_exists() {
  114. zone=$1
  115. domain=$2
  116. content_data=$3
  117. _debug "Getting existing records"
  118. if ! _alviy_rest GET "zone/$zone/domain/$domain/TXT/"; then
  119. _info "The record does not exist"
  120. return 1
  121. fi
  122. if ! _contains "$response" "$3"; then
  123. _info "The record has other value"
  124. return 1
  125. fi
  126. # GOOD code return - TRUE function
  127. return 0
  128. }
  129. _alviy_rest() {
  130. method=$1
  131. path="$2"
  132. content_data="$3"
  133. _debug "$path"
  134. export _H1="Authorization: Bearer $Alviy_token"
  135. export _H2="Content-Type: application/json"
  136. if [ "$content_data" ] || [ "$method" = "DELETE" ]; then
  137. _debug "data ($method): " "$content_data"
  138. response="$(_post "$content_data" "$Alviy_Api/$path" "" "$method")"
  139. else
  140. response="$(_get "$Alviy_Api/$path")"
  141. fi
  142. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  143. if [ "$_code" = "401" ]; then
  144. _err "It seems that your api key or secret is not correct."
  145. return 1
  146. fi
  147. if [ "$_code" != "200" ]; then
  148. _err "API call error ($method): $path Response code $_code"
  149. fi
  150. if [ "$?" != "0" ]; then
  151. _err "error on rest call ($method): $path. Response:"
  152. _err "$response"
  153. return 1
  154. fi
  155. _debug2 response "$response"
  156. return 0
  157. }