408d07026275fb4e13b1189ae9b38337d33dc26f
[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 nutil = require "nixio.util"
14 local http = require "luci.http"
15 local dispatcher = require "luci.dispatcher"
16 local enabledFlag = uci:get(packageName, "config", "enabled")
17 local command, outputFile, outputCache, outputGzip
18 local targetDNS = uci:get(packageName, "config", "dns")
19 local checkDnsmasq = sys.call("which dnsmasq >/dev/null 2>&1") == 0 and true
20 local checkUnbound = sys.call("which unbound >/dev/null 2>&1") == 0 and true
21 local checkDnsmasqIpset = sys.call("dnsmasq -v 2>/dev/null | grep -q 'no-ipset' || ! dnsmasq -v 2>/dev/null | grep -q -w 'ipset'") ~= 0
22 and sys.call("ipset help hash:net >/dev/null 2>&1") and true
23
24 if not targetDNS or targetDNS == "" then
25 targetDNS = "dnsmasq.servers"
26 end
27
28 if targetDNS ~= "dnsmasq.addnhosts" and targetDNS ~= "dnsmasq.conf" and
29 targetDNS ~= "dnsmasq.ipset" and targetDNS ~= "dnsmasq.servers" and
30 targetDNS ~= "unbound.adb_list" then
31 targetDNS = "dnsmasq.servers"
32 end
33
34 if targetDNS == "dnsmasq.addnhosts" then
35 outputFile="/var/run/" .. packageName .. ".addnhosts"
36 outputCache="/var/run/" .. packageName .. ".addnhosts.cache"
37 outputGzip="/etc/" .. packageName .. ".addnhosts.gz"
38 elseif targetDNS == "dnsmasq.conf" then
39 outputFile="/var/dnsmasq.d/" .. packageName .. ""
40 outputCache="/var/run/" .. packageName .. ".dnsmasq.cache"
41 outputGzip="/etc/" .. packageName .. ".dnsmasq.gz"
42 elseif targetDNS == "dnsmasq.servers" then
43 outputFile="/var/run/" .. packageName .. ".servers"
44 outputCache="/var/run/" .. packageName .. ".servers.cache"
45 outputGzip="/etc/" .. packageName .. ".servers.gz"
46 elseif targetDNS == "unbound.adb_list" then
47 outputFile="/var/lib/unbound/adb_list." .. packageName .. ""
48 outputCache="/var/run/" .. packageName .. ".unbound.cache"
49 outputGzip="/etc/" .. packageName .. ".unbound.gz"
50 end
51
52 local tmpfs, tmpfsMessage, tmpfsError, tmpfsStats
53 local tmpfsVersion, tmpfsStatus = "", "Stopped"
54 if fs.access("/var/run/" .. packageName .. ".json") then
55 tmpfs = jsonc.parse(util.trim(sys.exec("cat /var/run/" .. packageName .. ".json")))
56 end
57
58 if tmpfs and tmpfs['data'] then
59 if tmpfs['data']['status'] and tmpfs['data']['status'] ~= "" then
60 tmpfsStatus = tmpfs['data']['status']
61 end
62 if tmpfs['data']['message'] and tmpfs['data']['message'] ~= "" then
63 tmpfsMessage = tmpfs['data']['message']
64 end
65 if tmpfs['data']['error'] and tmpfs['data']['error'] ~= "" then
66 tmpfsError = tmpfs['data']['error']
67 end
68 if tmpfs['data']['stats'] and tmpfs['data']['stats'] ~= "" then
69 tmpfsStats = tmpfs['data']['stats']
70 end
71 if tmpfs['data']['version'] and tmpfs['data']['version'] ~= "" then
72 tmpfsVersion = packageName .. " " .. tmpfs['data']['version']
73 else
74 tmpfsVersion = packageName
75 end
76 end
77
78 local statusTable = {}
79 local errorTable = {}
80 statusTable["statusNoInstall"] = translatef("%s is not installed or not found", packageName)
81 statusTable["statusStopped"] = translate("Stopped")
82 statusTable["statusStarting"] = translate("Starting")
83 statusTable["statusRestarting"] = translate("Restarting")
84 statusTable["statusForceReloading"] = translate("Force Reloading")
85 statusTable["statusDownloading"] = translate("Downloading")
86 statusTable["statusError"] = translate("Error")
87 statusTable["statusWarning"] = translate("Warning")
88 statusTable["statusFail"] = translate("Fail")
89 statusTable["statusSuccess"] = translate("Success")
90 errorTable["errorOutputFileCreate"] = translatef("failed to create '%s' file", outputFile)
91 errorTable["errorFailDNSReload"] = translate("failed to restart/reload DNS resolver")
92 errorTable["errorSharedMemory"] = translate("failed to access shared memory")
93 errorTable["errorSorting"] = translate("failed to sort data file")
94 errorTable["errorOptimization"] = translate("failed to optimize data file")
95 errorTable["errorWhitelistProcessing"] = translate("failed to process whitelist")
96 errorTable["errorDataFileFormatting"] = translate("failed to format data file")
97 errorTable["errorMovingDataFile"] = translatef("failed to move temporary data file to '%s'", outputFile)
98 errorTable["errorCreatingCompressedCache"] = translate("failed to create compressed cache")
99 errorTable["errorRemovingTempFiles"] = translate("failed to remove temporary files")
100 errorTable["errorRestoreCompressedCache"] = translate("failed to unpack compressed cache")
101 errorTable["errorRestoreCache"] = translatef("failed to move '%s' to '%s'", outputCache, outputFile)
102 errorTable["errorOhSnap"] = translate("failed to create blocklist or restart DNS resolver")
103 errorTable["errorStopping"] = translatef("failed to stop %s", packageName)
104 errorTable["errorDNSReload"] = translate("failed to reload/restart DNS resolver")
105 errorTable["errorDownloadingList"] = translate("failed to download")
106 errorTable["errorParsingList"] = translate("failed to parse")
107
108 m = Map("simple-adblock", translate("Simple AdBlock Settings"))
109 m.apply_on_parse = true
110 m.on_after_apply = function(self)
111 sys.call("/etc/init.d/simple-adblock restart")
112 end
113
114 h = m:section(NamedSection, "config", "simple-adblock", translatef("Service Status [%s]", tmpfsVersion))
115
116 if tmpfsStatus == "statusStarting" or
117 tmpfsStatus == "statusRestarting" or
118 tmpfsStatus == "statusForceReloading" or
119 tmpfsStatus == "statusDownloading" then
120 ss = h:option(DummyValue, "_dummy", translate("Service Status"))
121 ss.template = "simple-adblock/status"
122 ss.value = statusTable[tmpfsStatus] .. '...'
123 if tmpfsMessage then
124 sm = h:option(DummyValue, "_dummy", translate("Task"))
125 sm.template = "simple-adblock/status"
126 sm.value = tmpfsMessage
127 end
128 else
129 if tmpfsStatus == "statusStopped" then
130 ss = h:option(DummyValue, "_dummy", translate("Service Status"))
131 ss.template = "simple-adblock/status"
132 ss.value = statusTable[tmpfsStatus]
133 if fs.access(outputCache) then
134 sm = h:option(DummyValue, "_dummy", translate("Info"))
135 sm.template = "simple-adblock/status"
136 sm.value = translatef("Cache file containing %s domains found.", util.trim(sys.exec("wc -l < " .. outputCache)))
137 elseif fs.access(outputGzip) then
138 sm = h:option(DummyValue, "_dummy", translate("Info"))
139 sm.template = "simple-adblock/status"
140 sm.value = translate("Compressed cache file found.")
141 end
142 else
143 ss = h:option(DummyValue, "_dummy", translate("Service Status"))
144 ss.template = "simple-adblock/status"
145 if tmpfsStatus == "statusSuccess" then
146 ss.value = translatef("%s is blocking %s domains (with %s).", tmpfsVersion, util.trim(sys.exec("wc -l < " .. outputFile)), targetDNS)
147 else
148 ss.value = statusTable[tmpfsStatus]
149 end
150 if tmpfsMessage then
151 ms = h:option(DummyValue, "_dummy", translate("Message"))
152 ms.template = "simple-adblock/status"
153 ms.value = tmpfsMessage
154 end
155 if tmpfsError then
156 es = h:option(DummyValue, "_dummy", translate("Collected Errors"))
157 es.template = "simple-adblock/error"
158 es.value = ""
159 local err, e, url
160 for err in tmpfsError:gmatch("[%p%w]+") do
161 if err:match("=") then
162 e,url = err:match("(.+)=(.+)")
163 es.value = translatef("%s Error: %s %s", es.value, errorTable[e], url) .. ".\n"
164 else
165 es.value = translatef("%s Error: %s", es.value, errorTable[err]) .. ".\n"
166 end
167 end
168 end
169 end
170 if tmpfsVersion ~= "" then
171 buttons = h:option(DummyValue, "_dummy")
172 buttons.template = packageName .. "/buttons"
173 end
174 end
175
176 s = m:section(NamedSection, "config", "simple-adblock", translate("Configuration"))
177 -- General options
178 s:tab("basic", translate("Basic Configuration"))
179
180 o2 = s:taboption("basic", ListValue, "verbosity", translate("Output Verbosity Setting"), translate("Controls system log and console output verbosity."))
181 o2:value("0", translate("Suppress output"))
182 o2:value("1", translate("Some output"))
183 o2:value("2", translate("Verbose output"))
184 o2.default = 2
185
186 o3 = s:taboption("basic", ListValue, "force_dns", translate("Force Router DNS"), translate("Forces Router DNS use on local devices, also known as DNS Hijacking."))
187 o3:value("0", translate("Let local devices use their own DNS servers if set"))
188 o3:value("1", translate("Force Router DNS server to all local devices"))
189 o3.default = 1
190
191 local sysfs_path = "/sys/class/leds/"
192 local leds = {}
193 if fs.access(sysfs_path) then
194 leds = nutil.consume((fs.dir(sysfs_path)))
195 end
196 if #leds ~= 0 then
197 o4 = s:taboption("basic", Value, "led", translate("LED to indicate status"),
198 translatef("Pick the LED not already used in <a href=\"%s\">System LED Configuration</a>.", dispatcher.build_url("admin", "system", "leds")))
199 o4.rmempty = false
200 o4:value("", translate("none"))
201 for k, v in ipairs(leds) do
202 o4:value(v)
203 end
204 end
205
206 s:tab("advanced", translate("Advanced Configuration"))
207
208 local dns_descr = translatef("Pick the DNS resolution option to create the adblock list for, see the <a href=\"%s#dns-resolution-option\" target=\"_blank\">README</a> for details.", readmeURL)
209
210 if not checkDnsmasq then
211 dns_descr = dns_descr .. "<br />" .. translatef("Please note that %s is not supported on this system.", "<i>dnsmasq.addnhosts</i>")
212 dns_descr = dns_descr .. "<br />" .. translatef("Please note that %s is not supported on this system.", "<i>dnsmasq.conf</i>")
213 dns_descr = dns_descr .. "<br />" .. translatef("Please note that %s is not supported on this system.", "<i>dnsmasq.ipset</i>")
214 dns_descr = dns_descr .. "<br />" .. translatef("Please note that %s is not supported on this system.", "<i>dnsmasq.servers</i>")
215 elseif not checkDnsmasqIpset then
216 dns_descr = dns_descr .. "<br />" .. translatef("Please note that %s is not supported on this system.", "<i>dnsmasq.ipset</i>")
217 end
218 if not checkUnbound then
219 dns_descr = dns_descr .. "<br />" .. translatef("Please note that %s is not supported on this system.", "<i>unbound.adb_list</i>")
220 end
221
222 dns = s:taboption("advanced", ListValue, "dns", translate("DNS Service"), dns_descr)
223 if checkDnsmasq then
224 dns:value("dnsmasq.addnhosts", translate("DNSMASQ Additional Hosts"))
225 dns:value("dnsmasq.conf", translate("DNSMASQ Config"))
226 if checkDnsmasqIpset then
227 dns:value("dnsmasq.ipset", translate("DNSMASQ IP Set"))
228 end
229 dns:value("dnsmasq.servers", translate("DNSMASQ Servers File"))
230 end
231 if checkUnbound then
232 dns:value("unbound.adb_list", translate("Unbound AdBlock List"))
233 end
234 dns.default = "dnsmasq.servers"
235
236 ipv6 = s:taboption("advanced", ListValue, "ipv6_enabled", translate("IPv6 Support"), translate("Add IPv6 entries to block-list."))
237 ipv6:value("", translate("Do not add IPv6 entries"))
238 ipv6:value("1", translate("Add IPv6 entries"))
239 ipv6:depends({dns="dnsmasq.addnhosts"})
240 ipv6.default = ""
241 ipv6.rmempty = true
242
243 o5 = s:taboption("advanced", Value, "boot_delay", translate("Delay (in seconds) for on-boot start"), translate("Run service after set delay on boot."))
244 o5.default = 120
245 o5.datatype = "range(1,600)"
246
247 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."))
248 o6.default = 10
249 o6.datatype = "range(1,60)"
250
251 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."))
252 o7.default = 3
253 o7.datatype = "range(0,30)"
254
255 o8 = s:taboption("advanced", ListValue, "parallel_downloads", translate("Simultaneous processing"), translate("Launch all lists downloads and processing simultaneously, reducing service start time."))
256 o8:value("0", translate("Do not use simultaneous processing"))
257 o8:value("1", translate("Use simultaneous processing"))
258 o8.default = 1
259
260 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."))
261 o10:value("0", translate("Do not store compressed cache"))
262 o10:value("1", translate("Store compressed cache"))
263 o10.default = "0"
264
265 o11 = s:taboption("advanced", ListValue, "debug", translate("Enable Debugging"), translate("Enables debug output to /tmp/simple-adblock.log."))
266 o11:value("0", translate("Disable Debugging"))
267 o11:value("1", translate("Enable Debugging"))
268 o11.default = "0"
269
270
271 s2 = m:section(NamedSection, "config", "simple-adblock", translate("Whitelist and Blocklist Management"))
272 -- Whitelisted Domains
273 d1 = s2:option(DynamicList, "whitelist_domain", translate("Whitelisted Domains"), translate("Individual domains to be whitelisted."))
274 d1.addremove = false
275 d1.optional = false
276
277 -- Blacklisted Domains
278 d3 = s2:option(DynamicList, "blacklist_domain", translate("Blacklisted Domains"), translate("Individual domains to be blacklisted."))
279 d3.addremove = false
280 d3.optional = false
281
282 -- Whitelisted Domains URLs
283 d2 = s2:option(DynamicList, "whitelist_domains_url", translate("Whitelisted Domain URLs"), translate("URLs to lists of domains to be whitelisted."))
284 d2.addremove = false
285 d2.optional = false
286
287 -- Blacklisted Domains URLs
288 d4 = s2:option(DynamicList, "blacklist_domains_url", translate("Blacklisted Domain URLs"), translate("URLs to lists of domains to be blacklisted."))
289 d4.addremove = false
290 d4.optional = false
291
292 -- Blacklisted Hosts URLs
293 d5 = s2:option(DynamicList, "blacklist_hosts_url", translate("Blacklisted Hosts URLs"), translate("URLs to lists of hosts to be blacklisted."))
294 d5.addremove = false
295 d5.optional = false
296
297 return m