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.

259 lines
7.0 KiB

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