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.

187 lines
4.5 KiB

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