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