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 if nixio.fs.access("/usr/sbin/tcpdump") then
20 entry({"admin", "services", "adblock", "report"}, template("adblock/report"), _("DNS Query Report"), 20).leaf = true
22 entry({"admin", "services", "adblock", "log"}, template("adblock/logread"), _("Logfile"), 30).leaf = true
23 entry({"admin", "services", "adblock", "advanced"}, firstchild(), _("Advanced"), 100)
24 entry({"admin", "services", "adblock", "advanced", "blacklist"}, form("adblock/blacklist_tab"), _("Edit Blacklist"), 110).leaf = true
25 entry({"admin", "services", "adblock", "advanced", "whitelist"}, form("adblock/whitelist_tab"), _("Edit Whitelist"), 120).leaf = true
26 entry({"admin", "services", "adblock", "advanced", "configuration"}, form("adblock/configuration_tab"), _("Edit Configuration"), 130).leaf = true
27 entry({"admin", "services", "adblock", "advanced", "query"}, template("adblock/query"), _("Query domains"), 140).leaf = true
28 entry({"admin", "services", "adblock", "advanced", "result"}, call("queryData"), nil, 150).leaf = true
29 entry({"admin", "services", "adblock", "logread"}, call("logread"), nil).leaf = true
30 entry({"admin", "services", "adblock", "status_update"}, call("status_update"), nil).leaf = true
31 entry({"admin", "services", "adblock", "report_json"}, call("report_json"), nil).leaf = true
32 entry({"admin", "services", "adblock", "report_text"}, call("report_text"), nil).leaf = true
33 entry({"admin", "services", "adblock", "action"}, call("adb_action"), nil).leaf = true
36 function adb_action(name, ...)
37 local domain = select(1, ...) or ""
38 local search = select(2, ...) or "+"
39 local count = select(3, ...) or "50"
40 local filter = select(4, ...) or "false"
41 local print = select(5, ...) or "false"
43 local report_params = {
50 if name == "do_suspend" then
51 luci.sys.call("/etc/init.d/adblock suspend >/dev/null 2>&1")
52 elseif name == "do_resume" then
53 luci.sys.call("/etc/init.d/adblock resume >/dev/null 2>&1")
54 elseif name == "do_refresh" then
55 luci.sys.call("/etc/init.d/adblock reload >/dev/null 2>&1")
56 elseif name == "do_report" then
57 luci.sys.call("/etc/init.d/adblock report " ..table.concat(report_params, " ").. " >/dev/null 2>&1")
58 local rep_dir = uci:get("adblock", "extra", "adb_repdir") or "/tmp"
61 until not nixio.fs.access(rep_dir.. "/adb_report.raw")
62 elseif name == "do_filter" then
63 luci.sys.call("/etc/init.d/adblock report " ..table.concat(report_params, " ").. " >/dev/null 2>&1")
64 local rep_dir = uci:get("adblock", "extra", "adb_repdir") or "/tmp"
67 until nixio.fs.access(rep_dir.. "/adb_report.final")
68 elseif name == "add_blacklist" then
69 local file = uci:get("adblock", "blacklist", "adb_src") or "/etc/adblock/adblock.blacklist"
70 if nixio.fs.access(file) then
71 local blacklist = nixio.fs.readfile(file)
72 if not string.find(blacklist, domain, 1, true)
74 nixio.fs.writefile(file, blacklist.. domain.. "\n")
77 elseif name == "add_whitelist" then
78 local file = uci:get("adblock", "global", "adb_whitelist") or "/etc/adblock/adblock.whitelist"
79 if nixio.fs.access(file) then
80 local whitelist = nixio.fs.readfile(file)
81 if not string.find(whitelist, domain, 1, true)
83 nixio.fs.writefile(file, whitelist.. domain.. "\n")
87 if name == "do_suspend" or name == "do_resume" or name == "do_refresh" then
88 local pid_file = "/var/run/adblock.pid"
89 if nixio.fs.access(pid_file) then
92 until nixio.fs.readfile(pid_file) == ""
95 luci.http.prepare_content("text/plain")
99 function status_update()
103 rt_file = uci:get("adblock", "global", "adb_rtfile") or "/tmp/adb_runtime.json"
105 if nixio.fs.access(rt_file) then
106 content = json.parse(nixio.fs.readfile(rt_file) or "")
107 http.prepare_content("application/json")
108 http.write_json(content)
112 function report_json()
117 rep_dir = uci:get("adblock", "extra", "adb_repdir") or "/tmp"
118 rep_file = rep_dir.. "/adb_report.json"
119 http.prepare_content("application/json")
121 if nixio.fs.access(rep_file) then
122 content = json.parse(nixio.fs.readfile(rep_file) or "")
123 http.write_json(content)
125 http.write_json("{}")
129 function report_text()
135 rep_dir = uci:get("adblock", "extra", "adb_repdir") or "/tmp"
136 rep_file = rep_dir.. "/adb_report.final"
137 http.prepare_content("text/plain")
139 if nixio.fs.access(rep_file) then
140 file = io.open(rep_file, "r")
141 content = file:read("*all")
150 local content = util.trim(util.exec("logread -e 'adblock-'")) or ""
152 if content == "" then
153 content = "No adblock related logs yet!"
158 function queryData(domain)
160 luci.http.prepare_content("text/plain")
161 local cmd = "/etc/init.d/adblock query %s 2>&1"
162 local util = io.popen(cmd % util.shellquote(domain))
165 local line = util:read("*l")
169 luci.http.write(line)
170 luci.http.write("\n")