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.

220 lines
5.6 KiB

7 years ago
1 month ago
1 month ago
1 month ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_ali_info='AlibabaCloud.com
  4. Domains: Aliyun.com
  5. Site: AlibabaCloud.com
  6. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_ali
  7. Options:
  8. Ali_Key API Key
  9. Ali_Secret API Secret
  10. '
  11. # NOTICE:
  12. # This file is referenced by Alibaba Cloud Services deploy hooks
  13. # https://github.com/acmesh-official/acme.sh/pull/5205#issuecomment-2357867276
  14. # Be careful when modifying this file, especially when making breaking changes for common functions
  15. Ali_DNS_API="https://alidns.aliyuncs.com/"
  16. #Usage: dns_ali_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  17. dns_ali_add() {
  18. fulldomain=$1
  19. txtvalue=$2
  20. _prepare_ali_credentials || return 1
  21. _debug "First detect the root zone"
  22. if ! _get_root "$fulldomain"; then
  23. return 1
  24. fi
  25. _debug "Add record"
  26. _add_record_query "$_domain" "$_sub_domain" "$txtvalue" && _ali_rest "Add record"
  27. }
  28. dns_ali_rm() {
  29. fulldomain=$1
  30. txtvalue=$2
  31. Ali_Key="${Ali_Key:-$(_readaccountconf_mutable Ali_Key)}"
  32. Ali_Secret="${Ali_Secret:-$(_readaccountconf_mutable Ali_Secret)}"
  33. _debug "First detect the root zone"
  34. if ! _get_root "$fulldomain"; then
  35. return 1
  36. fi
  37. _clean
  38. }
  39. #################### Alibaba Cloud common functions below ####################
  40. _prepare_ali_credentials() {
  41. Ali_Key="${Ali_Key:-$(_readaccountconf_mutable Ali_Key)}"
  42. Ali_Secret="${Ali_Secret:-$(_readaccountconf_mutable Ali_Secret)}"
  43. if [ -z "$Ali_Key" ] || [ -z "$Ali_Secret" ]; then
  44. Ali_Key=""
  45. Ali_Secret=""
  46. _err "You don't specify aliyun api key and secret yet."
  47. return 1
  48. fi
  49. #save the api key and secret to the account conf file.
  50. _saveaccountconf_mutable Ali_Key "$Ali_Key"
  51. _saveaccountconf_mutable Ali_Secret "$Ali_Secret"
  52. }
  53. # act ign mtd
  54. _ali_rest() {
  55. act="$1"
  56. ign="$2"
  57. mtd="${3:-GET}"
  58. signature=$(printf "%s" "$mtd&%2F&$(printf "%s" "$query" | _url_encode upper-hex)" | _hmac "sha1" "$(printf "%s" "$Ali_Secret&" | _hex_dump | tr -d " ")" | _base64)
  59. signature=$(printf "%s" "$signature" | _url_encode upper-hex)
  60. url="$endpoint?Signature=$signature"
  61. if [ "$mtd" = "GET" ]; then
  62. url="$url&$query"
  63. response="$(_get "$url")"
  64. else
  65. response="$(_post "$query" "$url" "" "$mtd" "application/x-www-form-urlencoded")"
  66. fi
  67. _ret="$?"
  68. _debug2 response "$response"
  69. if [ "$_ret" != "0" ]; then
  70. _err "Error <$act>"
  71. return 1
  72. fi
  73. if [ -z "$ign" ]; then
  74. message="$(echo "$response" | _egrep_o "\"Message\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \")"
  75. if [ "$message" ]; then
  76. _err "$message"
  77. return 1
  78. fi
  79. fi
  80. }
  81. _ali_nonce() {
  82. #_head_n 1 </dev/urandom | _digest "sha256" hex | cut -c 1-31
  83. #Not so good...
  84. date +"%s%N" | sed 's/%N//g'
  85. }
  86. _timestamp() {
  87. date -u +"%Y-%m-%dT%H%%3A%M%%3A%SZ"
  88. }
  89. #################### Private functions below ####################
  90. _get_root() {
  91. domain=$1
  92. i=1
  93. p=1
  94. while true; do
  95. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  96. if [ -z "$h" ]; then
  97. #not valid
  98. return 1
  99. fi
  100. _describe_records_query "$h"
  101. if ! _ali_rest "Get root" "ignore"; then
  102. return 1
  103. fi
  104. if _contains "$response" "PageNumber"; then
  105. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  106. _debug _sub_domain "$_sub_domain"
  107. _domain="$h"
  108. _debug _domain "$_domain"
  109. return 0
  110. fi
  111. p="$i"
  112. i=$(_math "$i" + 1)
  113. done
  114. return 1
  115. }
  116. _check_exist_query() {
  117. _qdomain="$1"
  118. _qsubdomain="$2"
  119. endpoint=$Ali_DNS_API
  120. query=''
  121. query=$query'AccessKeyId='$Ali_Key
  122. query=$query'&Action=DescribeDomainRecords'
  123. query=$query'&DomainName='$_qdomain
  124. query=$query'&Format=json'
  125. query=$query'&RRKeyWord='$_qsubdomain
  126. query=$query'&SignatureMethod=HMAC-SHA1'
  127. query=$query"&SignatureNonce=$(_ali_nonce)"
  128. query=$query'&SignatureVersion=1.0'
  129. query=$query'&Timestamp='$(_timestamp)
  130. query=$query'&TypeKeyWord=TXT'
  131. query=$query'&Version=2015-01-09'
  132. }
  133. _add_record_query() {
  134. endpoint=$Ali_DNS_API
  135. query=''
  136. query=$query'AccessKeyId='$Ali_Key
  137. query=$query'&Action=AddDomainRecord'
  138. query=$query'&DomainName='$1
  139. query=$query'&Format=json'
  140. query=$query'&RR='$2
  141. query=$query'&SignatureMethod=HMAC-SHA1'
  142. query=$query"&SignatureNonce=$(_ali_nonce)"
  143. query=$query'&SignatureVersion=1.0'
  144. query=$query'&Timestamp='$(_timestamp)
  145. query=$query'&Type=TXT'
  146. query=$query'&Value='$3
  147. query=$query'&Version=2015-01-09'
  148. }
  149. _delete_record_query() {
  150. endpoint=$Ali_DNS_API
  151. query=''
  152. query=$query'AccessKeyId='$Ali_Key
  153. query=$query'&Action=DeleteDomainRecord'
  154. query=$query'&Format=json'
  155. query=$query'&RecordId='$1
  156. query=$query'&SignatureMethod=HMAC-SHA1'
  157. query=$query"&SignatureNonce=$(_ali_nonce)"
  158. query=$query'&SignatureVersion=1.0'
  159. query=$query'&Timestamp='$(_timestamp)
  160. query=$query'&Version=2015-01-09'
  161. }
  162. _describe_records_query() {
  163. endpoint=$Ali_DNS_API
  164. query=''
  165. query=$query'AccessKeyId='$Ali_Key
  166. query=$query'&Action=DescribeDomainRecords'
  167. query=$query'&DomainName='$1
  168. query=$query'&Format=json'
  169. query=$query'&SignatureMethod=HMAC-SHA1'
  170. query=$query"&SignatureNonce=$(_ali_nonce)"
  171. query=$query'&SignatureVersion=1.0'
  172. query=$query'&Timestamp='$(_timestamp)
  173. query=$query'&Version=2015-01-09'
  174. }
  175. _clean() {
  176. _check_exist_query "$_domain" "$_sub_domain"
  177. # do not correct grammar here
  178. if ! _ali_rest "Check exist records" "ignore"; then
  179. return 1
  180. fi
  181. record_id="$(echo "$response" | tr '{' "\n" | grep "$_sub_domain" | grep -- "$txtvalue" | tr "," "\n" | grep RecordId | cut -d '"' -f 4)"
  182. _debug2 record_id "$record_id"
  183. if [ -z "$record_id" ]; then
  184. _debug "record not found, skip"
  185. else
  186. _delete_record_query "$record_id"
  187. _ali_rest "Delete record $record_id" "ignore"
  188. fi
  189. }