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.

201 lines
5.6 KiB

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