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.

137 lines
3.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/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. #fulldomain
  56. dns_dynu_rm() {
  57. fulldomain=$1
  58. }
  59. ######## Private functions below ##################################
  60. #_acme-challenge.www.domain.com
  61. #returns
  62. # _node=_acme-challenge.www
  63. # _domain_name=domain.com
  64. _get_root() {
  65. domain=$1
  66. if ! _dynu_rest GET "dns/getroot/$domain"; then
  67. return 1
  68. fi
  69. if ! _contains "$response" "domain_name"; then
  70. _debug "Domain name not found"
  71. return 1
  72. fi
  73. _domain_name=$(printf "%s" "$response" | tr -d "{}" | cut -d , -f 1 | cut -d : -f 2 | cut -d '"' -f 2)
  74. _node=$(printf "%s" "$response" | tr -d "{}" | cut -d , -f 3 | cut -d : -f 2 | cut -d '"' -f 2)
  75. return 0
  76. }
  77. _dynu_rest() {
  78. m=$1
  79. ep="$2"
  80. data="$3"
  81. _debug "$ep"
  82. export _H1="Authorization: Bearer $Dynu_Token"
  83. export _H2="Content-Type: application/json"
  84. if [ "$data" ]; then
  85. _debug data "$data"
  86. response="$(_post "$data" "$Dynu_EndPoint/$ep" "" "$m")"
  87. else
  88. echo "Getting $Dynu_EndPoint/$ep"
  89. response="$(_get "$Dynu_EndPoint/$ep")"
  90. fi
  91. if [ "$?" != "0" ]; then
  92. _err "error $ep"
  93. return 1
  94. fi
  95. _debug2 response "$response"
  96. return 0
  97. }
  98. _dynu_authentication() {
  99. realm="$(printf "%s" "$Dynu_ClientId:$Dynu_Secret" | _base64)"
  100. export _H1="Authorization: Basic $realm"
  101. export _H2="Content-Type: application/json"
  102. response="$(_get "$Dynu_EndPoint/oauth2/token")"
  103. if [ "$?" != "0" ]; then
  104. _err "Authentication failed."
  105. return 1
  106. fi
  107. if _contains "$response" "accessToken"; then
  108. Dynu_Token=$(printf "%s" "$response" | tr -d "[]" | cut -d , -f 2 | cut -d : -f 2 | cut -d '"' -f 2)
  109. fi
  110. if _contains "$Dynu_Token" "null"; then
  111. Dynu_Token=""
  112. fi
  113. _debug2 response "$response"
  114. return 0
  115. }