Merge pull request #1794 from dibdot/adblock
[project/luci.git] / applications / luci-app-adblock / luasrc / model / cbi / adblock / overview_tab.lua
1 -- Copyright 2017-2018 Dirk Brenken (dev@brenken.org)
2 -- This is free software, licensed under the Apache License, Version 2.0
3
4 local fs       = require("nixio.fs")
5 local uci      = require("luci.model.uci").cursor()
6 local sys      = require("luci.sys")
7 local util     = require("luci.util")
8 local dump     = util.ubus("network.interface", "dump", {})
9 local json     = require("luci.jsonc")
10 local adbinput = uci:get("adblock", "global", "adb_rtfile") or "/tmp/adb_runtime.json"
11
12 m = Map("adblock", translate("Adblock"),
13         translate("Configuration of the adblock package to block ad/abuse domains by using DNS. ")
14         .. translatef("For further information "
15         .. "<a href=\"%s\" target=\"_blank\">"
16         .. "check the online documentation</a>", "https://github.com/openwrt/packages/blob/master/net/adblock/files/README.md"))
17 m.apply_on_parse = true
18
19 function m.on_apply(self)
20         luci.sys.call("/etc/init.d/adblock reload >/dev/null 2>&1")
21         luci.http.redirect(luci.dispatcher.build_url("admin", "services", "adblock"))
22 end
23
24 -- Main adblock options
25
26 s = m:section(NamedSection, "global", "adblock")
27
28 local parse = json.parse(fs.readfile(adbinput) or "")
29 if parse then
30         status  = parse.data.adblock_status
31         version = parse.data.adblock_version
32         domains = parse.data.overall_domains
33         fetch   = parse.data.fetch_utility
34         backend = parse.data.dns_backend
35         rundate = parse.data.last_rundate
36 end
37
38 o1 = s:option(Flag, "adb_enabled", translate("Enable Adblock"))
39 o1.default = o1.disabled
40 o1.rmempty = false
41
42 btn = s:option(Button, "", translate("Suspend / Resume Adblock"))
43 if parse and status == "enabled" then
44         btn.inputtitle = translate("Suspend")
45         btn.inputstyle = "reset"
46         btn.disabled = false
47         function btn.write()
48                 luci.sys.call("/etc/init.d/adblock suspend >/dev/null 2>&1")
49                 luci.http.redirect(luci.dispatcher.build_url("admin", "services", "adblock"))
50         end
51 elseif parse and status == "paused" then
52         btn.inputtitle = translate("Resume")
53         btn.inputstyle = "apply"
54         btn.disabled = false
55         function btn.write()
56                 luci.sys.call("/etc/init.d/adblock resume >/dev/null 2>&1")
57                 luci.http.redirect(luci.dispatcher.build_url("admin", "services", "adblock"))
58         end
59 else
60         btn.inputtitle = translate("-------")
61         btn.inputstyle = "button"
62         btn.disabled = true
63 end
64
65 o2 = s:option(ListValue, "adb_dns", translate("DNS Backend (DNS Directory)"),
66         translate("List of supported DNS backends with their default list export directory.<br />")
67         .. translate("To overwrite the default path use the 'DNS Directory' option in the extra section below."))
68 o2:value("dnsmasq", "dnsmasq (/tmp)")
69 o2:value("unbound", "unbound (/var/lib/unbound)")
70 o2:value("named", "named (/var/lib/bind)")
71 o2:value("kresd", "kresd (/etc/kresd)")
72 o2:value("dnscrypt-proxy","dnscrypt-proxy (/tmp)")
73 o2.default = "dnsmasq (/tmp)"
74 o2.rmempty = false
75
76 o3 = s:option(ListValue, "adb_fetchutil", translate("Download Utility"),
77 translate("List of supported and fully pre-configured download utilities."))
78 o3:value("uclient-fetch")
79 o3:value("wget")
80 o3:value("curl")
81 o3:value("aria2c")
82 o3:value("wget-nossl", "wget-nossl (noSSL)")
83 o3:value("busybox", "wget-busybox (noSSL)")
84 o3.default = "uclient-fetch"
85 o3.rmempty = false
86
87 o4 = s:option(ListValue, "adb_trigger", translate("Startup Trigger"),
88         translate("List of available network interfaces. Usually the startup will be triggered by the 'wan' interface.<br />")
89         .. translate("Choose 'none' to disable automatic startups, 'timed' to use a classic timeout (default 30 sec.) or select another trigger interface."))
90 o4:value("none")
91 o4:value("timed")
92 if dump then
93         local i, v
94         for i, v in ipairs(dump.interface) do
95                 if v.interface ~= "loopback" then
96                         o4:value(v.interface)
97                 end
98         end
99 end
100 o4.rmempty = false
101
102 -- Runtime information
103
104 ds = m:section(NamedSection, "global", "adblock", translate("Runtime Information"))
105
106 dv1 = ds:option(DummyValue, "", translate("Adblock Status"))
107 dv1.template = "adblock/runtime"
108 if parse == nil then
109         dv1.value = translate("n/a")
110 else
111         if status == "error" then
112                 dv1.value = translate("error")
113         elseif status == "disabled" then
114                 dv1.value = translate("disabled")
115         elseif status == "paused" then
116                 dv1.value = translate("paused")
117         elseif status == "running" then
118                 dv1.value = translate("running")
119         else
120                 dv1.value = translate("enabled")
121         end
122 end
123
124 dv2 = ds:option(DummyValue, "", translate("Adblock Version"))
125 dv2.template = "adblock/runtime"
126 if parse == nil then
127         dv2.value = translate("n/a")
128 else
129         dv2.value = version
130 end
131
132 dv3 = ds:option(DummyValue, "", translate("Download Utility (SSL Library)"),
133         translate("For SSL protected blocklist sources you need a suitable SSL library, e.g. 'libustream-ssl' or 'built-in'."))
134 dv3.template = "adblock/runtime"
135 if parse == nil then
136         dv3.value = translate("n/a")
137 else
138         dv3.value = fetch
139 end
140
141 dv4 = ds:option(DummyValue, "", translate("DNS Backend (DNS Directory)"))
142 dv4.template = "adblock/runtime"
143 if parse == nil then
144         dv4.value = translate("n/a")
145 else
146         dv4.value = backend
147 end
148
149 dv5 = ds:option(DummyValue, "", translate("Overall Domains"))
150 dv5.template = "adblock/runtime"
151 if parse == nil then
152         dv5.value = translate("n/a")
153 else
154         dv5.value = domains
155 end
156
157 dv6 = ds:option(DummyValue, "", translate("Last Run"))
158 dv6.template = "adblock/runtime"
159 if parse == nil then
160         dv6.value = translate("n/a")
161 else
162         dv6.value = rundate
163 end
164
165 -- Blocklist table
166
167 bl = m:section(TypedSection, "source", translate("Blocklist Sources"),
168         translate("<b>Caution:</b> To prevent OOM exceptions on low memory devices with less than 64 MB free RAM, please only select a few of them!"))
169 bl.template = "adblock/blocklist"
170
171 name = bl:option(Flag, "enabled", translate("Enabled"))
172 name.rmempty = false
173
174 ssl = bl:option(DummyValue, "adb_src", translate("SSL req."))
175 function ssl.cfgvalue(self, section)
176         local source = self.map:get(section, "adb_src")
177         if source and source:match("https://") then
178                 return translate("Yes")
179         else
180                 return translate("No")
181         end
182 end
183
184 des = bl:option(DummyValue, "adb_src_desc", translate("Description"))
185
186 cat = bl:option(DynamicList, "adb_src_cat", translate("Categories"))
187 cat.datatype = "uciname"
188 cat.optional = true
189
190 -- Extra options
191
192 e = m:section(NamedSection, "extra", "adblock", translate("Extra Options"),
193         translate("Options for further tweaking in case the defaults are not suitable for you."))
194
195 e1 = e:option(Flag, "adb_debug", translate("Verbose Debug Logging"),
196         translate("Enable verbose debug logging in case of any processing error."))
197 e1.default = e1.disabled
198 e1.rmempty = false
199
200 e2 = e:option(Flag, "adb_forcedns", translate("Force Local DNS"),
201         translate("Redirect all DNS queries from 'lan' zone to the local resolver."))
202 e2.default = e2.disabled
203 e2.rmempty = false
204
205 e3 = e:option(Flag, "adb_forcesrt", translate("Force Overall Sort"),
206         translate("Enable memory intense overall sort / duplicate removal on low memory devices (&lt; 64 MB free RAM)"))
207 e3.default = e3.disabled
208 e3.rmempty = false
209
210 e4 = e:option(Flag, "adb_backup", translate("Enable Blocklist Backup"),
211         translate("Create compressed blocklist backups, they will be used in case of download errors or during startup in backup mode."))
212 e4.default = e4.disabled
213 e4.rmempty = false
214
215 e5 = e:option(Value, "adb_backupdir", translate("Backup Directory"),
216         translate("Target directory for adblock backups. Please use only non-volatile disks, e.g. an external usb stick."))
217 e5:depends("adb_backup", 1)
218 e5.datatype = "directory"
219 e5.default = "/mnt"
220 e5.rmempty = true
221
222 e6 = e:option(Flag, "adb_backup_mode", translate("Backup Mode"),
223         translate("Do not automatically update blocklists during startup, use blocklist backups instead."))
224 e6:depends("adb_backup", 1)
225 e6.default = e6.disabled
226 e6.rmempty = true
227
228 e7 = e:option(Value, "adb_maxqueue", translate("Max. Download Queue"),
229         translate("Size of the download queue to handle downloads &amp; list processing in parallel (default '4').<br />")
230         .. translate("For further performance improvements you can raise this value, e.g. '8' or '16' should be safe."))
231 e7.default = 4
232 e7.datatype = "range(1,32)"
233 e7.rmempty = false
234
235 e8 = e:option(Flag, "adb_jail", translate("'Jail' Blocklist Creation"),
236         translate("Builds an additional 'Jail' list (/tmp/adb_list.jail) to block access to all domains except those listed in the whitelist file.<br />")
237         .. translate("You can use this restrictive blocklist manually e.g. for guest wifi or kidsafe configurations."))
238 e8.default = e8.disabled
239 e8.rmempty = true
240
241 e9 = e:option(Flag, "adb_dnsflush", translate("Flush DNS Cache"),
242         translate("Flush DNS Cache after adblock processing."))
243 e9.default = e9.disabled
244 e9.rmempty = true
245
246 e10 = e:option(Flag, "adb_notify", translate("Email Notification"),
247         translate("Send notification emails in case of a processing error or if domain count is &le; 0.<br />")
248         .. translate("Please note: this needs additional 'msmtp' package installation and setup."))
249 e10.default = e10.disabled
250 e10.rmempty = true
251
252 e11 = e:option(Value, "adb_notifycnt", translate("Email Notification Count"),
253         translate("Raise the minimum email notification count, to get emails if the overall count is less or equal to the given limit (default 0),<br />")
254         .. translate("e.g. to receive an email notification with every adblock update set this value to 150000."))
255 e11.default = 0
256 e11.datatype = "min(0)"
257 e11.optional = true
258
259 e12 = e:option(Value, "adb_dnsdir", translate("DNS Directory"),
260         translate("Target directory for the generated blocklist 'adb_list.overall'."))
261 e12.datatype = "directory"
262 e12.optional = true
263
264 e13 = e:option(Value, "adb_whitelist", translate("Whitelist File"),
265         translate("Full path to the whitelist file."))
266 e13.datatype = "file"
267 e13.default = "/etc/adblock/adblock.whitelist"
268 e13.optional = true
269
270 e14 = e:option(Value, "adb_triggerdelay", translate("Trigger Delay"),
271         translate("Additional trigger delay in seconds before adblock processing begins."))
272 e14.datatype = "range(1,60)"
273 e14.optional = true
274
275 return m