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.

217 lines
6.1 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_tencent_info='Tencent.com
  4. Site: cloud.Tencent.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_tencent
  6. Options:
  7. Tencent_SecretId Secret ID
  8. Tencent_SecretKey Secret Key
  9. Issues: github.com/acmesh-official/acme.sh/issues/4781
  10. '
  11. Tencent_API="https://dnspod.tencentcloudapi.com"
  12. #Usage: dns_tencent_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  13. dns_tencent_add() {
  14. fulldomain=$1
  15. txtvalue=$2
  16. Tencent_SecretId="${Tencent_SecretId:-$(_readaccountconf_mutable Tencent_SecretId)}"
  17. Tencent_SecretKey="${Tencent_SecretKey:-$(_readaccountconf_mutable Tencent_SecretKey)}"
  18. if [ -z "$Tencent_SecretId" ] || [ -z "$Tencent_SecretKey" ]; then
  19. Tencent_SecretId=""
  20. Tencent_SecretKey=""
  21. _err "You don't specify tencent api SecretId and SecretKey yet."
  22. return 1
  23. fi
  24. #save the api SecretId and SecretKey to the account conf file.
  25. _saveaccountconf_mutable Tencent_SecretId "$Tencent_SecretId"
  26. _saveaccountconf_mutable Tencent_SecretKey "$Tencent_SecretKey"
  27. _debug "First detect the root zone"
  28. if ! _get_root "$fulldomain"; then
  29. return 1
  30. fi
  31. _debug "Add record"
  32. _add_record_query "$_domain" "$_sub_domain" "$txtvalue" && _tencent_rest "CreateRecord"
  33. }
  34. dns_tencent_rm() {
  35. fulldomain=$1
  36. txtvalue=$2
  37. Tencent_SecretId="${Tencent_SecretId:-$(_readaccountconf_mutable Tencent_SecretId)}"
  38. Tencent_SecretKey="${Tencent_SecretKey:-$(_readaccountconf_mutable Tencent_SecretKey)}"
  39. _debug "First detect the root zone"
  40. if ! _get_root "$fulldomain"; then
  41. return 1
  42. fi
  43. _debug "Get record list"
  44. attempt=1
  45. max_attempts=5
  46. while [ -z "$record_id" ] && [ "$attempt" -le $max_attempts ]; do
  47. _check_exist_query "$_domain" "$_sub_domain" "$txtvalue" && _tencent_rest "DescribeRecordFilterList"
  48. record_id="$(echo "$response" | _egrep_o "\"RecordId\":\s*[0-9]+" | _egrep_o "[0-9]+")"
  49. _debug2 record_id "$record_id"
  50. if [ -z "$record_id" ]; then
  51. _debug "Due to TencentCloud API synchronization delay, record not found, waiting 10 seconds and retrying"
  52. _sleep 10
  53. attempt=$(_math "$attempt + 1")
  54. fi
  55. done
  56. record_id="$(echo "$response" | _egrep_o "\"RecordId\":\s*[0-9]+" | _egrep_o "[0-9]+")"
  57. _debug2 record_id "$record_id"
  58. if [ -z "$record_id" ]; then
  59. _debug "record not found after $max_attempts attempts, skip"
  60. else
  61. _debug "Delete record"
  62. _delete_record_query "$record_id" && _tencent_rest "DeleteRecord"
  63. fi
  64. }
  65. #################### Private functions below ##################################
  66. _get_root() {
  67. domain=$1
  68. i=1
  69. p=1
  70. while true; do
  71. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  72. if [ -z "$h" ]; then
  73. #not valid
  74. return 1
  75. fi
  76. _describe_records_query "$h" "@"
  77. if ! _tencent_rest "DescribeRecordList" "ignore"; then
  78. return 1
  79. fi
  80. if _contains "$response" "\"TotalCount\":"; then
  81. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  82. _debug _sub_domain "$_sub_domain"
  83. _domain="$h"
  84. _debug _domain "$_domain"
  85. return 0
  86. fi
  87. p="$i"
  88. i=$(_math "$i" + 1)
  89. done
  90. return 1
  91. }
  92. _tencent_rest() {
  93. action=$1
  94. service="dnspod"
  95. payload="${query}"
  96. timestamp=$(date -u +%s)
  97. token=$(tencent_signature_v3 $service "$action" "$payload" "$timestamp")
  98. version="2021-03-23"
  99. if ! response="$(tencent_api_request $service $version "$action" "$payload" "$timestamp")"; then
  100. _err "Error <$1>"
  101. return 1
  102. fi
  103. _debug2 response "$response"
  104. if [ -z "$2" ]; then
  105. message="$(echo "$response" | _egrep_o "\"Message\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \")"
  106. if [ "$message" ]; then
  107. _err "$message"
  108. return 1
  109. fi
  110. fi
  111. }
  112. _add_record_query() {
  113. query="{\"Domain\":\"$1\",\"SubDomain\":\"$2\",\"RecordType\":\"TXT\",\"RecordLineId\":\"0\",\"RecordLine\":\"0\",\"Value\":\"$3\",\"TTL\":600}"
  114. }
  115. _describe_records_query() {
  116. query="{\"Domain\":\"$1\",\"Limit\":3000}"
  117. }
  118. _delete_record_query() {
  119. query="{\"Domain\":\"$_domain\",\"RecordId\":$1}"
  120. }
  121. _check_exist_query() {
  122. _domain="$1"
  123. _subdomain="$2"
  124. _value="$3"
  125. query="{\"Domain\":\"$_domain\",\"SubDomain\":\"$_subdomain\",\"RecordValue\":\"$_value\"}"
  126. }
  127. # shell client for tencent cloud api v3 | @author: rehiy
  128. tencent_sha256() {
  129. printf %b "$@" | _digest sha256 hex
  130. }
  131. tencent_hmac_sha256() {
  132. k=$1
  133. shift
  134. hex_key=$(printf %b "$k" | _hex_dump | tr -d ' ')
  135. printf %b "$@" | _hmac sha256 "$hex_key" hex
  136. }
  137. tencent_hmac_sha256_hexkey() {
  138. k=$1
  139. shift
  140. printf %b "$@" | _hmac sha256 "$k" hex
  141. }
  142. tencent_signature_v3() {
  143. service=$1
  144. action=$(echo "$2" | _lower_case)
  145. payload=${3:-'{}'}
  146. timestamp=${4:-$(date +%s)}
  147. domain="$service.tencentcloudapi.com"
  148. secretId=${Tencent_SecretId:-'tencent-cloud-secret-id'}
  149. secretKey=${Tencent_SecretKey:-'tencent-cloud-secret-key'}
  150. algorithm='TC3-HMAC-SHA256'
  151. date=$(date -u -d "@$timestamp" +%Y-%m-%d 2>/dev/null)
  152. [ -z "$date" ] && date=$(date -u -r "$timestamp" +%Y-%m-%d)
  153. canonicalUri='/'
  154. canonicalQuery=''
  155. canonicalHeaders="content-type:application/json\nhost:$domain\nx-tc-action:$action\n"
  156. signedHeaders='content-type;host;x-tc-action'
  157. canonicalRequest="POST\n$canonicalUri\n$canonicalQuery\n$canonicalHeaders\n$signedHeaders\n$(tencent_sha256 "$payload")"
  158. credentialScope="$date/$service/tc3_request"
  159. stringToSign="$algorithm\n$timestamp\n$credentialScope\n$(tencent_sha256 "$canonicalRequest")"
  160. secretDate=$(tencent_hmac_sha256 "TC3$secretKey" "$date")
  161. secretService=$(tencent_hmac_sha256_hexkey "$secretDate" "$service")
  162. secretSigning=$(tencent_hmac_sha256_hexkey "$secretService" 'tc3_request')
  163. signature=$(tencent_hmac_sha256_hexkey "$secretSigning" "$stringToSign")
  164. echo "$algorithm Credential=$secretId/$credentialScope, SignedHeaders=$signedHeaders, Signature=$signature"
  165. }
  166. tencent_api_request() {
  167. service=$1
  168. version=$2
  169. action=$3
  170. payload=${4:-'{}'}
  171. timestamp=${5:-$(date +%s)}
  172. token=$(tencent_signature_v3 "$service" "$action" "$payload" "$timestamp")
  173. _H1="Content-Type: application/json"
  174. _H2="Authorization: $token"
  175. _H3="X-TC-Version: $version"
  176. _H4="X-TC-Timestamp: $timestamp"
  177. _H5="X-TC-Action: $action"
  178. _post "$payload" "$Tencent_API" "" "POST" "application/json"
  179. }