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.

159 lines
3.8 KiB

  1. #!/usr/bin/env sh
  2. # bug reports to support+acmesh@misaka.io
  3. # based on dns_nsone.sh by dev@1e.ca
  4. #
  5. #Misaka_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
  6. #
  7. Misaka_Api="https://dnsapi.misaka.io/dns"
  8. ######## Public functions #####################
  9. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  10. dns_misaka_add() {
  11. fulldomain=$1
  12. txtvalue=$2
  13. if [ -z "$Misaka_Key" ]; then
  14. Misaka_Key=""
  15. _err "You didn't specify misaka.io dns api key yet."
  16. _err "Please create you key and try again."
  17. return 1
  18. fi
  19. #save the api key and email to the account conf file.
  20. _saveaccountconf Misaka_Key "$Misaka_Key"
  21. _debug "checking root zone [$fulldomain]"
  22. if ! _get_root "$fulldomain"; then
  23. _err "invalid domain"
  24. return 1
  25. fi
  26. _debug _sub_domain "$_sub_domain"
  27. _debug _domain "$_domain"
  28. _debug "Getting txt records"
  29. _misaka_rest GET "zones/${_domain}/recordsets?search=${_sub_domain}"
  30. if ! _contains "$response" "\"results\":"; then
  31. _err "Error"
  32. return 1
  33. fi
  34. count=$(printf "%s\n" "$response" | _egrep_o "\"name\":\"$_sub_domain\",[^{]*\"type\":\"TXT\"" | wc -l | tr -d " ")
  35. _debug count "$count"
  36. if [ "$count" = "0" ]; then
  37. _info "Adding record"
  38. if _misaka_rest PUT "zones/${_domain}/recordsets/${_sub_domain}/TXT" "{\"records\":[{\"value\":\"\\\"$txtvalue\\\"\"}],\"filters\":[],\"ttl\":1}"; then
  39. _debug response "$response"
  40. if _contains "$response" "$_sub_domain"; then
  41. _info "Added"
  42. return 0
  43. else
  44. _err "Add txt record error."
  45. return 1
  46. fi
  47. fi
  48. _err "Add txt record error."
  49. else
  50. _info "Updating record"
  51. _misaka_rest POST "zones/${_domain}/recordsets/${_sub_domain}/TXT?append=true" "{\"records\": [{\"value\": \"\\\"$txtvalue\\\"\"}],\"ttl\":1}"
  52. if [ "$?" = "0" ] && _contains "$response" "$_sub_domain"; then
  53. _info "Updated!"
  54. #todo: check if the record takes effect
  55. return 0
  56. fi
  57. _err "Update error"
  58. return 1
  59. fi
  60. }
  61. #fulldomain
  62. dns_misaka_rm() {
  63. fulldomain=$1
  64. txtvalue=$2
  65. _debug "First detect the root zone"
  66. if ! _get_root "$fulldomain"; then
  67. _err "invalid domain"
  68. return 1
  69. fi
  70. _debug _sub_domain "$_sub_domain"
  71. _debug _domain "$_domain"
  72. _debug "Getting txt records"
  73. _misaka_rest GET "zones/${_domain}/recordsets?search=${_sub_domain}"
  74. count=$(printf "%s\n" "$response" | _egrep_o "\"name\":\"$_sub_domain\",[^{]*\"type\":\"TXT\"" | wc -l | tr -d " ")
  75. _debug count "$count"
  76. if [ "$count" = "0" ]; then
  77. _info "Don't need to remove."
  78. else
  79. if ! _misaka_rest DELETE "zones/${_domain}/recordsets/${_sub_domain}/TXT"; then
  80. _err "Delete record error."
  81. return 1
  82. fi
  83. _contains "$response" ""
  84. fi
  85. }
  86. #################### Private functions below ##################################
  87. #_acme-challenge.www.domain.com
  88. #returns
  89. # _sub_domain=_acme-challenge.www
  90. # _domain=domain.com
  91. # _domain_id=sdjkglgdfewsdfg
  92. _get_root() {
  93. domain=$1
  94. i=2
  95. p=1
  96. if ! _misaka_rest GET "zones?limit=1000"; then
  97. return 1
  98. fi
  99. while true; do
  100. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  101. _debug h "$h"
  102. if [ -z "$h" ]; then
  103. #not valid
  104. return 1
  105. fi
  106. if _contains "$response" "\"name\":\"$h\""; then
  107. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  108. _domain="$h"
  109. return 0
  110. fi
  111. p=$i
  112. i=$(_math "$i" + 1)
  113. done
  114. return 1
  115. }
  116. _misaka_rest() {
  117. m=$1
  118. ep="$2"
  119. data="$3"
  120. _debug "$ep"
  121. export _H1="Content-Type: application/json"
  122. export _H2="User-Agent: acme.sh/$VER misaka-dns-acmesh/20191213"
  123. export _H3="Authorization: Token $Misaka_Key"
  124. if [ "$m" != "GET" ]; then
  125. _debug data "$data"
  126. response="$(_post "$data" "$Misaka_Api/$ep" "" "$m")"
  127. else
  128. response="$(_get "$Misaka_Api/$ep")"
  129. fi
  130. if [ "$?" != "0" ]; then
  131. _err "error $ep"
  132. return 1
  133. fi
  134. _debug2 response "$response"
  135. return 0
  136. }