adblock.lua 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. -- Copyright 2017-2018 Dirk Brenken (dev@brenken.org)
  2. -- This is free software, licensed under the Apache License, Version 2.0
  3. module("luci.controller.adblock", package.seeall)
  4. local sys = require("luci.sys")
  5. local util = require("luci.util")
  6. local http = require("luci.http")
  7. local i18n = require("luci.i18n")
  8. local json = require("luci.jsonc")
  9. local uci = require("luci.model.uci").cursor()
  10. function index()
  11. if not nixio.fs.access("/etc/config/adblock") then
  12. return
  13. end
  14. entry({"admin", "services", "adblock"}, firstchild(), _("Adblock"), 30).dependent = false
  15. entry({"admin", "services", "adblock", "tab_from_cbi"}, cbi("adblock/overview_tab", {hideresetbtn=true, hidesavebtn=true}), _("Overview"), 10).leaf = true
  16. entry({"admin", "services", "adblock", "log"}, template("adblock/logread"), _("View Logfile"), 20).leaf = true
  17. entry({"admin", "services", "adblock", "advanced"}, firstchild(), _("Advanced"), 100)
  18. entry({"admin", "services", "adblock", "advanced", "blacklist"}, form("adblock/blacklist_tab"), _("Edit Blacklist"), 110).leaf = true
  19. entry({"admin", "services", "adblock", "advanced", "whitelist"}, form("adblock/whitelist_tab"), _("Edit Whitelist"), 120).leaf = true
  20. entry({"admin", "services", "adblock", "advanced", "configuration"}, form("adblock/configuration_tab"), _("Edit Configuration"), 130).leaf = true
  21. entry({"admin", "services", "adblock", "advanced", "query"}, template("adblock/query"), _("Query domains"), 140).leaf = true
  22. entry({"admin", "services", "adblock", "advanced", "result"}, call("queryData"), nil, 150).leaf = true
  23. entry({"admin", "services", "adblock", "logread"}, call("logread"), nil).leaf = true
  24. entry({"admin", "services", "adblock", "status"}, call("status_update"), nil).leaf = true
  25. entry({"admin", "services", "adblock", "action"}, call("adb_action"), nil).leaf = true
  26. end
  27. function adb_action(name)
  28. if name == "do_suspend" then
  29. luci.sys.call("/etc/init.d/adblock suspend >/dev/null 2>&1")
  30. elseif name == "do_resume" then
  31. luci.sys.call("/etc/init.d/adblock resume >/dev/null 2>&1")
  32. elseif name == "do_refresh" then
  33. luci.sys.call("/etc/init.d/adblock reload >/dev/null 2>&1")
  34. end
  35. luci.http.prepare_content("text/plain")
  36. luci.http.write("0")
  37. end
  38. function status_update()
  39. local rt_file
  40. local content
  41. rt_file = uci:get("adblock", "global", "adb_rtfile") or "/tmp/adb_runtime.json"
  42. if nixio.fs.access(rt_file) then
  43. content = json.parse(nixio.fs.readfile(rt_file) or "")
  44. http.prepare_content("application/json")
  45. http.write_json(content)
  46. end
  47. end
  48. function logread()
  49. local content
  50. if nixio.fs.access("/var/log/messages") then
  51. content = util.trim(util.exec("grep -F 'adblock-' /var/log/messages"))
  52. else
  53. content = util.trim(util.exec("logread -e 'adblock-'"))
  54. end
  55. if content == "" then
  56. content = "No adblock related logs yet!"
  57. end
  58. http.write(content)
  59. end
  60. function queryData(domain)
  61. if domain then
  62. luci.http.prepare_content("text/plain")
  63. local cmd = "/etc/init.d/adblock query %s 2>&1"
  64. local util = io.popen(cmd % util.shellquote(domain))
  65. if util then
  66. while true do
  67. local line = util:read("*l")
  68. if not line then
  69. break
  70. end
  71. luci.http.write(line)
  72. luci.http.write("\n")
  73. end
  74. util:close()
  75. end
  76. end
  77. end