1 -- Copyright 2017-2018 Dirk Brenken (dev@brenken.org)
2 -- This is free software, licensed under the Apache License, Version 2.0
4 module("luci.controller.adblock", package.seeall)
6 local sys = require("luci.sys")
7 local util = require("luci.util")
8 local http = require("luci.http")
9 local templ = require("luci.template")
10 local i18n = require("luci.i18n")
11 local json = require("luci.jsonc")
12 local uci = require("luci.model.uci").cursor()
13 local fs = require("nixio.fs")
16 if not nixio.fs.access("/etc/config/adblock") then
19 entry({"admin", "services", "adblock"}, firstchild(), _("Adblock"), 30).dependent = false
20 entry({"admin", "services", "adblock", "tab_from_cbi"}, cbi("adblock/overview_tab", {hideresetbtn=true, hidesavebtn=true}), _("Overview"), 10).leaf = true
21 entry({"admin", "services", "adblock", "log"}, template("adblock/logread"), _("View Logfile"), 20).leaf = true
22 entry({"admin", "services", "adblock", "advanced"}, firstchild(), _("Advanced"), 100)
23 entry({"admin", "services", "adblock", "advanced", "blacklist"}, form("adblock/blacklist_tab"), _("Edit Blacklist"), 110).leaf = true
24 entry({"admin", "services", "adblock", "advanced", "whitelist"}, form("adblock/whitelist_tab"), _("Edit Whitelist"), 120).leaf = true
25 entry({"admin", "services", "adblock", "advanced", "configuration"}, form("adblock/configuration_tab"), _("Edit Configuration"), 130).leaf = true
26 entry({"admin", "services", "adblock", "advanced", "query"}, template("adblock/query"), _("Query domains"), 140).leaf = true
27 entry({"admin", "services", "adblock", "advanced", "result"}, call("queryData"), nil, 150).leaf = true
28 entry({"admin", "services", "adblock", "logread"}, call("logread"), nil).leaf = true
29 entry({"admin", "services", "adblock", "status"}, call("status_update"), nil).leaf = true
30 entry({"admin", "services", "adblock", "action"}, call("adb_action"), nil).leaf = true
33 function adb_action(value)
34 if value == "Suspend" then
35 luci.sys.call("/etc/init.d/adblock suspend >/dev/null 2>&1")
36 elseif value == "Resume" then
37 luci.sys.call("/etc/init.d/adblock resume >/dev/null 2>&1")
38 elseif value == "Refresh" then
39 luci.sys.call("/etc/init.d/adblock reload >/dev/null 2>&1")
41 luci.http.prepare_content("text/plain")
45 function status_update()
49 rt_file = uci:get("adblock", "global", "adb_rtfile") or "/tmp/adb_runtime.json"
51 if fs.access(rt_file) then
52 content = json.parse(fs.readfile(rt_file))
54 http.prepare_content("application/json")
55 http.write_json(content)
63 if nixio.fs.access("/var/log/messages") then
64 content = util.trim(util.exec("grep -F 'adblock-' /var/log/messages"))
66 content = util.trim(util.exec("logread -e 'adblock-'"))
70 content = "No adblock related logs yet!"
75 function queryData(domain)
77 luci.http.prepare_content("text/plain")
78 local cmd = "/etc/init.d/adblock query %s 2>&1"
79 local util = io.popen(cmd % util.shellquote(domain))
82 local line = util:read("*l")