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.

228 lines
5.0 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 ! _dynu_rest GET "dns/record/get?hostname=$fulldomain&rrtype=TXT"; then
  130. return 1
  131. fi
  132. if ! _contains "$response" "$txtvalue"; then
  133. _dns_record_id=0
  134. return 0
  135. fi
  136. _dns_record_id=$(printf "%s" "$response" | _egrep_o "{[^}]*}" | grep "\"text_data\":\"$txtvalue\"" | _egrep_o ",[^,]*," | grep ',"id":' | tr -d ",," | cut -d : -f 2)
  137. return 0
  138. }
  139. _delete_txt_record() {
  140. _dns_record_id=$1
  141. if ! _dynu_rest GET "dns/record/delete/$_dns_record_id"; then
  142. return 1
  143. fi
  144. if ! _contains "$response" "true"; then
  145. return 1
  146. fi
  147. return 0
  148. }
  149. _dynu_rest() {
  150. m=$1
  151. ep="$2"
  152. data="$3"
  153. _debug "$ep"
  154. export _H1="Authorization: Bearer $Dynu_Token"
  155. export _H2="Content-Type: application/json"
  156. if [ "$data" ]; then
  157. _debug data "$data"
  158. response="$(_post "$data" "$Dynu_EndPoint/$ep" "" "$m")"
  159. else
  160. _info "Getting $Dynu_EndPoint/$ep"
  161. response="$(_get "$Dynu_EndPoint/$ep")"
  162. fi
  163. if [ "$?" != "0" ]; then
  164. _err "error $ep"
  165. return 1
  166. fi
  167. _debug2 response "$response"
  168. return 0
  169. }
  170. _dynu_authentication() {
  171. realm="$(printf "%s" "$Dynu_ClientId:$Dynu_Secret" | _base64)"
  172. export _H1="Authorization: Basic $realm"
  173. export _H2="Content-Type: application/json"
  174. response="$(_get "$Dynu_EndPoint/oauth2/token")"
  175. if [ "$?" != "0" ]; then
  176. _err "Authentication failed."
  177. return 1
  178. fi
  179. if _contains "$response" "accessToken"; then
  180. Dynu_Token=$(printf "%s" "$response" | tr -d "[]" | cut -d , -f 2 | cut -d : -f 2 | cut -d '"' -f 2)
  181. fi
  182. if _contains "$Dynu_Token" "null"; then
  183. Dynu_Token=""
  184. fi
  185. _debug2 response "$response"
  186. return 0
  187. }