dns_neodigit.sh 4.4 KB

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