#!/usr/bin/env sh Ali_API="https://alidns.aliyuncs.com/" #Ali_Key="LTqIA87hOKdjevsf5" #Ali_Secret="0p5EYueFNq501xnCPzKNbx6K51qPH2" #Usage: dns_ali_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" dns_ali_add() { fulldomain=$1 txtvalue=$2 if [ -z "$Ali_Key" ] || [ -z "$Ali_Secret" ]; then Ali_Key="" Ali_Secret="" _err "You don't specify aliyun api key and secret yet." return 1 fi #save the api key and secret to the account conf file. _saveaccountconf Ali_Key "$Ali_Key" _saveaccountconf Ali_Secret "$Ali_Secret" _debug "First detect the root zone" if ! _get_root "$fulldomain"; then return 1 fi _add_record_query "$_domain" "$_sub_domain" "$txtvalue" _ali_rest } dns_ali_rm() { fulldomain=$1 _clean } #################### Private functions bellow ################################## _get_root() { domain=$1 i=2 p=1 while true; do h=$(printf "%s" "$domain" | cut -d . -f $i-100) if [ -z "$h" ]; then #not valid return 1 fi _describe_records_query "$h" if ! _ali_rest "ignore"; then return 1 fi if _contains "$response" "PageNumber"; then _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) _debug _sub_domain "$_sub_domain" _domain="$h" _debug _domain "$_domain" return 0 fi p="$i" i=$(_math "$i" + 1) done return 1 } _ali_rest() { signature=$(printf "%s" "GET&%2F&$(_ali_urlencode "$query")" | _hmac "sha1" "$(_hex "$Ali_Secret&")" | _base64) signature=$(_ali_urlencode "$signature") url="$Ali_API?$query&Signature=$signature" if ! response="$(_get "$url")"; then _err "error!" return 1 fi if [ -z "$1" ]; then message="$(printf "%s" "$response" | _egrep_o "\"Message\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \")" if [ -n "$message" ]; then _err "$message" return 1 fi fi _debug2 response "$response" return 0 } _ali_urlencode() { printf "%s" "$1" \ | sed -e 's/\(.\)/\1\n/g' \ | while read -r char; do case $char in [a-zA-Z0-9.~_-]) printf "%s" "$char" ;; *) printf "%%%02X" "'$char" ;; esac done } _check_exist_query() { query='' query=$query'AccessKeyId='$Ali_Key query=$query'&Action=DescribeDomainRecords' query=$query'&DomainName='$1 query=$query'&Format=json' query=$query'&RRKeyWord=_acme-challenge' query=$query'&SignatureMethod=HMAC-SHA1' query=$query"&SignatureNonce=$(tr