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.

135 lines
3.1 KiB

  1. #!/usr/bin/env sh
  2. my_dir="$(dirname "$0")"
  3. source "$my_dir/acme.sh"
  4. #Client ID
  5. Dynu_ClientId="0b71cae7-a099-4f6b-8ddf-94571cdb760d"
  6. #
  7. #Secret
  8. Dynu_Secret="aCUEY4BDCV45KI8CSIC3sp2LKQ9"
  9. #
  10. #Token
  11. Dynu_Token=""
  12. #
  13. #Endpoint
  14. Dynu_EndPoint="https://api.dynu.com/v1"
  15. ######## Public functions #####################
  16. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  17. dns_dynu_add() {
  18. fulldomain=$1
  19. txtvalue=$2
  20. if [ -z "$Dynu_ClientId" ] || [ -z "$Dynu_Secret" ]; then
  21. Dynu_ClientId=""
  22. Dynu_Secret=""
  23. _err "Dynu client id and secret is not specified."
  24. _err "Please create you API client id and secret and try again."
  25. return 1
  26. fi
  27. #save the client id and secret to the account conf file.
  28. _saveaccountconf Dynu_ClientId "$Dynu_ClientId"
  29. _saveaccountconf Dynu_Secret "$Dynu_Secret"
  30. if [ -z "$Dynu_Token" ]; then
  31. _info "Getting Dynu token"
  32. if ! _dynu_authentication; then
  33. _err "Can not get token."
  34. fi
  35. fi
  36. _debug "Detect root zone"
  37. if ! _get_root "$fulldomain"; then
  38. _err "Invalid domain"
  39. return 1
  40. fi
  41. _debug _node "$_node"
  42. _debug _domain_name "$_domain_name"
  43. _info "Creating TXT record"
  44. 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
  45. return 1
  46. fi
  47. if ! _contains "$response" "text_data"; then
  48. _err "Could not add TXT record"
  49. return 1
  50. fi
  51. return 0
  52. }
  53. #fulldomain
  54. dns_dynu_rm() {
  55. fulldomain=$1
  56. }
  57. ######## Private functions below ##################################
  58. #_acme-challenge.www.domain.com
  59. #returns
  60. # _node=_acme-challenge.www
  61. # _domain_name=domain.com
  62. _get_root() {
  63. domain=$1
  64. if ! _dynu_rest GET "dns/getroot/$domain"; then
  65. return 1
  66. fi
  67. if ! _contains "$response" "domain_name"; then
  68. _debug "Domain name not found"
  69. return 1
  70. fi
  71. _domain_name=$(printf "%s" "$response" | tr -d "{}" | cut -d , -f 1 | cut -d : -f 2 | cut -d '"' -f 2)
  72. _node=$(printf "%s" "$response" | tr -d "{}" | cut -d , -f 3 | cut -d : -f 2 | cut -d '"' -f 2)
  73. return 0
  74. }
  75. _dynu_rest() {
  76. m=$1
  77. ep="$2"
  78. data="$3"
  79. _debug "$ep"
  80. export _H1="Authorization: Bearer $Dynu_Token"
  81. export _H2="Content-Type: application/json"
  82. if [ "$data" ]; then
  83. _debug data "$data"
  84. response="$(_post "$data" "$Dynu_EndPoint/$ep" "" "$m")"
  85. else
  86. echo "Getting $Dynu_EndPoint/$ep"
  87. response="$(_get "$Dynu_EndPoint/$ep")"
  88. fi
  89. if [ "$?" != "0" ]; then
  90. _err "error $ep"
  91. return 1
  92. fi
  93. _debug2 response "$response"
  94. return 0
  95. }
  96. _dynu_authentication() {
  97. export _H1="Authorization: Basic $(printf "%s" "$Dynu_ClientId:$Dynu_Secret" | _base64)"
  98. export _H2="Content-Type: application/json"
  99. response="$(_get "$Dynu_EndPoint/oauth2/token")"
  100. if [ "$?" != "0" ]; then
  101. _err "Authentication failed."
  102. return 1
  103. fi
  104. if _contains "$response" "accessToken"; then
  105. Dynu_Token=$(printf "%s" "$response" | tr -d "[]" | cut -d , -f 2 | cut -d : -f 2 | cut -d '"' -f 2)
  106. fi
  107. if _contains "$Dynu_Token" "null"; then
  108. Dynu_Token=""
  109. fi
  110. _debug2 response "$response"
  111. return 0
  112. }