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.

211 lines
6.0 KiB

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