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.

157 lines
4.0 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
2 months ago
8 years ago
8 years ago
2 months ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_lua_info='LuaDNS.com
  4. Domains: LuaDNS.net
  5. Site: LuaDNS.com
  6. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_lua
  7. Options:
  8. LUA_Key API key
  9. LUA_Email Email
  10. Author: <dev@1e.ca>
  11. '
  12. LUA_Api="https://api.luadns.com/v1"
  13. ######## Public functions #####################
  14. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  15. dns_lua_add() {
  16. fulldomain=$1
  17. txtvalue=$2
  18. LUA_Key="${LUA_Key:-$(_readaccountconf_mutable LUA_Key)}"
  19. LUA_Email="${LUA_Email:-$(_readaccountconf_mutable LUA_Email)}"
  20. LUA_auth=$(printf "%s" "$LUA_Email:$LUA_Key" | _base64)
  21. if [ -z "$LUA_Key" ] || [ -z "$LUA_Email" ]; then
  22. LUA_Key=""
  23. LUA_Email=""
  24. _err "You don't specify luadns api key and email yet."
  25. _err "Please create you key and try again."
  26. return 1
  27. fi
  28. #save the api key and email to the account conf file.
  29. _saveaccountconf_mutable LUA_Key "$LUA_Key"
  30. _saveaccountconf_mutable LUA_Email "$LUA_Email"
  31. _debug "First detect the root zone"
  32. if ! _get_root "$fulldomain"; then
  33. _err "invalid domain"
  34. return 1
  35. fi
  36. _debug _domain_id "$_domain_id"
  37. _debug _sub_domain "$_sub_domain"
  38. _debug _domain "$_domain"
  39. _info "Adding record"
  40. if _LUA_rest POST "zones/$_domain_id/records" "{\"type\":\"TXT\",\"name\":\"$fulldomain.\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
  41. if _contains "$response" "$fulldomain"; then
  42. _info "Added"
  43. #todo: check if the record takes effect
  44. return 0
  45. else
  46. _err "Add txt record error."
  47. return 1
  48. fi
  49. fi
  50. }
  51. #fulldomain
  52. dns_lua_rm() {
  53. fulldomain=$1
  54. txtvalue=$2
  55. LUA_Key="${LUA_Key:-$(_readaccountconf_mutable LUA_Key)}"
  56. LUA_Email="${LUA_Email:-$(_readaccountconf_mutable LUA_Email)}"
  57. LUA_auth=$(printf "%s" "$LUA_Email:$LUA_Key" | _base64)
  58. _debug "First detect the root zone"
  59. if ! _get_root "$fulldomain"; then
  60. _err "invalid domain"
  61. return 1
  62. fi
  63. _debug _domain_id "$_domain_id"
  64. _debug _sub_domain "$_sub_domain"
  65. _debug _domain "$_domain"
  66. _debug "Getting txt records"
  67. _LUA_rest GET "zones/${_domain_id}/records"
  68. count=$(printf "%s\n" "$response" | _egrep_o "\"name\":\"$fulldomain.\",\"type\":\"TXT\"" | wc -l | tr -d " ")
  69. _debug count "$count"
  70. if [ "$count" = "0" ]; then
  71. _info "Don't need to remove."
  72. else
  73. record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*,\"name\":\"$fulldomain.\",\"type\":\"TXT\"" | _head_n 1 | cut -d: -f2 | cut -d, -f1)
  74. _debug "record_id" "$record_id"
  75. if [ -z "$record_id" ]; then
  76. _err "Can not get record id to remove."
  77. return 1
  78. fi
  79. if ! _LUA_rest DELETE "/zones/$_domain_id/records/$record_id"; then
  80. _err "Delete record error."
  81. return 1
  82. fi
  83. _contains "$response" "$record_id"
  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 ! _LUA_rest GET "zones"; 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. _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*,\"name\":\"$h\"" | cut -d : -f 2 | cut -d , -f 1)
  108. _debug _domain_id "$_domain_id"
  109. if [ "$_domain_id" ]; then
  110. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  111. _domain="$h"
  112. return 0
  113. fi
  114. return 1
  115. fi
  116. p=$i
  117. i=$(_math "$i" + 1)
  118. done
  119. return 1
  120. }
  121. _LUA_rest() {
  122. m=$1
  123. ep="$2"
  124. data="$3"
  125. _debug "$ep"
  126. export _H1="Accept: application/json"
  127. export _H2="Authorization: Basic $LUA_auth"
  128. if [ "$m" != "GET" ]; then
  129. _debug data "$data"
  130. response="$(_post "$data" "$LUA_Api/$ep" "" "$m")"
  131. else
  132. response="$(_get "$LUA_Api/$ep")"
  133. fi
  134. if [ "$?" != "0" ]; then
  135. _err "error $ep"
  136. return 1
  137. fi
  138. _debug2 response "$response"
  139. return 0
  140. }