luci-app-adblock: small controller changes
[project/luci.git] / applications / luci-app-adblock / luasrc / controller / adblock.lua
1 -- Copyright 2017-2019 Dirk Brenken (dev@brenken.org)
2 -- This is free software, licensed under the Apache License, Version 2.0
3
4 module("luci.controller.adblock", package.seeall)
5
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()
12
13 function index()
14 if not nixio.fs.access("/etc/config/adblock") then
15 return
16 end
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
21 end
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
34 end
35
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"
42
43 local report_params = {
44 search,
45 count,
46 filter,
47 print
48 }
49
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 local pid_file = "/var/run/adblock.pid"
57 if nixio.fs.access(pid_file) then
58 repeat
59 nixio.nanosleep(1)
60 until nixio.fs.readfile(pid_file) == ""
61 end
62 elseif name == "do_report" 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"
65 repeat
66 nixio.nanosleep(1)
67 until not nixio.fs.access(rep_dir.. "/adb_report.raw")
68 elseif name == "do_filter" then
69 luci.sys.call("/etc/init.d/adblock report " ..table.concat(report_params, " ").. " >/dev/null 2>&1")
70 local rep_dir = uci:get("adblock", "extra", "adb_repdir") or "/tmp"
71 repeat
72 nixio.nanosleep(1)
73 until nixio.fs.access(rep_dir.. "/adb_report.final")
74 elseif name == "add_blacklist" then
75 local file = uci:get("adblock", "blacklist", "adb_src") or "/etc/adblock/adblock.blacklist"
76 if nixio.fs.access(file) then
77 local blacklist = nixio.fs.readfile(file)
78 if not string.find(blacklist, domain, 1, true)
79 then
80 nixio.fs.writefile(file, blacklist.. domain.. "\n")
81 end
82 end
83 elseif name == "add_whitelist" then
84 local file = uci:get("adblock", "global", "adb_whitelist") or "/etc/adblock/adblock.whitelist"
85 if nixio.fs.access(file) then
86 local whitelist = nixio.fs.readfile(file)
87 if not string.find(whitelist, domain, 1, true)
88 then
89 nixio.fs.writefile(file, whitelist.. domain.. "\n")
90 end
91 end
92 end
93 luci.http.prepare_content("text/plain")
94 luci.http.write("0")
95 end
96
97 function status_update()
98 local rt_file
99 local content
100
101 rt_file = uci:get("adblock", "global", "adb_rtfile") or "/tmp/adb_runtime.json"
102
103 if nixio.fs.access(rt_file) then
104 content = json.parse(nixio.fs.readfile(rt_file) or "")
105 http.prepare_content("application/json")
106 http.write_json(content)
107 end
108 end
109
110 function report_json()
111 local rep_dir
112 local rep_file
113 local content
114
115 rep_dir = uci:get("adblock", "extra", "adb_repdir") or "/tmp"
116 rep_file = rep_dir.. "/adb_report.json"
117 http.prepare_content("application/json")
118
119 if nixio.fs.access(rep_file) then
120 content = json.parse(nixio.fs.readfile(rep_file) or "")
121 http.write_json(content)
122 else
123 http.write_json("{}")
124 end
125 end
126
127 function report_text()
128 local file
129 local rep_dir
130 local rep_file
131 local content
132
133 rep_dir = uci:get("adblock", "extra", "adb_repdir") or "/tmp"
134 rep_file = rep_dir.. "/adb_report.final"
135 http.prepare_content("text/plain")
136
137 if nixio.fs.access(rep_file) then
138 file = io.open(rep_file, "r")
139 content = file:read("*all")
140 file:close()
141 http.write(content)
142 else
143 http.write("")
144 end
145 end
146
147 function logread()
148 local content = util.trim(util.exec("logread -e 'adblock-'")) or ""
149
150 if content == "" then
151 content = "No adblock related logs yet!"
152 end
153 http.write(content)
154 end
155
156 function queryData(domain)
157 if domain then
158 luci.http.prepare_content("text/plain")
159 local cmd = "/etc/init.d/adblock query %s 2>&1"
160 local util = io.popen(cmd % util.shellquote(domain))
161 if util then
162 while true do
163 local line = util:read("*l")
164 if not line then
165 break
166 end
167 luci.http.write(line)
168 luci.http.write("\n")
169 end
170 util:close()
171 end
172 end
173 end