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.

161 lines
4.0 KiB

7 years ago
7 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_nsone_info='ns1.com
  4. Domains: ns1.net
  5. Site: ns1.com
  6. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_nsone
  7. Options:
  8. NS1_Key API Key
  9. Author: <dev@1e.ca>
  10. '
  11. NS1_Api="https://api.nsone.net/v1"
  12. ######## Public functions #####################
  13. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_nsone_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. if [ -z "$NS1_Key" ]; then
  18. NS1_Key=""
  19. _err "You didn't specify nsone dns api key yet."
  20. _err "Please create you key and try again."
  21. return 1
  22. fi
  23. #save the api key and email to the account conf file.
  24. _saveaccountconf NS1_Key "$NS1_Key"
  25. _debug "First detect the root zone"
  26. if ! _get_root "$fulldomain"; then
  27. _err "invalid domain"
  28. return 1
  29. fi
  30. _debug _sub_domain "$_sub_domain"
  31. _debug _domain "$_domain"
  32. _debug "Getting txt records"
  33. _nsone_rest GET "zones/${_domain}"
  34. if ! _contains "$response" "\"records\":"; then
  35. _err "Error"
  36. return 1
  37. fi
  38. count=$(printf "%s\n" "$response" | _egrep_o "\"domain\":\"$fulldomain\",[^{]*\"type\":\"TXT\"" | wc -l | tr -d " ")
  39. _debug count "$count"
  40. if [ "$count" = "0" ]; then
  41. _info "Adding record"
  42. if _nsone_rest PUT "zones/$_domain/$fulldomain/TXT" "{\"answers\":[{\"answer\":[\"$txtvalue\"]}],\"type\":\"TXT\",\"domain\":\"$fulldomain\",\"zone\":\"$_domain\",\"ttl\":0}"; then
  43. if _contains "$response" "$fulldomain"; then
  44. _info "Added"
  45. #todo: check if the record takes effect
  46. return 0
  47. else
  48. _err "Add txt record error."
  49. return 1
  50. fi
  51. fi
  52. _err "Add txt record error."
  53. else
  54. _info "Updating record"
  55. prev_txt=$(printf "%s\n" "$response" | _egrep_o "\"domain\":\"$fulldomain\",\"short_answers\":\[\"[^,]*\]" | _head_n 1 | cut -d: -f3 | cut -d, -f1)
  56. _debug "prev_txt" "$prev_txt"
  57. _nsone_rest POST "zones/$_domain/$fulldomain/TXT" "{\"answers\": [{\"answer\": [\"$txtvalue\"]},{\"answer\": $prev_txt}],\"type\": \"TXT\",\"domain\":\"$fulldomain\",\"zone\": \"$_domain\",\"ttl\":0}"
  58. if [ "$?" = "0" ] && _contains "$response" "$fulldomain"; then
  59. _info "Updated!"
  60. #todo: check if the record takes effect
  61. return 0
  62. fi
  63. _err "Update error"
  64. return 1
  65. fi
  66. }
  67. #fulldomain
  68. dns_nsone_rm() {
  69. fulldomain=$1
  70. txtvalue=$2
  71. _debug "First detect the root zone"
  72. if ! _get_root "$fulldomain"; then
  73. _err "invalid domain"
  74. return 1
  75. fi
  76. _debug _sub_domain "$_sub_domain"
  77. _debug _domain "$_domain"
  78. _debug "Getting txt records"
  79. _nsone_rest GET "zones/${_domain}/$fulldomain/TXT"
  80. count=$(printf "%s\n" "$response" | _egrep_o "\"domain\":\"$fulldomain\",.*\"type\":\"TXT\"" | wc -l | tr -d " ")
  81. _debug count "$count"
  82. if [ "$count" = "0" ]; then
  83. _info "Don't need to remove."
  84. else
  85. if ! _nsone_rest DELETE "zones/${_domain}/$fulldomain/TXT"; then
  86. _err "Delete record error."
  87. return 1
  88. fi
  89. _contains "$response" ""
  90. fi
  91. }
  92. #################### Private functions below ##################################
  93. #_acme-challenge.www.domain.com
  94. #returns
  95. # _sub_domain=_acme-challenge.www
  96. # _domain=domain.com
  97. # _domain_id=sdjkglgdfewsdfg
  98. _get_root() {
  99. domain=$1
  100. i=2
  101. p=1
  102. if ! _nsone_rest GET "zones"; then
  103. return 1
  104. fi
  105. while true; do
  106. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  107. _debug h "$h"
  108. if [ -z "$h" ]; then
  109. #not valid
  110. return 1
  111. fi
  112. if _contains "$response" "\"zone\":\"$h\""; then
  113. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  114. _domain="$h"
  115. return 0
  116. fi
  117. p=$i
  118. i=$(_math "$i" + 1)
  119. done
  120. return 1
  121. }
  122. _nsone_rest() {
  123. m=$1
  124. ep="$2"
  125. data="$3"
  126. _debug "$ep"
  127. export _H1="Accept: application/json"
  128. export _H2="X-NSONE-Key: $NS1_Key"
  129. if [ "$m" != "GET" ]; then
  130. _debug data "$data"
  131. response="$(_post "$data" "$NS1_Api/$ep" "" "$m")"
  132. else
  133. response="$(_get "$NS1_Api/$ep")"
  134. fi
  135. if [ "$?" != "0" ]; then
  136. _err "error $ep"
  137. return 1
  138. fi
  139. _debug2 response "$response"
  140. return 0
  141. }