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.

286 lines
8.2 KiB

  1. #!/usr/bin/env sh
  2. #
  3. #JD_ACCESS_KEY_ID="sdfsdfsdfljlbjkljlkjsdfoiwje"
  4. #JD_ACCESS_KEY_SECRET="xxxxxxx"
  5. #JD_REGION="cn-north-1"
  6. _JD_ACCOUNT="https://uc.jdcloud.com/account/accesskey"
  7. _JD_PROD="clouddnsservice"
  8. _JD_API="jdcloud-api.com"
  9. _JD_API_VERSION="v1"
  10. _JD_DEFAULT_REGION="cn-north-1"
  11. _JD_HOST="$_JD_PROD.$_JD_API"
  12. ######## Public functions #####################
  13. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_jd_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. JD_ACCESS_KEY_ID="${JD_ACCESS_KEY_ID:-$(_readaccountconf_mutable JD_ACCESS_KEY_ID)}"
  18. JD_ACCESS_KEY_SECRET="${JD_ACCESS_KEY_SECRET:-$(_readaccountconf_mutable JD_ACCESS_KEY_SECRET)}"
  19. JD_REGION="${JD_REGION:-$(_readaccountconf_mutable JD_REGION)}"
  20. if [ -z "$JD_ACCESS_KEY_ID" ] || [ -z "$JD_ACCESS_KEY_SECRET" ]; then
  21. JD_ACCESS_KEY_ID=""
  22. JD_ACCESS_KEY_SECRET=""
  23. _err "You haven't specifed the jdcloud api key id or api key secret yet."
  24. _err "Please create your key and try again. see $(__green $_JD_ACCOUNT)"
  25. return 1
  26. fi
  27. _saveaccountconf_mutable JD_ACCESS_KEY_ID "$JD_ACCESS_KEY_ID"
  28. _saveaccountconf_mutable JD_ACCESS_KEY_SECRET "$JD_ACCESS_KEY_SECRET"
  29. if [ -z "$JD_REGION" ]; then
  30. _debug "Using default region: $_JD_DEFAULT_REGION"
  31. JD_REGION="$_JD_DEFAULT_REGION"
  32. else
  33. _saveaccountconf_mutable JD_REGION "$JD_REGION"
  34. fi
  35. _JD_BASE_URI="$_JD_API_VERSION/regions/$JD_REGION"
  36. _debug "First detect the root zone"
  37. if ! _get_root "$fulldomain"; then
  38. _err "invalid domain"
  39. return 1
  40. fi
  41. _debug _domain_id "$_domain_id"
  42. _debug _sub_domain "$_sub_domain"
  43. _debug _domain "$_domain"
  44. #_debug "Getting getViewTree"
  45. _debug "Adding records"
  46. _addrr="{\"req\":{\"hostRecord\":\"$_sub_domain\",\"hostValue\":\"$txtvalue\",\"ttl\":300,\"type\":\"TXT\",\"viewValue\":-1},\"regionId\":\"$JD_REGION\",\"domainId\":\"$_domain_id\"}"
  47. #_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"}'
  48. if jd_rest POST "domain/$_domain_id/RRAdd" "" "$_addrr"; then
  49. _rid="$(echo "$response" | tr '{},' '\n' | grep '"id":' | cut -d : -f 2)"
  50. if [ -z "$_rid" ]; then
  51. _err "Can not find record id from the result."
  52. return 1
  53. fi
  54. _info "TXT record added successfully."
  55. _srid="$(_readdomainconf "JD_CLOUD_RIDS")"
  56. if [ "$_srid" ]; then
  57. _rid="$_srid,$_rid"
  58. fi
  59. _savedomainconf "JD_CLOUD_RIDS" "$_rid"
  60. return 0
  61. fi
  62. return 1
  63. }
  64. dns_jd_rm() {
  65. fulldomain=$1
  66. txtvalue=$2
  67. JD_ACCESS_KEY_ID="${JD_ACCESS_KEY_ID:-$(_readaccountconf_mutable JD_ACCESS_KEY_ID)}"
  68. JD_ACCESS_KEY_SECRET="${JD_ACCESS_KEY_SECRET:-$(_readaccountconf_mutable JD_ACCESS_KEY_SECRET)}"
  69. JD_REGION="${JD_REGION:-$(_readaccountconf_mutable JD_REGION)}"
  70. if [ -z "$JD_REGION" ]; then
  71. _debug "Using default region: $_JD_DEFAULT_REGION"
  72. JD_REGION="$_JD_DEFAULT_REGION"
  73. fi
  74. _JD_BASE_URI="$_JD_API_VERSION/regions/$JD_REGION"
  75. _info "Getting existing records for $fulldomain"
  76. _srid="$(_readdomainconf "JD_CLOUD_RIDS")"
  77. _debug _srid "$_srid"
  78. if [ -z "$_srid" ]; then
  79. _err "Not rid skip"
  80. return 0
  81. fi
  82. _debug "First detect the root zone"
  83. if ! _get_root "$fulldomain"; then
  84. _err "invalid domain"
  85. return 1
  86. fi
  87. _debug _domain_id "$_domain_id"
  88. _debug _sub_domain "$_sub_domain"
  89. _debug _domain "$_domain"
  90. _cleardomainconf JD_CLOUD_RIDS
  91. _aws_tmpl_xml="{\"ids\":[$_srid],\"action\":\"del\",\"regionId\":\"$JD_REGION\",\"domainId\":\"$_domain_id\"}"
  92. if jd_rest POST "domain/$_domain_id/RROperate" "" "$_aws_tmpl_xml" && _contains "$response" "\"code\":\"OK\""; then
  93. _info "TXT record deleted successfully."
  94. return 0
  95. fi
  96. return 1
  97. }
  98. #################### Private functions below ##################################
  99. _get_root() {
  100. domain=$1
  101. i=1
  102. p=1
  103. while true; do
  104. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  105. _debug2 "Checking domain: $h"
  106. if ! jd_rest GET "domain"; then
  107. _err "error get domain list"
  108. return 1
  109. fi
  110. if [ -z "$h" ]; then
  111. #not valid
  112. _err "Invalid domain"
  113. return 1
  114. fi
  115. if _contains "$response" "\"domainName\":\"$h\""; then
  116. hostedzone="$(echo "$response" | tr '{}' '\n' | grep "\"domainName\":\"$h\"")"
  117. _debug hostedzone "$hostedzone"
  118. if [ "$hostedzone" ]; then
  119. _domain_id="$(echo "$hostedzone" | tr ',' '\n' | grep "\"id\":" | cut -d : -f 2)"
  120. if [ "$_domain_id" ]; then
  121. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  122. _domain=$h
  123. return 0
  124. fi
  125. fi
  126. _err "Can't find domain with id: $h"
  127. return 1
  128. fi
  129. p=$i
  130. i=$(_math "$i" + 1)
  131. done
  132. return 1
  133. }
  134. #method uri qstr data
  135. jd_rest() {
  136. mtd="$1"
  137. ep="$2"
  138. qsr="$3"
  139. data="$4"
  140. _debug mtd "$mtd"
  141. _debug ep "$ep"
  142. _debug qsr "$qsr"
  143. _debug data "$data"
  144. CanonicalURI="/$_JD_BASE_URI/$ep"
  145. _debug2 CanonicalURI "$CanonicalURI"
  146. CanonicalQueryString="$qsr"
  147. _debug2 CanonicalQueryString "$CanonicalQueryString"
  148. RequestDate="$(date -u +"%Y%m%dT%H%M%SZ")"
  149. #RequestDate="20190713T082155Z" ######################################################
  150. _debug2 RequestDate "$RequestDate"
  151. export _H1="X-Jdcloud-Date: $RequestDate"
  152. RequestNonce="2bd0852a-8bae-4087-b2d5-$(_time)"
  153. #RequestNonce="894baff5-72d4-4244-883a-7b2eb51e7fbe" #################################
  154. _debug2 RequestNonce "$RequestNonce"
  155. export _H2="X-Jdcloud-Nonce: $RequestNonce"
  156. if [ "$data" ]; then
  157. CanonicalHeaders="content-type:application/json\n"
  158. SignedHeaders="content-type;"
  159. else
  160. CanonicalHeaders=""
  161. SignedHeaders=""
  162. fi
  163. CanonicalHeaders="${CanonicalHeaders}host:$_JD_HOST\nx-jdcloud-date:$RequestDate\nx-jdcloud-nonce:$RequestNonce\n"
  164. SignedHeaders="${SignedHeaders}host;x-jdcloud-date;x-jdcloud-nonce"
  165. _debug2 CanonicalHeaders "$CanonicalHeaders"
  166. _debug2 SignedHeaders "$SignedHeaders"
  167. Hash="sha256"
  168. RequestPayload="$data"
  169. _debug2 RequestPayload "$RequestPayload"
  170. RequestPayloadHash="$(printf "%s" "$RequestPayload" | _digest "$Hash" hex | _lower_case)"
  171. _debug2 RequestPayloadHash "$RequestPayloadHash"
  172. CanonicalRequest="$mtd\n$CanonicalURI\n$CanonicalQueryString\n$CanonicalHeaders\n$SignedHeaders\n$RequestPayloadHash"
  173. _debug2 CanonicalRequest "$CanonicalRequest"
  174. HashedCanonicalRequest="$(printf "$CanonicalRequest%s" | _digest "$Hash" hex)"
  175. _debug2 HashedCanonicalRequest "$HashedCanonicalRequest"
  176. Algorithm="JDCLOUD2-HMAC-SHA256"
  177. _debug2 Algorithm "$Algorithm"
  178. RequestDateOnly="$(echo "$RequestDate" | cut -c 1-8)"
  179. _debug2 RequestDateOnly "$RequestDateOnly"
  180. Region="$JD_REGION"
  181. Service="$_JD_PROD"
  182. CredentialScope="$RequestDateOnly/$Region/$Service/jdcloud2_request"
  183. _debug2 CredentialScope "$CredentialScope"
  184. StringToSign="$Algorithm\n$RequestDate\n$CredentialScope\n$HashedCanonicalRequest"
  185. _debug2 StringToSign "$StringToSign"
  186. kSecret="JDCLOUD2$JD_ACCESS_KEY_SECRET"
  187. _secure_debug2 kSecret "$kSecret"
  188. kSecretH="$(printf "%s" "$kSecret" | _hex_dump | tr -d " ")"
  189. _secure_debug2 kSecretH "$kSecretH"
  190. kDateH="$(printf "$RequestDateOnly%s" | _hmac "$Hash" "$kSecretH" hex)"
  191. _debug2 kDateH "$kDateH"
  192. kRegionH="$(printf "$Region%s" | _hmac "$Hash" "$kDateH" hex)"
  193. _debug2 kRegionH "$kRegionH"
  194. kServiceH="$(printf "$Service%s" | _hmac "$Hash" "$kRegionH" hex)"
  195. _debug2 kServiceH "$kServiceH"
  196. kSigningH="$(printf "%s" "jdcloud2_request" | _hmac "$Hash" "$kServiceH" hex)"
  197. _debug2 kSigningH "$kSigningH"
  198. signature="$(printf "$StringToSign%s" | _hmac "$Hash" "$kSigningH" hex)"
  199. _debug2 signature "$signature"
  200. Authorization="$Algorithm Credential=$JD_ACCESS_KEY_ID/$CredentialScope, SignedHeaders=$SignedHeaders, Signature=$signature"
  201. _debug2 Authorization "$Authorization"
  202. _H3="Authorization: $Authorization"
  203. _debug _H3 "$_H3"
  204. url="https://$_JD_HOST$CanonicalURI"
  205. if [ "$qsr" ]; then
  206. url="https://$_JD_HOST$CanonicalURI?$qsr"
  207. fi
  208. if [ "$mtd" = "GET" ]; then
  209. response="$(_get "$url")"
  210. else
  211. response="$(_post "$data" "$url" "" "$mtd" "application/json")"
  212. fi
  213. _ret="$?"
  214. _debug2 response "$response"
  215. if [ "$_ret" = "0" ]; then
  216. if _contains "$response" "\"error\""; then
  217. _err "Response error:$response"
  218. return 1
  219. fi
  220. fi
  221. return "$_ret"
  222. }