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.

232 lines
5.2 KiB

  1. #!/usr/bin/env sh
  2. #Client ID
  3. #Dynu_ClientId="0b71cae7-a099-4f6b-8ddf-94571cdb760d"
  4. #
  5. #Secret
  6. #Dynu_Secret="aCUEY4BDCV45KI8CSIC3sp2LKQ9"
  7. #
  8. #Token
  9. Dynu_Token=""
  10. #
  11. #Endpoint
  12. Dynu_EndPoint="https://api.dynu.com/v2"
  13. #
  14. #Author: Dynu Systems, Inc.
  15. #Report Bugs here: https://github.com/shar0119/acme.sh
  16. #
  17. ######## Public functions #####################
  18. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  19. dns_dynu_add() {
  20. fulldomain=$1
  21. txtvalue=$2
  22. if [ -z "$Dynu_ClientId" ] || [ -z "$Dynu_Secret" ]; then
  23. Dynu_ClientId=""
  24. Dynu_Secret=""
  25. _err "Dynu client id and secret is not specified."
  26. _err "Please create you API client id and secret and try again."
  27. return 1
  28. fi
  29. #save the client id and secret to the account conf file.
  30. _saveaccountconf Dynu_ClientId "$Dynu_ClientId"
  31. _saveaccountconf Dynu_Secret "$Dynu_Secret"
  32. if [ -z "$Dynu_Token" ]; then
  33. _info "Getting Dynu token."
  34. if ! _dynu_authentication; then
  35. _err "Can not get token."
  36. fi
  37. fi
  38. _debug "Detect root zone"
  39. if ! _get_root "$fulldomain"; then
  40. _err "Invalid domain."
  41. return 1
  42. fi
  43. _debug _node "$_node"
  44. _debug _domain_name "$_domain_name"
  45. _info "Creating TXT record."
  46. if ! _dynu_rest POST "dns/$dnsId/record" "{\"domainId\":\"$dnsId\",\"nodeName\":\"$_node\",\"recordType\":\"TXT\",\"textData\":\"$txtvalue\",\"state\":true,\"ttl\":90}"; then
  47. return 1
  48. fi
  49. if ! _contains "$response" "200"; then
  50. _err "Could not add TXT record."
  51. return 1
  52. fi
  53. return 0
  54. }
  55. #Usage: rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  56. dns_dynu_rm() {
  57. fulldomain=$1
  58. txtvalue=$2
  59. if [ -z "$Dynu_ClientId" ] || [ -z "$Dynu_Secret" ]; then
  60. Dynu_ClientId=""
  61. Dynu_Secret=""
  62. _err "Dynu client id and secret is not specified."
  63. _err "Please create you API client id and secret and try again."
  64. return 1
  65. fi
  66. #save the client id and secret to the account conf file.
  67. _saveaccountconf Dynu_ClientId "$Dynu_ClientId"
  68. _saveaccountconf Dynu_Secret "$Dynu_Secret"
  69. if [ -z "$Dynu_Token" ]; then
  70. _info "Getting Dynu token."
  71. if ! _dynu_authentication; then
  72. _err "Can not get token."
  73. fi
  74. fi
  75. _debug "Detect root zone."
  76. if ! _get_root "$fulldomain"; then
  77. _err "Invalid domain."
  78. return 1
  79. fi
  80. _debug _node "$_node"
  81. _debug _domain_name "$_domain_name"
  82. _info "Checking for TXT record."
  83. if ! _get_recordid "$fulldomain" "$txtvalue"; then
  84. _err "Could not get TXT record id."
  85. return 1
  86. fi
  87. if [ "$_dns_record_id" = "" ]; then
  88. _err "TXT record not found."
  89. return 1
  90. fi
  91. _info "Removing TXT record."
  92. if ! _delete_txt_record "$_dns_record_id"; then
  93. _err "Could not remove TXT record $_dns_record_id."
  94. fi
  95. return 0
  96. }
  97. ######## Private functions below ##################################
  98. #_acme-challenge.www.domain.com
  99. #returns
  100. # _node=_acme-challenge.www
  101. # _domain_name=domain.com
  102. _get_root() {
  103. domain=$1
  104. i=2
  105. p=1
  106. while true; do
  107. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  108. _debug h "$h"
  109. if [ -z "$h" ]; then
  110. #not valid
  111. return 1
  112. fi
  113. if ! _dynu_rest GET "dns/getroot/$h"; then
  114. return 1
  115. fi
  116. if _contains "$response" "\"domainName\":\"$h\"" >/dev/null; then
  117. dnsId=$(printf "%s" "$response" | tr -d "{}" | cut -d , -f 2 | cut -d : -f 2)
  118. _domain_name=$h
  119. _node=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  120. return 0
  121. fi
  122. p=$i
  123. i=$(_math "$i" + 1)
  124. done
  125. return 1
  126. }
  127. _get_recordid() {
  128. fulldomain=$1
  129. txtvalue=$2
  130. if ! _dynu_rest GET "dns/$dnsId/record"; then
  131. return 1
  132. fi
  133. if ! _contains "$response" "$txtvalue"; then
  134. _dns_record_id=0
  135. return 0
  136. fi
  137. _dns_record_id=$(printf "%s" "$response" | sed -e 's/[^{]*\({[^}]*}\)[^{]*/\1\n/g' | grep "\"textData\":\"$txtvalue\"" | sed -e 's/.*"id":\([^,]*\).*/\1/')
  138. return 0
  139. }
  140. _delete_txt_record() {
  141. _dns_record_id=$1
  142. if ! _dynu_rest DELETE "dns/$dnsId/record/$_dns_record_id"; then
  143. return 1
  144. fi
  145. if ! _contains "$response" "200"; then
  146. return 1
  147. fi
  148. return 0
  149. }
  150. _dynu_rest() {
  151. m=$1
  152. ep="$2"
  153. data="$3"
  154. _debug "$ep"
  155. export _H1="Authorization: Bearer $Dynu_Token"
  156. export _H2="Content-Type: application/json"
  157. if [ "$data" ] || [ "$m" = "DELETE" ]; then
  158. _debug data "$data"
  159. response="$(_post "$data" "$Dynu_EndPoint/$ep" "" "$m")"
  160. else
  161. _info "Getting $Dynu_EndPoint/$ep"
  162. response="$(_get "$Dynu_EndPoint/$ep")"
  163. fi
  164. if [ "$?" != "0" ]; then
  165. _err "error $ep"
  166. return 1
  167. fi
  168. _debug2 response "$response"
  169. return 0
  170. }
  171. _dynu_authentication() {
  172. realm="$(printf "%s" "$Dynu_ClientId:$Dynu_Secret" | _base64)"
  173. export _H1="Authorization: Basic $realm"
  174. export _H2="Content-Type: application/json"
  175. response="$(_get "$Dynu_EndPoint/oauth2/token")"
  176. if [ "$?" != "0" ]; then
  177. _err "Authentication failed."
  178. return 1
  179. fi
  180. if _contains "$response" "Authentication Exception"; then
  181. _err "Authentication failed."
  182. return 1
  183. fi
  184. if _contains "$response" "access_token"; then
  185. Dynu_Token=$(printf "%s" "$response" | tr -d "{}" | cut -d , -f 1 | cut -d : -f 2 | cut -d '"' -f 2)
  186. fi
  187. if _contains "$Dynu_Token" "null"; then
  188. Dynu_Token=""
  189. fi
  190. _debug2 response "$response"
  191. return 0
  192. }