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.

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