1 -- Copyright 2017-2019 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 i18n = require("luci.i18n")
10 local json = require("luci.jsonc")
11 local uci = require("luci.model.uci").cursor()
14 if not nixio.fs.access("/etc/config/adblock") then
17 entry({"admin", "services", "adblock"}, firstchild(), _("Adblock"), 30).dependent = false
18 entry({"admin", "services", "adblock", "tab_from_cbi"}, cbi("adblock/overview_tab", {hideresetbtn=true, hidesavebtn=true}), _("Overview"), 10).leaf = true
19 entry({"admin", "services", "adblock", "report"}, template("adblock/report"), _("DNS Query Report"), 20).leaf = true
20 entry({"admin", "services", "adblock", "log"}, template("adblock/logread"), _("Logfile"), 30).leaf = true
21 entry({"admin", "services", "adblock", "advanced"}, firstchild(), _("Advanced"), 100)
22 entry({"admin", "services", "adblock", "advanced", "blacklist"}, form("adblock/blacklist_tab"), _("Edit Blacklist"), 110).leaf = true
23 entry({"admin", "services", "adblock", "advanced", "whitelist"}, form("adblock/whitelist_tab"), _("Edit Whitelist"), 120).leaf = true
24 entry({"admin", "services", "adblock", "advanced", "configuration"}, form("adblock/configuration_tab"), _("Edit Configuration"), 130).leaf = true
25 entry({"admin", "services", "adblock", "advanced", "query"}, template("adblock/query"), _("Query domains"), 140).leaf = true
26 entry({"admin", "services", "adblock", "advanced", "result"}, call("queryData"), nil, 150).leaf = true
27 entry({"admin", "services", "adblock", "logread"}, call("logread"), nil).leaf = true
28 entry({"admin", "services", "adblock", "status_update"}, call("status_update"), nil).leaf = true
29 entry({"admin", "services", "adblock", "report_json"}, call("report_json"), nil).leaf = true
30 entry({"admin", "services", "adblock", "report_text"}, call("report_text"), nil).leaf = true
31 entry({"admin", "services", "adblock", "action"}, call("adb_action"), nil).leaf = true
34 function adb_action(name, ...)
35 local domain = select(1, ...) or ""
36 local search = select(2, ...) or "+"
37 local count = select(3, ...) or "50"
38 local filter = select(4, ...) or "false"
39 local print = select(5, ...) or "false"
41 local report_params = {
48 if name == "do_suspend" then
49 luci.sys.call("/etc/init.d/adblock suspend >/dev/null 2>&1")
50 elseif name == "do_resume" then
51 luci.sys.call("/etc/init.d/adblock resume >/dev/null 2>&1")
52 elseif name == "do_refresh" then
53 luci.sys.call("/etc/init.d/adblock reload >/dev/null 2>&1")
54 local pid_file = "/var/run/adblock.pid"
55 if nixio.fs.access(pid_file) then
58 until nixio.fs.readfile(pid_file) == ""
60 elseif name == "do_report" then
61 luci.sys.call("/etc/init.d/adblock report " ..table.concat(report_params, " ").. " >/dev/null 2>&1")
62 local rep_dir = uci:get("adblock", "extra", "adb_repdir") or "/tmp"
65 until not nixio.fs.access(rep_dir.. "/adb_report.raw")
66 elseif name == "do_filter" then
67 luci.sys.call("/etc/init.d/adblock report " ..table.concat(report_params, " ").. " >/dev/null 2>&1")
68 local rep_dir = uci:get("adblock", "extra", "adb_repdir") or "/tmp"
71 until nixio.fs.access(rep_dir.. "/adb_report.final")
72 elseif name == "add_blacklist" then
73 local file = uci:get("adblock", "blacklist", "adb_src") or "/etc/adblock/adblock.blacklist"
74 if nixio.fs.access(file) then
75 local blacklist = nixio.fs.readfile(file)
76 if not string.find(blacklist, domain, 1, true)
78 nixio.fs.writefile(file, blacklist.. domain.. "\n")
81 elseif name == "add_whitelist" then
82 local file = uci:get("adblock", "global", "adb_whitelist") or "/etc/adblock/adblock.whitelist"
83 if nixio.fs.access(file) then
84 local whitelist = nixio.fs.readfile(file)
85 if not string.find(whitelist, domain, 1, true)
87 nixio.fs.writefile(file, whitelist.. domain.. "\n")
91 luci.http.prepare_content("text/plain")
95 function status_update()
99 rt_file = uci:get("adblock", "global", "adb_rtfile") or "/tmp/adb_runtime.json"
101 if nixio.fs.access(rt_file) then
102 content = json.parse(nixio.fs.readfile(rt_file) or "")
103 http.prepare_content("application/json")
104 http.write_json(content)
108 function report_json()
113 rep_dir = uci:get("adblock", "extra", "adb_repdir") or "/tmp"
114 rep_file = rep_dir.. "/adb_report.json"
115 http.prepare_content("application/json")
117 if nixio.fs.access(rep_file) then
118 content = json.parse(nixio.fs.readfile(rep_file) or "")
119 http.write_json(content)
121 http.write_json("{}")
125 function report_text()
131 rep_dir = uci:get("adblock", "extra", "adb_repdir") or "/tmp"
132 rep_file = rep_dir.. "/adb_report.final"
133 http.prepare_content("text/plain")
135 if nixio.fs.access(rep_file) then
136 file = io.open(rep_file, "r")
137 content = file:read("*all")
148 if nixio.fs.access("/var/log/messages") then
149 content = util.trim(util.exec("grep -F 'adblock-' /var/log/messages"))
151 content = util.trim(util.exec("logread -e 'adblock-'"))
154 if content == "" then
155 content = "No adblock related logs yet!"
160 function queryData(domain)
162 luci.http.prepare_content("text/plain")
163 local cmd = "/etc/init.d/adblock query %s 2>&1"
164 local util = io.popen(cmd % util.shellquote(domain))
167 local line = util:read("*l")
171 luci.http.write(line)
172 luci.http.write("\n")