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.

236 lines
5.1 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/v1"
  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/record/add" "{\"domain_name\":\"$_domain_name\",\"node_name\":\"$_node\",\"record_type\":\"TXT\",\"text_data\":\"$txtvalue\",\"state\":true,\"ttl\":90}"; then
  47. return 1
  48. fi
  49. if ! _contains "$response" "text_data"; 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/get/$h"; then
  114. return 1
  115. fi
  116. if _contains "$response" "\"name\":\"$h\"" >/dev/null; then
  117. _domain_name=$h
  118. _node=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  119. return 0
  120. fi
  121. p=$i
  122. i=$(_math "$i" + 1)
  123. done
  124. return 1
  125. }
  126. _get_recordid() {
  127. fulldomain=$1
  128. txtvalue=$2
  129. if ! _get_root "$fulldomain"; then
  130. _err "Invalid domain."
  131. return 1
  132. fi
  133. _debug _node "$_node"
  134. _debug _domain_name "$_domain_name"
  135. if ! _dynu_rest GET "dns/records/$_domain_name"; then
  136. return 1
  137. fi
  138. if ! _contains "$response" "$txtvalue"; then
  139. _dns_record_id=0
  140. return 0
  141. fi
  142. _dns_record_id=$(printf "%s" "$response" | sed -e 's/[^{]*\({[^}]*}\)[^{]*/\1\n/g' | grep "\"text_data\":\"$txtvalue\"" | sed -n -e 's/.*"id":\([^,]*\).*/\1/p')
  143. return 0
  144. }
  145. _delete_txt_record() {
  146. _dns_record_id=$1
  147. if ! _dynu_rest GET "dns/record/delete/$_dns_record_id"; then
  148. return 1
  149. fi
  150. if ! _contains "$response" "true"; then
  151. return 1
  152. fi
  153. return 0
  154. }
  155. _dynu_rest() {
  156. m=$1
  157. ep="$2"
  158. data="$3"
  159. _debug "$ep"
  160. export _H1="Authorization: Bearer $Dynu_Token"
  161. export _H2="Content-Type: application/json"
  162. if [ "$data" ]; then
  163. _debug data "$data"
  164. response="$(_post "$data" "$Dynu_EndPoint/$ep" "" "$m")"
  165. else
  166. _info "Getting $Dynu_EndPoint/$ep"
  167. response="$(_get "$Dynu_EndPoint/$ep")"
  168. fi
  169. if [ "$?" != "0" ]; then
  170. _err "error $ep"
  171. return 1
  172. fi
  173. _debug2 response "$response"
  174. return 0
  175. }
  176. _dynu_authentication() {
  177. realm="$(printf "%s" "$Dynu_ClientId:$Dynu_Secret" | _base64)"
  178. export _H1="Authorization: Basic $realm"
  179. export _H2="Content-Type: application/json"
  180. response="$(_get "$Dynu_EndPoint/oauth2/token")"
  181. if [ "$?" != "0" ]; then
  182. _err "Authentication failed."
  183. return 1
  184. fi
  185. if _contains "$response" "accessToken"; then
  186. Dynu_Token=$(printf "%s" "$response" | tr -d "[]" | cut -d , -f 2 | cut -d : -f 2 | cut -d '"' -f 2)
  187. fi
  188. if _contains "$Dynu_Token" "null"; then
  189. Dynu_Token=""
  190. fi
  191. _debug2 response "$response"
  192. return 0
  193. }