Merge pull request #980 from NvrBst/pull-request-upnp_description
[project/luci.git] / applications / luci-app-adblock / luasrc / model / cbi / adblock / overview_tab.lua
1 -- Copyright 2017 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/dnsmasq.d)")
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.rmempty = false
83
84 o3 = s:option(ListValue, "adb_trigger", translate("Startup Trigger"),
85 translate("List of available network interfaces. Usually the startup will be triggered by the 'wan' interface.<br />")
86 .. translate("Choose 'none' to disable automatic startups, 'timed' to use a classic timeout (default 30 sec.) or select another trigger interface."))
87 o3:value("none")
88 o3:value("timed")
89 if dump then
90 local i, v
91 for i, v in ipairs(dump.interface) do
92 if v.interface ~= "loopback" then
93 o3:value(v.interface)
94 end
95 end
96 end
97 o3.rmempty = false
98
99 -- Runtime information
100
101 ds = s:option(DummyValue, "", translate("Runtime Information"))
102 ds.template = "cbi/nullsection"
103
104 dv1 = s:option(DummyValue, "", translate("Adblock Status"))
105 dv1.template = "adblock/runtime"
106 if parse == nil then
107 dv1.value = translate("n/a")
108 else
109 if status == "error" then
110 dv1.value = translate("error")
111 elseif status == "disabled" then
112 dv1.value = translate("disabled")
113 elseif status == "paused" then
114 dv1.value = translate("paused")
115 else
116 dv1.value = translate("enabled")
117 end
118 end
119
120 dv2 = s:option(DummyValue, "", translate("Adblock Version"))
121 dv2.template = "adblock/runtime"
122 if parse == nil then
123 dv2.value = translate("n/a")
124 else
125 dv2.value = version
126 end
127
128 dv3 = s:option(DummyValue, "", translate("Download Utility (SSL Library)"),
129 translate("For SSL protected blocklist sources you need a suitable SSL library, e.g. 'libustream-ssl' or the wget 'built-in'."))
130 dv3.template = "adblock/runtime"
131 if parse == nil then
132 dv3.value = translate("n/a")
133 else
134 dv3.value = fetch
135 end
136
137 dv4 = s:option(DummyValue, "", translate("DNS Backend (DNS Directory)"))
138 dv4.template = "adblock/runtime"
139 if parse == nil then
140 dv4.value = translate("n/a")
141 else
142 dv4.value = backend
143 end
144
145 dv5 = s:option(DummyValue, "", translate("Overall Domains"))
146 dv5.template = "adblock/runtime"
147 if parse == nil then
148 dv5.value = translate("n/a")
149 else
150 dv5.value = domains
151 end
152
153 dv6 = s:option(DummyValue, "", translate("Last Run"))
154 dv6.template = "adblock/runtime"
155 if parse == nil then
156 dv6.value = translate("n/a")
157 else
158 dv6.value = rundate
159 end
160
161 -- Blocklist table
162
163 bl = m:section(TypedSection, "source", translate("Blocklist Sources"),
164 translate("Available blocklist sources. ")
165 .. translate("List URLs and Shallalist category selections are configurable in the 'Advanced' section.<br />")
166 .. translate("Caution: To prevent OOM exceptions on low memory devices with less than 64 MB free RAM, please do not select too many lists - 5-6 should be sufficient!"))
167 bl.template = "cbi/tblsection"
168
169 name = bl:option(Flag, "enabled", translate("Enabled"))
170 name.rmempty = false
171
172 ssl = bl:option(DummyValue, "adb_src", translate("SSL req."))
173 function ssl.cfgvalue(self, section)
174 local source = self.map:get(section, "adb_src")
175 if source and source:match("https://") then
176 return translate("Yes")
177 else
178 return translate("No")
179 end
180 end
181 des = bl:option(DummyValue, "adb_src_desc", translate("Description"))
182
183 -- Extra options
184
185 e = m:section(NamedSection, "extra", "adblock", translate("Extra Options"),
186 translate("Options for further tweaking in case the defaults are not suitable for you."))
187
188 e1 = e:option(Flag, "adb_debug", translate("Verbose Debug Logging"),
189 translate("Enable verbose debug logging in case of any processing error."))
190 e1.default = e1.disabled
191 e1.rmempty = false
192
193 e2 = e:option(Flag, "adb_forcedns", translate("Force Local DNS"),
194 translate("Redirect all DNS queries from 'lan' zone to the local resolver."))
195 e2.default = e2.disabled
196 e2.rmempty = false
197
198 e3 = e:option(Flag, "adb_forcesrt", translate("Force Overall Sort"),
199 translate("Enable memory intense overall sort / duplicate removal on low memory devices (&lt; 64 MB free RAM)"))
200 e3.default = e3.disabled
201 e3.rmempty = false
202
203 e4 = e:option(Flag, "adb_backup", translate("Enable Blocklist Backup"),
204 translate("Create compressed blocklist backups, they will be used in case of download errors or during startup in backup mode."))
205 e4.default = e4.disabled
206 e4.rmempty = false
207
208 e5 = e:option(Value, "adb_backupdir", translate("Backup Directory"),
209 translate("Target directory for adblock backups. Please use only non-volatile disks, e.g. an external usb stick."))
210 e5:depends("adb_backup", 1)
211 e5.datatype = "directory"
212 e5.default = "/mnt"
213 e5.rmempty = true
214
215 e6 = e:option(Flag, "adb_backup_mode", translate("Backup Mode"),
216 translate("Do not automatically update blocklists during startup, use blocklist backups instead."))
217 e6:depends("adb_backup", 1)
218 e6.default = e6.disabled
219 e6.rmempty = true
220
221 e7 = e:option(Flag, "adb_whitelist_mode", translate("Whitelist Mode"),
222 translate("Block access to all domains except those explicitly listed in the whitelist file."))
223 e7.default = e7.disabled
224 e7.rmempty = true
225
226 e8 = e:option(Value, "adb_dnsdir", translate("DNS Directory"),
227 translate("Target directory for the generated blocklist 'adb_list.overall'."))
228 e8.datatype = "directory"
229 e8.optional = true
230
231 e9 = e:option(Value, "adb_whitelist", translate("Whitelist File"),
232 translate("Full path to the whitelist file."))
233 e9.datatype = "file"
234 e9.default = "/etc/adblock/adblock.whitelist"
235 e9.optional = true
236
237 e10 = e:option(Value, "adb_triggerdelay", translate("Trigger Delay"),
238 translate("Additional trigger delay in seconds before adblock processing begins."))
239 e10.datatype = "range(1,60)"
240 e10.optional = true
241
242 return m