2016-11-24 14:49:45 +01:00
|
|
|
#!/usr/bin/env sh
|
2023-11-18 17:57:12 +01:00
|
|
|
# shellcheck disable=SC2034
|
|
|
|
dns_ali_info='AlibabaCloud.com
|
|
|
|
Domains: Aliyun.com
|
|
|
|
Site: AlibabaCloud.com
|
|
|
|
Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_ali
|
|
|
|
Options:
|
|
|
|
Ali_Key API Key
|
|
|
|
Ali_Secret API Secret
|
|
|
|
'
|
2016-11-24 14:49:45 +01:00
|
|
|
|
2024-09-19 09:49:42 +02:00
|
|
|
# NOTICE:
|
|
|
|
# This file is referenced by Alibaba Cloud Services deploy hooks
|
|
|
|
# https://github.com/acmesh-official/acme.sh/pull/5205#issuecomment-2357867276
|
|
|
|
# Be careful when modifying this file, especially when making breaking changes for common functions
|
|
|
|
|
2024-09-19 10:30:04 +02:00
|
|
|
Ali_DNS_API="https://alidns.aliyuncs.com/"
|
2016-11-24 14:49:45 +01:00
|
|
|
|
|
|
|
#Usage: dns_ali_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
|
|
|
|
dns_ali_add() {
|
|
|
|
fulldomain=$1
|
|
|
|
txtvalue=$2
|
|
|
|
|
2024-09-20 08:55:33 +02:00
|
|
|
_prepare_ali_credentials || return 1
|
2016-11-24 14:49:45 +01:00
|
|
|
|
|
|
|
_debug "First detect the root zone"
|
|
|
|
if ! _get_root "$fulldomain"; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2016-11-24 15:27:14 +01:00
|
|
|
_debug "Add record"
|
2016-11-24 14:49:45 +01:00
|
|
|
_add_record_query "$_domain" "$_sub_domain" "$txtvalue" && _ali_rest "Add record"
|
|
|
|
}
|
|
|
|
|
|
|
|
dns_ali_rm() {
|
|
|
|
fulldomain=$1
|
2018-02-15 05:26:35 +01:00
|
|
|
txtvalue=$2
|
|
|
|
Ali_Key="${Ali_Key:-$(_readaccountconf_mutable Ali_Key)}"
|
|
|
|
Ali_Secret="${Ali_Secret:-$(_readaccountconf_mutable Ali_Secret)}"
|
|
|
|
|
|
|
|
_debug "First detect the root zone"
|
|
|
|
if ! _get_root "$fulldomain"; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2016-11-24 14:49:45 +01:00
|
|
|
_clean
|
|
|
|
}
|
|
|
|
|
2024-09-19 09:49:42 +02:00
|
|
|
#################### Alibaba Cloud common functions below ####################
|
|
|
|
|
|
|
|
_prepare_ali_credentials() {
|
|
|
|
Ali_Key="${Ali_Key:-$(_readaccountconf_mutable Ali_Key)}"
|
|
|
|
Ali_Secret="${Ali_Secret:-$(_readaccountconf_mutable Ali_Secret)}"
|
|
|
|
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_mutable Ali_Key "$Ali_Key"
|
|
|
|
_saveaccountconf_mutable Ali_Secret "$Ali_Secret"
|
|
|
|
}
|
|
|
|
|
|
|
|
# act ign mtd
|
|
|
|
_ali_rest() {
|
|
|
|
act="$1"
|
|
|
|
ign="$2"
|
|
|
|
mtd="${3:-GET}"
|
|
|
|
|
|
|
|
signature=$(printf "%s" "$mtd&%2F&$(printf "%s" "$query" | _url_encode upper-hex)" | _hmac "sha1" "$(printf "%s" "$Ali_Secret&" | _hex_dump | tr -d " ")" | _base64)
|
|
|
|
signature=$(printf "%s" "$signature" | _url_encode upper-hex)
|
2024-09-19 10:30:04 +02:00
|
|
|
url="$endpoint?Signature=$signature"
|
2024-09-19 09:49:42 +02:00
|
|
|
|
|
|
|
if [ "$mtd" = "GET" ]; then
|
|
|
|
url="$url&$query"
|
|
|
|
response="$(_get "$url")"
|
|
|
|
else
|
|
|
|
response="$(_post "$query" "$url" "" "$mtd" "application/x-www-form-urlencoded")"
|
|
|
|
fi
|
|
|
|
|
|
|
|
_ret="$?"
|
|
|
|
_debug2 response "$response"
|
|
|
|
if [ "$_ret" != "0" ]; then
|
|
|
|
_err "Error <$act>"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$ign" ]; then
|
|
|
|
message="$(echo "$response" | _egrep_o "\"Message\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \")"
|
|
|
|
if [ "$message" ]; then
|
|
|
|
_err "$message"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
_ali_nonce() {
|
|
|
|
#_head_n 1 </dev/urandom | _digest "sha256" hex | cut -c 1-31
|
|
|
|
#Not so good...
|
|
|
|
date +"%s%N" | sed 's/%N//g'
|
|
|
|
}
|
|
|
|
|
|
|
|
_timestamp() {
|
|
|
|
date -u +"%Y-%m-%dT%H%%3A%M%%3A%SZ"
|
|
|
|
}
|
|
|
|
|
|
|
|
#################### Private functions below ####################
|
2016-11-24 14:49:45 +01:00
|
|
|
|
|
|
|
_get_root() {
|
|
|
|
domain=$1
|
2024-10-13 17:41:22 +02:00
|
|
|
i=1
|
2016-11-24 14:49:45 +01:00
|
|
|
p=1
|
|
|
|
while true; do
|
2024-10-13 17:41:22 +02:00
|
|
|
h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
|
2016-11-24 14:49:45 +01:00
|
|
|
if [ -z "$h" ]; then
|
|
|
|
#not valid
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
_describe_records_query "$h"
|
|
|
|
if ! _ali_rest "Get root" "ignore"; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if _contains "$response" "PageNumber"; then
|
2024-10-13 17:41:22 +02:00
|
|
|
_sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
|
2016-11-24 14:49:45 +01:00
|
|
|
_debug _sub_domain "$_sub_domain"
|
|
|
|
_domain="$h"
|
|
|
|
_debug _domain "$_domain"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
p="$i"
|
|
|
|
i=$(_math "$i" + 1)
|
|
|
|
done
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
_check_exist_query() {
|
2018-02-15 05:26:35 +01:00
|
|
|
_qdomain="$1"
|
|
|
|
_qsubdomain="$2"
|
2024-09-19 10:30:04 +02:00
|
|
|
endpoint=$Ali_DNS_API
|
2016-11-24 14:49:45 +01:00
|
|
|
query=''
|
|
|
|
query=$query'AccessKeyId='$Ali_Key
|
|
|
|
query=$query'&Action=DescribeDomainRecords'
|
2018-02-15 05:26:35 +01:00
|
|
|
query=$query'&DomainName='$_qdomain
|
2016-11-24 14:49:45 +01:00
|
|
|
query=$query'&Format=json'
|
2018-02-15 05:26:35 +01:00
|
|
|
query=$query'&RRKeyWord='$_qsubdomain
|
2016-11-24 14:49:45 +01:00
|
|
|
query=$query'&SignatureMethod=HMAC-SHA1'
|
|
|
|
query=$query"&SignatureNonce=$(_ali_nonce)"
|
|
|
|
query=$query'&SignatureVersion=1.0'
|
|
|
|
query=$query'&Timestamp='$(_timestamp)
|
|
|
|
query=$query'&TypeKeyWord=TXT'
|
|
|
|
query=$query'&Version=2015-01-09'
|
|
|
|
}
|
|
|
|
|
|
|
|
_add_record_query() {
|
2024-09-19 10:30:04 +02:00
|
|
|
endpoint=$Ali_DNS_API
|
2016-11-24 14:49:45 +01:00
|
|
|
query=''
|
|
|
|
query=$query'AccessKeyId='$Ali_Key
|
|
|
|
query=$query'&Action=AddDomainRecord'
|
|
|
|
query=$query'&DomainName='$1
|
|
|
|
query=$query'&Format=json'
|
|
|
|
query=$query'&RR='$2
|
|
|
|
query=$query'&SignatureMethod=HMAC-SHA1'
|
|
|
|
query=$query"&SignatureNonce=$(_ali_nonce)"
|
|
|
|
query=$query'&SignatureVersion=1.0'
|
|
|
|
query=$query'&Timestamp='$(_timestamp)
|
|
|
|
query=$query'&Type=TXT'
|
|
|
|
query=$query'&Value='$3
|
|
|
|
query=$query'&Version=2015-01-09'
|
|
|
|
}
|
|
|
|
|
|
|
|
_delete_record_query() {
|
2024-09-19 10:30:04 +02:00
|
|
|
endpoint=$Ali_DNS_API
|
2016-11-24 14:49:45 +01:00
|
|
|
query=''
|
|
|
|
query=$query'AccessKeyId='$Ali_Key
|
|
|
|
query=$query'&Action=DeleteDomainRecord'
|
|
|
|
query=$query'&Format=json'
|
|
|
|
query=$query'&RecordId='$1
|
|
|
|
query=$query'&SignatureMethod=HMAC-SHA1'
|
|
|
|
query=$query"&SignatureNonce=$(_ali_nonce)"
|
|
|
|
query=$query'&SignatureVersion=1.0'
|
|
|
|
query=$query'&Timestamp='$(_timestamp)
|
|
|
|
query=$query'&Version=2015-01-09'
|
|
|
|
}
|
|
|
|
|
|
|
|
_describe_records_query() {
|
2024-09-19 10:30:04 +02:00
|
|
|
endpoint=$Ali_DNS_API
|
2016-11-24 14:49:45 +01:00
|
|
|
query=''
|
|
|
|
query=$query'AccessKeyId='$Ali_Key
|
|
|
|
query=$query'&Action=DescribeDomainRecords'
|
|
|
|
query=$query'&DomainName='$1
|
|
|
|
query=$query'&Format=json'
|
|
|
|
query=$query'&SignatureMethod=HMAC-SHA1'
|
|
|
|
query=$query"&SignatureNonce=$(_ali_nonce)"
|
|
|
|
query=$query'&SignatureVersion=1.0'
|
|
|
|
query=$query'&Timestamp='$(_timestamp)
|
|
|
|
query=$query'&Version=2015-01-09'
|
|
|
|
}
|
|
|
|
|
|
|
|
_clean() {
|
2018-02-15 05:26:35 +01:00
|
|
|
_check_exist_query "$_domain" "$_sub_domain"
|
2020-06-29 20:29:10 +02:00
|
|
|
# do not correct grammar here
|
2016-11-24 14:49:45 +01:00
|
|
|
if ! _ali_rest "Check exist records" "ignore"; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2019-09-14 17:06:25 +02:00
|
|
|
record_id="$(echo "$response" | tr '{' "\n" | grep "$_sub_domain" | grep -- "$txtvalue" | tr "," "\n" | grep RecordId | cut -d '"' -f 4)"
|
2018-02-15 05:26:35 +01:00
|
|
|
_debug2 record_id "$record_id"
|
|
|
|
|
|
|
|
if [ -z "$record_id" ]; then
|
|
|
|
_debug "record not found, skip"
|
|
|
|
else
|
|
|
|
_delete_record_query "$record_id"
|
|
|
|
_ali_rest "Delete record $record_id" "ignore"
|
|
|
|
fi
|
|
|
|
|
2016-11-24 14:49:45 +01:00
|
|
|
}
|