dns_lua.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/env sh
  2. # bug reports to dev@1e.ca
  3. #
  4. #LUA_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
  5. #
  6. #LUA_Email="user@luadns.net"
  7. LUA_Api="https://api.luadns.com/v1"
  8. ######## Public functions #####################
  9. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  10. dns_lua_add() {
  11. fulldomain=$1
  12. txtvalue=$2
  13. LUA_Key="${LUA_Key:-$(_readaccountconf_mutable LUA_Key)}"
  14. LUA_Email="${LUA_Email:-$(_readaccountconf_mutable LUA_Email)}"
  15. LUA_auth=$(printf "%s" "$LUA_Email:$LUA_Key" | _base64)
  16. if [ -z "$LUA_Key" ] || [ -z "$LUA_Email" ]; then
  17. LUA_Key=""
  18. LUA_Email=""
  19. _err "You don't specify luadns api key and email 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_mutable LUA_Key "$LUA_Key"
  25. _saveaccountconf_mutable LUA_Email "$LUA_Email"
  26. _debug "First detect the root zone"
  27. if ! _get_root "$fulldomain"; then
  28. _err "invalid domain"
  29. return 1
  30. fi
  31. _debug _domain_id "$_domain_id"
  32. _debug _sub_domain "$_sub_domain"
  33. _debug _domain "$_domain"
  34. _info "Adding record"
  35. if _LUA_rest POST "zones/$_domain_id/records" "{\"type\":\"TXT\",\"name\":\"$fulldomain.\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
  36. if _contains "$response" "$fulldomain"; then
  37. _info "Added"
  38. #todo: check if the record takes effect
  39. return 0
  40. else
  41. _err "Add txt record error."
  42. return 1
  43. fi
  44. fi
  45. }
  46. #fulldomain
  47. dns_lua_rm() {
  48. fulldomain=$1
  49. txtvalue=$2
  50. LUA_Key="${LUA_Key:-$(_readaccountconf_mutable LUA_Key)}"
  51. LUA_Email="${LUA_Email:-$(_readaccountconf_mutable LUA_Email)}"
  52. LUA_auth=$(printf "%s" "$LUA_Email:$LUA_Key" | _base64)
  53. _debug "First detect the root zone"
  54. if ! _get_root "$fulldomain"; then
  55. _err "invalid domain"
  56. return 1
  57. fi
  58. _debug _domain_id "$_domain_id"
  59. _debug _sub_domain "$_sub_domain"
  60. _debug _domain "$_domain"
  61. _debug "Getting txt records"
  62. _LUA_rest GET "zones/${_domain_id}/records"
  63. count=$(printf "%s\n" "$response" | _egrep_o "\"name\":\"$fulldomain.\",\"type\":\"TXT\"" | wc -l | tr -d " ")
  64. _debug count "$count"
  65. if [ "$count" = "0" ]; then
  66. _info "Don't need to remove."
  67. else
  68. record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*,\"name\":\"$fulldomain.\",\"type\":\"TXT\"" | _head_n 1 | cut -d: -f2 | cut -d, -f1)
  69. _debug "record_id" "$record_id"
  70. if [ -z "$record_id" ]; then
  71. _err "Can not get record id to remove."
  72. return 1
  73. fi
  74. if ! _LUA_rest DELETE "/zones/$_domain_id/records/$record_id"; then
  75. _err "Delete record error."
  76. return 1
  77. fi
  78. _contains "$response" "$record_id"
  79. fi
  80. }
  81. #################### Private functions below ##################################
  82. #_acme-challenge.www.domain.com
  83. #returns
  84. # _sub_domain=_acme-challenge.www
  85. # _domain=domain.com
  86. # _domain_id=sdjkglgdfewsdfg
  87. _get_root() {
  88. domain=$1
  89. i=2
  90. p=1
  91. if ! _LUA_rest GET "zones"; then
  92. return 1
  93. fi
  94. while true; do
  95. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  96. _debug h "$h"
  97. if [ -z "$h" ]; then
  98. #not valid
  99. return 1
  100. fi
  101. if _contains "$response" "\"name\":\"$h\""; then
  102. _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*,\"name\":\"$h\"" | cut -d : -f 2 | cut -d , -f 1)
  103. _debug _domain_id "$_domain_id"
  104. if [ "$_domain_id" ]; then
  105. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  106. _domain="$h"
  107. return 0
  108. fi
  109. return 1
  110. fi
  111. p=$i
  112. i=$(_math "$i" + 1)
  113. done
  114. return 1
  115. }
  116. _LUA_rest() {
  117. m=$1
  118. ep="$2"
  119. data="$3"
  120. _debug "$ep"
  121. export _H1="Accept: application/json"
  122. export _H2="Authorization: Basic $LUA_auth"
  123. if [ "$m" != "GET" ]; then
  124. _debug data "$data"
  125. response="$(_post "$data" "$LUA_Api/$ep" "" "$m")"
  126. else
  127. response="$(_get "$LUA_Api/$ep")"
  128. fi
  129. if [ "$?" != "0" ]; then
  130. _err "error $ep"
  131. return 1
  132. fi
  133. _debug2 response "$response"
  134. return 0
  135. }