Merge pull request #3058 from ewsi/feature_dcwifi
[project/luci.git] / applications / luci-app-simple-adblock / luasrc / model / cbi / simple-adblock.lua
1 -- Copyright 2016-2018 Stan Grishin <stangri@melmac.net>
2 -- Licensed to the public under the Apache License 2.0.
3
4 local readmeURL = "https://github.com/openwrt/packages/tree/master/net/simple-adblock/files/README.md"
5 -- local readmeURL = "https://github.com/stangri/openwrt_packages/tree/master/simple-adblock/files/README.md"
6
7 local packageName = "simple-adblock"
8 local uci = require "luci.model.uci".cursor()
9 local util = require "luci.util"
10 local sys = require "luci.sys"
11 local jsonc = require "luci.jsonc"
12 local fs = require "nixio.fs"
13 local http = require "luci.http"
14 local dispatcher = require "luci.dispatcher"
15 local enabledFlag = uci:get(packageName, "config", "enabled")
16 local command, outputFile, outputCache, outputGzip
17 local targetDNS = uci:get(packageName, "config", "dns")
18 local checkDnsmasq = sys.call("which dnsmasq >/dev/null 2>&1") == 0 and true
19 local checkUnbound = sys.call("which unbound >/dev/null 2>&1") == 0 and true
20 local checkDnsmasqIpset = sys.call("dnsmasq -v 2>/dev/null | grep -q 'no-ipset' || ! dnsmasq -v 2>/dev/null | grep -q -w 'ipset'") ~= 0
21 and sys.call("ipset help hash:net >/dev/null 2>&1") and true
22
23 if not targetDNS or targetDNS == "" then
24 targetDNS = "dnsmasq.servers"
25 end
26
27 if targetDNS ~= "dnsmasq.addnhosts" and targetDNS ~= "dnsmasq.conf" and
28 targetDNS ~= "dnsmasq.ipset" and targetDNS ~= "dnsmasq.servers" and
29 targetDNS ~= "unbound.adb_list" then
30 targetDNS = "dnsmasq.servers"
31 end
32
33 if targetDNS == "dnsmasq.addnhosts" then
34 outputFile="/var/run/" .. packageName .. ".addnhosts"
35 outputCache="/var/run/" .. packageName .. ".addnhosts.cache"
36 outputGzip="/etc/" .. packageName .. ".addnhosts.gz"
37 elseif targetDNS == "dnsmasq.conf" then
38 outputFile="/var/dnsmasq.d/" .. packageName .. ""
39 outputCache="/var/run/" .. packageName .. ".dnsmasq.cache"
40 outputGzip="/etc/" .. packageName .. ".dnsmasq.gz"
41 elseif targetDNS == "dnsmasq.servers" then
42 outputFile="/var/run/" .. packageName .. ".servers"
43 outputCache="/var/run/" .. packageName .. ".servers.cache"
44 outputGzip="/etc/" .. packageName .. ".servers.gz"
45 elseif targetDNS == "unbound.adb_list" then
46 outputFile="/var/lib/unbound/adb_list." .. packageName .. ""
47 outputCache="/var/run/" .. packageName .. ".unbound.cache"
48 outputGzip="/etc/" .. packageName .. ".unbound.gz"
49 end
50
51 m = Map("simple-adblock", translate("Simple AdBlock Settings"))
52 m.apply_on_parse = true
53 m.on_after_apply = function(self)
54 sys.call("/etc/init.d/simple-adblock restart")
55 end
56
57 local tmpfs
58 if fs.access("/var/run/" .. packageName .. ".json") then
59 tmpfs = jsonc.parse(util.trim(sys.exec("cat /var/run/" .. packageName .. ".json")))
60 end
61
62 local tmpfsVersion, tmpfsStatus, tmpfsMessage, tmpfsError, tmpfsStats = "", "Stopped"
63 if tmpfs and tmpfs['data'] then
64 if tmpfs['data']['status'] and tmpfs['data']['status'] ~= "" then
65 tmpfsStatus = tmpfs['data']['status']
66 end
67 if tmpfs['data']['message'] and tmpfs['data']['message'] ~= "" then
68 tmpfsMessage = tmpfs['data']['message']
69 end
70 if tmpfs['data']['error'] and tmpfs['data']['error'] ~= "" then
71 tmpfsError = tmpfs['data']['error']
72 end
73 if tmpfs['data']['stats'] and tmpfs['data']['stats'] ~= "" then
74 tmpfsStats = tmpfs['data']['stats']
75 end
76 if tmpfs['data']['version'] and tmpfs['data']['version'] ~= "" then
77 tmpfsVersion = " (" .. packageName .. " " .. tmpfs['data']['version'] .. ")"
78 end
79 end
80
81 h = m:section(NamedSection, "config", "simple-adblock", translate("Service Status") .. tmpfsVersion)
82
83 if tmpfsStatus and tmpfsStatus:match("ing") then
84 ss = h:option(DummyValue, "_dummy", translate("Service Status"))
85 ss.template = "simple-adblock/status"
86 ss.value = tmpfsStatus .. '...'
87 if tmpfsMessage then
88 sm = h:option(DummyValue, "_dummy", translate("Task"))
89 sm.template = "simple-adblock/status"
90 sm.value = tmpfsMessage
91 end
92 else
93 en = h:option(Button, "__toggle")
94 if enabledFlag ~= "1" or tmpfsStatus:match("Stopped") then
95 en.title = translate("Service is disabled/stopped")
96 en.inputtitle = translate("Enable/Start")
97 en.inputstyle = "apply important"
98 if fs.access(outputCache) then
99 sm = h:option(DummyValue, "_dummy", translate("Info"))
100 sm.template = "simple-adblock/status"
101 sm.value = "Cache file containing " .. util.trim(sys.exec("wc -l < " .. outputCache)) .. " domains found."
102 elseif fs.access(outputGzip) then
103 sm = h:option(DummyValue, "_dummy", translate("Info"))
104 sm.template = "simple-adblock/status"
105 sm.value = "Compressed cache file found."
106 end
107 else
108 en.title = translate("Service is enabled/started")
109 en.inputtitle = translate("Stop/Disable")
110 en.inputstyle = "reset important"
111 ss = h:option(DummyValue, "_dummy", translate("Service Status"))
112 ss.template = "simple-adblock/status"
113 ss.value = tmpfsStatus
114 if tmpfsMessage then
115 ms = h:option(DummyValue, "_dummy", translate("Message"))
116 ms.template = "simple-adblock/status"
117 ms.value = tmpfsMessage
118 end
119 if tmpfsError then
120 es = h:option(DummyValue, "_dummy", translate("Collected Errors"))
121 es.template = "simple-adblock/status"
122 es.value = tmpfsError
123 reload = h:option(Button, "__reload")
124 reload.title = translate("Service started with error")
125 reload.inputtitle = translate("Reload")
126 reload.inputstyle = "apply important"
127 function reload.write()
128 sys.exec("/etc/init.d/simple-adblock reload")
129 http.redirect(dispatcher.build_url("admin/services/" .. packageName))
130 end
131 end
132 end
133 function en.write()
134 if tmpfsStatus and tmpfsStatus:match("Stopped") then
135 enabledFlag = "1"
136 else
137 enabledFlag = enabledFlag == "1" and "0" or "1"
138 end
139 uci:set(packageName, "config", "enabled", enabledFlag)
140 uci:save(packageName)
141 uci:commit(packageName)
142 if enabledFlag == "0" then
143 luci.sys.init.stop(packageName)
144 else
145 luci.sys.init.enable(packageName)
146 luci.sys.init.start(packageName)
147 end
148 luci.http.redirect(luci.dispatcher.build_url("admin/services/" .. packageName))
149 end
150 end
151
152 s = m:section(NamedSection, "config", "simple-adblock", translate("Configuration"))
153 -- General options
154 s:tab("basic", translate("Basic Configuration"))
155
156 o2 = s:taboption("basic", ListValue, "verbosity", translate("Output Verbosity Setting"),translate("Controls system log and console output verbosity."))
157 o2:value("0", translate("Suppress output"))
158 o2:value("1", translate("Some output"))
159 o2:value("2", translate("Verbose output"))
160 o2.default = 2
161
162 o3 = s:taboption("basic", ListValue, "force_dns", translate("Force Router DNS"), translate("Forces Router DNS use on local devices, also known as DNS Hijacking."))
163 o3:value("0", translate("Let local devices use their own DNS servers if set"))
164 o3:value("1", translate("Force Router DNS server to all local devices"))
165 o3.default = 1
166
167 local sysfs_path = "/sys/class/leds/"
168 local leds = {}
169 if nixio.fs.access(sysfs_path) then
170 leds = nixio.util.consume((nixio.fs.dir(sysfs_path)))
171 end
172 if #leds ~= 0 then
173 o4 = s:taboption("basic", Value, "led", translate("LED to indicate status"), translate("Pick the LED not already used in")
174 .. [[ <a href="]] .. luci.dispatcher.build_url("admin/system/leds") .. [[">]]
175 .. translate("System LED Configuration") .. [[</a>]] .. ".")
176 o4.rmempty = false
177 o4:value("", translate("none"))
178 for k, v in ipairs(leds) do
179 o4:value(v)
180 end
181 end
182
183 s:tab("advanced", translate("Advanced Configuration"))
184
185 local dns_descr = translate("Pick the DNS resolution option to create the adblock list for, see the") .. " "
186 .. [[<a href="]] .. readmeURL .. [[#dns-resolution-option" target="_blank">]]
187 .. translate("README") .. [[</a>]] .. " " .. translate("for details.")
188
189 if not checkDnsmasq then
190 dns_descr = dns_descr .. "<br />" .. translate("Please note that") .. " <i>dnsmasq.addnhosts</i> " .. translate("is not supported on this system.")
191 dns_descr = dns_descr .. "<br />" .. translate("Please note that") .. " <i>dnsmasq.conf</i> " .. translate("is not supported on this system.")
192 dns_descr = dns_descr .. "<br />" .. translate("Please note that") .. " <i>dnsmasq.ipset</i> " .. translate("is not supported on this system.")
193 dns_descr = dns_descr .. "<br />" .. translate("Please note that") .. " <i>dnsmasq.servers</i> " .. translate("is not supported on this system.")
194 elseif not checkDnsmasqIpset then
195 dns_descr = dns_descr .. "<br />" .. translate("Please note that") .. " <i>dnsmasq.ipset</i> " .. translate("is not supported on this system.")
196 end
197 if not checkUnbound then
198 dns_descr = dns_descr .. "<br />" .. translate("Please note that") .. " <i>unbound.adb_list</i> " .. translate("is not supported on this system.")
199 end
200
201 dns = s:taboption("advanced", ListValue, "dns", translate("DNS Service"), dns_descr)
202 if checkDnsmasq then
203 dns:value("dnsmasq.addnhosts", translate("DNSMASQ Additional Hosts"))
204 dns:value("dnsmasq.conf", translate("DNSMASQ Config"))
205 if checkDnsmasqIpset then
206 dns:value("dnsmasq.ipset", translate("DNSMASQ IP Set"))
207 end
208 dns:value("dnsmasq.servers", translate("DNSMASQ Servers File"))
209 end
210 if checkUnbound then
211 dns:value("unbound.adb_list", translate("Unbound AdBlock List"))
212 end
213 dns.default = "dnsmasq.servers"
214
215 ipv6 = s:taboption("advanced", ListValue, "ipv6_enabled", translate("IPv6 Support"), translate("Add IPv6 entries to block-list."))
216 ipv6:value("", translate("Do not add IPv6 entries"))
217 ipv6:value("1", translate("Add IPv6 entries"))
218 ipv6:depends({dns="dnsmasq.addnhosts"})
219 ipv6.default = ""
220 ipv6.rmempty = true
221
222 o5 = s:taboption("advanced", Value, "boot_delay", translate("Delay (in seconds) for on-boot start"), translate("Run service after set delay on boot."))
223 o5.default = 120
224 o5.datatype = "range(1,600)"
225
226 o6 = s:taboption("advanced", Value, "download_timeout", translate("Download time-out (in seconds)"), translate("Stop the download if it is stalled for set number of seconds."))
227 o6.default = 10
228 o6.datatype = "range(1,60)"
229
230 o7 = s:taboption("advanced", Value, "curl_retry", translate("Curl download retry"), translate("If curl is installed and detected, it would retry download this many times on timeout/fail."))
231 o7.default = 3
232 o7.datatype = "range(0,30)"
233
234 o8 = s:taboption("advanced", ListValue, "parallel_downloads", translate("Simultaneous processing"), translate("Launch all lists downloads and processing simultaneously, reducing service start time."))
235 o8:value("0", translate("Do not use simultaneous processing"))
236 o8:value("1", translate("Use simultaneous processing"))
237 o8.default = 1
238
239 o10 = s:taboption("advanced", ListValue, "compressed_cache", translate("Store compressed cache file on router"), translate("Attempt to create a compressed cache of block-list in the persistent memory."))
240 o10:value("0", translate("Do not store compressed cache"))
241 o10:value("1", translate("Store compressed cache"))
242 o10.default = "0"
243
244 o11 = s:taboption("advanced", ListValue, "debug", translate("Enable Debugging"), translate("Enables debug output to /tmp/simple-adblock.log."))
245 o11:value("0", translate("Disable Debugging"))
246 o11:value("1", translate("Enable Debugging"))
247 o11.default = "0"
248
249
250 s2 = m:section(NamedSection, "config", "simple-adblock", translate("Whitelist and Blocklist Management"))
251 -- Whitelisted Domains
252 d1 = s2:option(DynamicList, "whitelist_domain", translate("Whitelisted Domains"), translate("Individual domains to be whitelisted."))
253 d1.addremove = false
254 d1.optional = false
255
256 -- Blacklisted Domains
257 d3 = s2:option(DynamicList, "blacklist_domain", translate("Blacklisted Domains"), translate("Individual domains to be blacklisted."))
258 d3.addremove = false
259 d3.optional = false
260
261 -- Whitelisted Domains URLs
262 d2 = s2:option(DynamicList, "whitelist_domains_url", translate("Whitelisted Domain URLs"), translate("URLs to lists of domains to be whitelisted."))
263 d2.addremove = false
264 d2.optional = false
265
266 -- Blacklisted Domains URLs
267 d4 = s2:option(DynamicList, "blacklist_domains_url", translate("Blacklisted Domain URLs"), translate("URLs to lists of domains to be blacklisted."))
268 d4.addremove = false
269 d4.optional = false
270
271 -- Blacklisted Hosts URLs
272 d5 = s2:option(DynamicList, "blacklist_hosts_url", translate("Blacklisted Hosts URLs"), translate("URLs to lists of hosts to be blacklisted."))
273 d5.addremove = false
274 d5.optional = false
275
276 return m