AdGuardHome.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. module("luci.controller.AdGuardHome",package.seeall)
  2. local fs=require"nixio.fs"
  3. local http=require"luci.http"
  4. local uci=require"luci.model.uci".cursor()
  5. function index()
  6. entry({"admin", "services", "AdGuardHome"},alias("admin", "services", "AdGuardHome", "base"),_("AdGuard Home"), 10).dependent = true
  7. entry({"admin","services","AdGuardHome","base"},cbi("AdGuardHome/base"),_("Base Setting"),1).leaf = true
  8. entry({"admin","services","AdGuardHome","log"},form("AdGuardHome/log"),_("Log"),2).leaf = true
  9. entry({"admin","services","AdGuardHome","manual"},cbi("AdGuardHome/manual"),_("Manual Config"),3).leaf = true
  10. entry({"admin","services","AdGuardHome","status"},call("act_status")).leaf=true
  11. entry({"admin", "services", "AdGuardHome", "check"}, call("check_update"))
  12. entry({"admin", "services", "AdGuardHome", "doupdate"}, call("do_update"))
  13. entry({"admin", "services", "AdGuardHome", "getlog"}, call("get_log"))
  14. entry({"admin", "services", "AdGuardHome", "dodellog"}, call("do_dellog"))
  15. entry({"admin", "services", "AdGuardHome", "reloadconfig"}, call("reload_config"))
  16. entry({"admin", "services", "AdGuardHome", "gettemplateconfig"}, call("get_template_config"))
  17. end
  18. function get_template_config()
  19. local b
  20. local d=""
  21. for cnt in io.lines("/tmp/resolv.conf.auto") do
  22. b=string.match (cnt,"^[^#]*nameserver%s+([^%s]+)$")
  23. if (b~=nil) then
  24. d=d.." - "..b.."\n"
  25. end
  26. end
  27. local f=io.open("/usr/share/AdGuardHome/AdGuardHome_template.yaml", "r+")
  28. local tbl = {}
  29. local a=""
  30. while (1) do
  31. a=f:read("*l")
  32. if (a=="#bootstrap_dns") then
  33. a=d
  34. elseif (a=="#upstream_dns") then
  35. a=d
  36. elseif (a==nil) then
  37. break
  38. end
  39. table.insert(tbl, a)
  40. end
  41. f:close()
  42. http.prepare_content("text/plain; charset=utf-8")
  43. http.write(table.concat(tbl, "\n"))
  44. end
  45. function reload_config()
  46. fs.remove("/tmp/AdGuardHometmpconfig.yaml")
  47. http.prepare_content("application/json")
  48. http.write('')
  49. end
  50. function act_status()
  51. local e={}
  52. local binpath=uci:get("AdGuardHome","AdGuardHome","binpath")
  53. e.running=luci.sys.call("pgrep "..binpath.." >/dev/null")==0
  54. e.redirect=(fs.readfile("/var/run/AdGredir")=="1")
  55. http.prepare_content("application/json")
  56. http.write_json(e)
  57. end
  58. function do_update()
  59. fs.writefile("/var/run/lucilogpos","0")
  60. http.prepare_content("application/json")
  61. http.write('')
  62. local arg
  63. if luci.http.formvalue("force") == "1" then
  64. arg="force"
  65. else
  66. arg=""
  67. end
  68. if fs.access("/var/run/update_core") then
  69. if arg=="force" then
  70. luci.sys.exec("kill $(pgrep /usr/share/AdGuardHome/update_core.sh) ; sh /usr/share/AdGuardHome/update_core.sh "..arg.." >/tmp/AdGuardHome_update.log 2>&1 &")
  71. end
  72. else
  73. luci.sys.exec("sh /usr/share/AdGuardHome/update_core.sh "..arg.." >/tmp/AdGuardHome_update.log 2>&1 &")
  74. end
  75. end
  76. function get_log()
  77. local logfile=uci:get("AdGuardHome","AdGuardHome","logfile")
  78. if (logfile==nil) then
  79. http.write("no log available\n")
  80. return
  81. elseif (logfile=="syslog") then
  82. if not fs.access("/var/run/AdGuardHomesyslog") then
  83. luci.sys.exec("(/usr/share/AdGuardHome/getsyslog.sh &); sleep 1;")
  84. end
  85. logfile="/tmp/AdGuardHometmp.log"
  86. fs.writefile("/var/run/AdGuardHomesyslog","1")
  87. elseif not fs.access(logfile) then
  88. http.write("")
  89. return
  90. end
  91. http.prepare_content("text/plain; charset=utf-8")
  92. local fdp
  93. if fs.access("/var/run/lucilogreload") then
  94. fdp=0
  95. fs.remove("/var/run/lucilogreload")
  96. else
  97. fdp=tonumber(fs.readfile("/var/run/lucilogpos")) or 0
  98. end
  99. local f=io.open(logfile, "r+")
  100. f:seek("set",fdp)
  101. local a=f:read(2048000) or ""
  102. fdp=f:seek()
  103. fs.writefile("/var/run/lucilogpos",tostring(fdp))
  104. f:close()
  105. http.write(a)
  106. end
  107. function do_dellog()
  108. local logfile=uci:get("AdGuardHome","AdGuardHome","logfile")
  109. fs.writefile(logfile,"")
  110. http.prepare_content("application/json")
  111. http.write('')
  112. end
  113. function check_update()
  114. http.prepare_content("text/plain; charset=utf-8")
  115. local fdp=tonumber(fs.readfile("/var/run/lucilogpos")) or 0
  116. local f=io.open("/tmp/AdGuardHome_update.log", "r+")
  117. f:seek("set",fdp)
  118. local a=f:read(2048000) or ""
  119. fdp=f:seek()
  120. fs.writefile("/var/run/lucilogpos",tostring(fdp))
  121. f:close()
  122. if fs.access("/var/run/update_core") then
  123. http.write(a)
  124. else
  125. http.write(a.."\0")
  126. end
  127. end