Merge pull request #2643 from kuoruan/aria2
[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 packageName = "simple-adblock"
5 local uci = require "luci.model.uci".cursor()
6 local util = require "luci.util"
7 local sys = require "luci.sys"
8 local jsonc = require "luci.jsonc"
9 local fs = require "nixio.fs"
10 local http = require "luci.http"
11 local dispatcher = require "luci.dispatcher"
12 local enabledFlag = uci:get(packageName, "config", "enabled")
13 local command
14
15 m = Map("simple-adblock", translate("Simple AdBlock Settings"))
16 m.apply_on_parse = true
17 m.on_after_apply = function(self)
18 sys.call("/etc/init.d/simple-adblock restart")
19 end
20
21 local tmpfs
22 if fs.access("/var/run/" .. packageName .. ".json") then
23 tmpfs = jsonc.parse(util.trim(sys.exec("cat /var/run/" .. packageName .. ".json")))
24 end
25
26 local tmpfsVersion, tmpfsStatus, tmpfsMessage, tmpfsError, tmpfsStats = "", "Stopped"
27 if tmpfs and tmpfs['data'] then
28 if tmpfs['data']['status'] and tmpfs['data']['status'] ~= "" then
29 tmpfsStatus = tmpfs['data']['status']
30 end
31 if tmpfs['data']['message'] and tmpfs['data']['message'] ~= "" then
32 tmpfsMessage = tmpfs['data']['message']
33 end
34 if tmpfs['data']['error'] and tmpfs['data']['error'] ~= "" then
35 tmpfsError = tmpfs['data']['error']
36 end
37 if tmpfs['data']['stats'] and tmpfs['data']['stats'] ~= "" then
38 tmpfsStats = tmpfs['data']['stats']
39 end
40 if tmpfs['data']['version'] and tmpfs['data']['version'] ~= "" then
41 tmpfsVersion = " (" .. packageName .. " " .. tmpfs['data']['version'] .. ")"
42 end
43 end
44
45 h = m:section(NamedSection, "config", "simple-adblock", translate("Service Status") .. tmpfsVersion)
46
47 if tmpfsStatus and tmpfsStatus:match("ing") then
48 ss = h:option(DummyValue, "_dummy", translate("Service Status"))
49 ss.template = "simple-adblock/status"
50 ss.value = tmpfsStatus .. '...'
51 if tmpfsMessage then
52 sm = h:option(DummyValue, "_dummy", translate("Task"))
53 sm.template = "simple-adblock/status"
54 sm.value = tmpfsMessage
55 end
56 else
57 en = h:option(Button, "__toggle")
58 if enabledFlag ~= "1" or tmpfsStatus:match("Stopped") then
59 en.title = translate("Service is disabled/stopped")
60 en.inputtitle = translate("Enable/Start")
61 en.inputstyle = "apply important"
62 if fs.access("/var/run/simple-adblock.cache") then
63 sm = h:option(DummyValue, "_dummy", translate("Info"))
64 sm.template = "simple-adblock/status"
65 sm.value = "Cache file containing " .. util.trim(sys.exec("wc -l < /var/run/simple-adblock.cache")) .. " domains found."
66 elseif fs.access("/etc/$simple-adblock.gz") then
67 sm = h:option(DummyValue, "_dummy", translate("Info"))
68 sm.template = "simple-adblock/status"
69 sm.value = "Compressed cache file found."
70 end
71 else
72 en.title = translate("Service is enabled/started")
73 en.inputtitle = translate("Stop/Disable")
74 en.inputstyle = "reset important"
75 ss = h:option(DummyValue, "_dummy", translate("Service Status"))
76 ss.template = "simple-adblock/status"
77 ss.value = tmpfsStatus
78 if tmpfsMessage then
79 ms = h:option(DummyValue, "_dummy", translate("Message"))
80 ms.template = "simple-adblock/status"
81 ms.value = tmpfsMessage
82 end
83 if tmpfsError then
84 es = h:option(DummyValue, "_dummy", translate("Collected Errors"))
85 es.template = "simple-adblock/status"
86 es.value = tmpfsError
87 reload = h:option(Button, "__reload")
88 reload.title = translate("Service started with error")
89 reload.inputtitle = translate("Reload")
90 reload.inputstyle = "apply important"
91 function reload.write()
92 sys.exec("/etc/init.d/simple-adblock reload")
93 http.redirect(dispatcher.build_url("admin/services/" .. packageName))
94 end
95 end
96 end
97 function en.write()
98 if tmpfsStatus and tmpfsStatus:match("Stopped") then
99 enabledFlag = "1"
100 else
101 enabledFlag = enabledFlag == "1" and "0" or "1"
102 end
103 uci:set(packageName, "config", "enabled", enabledFlag)
104 uci:save(packageName)
105 uci:commit(packageName)
106 if enabledFlag == "0" then
107 luci.sys.init.stop(packageName)
108 else
109 luci.sys.init.enable(packageName)
110 luci.sys.init.start(packageName)
111 end
112 luci.http.redirect(luci.dispatcher.build_url("admin/services/" .. packageName))
113 end
114 end
115
116 s = m:section(NamedSection, "config", "simple-adblock", translate("Configuration"))
117 -- General options
118 s:tab("basic", translate("Basic Configuration"))
119
120 o2 = s:taboption("basic", ListValue, "verbosity", translate("Output Verbosity Setting"),translate("Controls system log and console output verbosity"))
121 o2:value("0", translate("Suppress output"))
122 o2:value("1", translate("Some output"))
123 o2:value("2", translate("Verbose output"))
124 o2.rmempty = false
125 o2.default = 2
126
127 o3 = s:taboption("basic", ListValue, "force_dns", translate("Force Router DNS"), translate("Forces Router DNS use on local devices, also known as DNS Hijacking"))
128 o3:value("0", translate("Let local devices use their own DNS servers if set"))
129 o3:value("1", translate("Force Router DNS server to all local devices"))
130 o3.rmempty = false
131 o3.default = 1
132
133 local sysfs_path = "/sys/class/leds/"
134 local leds = {}
135 if nixio.fs.access(sysfs_path) then
136 leds = nixio.util.consume((nixio.fs.dir(sysfs_path)))
137 end
138 if #leds ~= 0 then
139 o4 = s:taboption("basic", Value, "led", translate("LED to indicate status"), translate("Pick the LED not already used in")
140 .. [[ <a href="]] .. luci.dispatcher.build_url("admin/system/leds") .. [[">]]
141 .. translate("System LED Configuration") .. [[</a>]])
142 o4.rmempty = false
143 o4:value("", translate("none"))
144 for k, v in ipairs(leds) do
145 o4:value(v)
146 end
147 end
148
149 s:tab("advanced", translate("Advanced Configuration"))
150
151 o6 = s:taboption("advanced", Value, "boot_delay", translate("Delay (in seconds) for on-boot start"), translate("Run service after set delay on boot"))
152 o6.default = 120
153 o6.datatype = "range(1,600)"
154
155 o7 = 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"))
156 o7.default = 10
157 o7.datatype = "range(1,60)"
158
159 o5 = s:taboption("advanced", ListValue, "parallel_downloads", translate("Simultaneous processing"), translate("Launch all lists downloads and processing simultaneously, reducing service start time"))
160 o5:value("0", translate("Do not use simultaneous processing"))
161 o5:value("1", translate("Use simultaneous processing"))
162 o5.rmempty = false
163 o5.default = 1
164
165 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."))
166 o9:value("0", translate("Do not allow Non-ASCII"))
167 o9:value("1", translate("Allow Non-ASCII"))
168 o9.rmempty = false
169 o9.default = "0"
170
171 o10 = s:taboption("advanced", ListValue, "compressed_cache", translate("Store compressed cache file on router"), translate("Attempt to create a compressed cache of final block-list on the router."))
172 o10:value("0", translate("Do not store compressed cache"))
173 o10:value("1", translate("Store compressed cache"))
174 o10.rmempty = false
175 o10.default = "0"
176
177 o8 = s:taboption("advanced", ListValue, "debug", translate("Enable Debugging"), translate("Enables debug output to /tmp/simple-adblock.log"))
178 o8:value("0", translate("Disable Debugging"))
179 o8:value("1", translate("Enable Debugging"))
180 o8.rmempty = false
181 o8.default = "0"
182
183
184 s2 = m:section(NamedSection, "config", "simple-adblock", translate("Whitelist and Blocklist Management"))
185 -- Whitelisted Domains
186 d1 = s2:option(DynamicList, "whitelist_domain", translate("Whitelisted Domains"), translate("Individual domains to be whitelisted"))
187 d1.addremove = false
188 d1.optional = false
189
190 -- Blacklisted Domains
191 d3 = s2:option(DynamicList, "blacklist_domain", translate("Blacklisted Domains"), translate("Individual domains to be blacklisted"))
192 d3.addremove = false
193 d3.optional = false
194
195 -- Whitelisted Domains URLs
196 d2 = s2:option(DynamicList, "whitelist_domains_url", translate("Whitelisted Domain URLs"), translate("URLs to lists of domains to be whitelisted"))
197 d2.addremove = false
198 d2.optional = false
199
200 -- Blacklisted Domains URLs
201 d4 = s2:option(DynamicList, "blacklist_domains_url", translate("Blacklisted Domain URLs"), translate("URLs to lists of domains to be blacklisted"))
202 d4.addremove = false
203 d4.optional = false
204
205 -- Blacklisted Hosts URLs
206 d5 = s2:option(DynamicList, "blacklist_hosts_url", translate("Blacklisted Hosts URLs"), translate("URLs to lists of hosts to be blacklisted"))
207 d5.addremove = false
208 d5.optional = false
209
210 return m