1
0

adblock.lua 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. -- Copyright 2017 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 fs = require("nixio.fs")
  5. local util = require("luci.util")
  6. local templ = require("luci.template")
  7. local i18n = require("luci.i18n")
  8. function index()
  9. if not nixio.fs.access("/etc/config/adblock") then
  10. return
  11. end
  12. entry({"admin", "services", "adblock"}, firstchild(), _("Adblock"), 30).dependent = false
  13. entry({"admin", "services", "adblock", "tab_from_cbi"}, cbi("adblock/overview_tab", {hideresetbtn=true, hidesavebtn=true}), _("Overview"), 10).leaf = true
  14. entry({"admin", "services", "adblock", "logfile"}, call("logread"), _("View Logfile"), 20).leaf = true
  15. entry({"admin", "services", "adblock", "advanced"}, firstchild(), _("Advanced"), 100)
  16. entry({"admin", "services", "adblock", "advanced", "blacklist"}, cbi("adblock/blacklist_tab"), _("Edit Blacklist"), 110).leaf = true
  17. entry({"admin", "services", "adblock", "advanced", "whitelist"}, cbi("adblock/whitelist_tab"), _("Edit Whitelist"), 120).leaf = true
  18. entry({"admin", "services", "adblock", "advanced", "configuration"}, cbi("adblock/configuration_tab"), _("Edit Configuration"), 130).leaf = true
  19. entry({"admin", "services", "adblock", "advanced", "query"}, template("adblock/query"), _("Query domains"), 140).leaf = true
  20. entry({"admin", "services", "adblock", "advanced", "result"}, call("queryData"), nil, 150).leaf = true
  21. end
  22. function logread()
  23. local logfile
  24. if nixio.fs.access("/var/log/messages") then
  25. logfile = util.trim(util.exec("cat /var/log/messages | grep 'adblock'"))
  26. else
  27. logfile = util.trim(util.exec("logread -e 'adblock'"))
  28. end
  29. templ.render("adblock/logread", {title = i18n.translate("Adblock Logfile"), content = logfile})
  30. end
  31. function queryData(domain)
  32. if domain and domain:match("^[a-zA-Z0-9%-%._]+$") then
  33. luci.http.prepare_content("text/plain")
  34. local cmd = "/etc/init.d/adblock query %q 2>&1"
  35. local util = io.popen(cmd % domain)
  36. if util then
  37. while true do
  38. local line = util:read("*l")
  39. if not line then
  40. break
  41. end
  42. luci.http.write(line)
  43. luci.http.write("\n")
  44. end
  45. util:close()
  46. end
  47. end
  48. end