AdGuardHome.lua 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. module("luci.controller.AdGuardHome",package.seeall)
  2. nixio=require"nixio"
  3. local uci=require"luci.model.uci".cursor()
  4. function index()
  5. entry({"admin", "services", "AdGuardHome"},
  6. alias("admin", "services", "AdGuardHome", "base"),
  7. _("AdGuard Home"), 10).dependent = true
  8. entry({"admin","services","AdGuardHome","base"},cbi("AdGuardHome/base"),_("Base Setting"),1).leaf = true
  9. entry({"admin","services","AdGuardHome","log"},form("AdGuardHome/log"),_("Log"),2).leaf = true
  10. entry({"admin","services","AdGuardHome","manual"},cbi("AdGuardHome/manual"),_("Manual Config"),3).leaf = true
  11. entry({"admin","services","AdGuardHome","status"},call("act_status")).leaf=true
  12. entry({"admin", "services", "AdGuardHome", "check"}, call("check_update"))
  13. entry({"admin", "services", "AdGuardHome", "doupdate"}, call("do_update"))
  14. entry({"admin", "services", "AdGuardHome", "getlog"}, call("get_log"))
  15. entry({"admin", "services", "AdGuardHome", "dodellog"}, call("do_dellog"))
  16. end
  17. function act_status()
  18. local e={}
  19. binpath=uci:get("AdGuardHome","AdGuardHome","binpath")
  20. e.running=luci.sys.call("pgrep "..binpath.." >/dev/null")==0
  21. luci.http.prepare_content("application/json")
  22. luci.http.write_json(e)
  23. end
  24. function do_update()
  25. nixio.fs.writefile("/var/run/lucilogpos","0")
  26. luci.sys.exec("(touch /var/run/update_core ; sh /usr/share/AdGuardHome/update_core.sh >/tmp/AdGuardHome_update.log;rm /var/run/update_core) &")
  27. luci.http.prepare_content("application/json")
  28. luci.http.write('')
  29. end
  30. function get_log()
  31. local logfile=uci:get("AdGuardHome","AdGuardHome","logfile")
  32. if (logfile==nil) then
  33. luci.http.write("no log available\n")
  34. return
  35. elseif (logfile=="syslog") then
  36. if not nixio.fs.access("/var/run/AdGuardHomesyslog") then
  37. luci.sys.exec("(/usr/share/AdGuardHome/getsyslog.sh &); sleep 1;")
  38. end
  39. logfile="/tmp/AdGuardHometmp.log"
  40. nixio.fs.writefile("/var/run/AdGuardHomesyslog","1")
  41. end
  42. luci.http.prepare_content("text/plain; charset=utf-8")
  43. logpos=nixio.fs.readfile("/var/run/lucilogpos")
  44. if (logpos ~= nil) then
  45. fdp=tonumber(logpos)
  46. else
  47. fdp=0
  48. end
  49. f=io.open(logfile, "r+")
  50. f:seek("set",fdp)
  51. a=f:read(2048000)
  52. if (a==nil) then
  53. a=""
  54. end
  55. fdp=f:seek()
  56. nixio.fs.writefile("/var/run/lucilogpos",tostring(fdp))
  57. f:close()
  58. luci.http.write(a)
  59. end
  60. function do_dellog()
  61. logfile=uci:get("AdGuardHome","AdGuardHome","logfile")
  62. nixio.fs.writefile(logfile,"")
  63. luci.http.prepare_content("application/json")
  64. luci.http.write('')
  65. end
  66. function check_update()
  67. luci.http.prepare_content("text/plain; charset=utf-8")
  68. logpos=nixio.fs.readfile("/var/run/lucilogpos")
  69. if (logpos ~= nil) then
  70. fdp=tonumber(logpos)
  71. else
  72. fdp=0
  73. end
  74. f=io.open("/tmp/AdGuardHome_update.log", "r+")
  75. f:seek("set",fdp)
  76. a=f:read(2048000)
  77. if (a==nil) then
  78. a=""
  79. end
  80. fdp=f:seek()
  81. nixio.fs.writefile("/var/run/lucilogpos",tostring(fdp))
  82. f:close()
  83. if nixio.fs.access("/var/run/update_core") then
  84. luci.http.write(a)
  85. else
  86. luci.http.write(a.."\0")
  87. end
  88. end