Merge pull request #3024 from TDT-AG/pr/20190829-material-logo
[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
19 if not targetDNS or targetDNS == "" then
20 targetDNS = "dnsmasq.servers"
21 end
22
23 if targetDNS ~= "dnsmasq.addnhosts" and targetDNS ~= "dnsmasq.conf" and
24 targetDNS ~= "dnsmasq.servers" and targetDNS ~= "unbound.adb_list" then
25 targetDNS = "dnsmasq.servers"
26 end
27
28 if targetDNS == "dnsmasq.addnhosts" then
29 outputFile="/var/run/" .. packageName .. ".addnhosts"
30 outputCache="/var/run/" .. packageName .. ".addnhosts.cache"
31 outputGzip="/etc/" .. packageName .. ".addnhosts.gz"
32 elseif targetDNS == "dnsmasq.conf" then
33 outputFile="/var/dnsmasq.d/" .. packageName .. ""
34 outputCache="/var/run/" .. packageName .. ".dnsmasq.cache"
35 outputGzip="/etc/" .. packageName .. ".dnsmasq.gz"
36 elseif targetDNS == "dnsmasq.servers" then
37 outputFile="/var/run/" .. packageName .. ".servers"
38 outputCache="/var/run/" .. packageName .. ".servers.cache"
39 outputGzip="/etc/" .. packageName .. ".servers.gz"
40 elseif targetDNS == "unbound.adb_list" then
41 outputFile="/var/lib/unbound/adb_list." .. packageName .. ""
42 outputCache="/var/run/" .. packageName .. ".unbound.cache"
43 outputGzip="/etc/" .. packageName .. ".unbound.gz"
44 end
45
46 m = Map("simple-adblock", translate("Simple AdBlock Settings"))
47 m.apply_on_parse = true
48 m.on_after_apply = function(self)
49 sys.call("/etc/init.d/simple-adblock restart")
50 end
51
52 local tmpfs
53 if fs.access("/var/run/" .. packageName .. ".json") then
54 tmpfs = jsonc.parse(util.trim(sys.exec("cat /var/run/" .. packageName .. ".json")))
55 end
56
57 local tmpfsVersion, tmpfsStatus, tmpfsMessage, tmpfsError, tmpfsStats = "", "Stopped"
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 end
74 end
75
76 h = m:section(NamedSection, "config", "simple-adblock", translate("Service Status") .. tmpfsVersion)
77
78 if tmpfsStatus and tmpfsStatus:match("ing") then
79 ss = h:option(DummyValue, "_dummy", translate("Service Status"))
80 ss.template = "simple-adblock/status"
81 ss.value = tmpfsStatus .. '...'
82 if tmpfsMessage then
83 sm = h:option(DummyValue, "_dummy", translate("Task"))
84 sm.template = "simple-adblock/status"
85 sm.value = tmpfsMessage
86 end
87 else
88 en = h:option(Button, "__toggle")
89 if enabledFlag ~= "1" or tmpfsStatus:match("Stopped") then
90 en.title = translate("Service is disabled/stopped")
91 en.inputtitle = translate("Enable/Start")
92 en.inputstyle = "apply important"
93 if fs.access(outputCache) then
94 sm = h:option(DummyValue, "_dummy", translate("Info"))
95 sm.template = "simple-adblock/status"
96 sm.value = "Cache file containing " .. util.trim(sys.exec("wc -l < " .. outputCache)) .. " domains found."
97 elseif fs.access(outputGzip) then
98 sm = h:option(DummyValue, "_dummy", translate("Info"))
99 sm.template = "simple-adblock/status"
100 sm.value = "Compressed cache file found."
101 end
102 else
103 en.title = translate("Service is enabled/started")
104 en.inputtitle = translate("Stop/Disable")
105 en.inputstyle = "reset important"
106 ss = h:option(DummyValue, "_dummy", translate("Service Status"))
107 ss.template = "simple-adblock/status"
108 ss.value = tmpfsStatus
109 if tmpfsMessage then
110 ms = h:option(DummyValue, "_dummy", translate("Message"))
111 ms.template = "simple-adblock/status"
112 ms.value = tmpfsMessage
113 end
114 if tmpfsError then
115 es = h:option(DummyValue, "_dummy", translate("Collected Errors"))
116 es.template = "simple-adblock/status"
117 es.value = tmpfsError
118 reload = h:option(Button, "__reload")
119 reload.title = translate("Service started with error")
120 reload.inputtitle = translate("Reload")
121 reload.inputstyle = "apply important"
122 function reload.write()
123 sys.exec("/etc/init.d/simple-adblock reload")
124 http.redirect(dispatcher.build_url("admin/services/" .. packageName))
125 end
126 end
127 end
128 function en.write()
129 if tmpfsStatus and tmpfsStatus:match("Stopped") then
130 enabledFlag = "1"
131 else
132 enabledFlag = enabledFlag == "1" and "0" or "1"
133 end
134 uci:set(packageName, "config", "enabled", enabledFlag)
135 uci:save(packageName)
136 uci:commit(packageName)
137 if enabledFlag == "0" then
138 luci.sys.init.stop(packageName)
139 else
140 luci.sys.init.enable(packageName)
141 luci.sys.init.start(packageName)
142 end
143 luci.http.redirect(luci.dispatcher.build_url("admin/services/" .. packageName))
144 end
145 end
146
147 s = m:section(NamedSection, "config", "simple-adblock", translate("Configuration"))
148 -- General options
149 s:tab("basic", translate("Basic Configuration"))
150
151 o2 = s:taboption("basic", ListValue, "verbosity", translate("Output Verbosity Setting"),translate("Controls system log and console output verbosity."))
152 o2:value("0", translate("Suppress output"))
153 o2:value("1", translate("Some output"))
154 o2:value("2", translate("Verbose output"))
155 o2.default = 2
156
157 o3 = s:taboption("basic", ListValue, "force_dns", translate("Force Router DNS"), translate("Forces Router DNS use on local devices, also known as DNS Hijacking."))
158 o3:value("0", translate("Let local devices use their own DNS servers if set"))
159 o3:value("1", translate("Force Router DNS server to all local devices"))
160 o3.default = 1
161
162 local sysfs_path = "/sys/class/leds/"
163 local leds = {}
164 if nixio.fs.access(sysfs_path) then
165 leds = nixio.util.consume((nixio.fs.dir(sysfs_path)))
166 end
167 if #leds ~= 0 then
168 o4 = s:taboption("basic", Value, "led", translate("LED to indicate status"), translate("Pick the LED not already used in")
169 .. [[ <a href="]] .. luci.dispatcher.build_url("admin/system/leds") .. [[">]]
170 .. translate("System LED Configuration") .. [[</a>]] .. ".")
171 o4.rmempty = false
172 o4:value("", translate("none"))
173 for k, v in ipairs(leds) do
174 o4:value(v)
175 end
176 end
177
178 s:tab("advanced", translate("Advanced Configuration"))
179
180 dns = s:taboption("advanced", ListValue, "dns", translate("DNS Service"), translate("Pick the DNS resolution option to create the adblock list for, see the") .. " "
181 .. [[<a href="]] .. readmeURL .. [[#dns-resolution-option" target="_blank">]]
182 .. translate("README") .. [[</a>]] .. " " .. translate("for details."))
183 dns:value("dnsmasq.addnhosts", translate("DNSMASQ Additional Hosts"))
184 dns:value("dnsmasq.conf", translate("DNSMASQ Config"))
185 dns:value("dnsmasq.servers", translate("DNSMASQ Servers File"))
186 dns:value("unbound.adb_list", translate("Unbound AdBlock List"))
187 dns.default = "dnsmasq.servers"
188
189 ipv6 = s:taboption("advanced", ListValue, "ipv6_enabled", translate("IPv6 Support"), translate("Add IPv6 entries to block-list."))
190 ipv6:value("", translate("Do not add IPv6 entries"))
191 ipv6:value("1", translate("Add IPv6 entries"))
192 ipv6:depends({dns="dnsmasq.addnhosts"})
193 ipv6.default = ""
194 ipv6.rmempty = true
195
196 o5 = s:taboption("advanced", Value, "boot_delay", translate("Delay (in seconds) for on-boot start"), translate("Run service after set delay on boot."))
197 o5.default = 120
198 o5.datatype = "range(1,600)"
199
200 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."))
201 o6.default = 10
202 o6.datatype = "range(1,60)"
203
204 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."))
205 o7.default = 3
206 o7.datatype = "range(0,30)"
207
208 o8 = s:taboption("advanced", ListValue, "parallel_downloads", translate("Simultaneous processing"), translate("Launch all lists downloads and processing simultaneously, reducing service start time."))
209 o8:value("0", translate("Do not use simultaneous processing"))
210 o8:value("1", translate("Use simultaneous processing"))
211 o8.default = 1
212
213 o9 = s:taboption("advanced", ListValue, "allow_non_ascii", translate("Allow Non-ASCII characters in DNSMASQ file"), translate("Only enable if your version of DNSMASQ supports the use of Non-ASCII characters, otherwise DNSMASQ will fail to start."))
214 o9:value("0", translate("Do not allow Non-ASCII"))
215 o9:value("1", translate("Allow Non-ASCII"))
216 o9.default = "0"
217
218 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."))
219 o10:value("0", translate("Do not store compressed cache"))
220 o10:value("1", translate("Store compressed cache"))
221 o10.default = "0"
222
223 o11 = s:taboption("advanced", ListValue, "debug", translate("Enable Debugging"), translate("Enables debug output to /tmp/simple-adblock.log."))
224 o11:value("0", translate("Disable Debugging"))
225 o11:value("1", translate("Enable Debugging"))
226 o11.default = "0"
227
228
229 s2 = m:section(NamedSection, "config", "simple-adblock", translate("Whitelist and Blocklist Management"))
230 -- Whitelisted Domains
231 d1 = s2:option(DynamicList, "whitelist_domain", translate("Whitelisted Domains"), translate("Individual domains to be whitelisted."))
232 d1.addremove = false
233 d1.optional = false
234
235 -- Blacklisted Domains
236 d3 = s2:option(DynamicList, "blacklist_domain", translate("Blacklisted Domains"), translate("Individual domains to be blacklisted."))
237 d3.addremove = false
238 d3.optional = false
239
240 -- Whitelisted Domains URLs
241 d2 = s2:option(DynamicList, "whitelist_domains_url", translate("Whitelisted Domain URLs"), translate("URLs to lists of domains to be whitelisted."))
242 d2.addremove = false
243 d2.optional = false
244
245 -- Blacklisted Domains URLs
246 d4 = s2:option(DynamicList, "blacklist_domains_url", translate("Blacklisted Domain URLs"), translate("URLs to lists of domains to be blacklisted."))
247 d4.addremove = false
248 d4.optional = false
249
250 -- Blacklisted Hosts URLs
251 d5 = s2:option(DynamicList, "blacklist_hosts_url", translate("Blacklisted Hosts URLs"), translate("URLs to lists of hosts to be blacklisted."))
252 d5.addremove = false
253 d5.optional = false
254
255 return m