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.

184 lines
4.5 KiB

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