4 #JD_ACCESS_KEY_ID="sdfsdfsdfljlbjkljlkjsdfoiwje"
5 #JD_ACCESS_KEY_SECRET="xxxxxxx"
6 #JD_REGION="cn-north-1"
8 _JD_ACCOUNT
="https://uc.jdcloud.com/account/accesskey"
10 _JD_PROD
="clouddnsservice"
11 _JD_API
="jdcloud-api.com"
14 _JD_DEFAULT_REGION
="cn-north-1"
16 _JD_HOST
="$_JD_PROD.$_JD_API"
18 ######## Public functions #####################
20 #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
25 JD_ACCESS_KEY_ID
="${JD_ACCESS_KEY_ID:-$(_readaccountconf_mutable JD_ACCESS_KEY_ID)}"
26 JD_ACCESS_KEY_SECRET
="${JD_ACCESS_KEY_SECRET:-$(_readaccountconf_mutable JD_ACCESS_KEY_SECRET)}"
27 JD_REGION
="${JD_REGION:-$(_readaccountconf_mutable JD_REGION)}"
29 if [ -z "$JD_ACCESS_KEY_ID" ] ||
[ -z "$JD_ACCESS_KEY_SECRET" ]; then
31 JD_ACCESS_KEY_SECRET
=""
32 _err
"You haven't specifed the jdcloud api key id or api key secret yet."
33 _err
"Please create your key and try again. see $(__green $_JD_ACCOUNT)"
37 _saveaccountconf_mutable JD_ACCESS_KEY_ID
"$JD_ACCESS_KEY_ID"
38 _saveaccountconf_mutable JD_ACCESS_KEY_SECRET
"$JD_ACCESS_KEY_SECRET"
39 if [ -z "$JD_REGION" ]; then
40 _debug
"Using default region: $_JD_DEFAULT_REGION"
41 JD_REGION
="$_JD_DEFAULT_REGION"
43 _saveaccountconf_mutable JD_REGION
"$JD_REGION"
45 _JD_BASE_URI
="$_JD_API_VERSION/regions/$JD_REGION"
47 _debug
"First detect the root zone"
48 if ! _get_root
"$fulldomain"; then
52 _debug _domain_id
"$_domain_id"
53 _debug _sub_domain
"$_sub_domain"
54 _debug _domain
"$_domain"
56 #_debug "Getting getViewTree"
58 _debug
"Adding records"
60 _addrr
="{\"req\":{\"hostRecord\":\"$_sub_domain\",\"hostValue\":\"$txtvalue\",\"ttl\":300,\"type\":\"TXT\",\"viewValue\":-1},\"regionId\":\"$JD_REGION\",\"domainId\":\"$_domain_id\"}"
61 #_addrr='{"req":{"hostRecord":"xx","hostValue":"\"value4\"","jcloudRes":false,"mxPriority":null,"port":null,"ttl":300,"type":"TXT","weight":null,"viewValue":-1},"regionId":"cn-north-1","domainId":"8824"}'
62 if jd_rest POST
"domain/$_domain_id/RRAdd" "" "$_addrr"; then
63 _rid
="$(echo "$response" | tr '{},' '\n' | grep '"id
":' | cut -d : -f 2)"
64 if [ -z "$_rid" ]; then
65 _err
"Can not find record id from the result."
68 _info
"TXT record added successfully."
69 _srid
="$(_readdomainconf "JD_CLOUD_RIDS
")"
73 _savedomainconf
"JD_CLOUD_RIDS" "$_rid"
84 JD_ACCESS_KEY_ID
="${JD_ACCESS_KEY_ID:-$(_readaccountconf_mutable JD_ACCESS_KEY_ID)}"
85 JD_ACCESS_KEY_SECRET
="${JD_ACCESS_KEY_SECRET:-$(_readaccountconf_mutable JD_ACCESS_KEY_SECRET)}"
86 JD_REGION
="${JD_REGION:-$(_readaccountconf_mutable JD_REGION)}"
88 if [ -z "$JD_REGION" ]; then
89 _debug
"Using default region: $_JD_DEFAULT_REGION"
90 JD_REGION
="$_JD_DEFAULT_REGION"
93 _JD_BASE_URI
="$_JD_API_VERSION/regions/$JD_REGION"
95 _info
"Getting existing records for $fulldomain"
96 _srid
="$(_readdomainconf "JD_CLOUD_RIDS
")"
99 if [ -z "$_srid" ]; then
104 _debug
"First detect the root zone"
105 if ! _get_root
"$fulldomain"; then
106 _err
"invalid domain"
109 _debug _domain_id
"$_domain_id"
110 _debug _sub_domain
"$_sub_domain"
111 _debug _domain
"$_domain"
113 _cleardomainconf JD_CLOUD_RIDS
115 _aws_tmpl_xml
="{\"ids\":[$_srid],\"action\":\"del\",\"regionId\":\"$JD_REGION\",\"domainId\":\"$_domain_id\"}"
117 if jd_rest POST
"domain/$_domain_id/RROperate" "" "$_aws_tmpl_xml" && _contains
"$response" "\"code\":\"OK\""; then
118 _info
"TXT record deleted successfully."
125 #################### Private functions below ##################################
133 h
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
134 _debug2
"Checking domain: $h"
135 if ! jd_rest GET
"domain"; then
136 _err
"error get domain list"
141 _err
"Invalid domain"
145 if _contains
"$response" "\"domainName\":\"$h\""; then
146 hostedzone
="$(echo "$response" | tr '{}' '\n' | grep "\"domainName
\":\"$h\"")"
147 _debug hostedzone
"$hostedzone"
148 if [ "$hostedzone" ]; then
149 _domain_id
="$(echo "$hostedzone" | tr ',' '\n' | grep "\"id
\":" | cut -d : -f 2)"
150 if [ "$_domain_id" ]; then
151 _sub_domain
=$
(printf "%s" "$domain" | cut
-d .
-f 1-$p)
156 _err
"Can't find domain with id: $h"
166 #method uri qstr data
178 CanonicalURI
="/$_JD_BASE_URI/$ep"
179 _debug2 CanonicalURI
"$CanonicalURI"
181 CanonicalQueryString
="$qsr"
182 _debug2 CanonicalQueryString
"$CanonicalQueryString"
184 RequestDate
="$(date -u +"%Y
%m
%dT
%H
%M
%SZ
")"
185 #RequestDate="20190713T082155Z" ######################################################
186 _debug2 RequestDate
"$RequestDate"
187 export _H1
="X-Jdcloud-Date: $RequestDate"
189 RequestNonce
="2bd0852a-8bae-4087-b2d5-$(_time)"
190 #RequestNonce="894baff5-72d4-4244-883a-7b2eb51e7fbe" #################################
191 _debug2 RequestNonce
"$RequestNonce"
192 export _H2
="X-Jdcloud-Nonce: $RequestNonce"
195 CanonicalHeaders
="content-type:application/json\n"
196 SignedHeaders
="content-type;"
201 CanonicalHeaders
="${CanonicalHeaders}host:$_JD_HOST\nx-jdcloud-date:$RequestDate\nx-jdcloud-nonce:$RequestNonce\n"
202 SignedHeaders
="${SignedHeaders}host;x-jdcloud-date;x-jdcloud-nonce"
204 _debug2 CanonicalHeaders
"$CanonicalHeaders"
205 _debug2 SignedHeaders
"$SignedHeaders"
209 RequestPayload
="$data"
210 _debug2 RequestPayload
"$RequestPayload"
212 RequestPayloadHash
="$(printf "%s
" "$RequestPayload" | _digest "$Hash" hex | _lower_case)"
213 _debug2 RequestPayloadHash
"$RequestPayloadHash"
215 CanonicalRequest
="$mtd\n$CanonicalURI\n$CanonicalQueryString\n$CanonicalHeaders\n$SignedHeaders\n$RequestPayloadHash"
216 _debug2 CanonicalRequest
"$CanonicalRequest"
218 HashedCanonicalRequest
="$(printf "$CanonicalRequest%s
" | _digest "$Hash" hex)"
219 _debug2 HashedCanonicalRequest
"$HashedCanonicalRequest"
221 Algorithm
="JDCLOUD2-HMAC-SHA256"
222 _debug2 Algorithm
"$Algorithm"
224 RequestDateOnly
="$(echo "$RequestDate" | cut -c 1-8)"
225 _debug2 RequestDateOnly
"$RequestDateOnly"
230 CredentialScope
="$RequestDateOnly/$Region/$Service/jdcloud2_request"
231 _debug2 CredentialScope
"$CredentialScope"
233 StringToSign
="$Algorithm\n$RequestDate\n$CredentialScope\n$HashedCanonicalRequest"
235 _debug2 StringToSign
"$StringToSign"
237 kSecret
="JDCLOUD2$JD_ACCESS_KEY_SECRET"
239 _secure_debug2 kSecret
"$kSecret"
241 kSecretH
="$(printf "%s
" "$kSecret" | _hex_dump | tr -d " ")"
242 _secure_debug2 kSecretH
"$kSecretH"
244 kDateH
="$(printf "$RequestDateOnly%s
" | _hmac "$Hash" "$kSecretH" hex)"
245 _debug2 kDateH
"$kDateH"
247 kRegionH
="$(printf "$Region%s
" | _hmac "$Hash" "$kDateH" hex)"
248 _debug2 kRegionH
"$kRegionH"
250 kServiceH
="$(printf "$Service%s
" | _hmac "$Hash" "$kRegionH" hex)"
251 _debug2 kServiceH
"$kServiceH"
253 kSigningH
="$(printf "%s
" "jdcloud2_request
" | _hmac "$Hash" "$kServiceH" hex)"
254 _debug2 kSigningH
"$kSigningH"
256 signature
="$(printf "$StringToSign%s
" | _hmac "$Hash" "$kSigningH" hex)"
257 _debug2 signature
"$signature"
259 Authorization
="$Algorithm Credential=$JD_ACCESS_KEY_ID/$CredentialScope, SignedHeaders=$SignedHeaders, Signature=$signature"
260 _debug2 Authorization
"$Authorization"
262 _H3
="Authorization: $Authorization"
265 url
="https://$_JD_HOST$CanonicalURI"
267 url
="https://$_JD_HOST$CanonicalURI?$qsr"
270 if [ "$mtd" = "GET" ]; then
271 response
="$(_get "$url")"
273 response
="$(_post "$data" "$url" "" "$mtd" "application
/json
")"
277 _debug2 response
"$response"
278 if [ "$_ret" = "0" ]; then
279 if _contains
"$response" "\"error\""; then
280 _err
"Response error:$response"