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.

209 lines
5.2 KiB

7 years ago
7 years ago
7 years ago
8 years ago
8 years ago
7 years ago
7 years 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. Ali_API="https://alidns.aliyuncs.com/"
  12. #Usage: dns_ali_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  13. dns_ali_add() {
  14. fulldomain=$1
  15. txtvalue=$2
  16. Ali_Key="${Ali_Key:-$(_readaccountconf_mutable Ali_Key)}"
  17. Ali_Secret="${Ali_Secret:-$(_readaccountconf_mutable Ali_Secret)}"
  18. if [ -z "$Ali_Key" ] || [ -z "$Ali_Secret" ]; then
  19. Ali_Key=""
  20. Ali_Secret=""
  21. _err "You don't specify aliyun api key and secret yet."
  22. return 1
  23. fi
  24. #save the api key and secret to the account conf file.
  25. _saveaccountconf_mutable Ali_Key "$Ali_Key"
  26. _saveaccountconf_mutable Ali_Secret "$Ali_Secret"
  27. _debug "First detect the root zone"
  28. if ! _get_root "$fulldomain"; then
  29. return 1
  30. fi
  31. _debug "Add record"
  32. _add_record_query "$_domain" "$_sub_domain" "$txtvalue" && _ali_rest "Add record"
  33. }
  34. dns_ali_rm() {
  35. fulldomain=$1
  36. txtvalue=$2
  37. Ali_Key="${Ali_Key:-$(_readaccountconf_mutable Ali_Key)}"
  38. Ali_Secret="${Ali_Secret:-$(_readaccountconf_mutable Ali_Secret)}"
  39. _debug "First detect the root zone"
  40. if ! _get_root "$fulldomain"; then
  41. return 1
  42. fi
  43. _clean
  44. }
  45. #################### Private functions below ##################################
  46. _get_root() {
  47. domain=$1
  48. i=2
  49. p=1
  50. while true; do
  51. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  52. if [ -z "$h" ]; then
  53. #not valid
  54. return 1
  55. fi
  56. _describe_records_query "$h"
  57. if ! _ali_rest "Get root" "ignore"; then
  58. return 1
  59. fi
  60. if _contains "$response" "PageNumber"; then
  61. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  62. _debug _sub_domain "$_sub_domain"
  63. _domain="$h"
  64. _debug _domain "$_domain"
  65. return 0
  66. fi
  67. p="$i"
  68. i=$(_math "$i" + 1)
  69. done
  70. return 1
  71. }
  72. _ali_rest() {
  73. signature=$(printf "%s" "GET&%2F&$(_ali_urlencode "$query")" | _hmac "sha1" "$(printf "%s" "$Ali_Secret&" | _hex_dump | tr -d " ")" | _base64)
  74. signature=$(_ali_urlencode "$signature")
  75. url="$Ali_API?$query&Signature=$signature"
  76. if ! response="$(_get "$url")"; then
  77. _err "Error <$1>"
  78. return 1
  79. fi
  80. _debug2 response "$response"
  81. if [ -z "$2" ]; then
  82. message="$(echo "$response" | _egrep_o "\"Message\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \")"
  83. if [ "$message" ]; then
  84. _err "$message"
  85. return 1
  86. fi
  87. fi
  88. }
  89. _ali_urlencode() {
  90. _str="$1"
  91. _str_len=${#_str}
  92. _u_i=1
  93. while [ "$_u_i" -le "$_str_len" ]; do
  94. _str_c="$(printf "%s" "$_str" | cut -c "$_u_i")"
  95. case $_str_c in [a-zA-Z0-9.~_-])
  96. printf "%s" "$_str_c"
  97. ;;
  98. *)
  99. printf "%%%02X" "'$_str_c"
  100. ;;
  101. esac
  102. _u_i="$(_math "$_u_i" + 1)"
  103. done
  104. }
  105. _ali_nonce() {
  106. #_head_n 1 </dev/urandom | _digest "sha256" hex | cut -c 1-31
  107. #Not so good...
  108. date +"%s%N" | sed 's/%N//g'
  109. }
  110. _check_exist_query() {
  111. _qdomain="$1"
  112. _qsubdomain="$2"
  113. query=''
  114. query=$query'AccessKeyId='$Ali_Key
  115. query=$query'&Action=DescribeDomainRecords'
  116. query=$query'&DomainName='$_qdomain
  117. query=$query'&Format=json'
  118. query=$query'&RRKeyWord='$_qsubdomain
  119. query=$query'&SignatureMethod=HMAC-SHA1'
  120. query=$query"&SignatureNonce=$(_ali_nonce)"
  121. query=$query'&SignatureVersion=1.0'
  122. query=$query'&Timestamp='$(_timestamp)
  123. query=$query'&TypeKeyWord=TXT'
  124. query=$query'&Version=2015-01-09'
  125. }
  126. _add_record_query() {
  127. query=''
  128. query=$query'AccessKeyId='$Ali_Key
  129. query=$query'&Action=AddDomainRecord'
  130. query=$query'&DomainName='$1
  131. query=$query'&Format=json'
  132. query=$query'&RR='$2
  133. query=$query'&SignatureMethod=HMAC-SHA1'
  134. query=$query"&SignatureNonce=$(_ali_nonce)"
  135. query=$query'&SignatureVersion=1.0'
  136. query=$query'&Timestamp='$(_timestamp)
  137. query=$query'&Type=TXT'
  138. query=$query'&Value='$3
  139. query=$query'&Version=2015-01-09'
  140. }
  141. _delete_record_query() {
  142. query=''
  143. query=$query'AccessKeyId='$Ali_Key
  144. query=$query'&Action=DeleteDomainRecord'
  145. query=$query'&Format=json'
  146. query=$query'&RecordId='$1
  147. query=$query'&SignatureMethod=HMAC-SHA1'
  148. query=$query"&SignatureNonce=$(_ali_nonce)"
  149. query=$query'&SignatureVersion=1.0'
  150. query=$query'&Timestamp='$(_timestamp)
  151. query=$query'&Version=2015-01-09'
  152. }
  153. _describe_records_query() {
  154. query=''
  155. query=$query'AccessKeyId='$Ali_Key
  156. query=$query'&Action=DescribeDomainRecords'
  157. query=$query'&DomainName='$1
  158. query=$query'&Format=json'
  159. query=$query'&SignatureMethod=HMAC-SHA1'
  160. query=$query"&SignatureNonce=$(_ali_nonce)"
  161. query=$query'&SignatureVersion=1.0'
  162. query=$query'&Timestamp='$(_timestamp)
  163. query=$query'&Version=2015-01-09'
  164. }
  165. _clean() {
  166. _check_exist_query "$_domain" "$_sub_domain"
  167. # do not correct grammar here
  168. if ! _ali_rest "Check exist records" "ignore"; then
  169. return 1
  170. fi
  171. record_id="$(echo "$response" | tr '{' "\n" | grep "$_sub_domain" | grep -- "$txtvalue" | tr "," "\n" | grep RecordId | cut -d '"' -f 4)"
  172. _debug2 record_id "$record_id"
  173. if [ -z "$record_id" ]; then
  174. _debug "record not found, skip"
  175. else
  176. _delete_record_query "$record_id"
  177. _ali_rest "Delete record $record_id" "ignore"
  178. fi
  179. }
  180. _timestamp() {
  181. date -u +"%Y-%m-%dT%H%%3A%M%%3A%SZ"
  182. }