Mirror of acme.sh — A pure Unix shell script implementing ACME client protocol
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.

dns_ali.sh 5.0KB

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