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.

216 lines
4.9 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. if ! _dynu_rest GET "dns/getroot/$domain"; then
  105. return 1
  106. fi
  107. if ! _contains "$response" "domain_name"; then
  108. _debug "Domain name not found."
  109. return 1
  110. fi
  111. _domain_name=$(printf "%s" "$response" | tr -d "{}" | cut -d , -f 1 | cut -d : -f 2 | cut -d '"' -f 2)
  112. _node=$(printf "%s" "$response" | tr -d "{}" | cut -d , -f 3 | cut -d : -f 2 | cut -d '"' -f 2)
  113. return 0
  114. }
  115. _get_recordid() {
  116. fulldomain=$1
  117. txtvalue=$2
  118. if ! _dynu_rest GET "dns/record/get?hostname=$fulldomain&rrtype=TXT"; then
  119. return 1
  120. fi
  121. if ! _contains "$response" "$txtvalue"; then
  122. _dns_record_id=0
  123. return 0
  124. fi
  125. _dns_record_id=$(printf "%s" "$response" | _egrep_o "{[^}]*}" | grep "\"text_data\":\"$txtvalue\"" | grep -Po '"id":\K[0-9]+')
  126. return 0
  127. }
  128. _delete_txt_record() {
  129. _dns_record_id=$1
  130. if ! _dynu_rest GET "dns/record/delete/$_dns_record_id"; then
  131. return 1
  132. fi
  133. if ! _contains "$response" "true"; then
  134. return 1
  135. fi
  136. return 0
  137. }
  138. _dynu_rest() {
  139. m=$1
  140. ep="$2"
  141. data="$3"
  142. _debug "$ep"
  143. export _H1="Authorization: Bearer $Dynu_Token"
  144. export _H2="Content-Type: application/json"
  145. if [ "$data" ]; then
  146. _debug data "$data"
  147. response="$(_post "$data" "$Dynu_EndPoint/$ep" "" "$m")"
  148. else
  149. _info "Getting $Dynu_EndPoint/$ep"
  150. response="$(_get "$Dynu_EndPoint/$ep")"
  151. fi
  152. if [ "$?" != "0" ]; then
  153. _err "error $ep"
  154. return 1
  155. fi
  156. _debug2 response "$response"
  157. return 0
  158. }
  159. _dynu_authentication() {
  160. realm="$(printf "%s" "$Dynu_ClientId:$Dynu_Secret" | _base64)"
  161. export _H1="Authorization: Basic $realm"
  162. export _H2="Content-Type: application/json"
  163. response="$(_get "$Dynu_EndPoint/oauth2/token")"
  164. if [ "$?" != "0" ]; then
  165. _err "Authentication failed."
  166. return 1
  167. fi
  168. if _contains "$response" "accessToken"; then
  169. Dynu_Token=$(printf "%s" "$response" | tr -d "[]" | cut -d , -f 2 | cut -d : -f 2 | cut -d '"' -f 2)
  170. fi
  171. if _contains "$Dynu_Token" "null"; then
  172. Dynu_Token=""
  173. fi
  174. _debug2 response "$response"
  175. return 0
  176. }