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.

181 lines
4.4 KiB

6 years ago
6 years ago
1 month ago
1 month ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_neodigit_info='Neodigit.net
  4. Site: Neodigit.net
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_neodigit
  6. Options:
  7. NEODIGIT_API_TOKEN API Token
  8. Author: Adrian Almenar
  9. '
  10. NEODIGIT_API_URL="https://api.neodigit.net/v1"
  11. #
  12. ######## Public functions #####################
  13. # Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_neodigit_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. NEODIGIT_API_TOKEN="${NEODIGIT_API_TOKEN:-$(_readaccountconf_mutable NEODIGIT_API_TOKEN)}"
  18. if [ -z "$NEODIGIT_API_TOKEN" ]; then
  19. NEODIGIT_API_TOKEN=""
  20. _err "You haven't specified a Token api key."
  21. _err "Please create the key and try again."
  22. return 1
  23. fi
  24. #save the api key and email to the account conf file.
  25. _saveaccountconf_mutable NEODIGIT_API_TOKEN "$NEODIGIT_API_TOKEN"
  26. _debug "First detect the root zone"
  27. if ! _get_root "$fulldomain"; then
  28. _err "invalid domain"
  29. return 1
  30. fi
  31. _debug fulldomain "$fulldomain"
  32. _debug txtvalue "$txtvalue"
  33. _debug domain "$_domain"
  34. _debug sub_domain "$_sub_domain"
  35. _debug "Getting txt records"
  36. _neo_rest GET "dns/zones/${_domain_id}/records?type=TXT&name=$fulldomain"
  37. _debug _code "$_code"
  38. if [ "$_code" != "200" ]; then
  39. _err "error retrieving data!"
  40. return 1
  41. fi
  42. _debug fulldomain "$fulldomain"
  43. _debug txtvalue "$txtvalue"
  44. _debug domain "$_domain"
  45. _debug sub_domain "$_sub_domain"
  46. _info "Adding record"
  47. if _neo_rest POST "dns/zones/$_domain_id/records" "{\"record\":{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"content\":\"$txtvalue\",\"ttl\":60}}"; then
  48. if printf -- "%s" "$response" | grep "$_sub_domain" >/dev/null; then
  49. _info "Added, OK"
  50. return 0
  51. else
  52. _err "Add txt record error."
  53. return 1
  54. fi
  55. fi
  56. _err "Add txt record error."
  57. return 1
  58. }
  59. #fulldomain txtvalue
  60. dns_neodigit_rm() {
  61. fulldomain=$1
  62. txtvalue=$2
  63. NEODIGIT_API_TOKEN="${NEODIGIT_API_TOKEN:-$(_readaccountconf_mutable NEODIGIT_API_TOKEN)}"
  64. if [ -z "$NEODIGIT_API_TOKEN" ]; then
  65. NEODIGIT_API_TOKEN=""
  66. _err "You haven't specified a Token api key."
  67. _err "Please create the key and try again."
  68. return 1
  69. fi
  70. #save the api key and email to the account conf file.
  71. _saveaccountconf_mutable NEODIGIT_API_TOKEN "$NEODIGIT_API_TOKEN"
  72. _debug "First detect the root zone"
  73. if ! _get_root "$fulldomain"; then
  74. _err "invalid domain"
  75. return 1
  76. fi
  77. _debug _domain_id "$_domain_id"
  78. _debug _sub_domain "$_sub_domain"
  79. _debug _domain "$_domain"
  80. _debug "Getting txt records"
  81. _neo_rest GET "dns/zones/${_domain_id}/records?type=TXT&name=$fulldomain&content=$txtvalue"
  82. if [ "$_code" != "200" ]; then
  83. _err "error retrieving data!"
  84. return 1
  85. fi
  86. record_id=$(echo "$response" | _egrep_o "\"id\":\s*[0-9]+" | _head_n 1 | cut -d: -f2 | cut -d, -f1)
  87. _debug "record_id" "$record_id"
  88. if [ -z "$record_id" ]; then
  89. _err "Can not get record id to remove."
  90. return 1
  91. fi
  92. if ! _neo_rest DELETE "dns/zones/$_domain_id/records/$record_id"; then
  93. _err "Delete record error."
  94. return 1
  95. fi
  96. }
  97. #################### Private functions below ##################################
  98. #_acme-challenge.www.domain.com
  99. #returns
  100. # _sub_domain=_acme-challenge.www
  101. # _domain=domain.com
  102. # _domain_id=dasfdsafsadg5ythd
  103. _get_root() {
  104. domain=$1
  105. i=2
  106. p=1
  107. while true; do
  108. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  109. _debug h "$h"
  110. if [ -z "$h" ]; then
  111. #not valid
  112. return 1
  113. fi
  114. if ! _neo_rest GET "dns/zones?name=$h"; then
  115. return 1
  116. fi
  117. _debug p "$p"
  118. if _contains "$response" "\"name\":\"$h\"" >/dev/null; then
  119. _domain_id=$(echo "$response" | _egrep_o "\"id\":\s*[0-9]+" | _head_n 1 | cut -d: -f2 | cut -d, -f1)
  120. if [ "$_domain_id" ]; then
  121. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  122. _domain=$h
  123. return 0
  124. fi
  125. return 1
  126. fi
  127. p=$i
  128. i=$(_math "$i" + 1)
  129. done
  130. return 1
  131. }
  132. _neo_rest() {
  133. m=$1
  134. ep="$2"
  135. data="$3"
  136. _debug "$ep"
  137. export _H1="X-TCPanel-Token: $NEODIGIT_API_TOKEN"
  138. export _H2="Content-Type: application/json"
  139. if [ "$m" != "GET" ]; then
  140. _debug data "$data"
  141. response="$(_post "$data" "$NEODIGIT_API_URL/$ep" "" "$m")"
  142. else
  143. response="$(_get "$NEODIGIT_API_URL/$ep")"
  144. fi
  145. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  146. if [ "$?" != "0" ]; then
  147. _err "error $ep"
  148. return 1
  149. fi
  150. _debug2 response "$response"
  151. return 0
  152. }