AdGuardHome.lua 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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"},firstchild(),_("AdGuard Home"),30).dependent=true
  6. entry({"admin","services","AdGuardHome","general"},cbi("AdGuardHome/base"),_("Base Setting"),1).leaf = true
  7. entry({"admin","services","AdGuardHome","log"},form("AdGuardHome/log"),_("Log"),2).leaf = true
  8. entry({"admin","services","AdGuardHome","manual"},cbi("AdGuardHome/manual"),_("Manual Config"),3).leaf = true
  9. entry({"admin","services","AdGuardHome","status"},call("act_status")).leaf=true
  10. entry({"admin", "services", "AdGuardHome", "check"}, call("check_update"))
  11. entry({"admin", "services", "AdGuardHome", "doupdate"}, call("do_update"))
  12. end
  13. function act_status()
  14. local e={}
  15. binpath=uci:get("AdGuardHome","AdGuardHome","binpath")
  16. e.running=luci.sys.call("pgrep "..binpath.." >/dev/null")==0
  17. luci.http.prepare_content("application/json")
  18. luci.http.write_json(e)
  19. end
  20. function do_update()
  21. nixio.fs.writefile("/var/run/lucilogpos","0")
  22. luci.sys.exec("(touch /var/run/update_core ; sh /usr/share/AdGuardHome/update_core.sh >/tmp/AdGuardHome_update.log;rm /var/run/update_core) &")
  23. luci.http.prepare_content("application/json")
  24. luci.http.write('')
  25. end
  26. function check_update()
  27. luci.http.prepare_content("text/plain; charset=utf-8")
  28. logpos=nixio.fs.readfile("/var/run/lucilogpos")
  29. if (logpos ~= nil) then
  30. fdp=tonumber(logpos)
  31. else
  32. fdp=0
  33. end
  34. f=io.open("/tmp/AdGuardHome_update.log", "r+")
  35. f:seek("set",fdp)
  36. a=f:read(8192)
  37. if (a==nil) then
  38. a=""
  39. end
  40. fdp=f:seek()
  41. nixio.fs.writefile("/var/run/lucilogpos",tostring(fdp))
  42. f:close()
  43. if nixio.fs.access("/var/run/update_core") then
  44. luci.http.write(a)
  45. else
  46. luci.http.write(a.."\0")
  47. end
  48. end