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.

160 lines
3.9 KiB

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