2 Tencent_API
="https://dnspod.tencentcloudapi.com"
4 #Tencent_SecretId="AKIDz81d2cd22cdcdc2dcd1cc1d1A"
5 #Tencent_SecretKey="Gu5t9abcabcaabcbabcbbbcbcbbccbbcb"
7 #Usage: dns_tencent_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
12 Tencent_SecretId
="${Tencent_SecretId:-$(_readaccountconf_mutable Tencent_SecretId)}"
13 Tencent_SecretKey
="${Tencent_SecretKey:-$(_readaccountconf_mutable Tencent_SecretKey)}"
14 if [ -z "$Tencent_SecretId" ] ||
[ -z "$Tencent_SecretKey" ]; then
17 _err
"You don't specify tencent api SecretId and SecretKey yet."
21 #save the api SecretId and SecretKey to the account conf file.
22 _saveaccountconf_mutable Tencent_SecretId
"$Tencent_SecretId"
23 _saveaccountconf_mutable Tencent_SecretKey
"$Tencent_SecretKey"
25 _debug
"First detect the root zone"
26 if ! _get_root
"$fulldomain"; then
31 _add_record_query
"$_domain" "$_sub_domain" "$txtvalue" && _tencent_rest
"CreateRecord"
37 Tencent_SecretId
="${Tencent_SecretId:-$(_readaccountconf_mutable Tencent_SecretId)}"
38 Tencent_SecretKey
="${Tencent_SecretKey:-$(_readaccountconf_mutable Tencent_SecretKey)}"
40 _debug
"First detect the root zone"
41 if ! _get_root
"$fulldomain"; then
45 _debug
"Get record list"
48 while [ -z "$record_id" ] && [ "$attempt" -le $max_attempts ]; do
49 _check_exist_query
"$_domain" "$_sub_domain" "$txtvalue" && _tencent_rest
"DescribeRecordFilterList"
50 record_id
="$(echo "$response" | _egrep_o "\"RecordId
\":\s
*[0-9]+" | _egrep_o "[0-9]+")"
51 _debug2 record_id
"$record_id"
52 if [ -z "$record_id" ]; then
53 _debug
"Due to TencentCloud API synchronization delay, record not found, waiting 10 seconds and retrying"
55 attempt
=$
(_math
"$attempt + 1")
59 record_id
="$(echo "$response" | _egrep_o "\"RecordId
\":\s
*[0-9]+" | _egrep_o "[0-9]+")"
60 _debug2 record_id
"$record_id"
62 if [ -z "$record_id" ]; then
63 _debug
"record not found after $max_attempts attempts, skip"
65 _debug
"Delete record"
66 _delete_record_query
"$record_id" && _tencent_rest
"DeleteRecord"
70 #################### Private functions below ##################################
77 h
=$
(printf "%s" "$domain" | cut
-d .
-f "$i"-100)
83 _describe_records_query
"$h" "@"
84 if ! _tencent_rest
"DescribeRecordList" "ignore"; then
88 if _contains
"$response" "\"TotalCount\":"; then
89 _sub_domain
=$
(printf "%s" "$domain" | cut
-d .
-f 1-"$p")
90 _debug _sub_domain
"$_sub_domain"
92 _debug _domain
"$_domain"
105 timestamp
=$
(date -u +%s
)
107 token
=$
(tencent_signature_v3
$service "$action" "$payload" "$timestamp")
110 if ! response
="$(tencent_api_request $service $version "$action" "$payload" "$timestamp")"; then
115 _debug2 response
"$response"
117 message
="$(echo "$response" | _egrep_o "\"Message
\":\"[^
\"]*\"" | cut -d : -f 2 | tr -d \")"
118 if [ "$message" ]; then
125 _add_record_query
() {
126 query
="{\"Domain\":\"$1\",\"SubDomain\":\"$2\",\"RecordType\":\"TXT\",\"RecordLineId\":\"0\",\"RecordLine\":\"0\",\"Value\":\"$3\",\"TTL\":600}"
129 _describe_records_query
() {
130 query
="{\"Domain\":\"$1\",\"Limit\":3000}"
133 _delete_record_query
() {
134 query
="{\"Domain\":\"$_domain\",\"RecordId\":$1}"
137 _check_exist_query
() {
141 query
="{\"Domain\":\"$_domain\",\"SubDomain\":\"$_subdomain\",\"RecordValue\":\"$_value\"}"
144 # shell client for tencent cloud api v3 | @author: rehiy
147 printf %b
"$@" | _digest sha256 hex
150 tencent_hmac_sha256
() {
153 hex_key
=$
(printf %b
"$k" | _hex_dump |
tr -d ' ')
154 printf %b
"$@" | _hmac sha256
"$hex_key" hex
157 tencent_hmac_sha256_hexkey
() {
160 printf %b
"$@" | _hmac sha256
"$k" hex
163 tencent_signature_v3
() {
165 action
=$
(echo "$2" | _lower_case
)
167 timestamp=${4:-$(date +%s)}
169 domain="$service.tencentcloudapi.com"
170 secretId=${Tencent_SecretId:-'tencent-cloud-secret-id'}
171 secretKey=${Tencent_SecretKey:-'tencent-cloud-secret-key'}
173 algorithm='TC3-HMAC-SHA256
'
174 date=$(date -u -d "@$timestamp" +%Y-%m-%d 2>/dev/null)
175 [ -z "$date" ] && date=$(date -u -r "$timestamp" +%Y-%m-%d)
179 canonicalHeaders="content-type:application/json\nhost:$domain\nx-tc-action:$action\n"
181 signedHeaders='content-type
;host;x-tc-action
'
182 canonicalRequest="POST\n$canonicalUri\n$canonicalQuery\n$canonicalHeaders\n$signedHeaders\n$(tencent_sha256 "$payload")"
184 credentialScope="$date/$service/tc3_request"
185 stringToSign="$algorithm\n$timestamp\n$credentialScope\n$(tencent_sha256 "$canonicalRequest")"
186 _debug "stringToSign: $stringToSign"
188 secretDate=$(tencent_hmac_sha256 "TC3$secretKey" "$date")
189 secretService=$(tencent_hmac_sha256_hexkey "$secretDate" "$service")
190 secretSigning=$(tencent_hmac_sha256_hexkey "$secretService" 'tc3_request
')
191 signature=$(tencent_hmac_sha256_hexkey "$secretSigning" "$stringToSign")
193 echo "$algorithm Credential=$secretId/$credentialScope, SignedHeaders=$signedHeaders, Signature=$signature"
196 tencent_api_request() {
201 timestamp
=${5:-$(date +%s)}
203 token
=$
(tencent_signature_v3
"$service" "$action" "$payload" "$timestamp")
205 _H1
="Content-Type: application/json"
206 _H2
="Authorization: $token"
207 _H3
="X-TC-Version: $version"
208 _H4
="X-TC-Timestamp: $timestamp"
209 _H5
="X-TC-Action: $action"
211 _post
"$payload" "$Tencent_API" "" "POST" "application/json"