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