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.

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