Merge pull request #1361 from musashino205/adblk-upd-ja
[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_first("adblock", "adblock", "adb_trigger") then
13 m = SimpleForm("error", nil, translate("Please update your adblock config file to use this package. ")
14 .. translatef("In OPKG 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 .. "see online documentation</a>", "https://github.com/openwrt/packages/blob/master/net/adblock/files/README.md"))
27
28 function m.on_after_commit(self)
29 function e4.validate(self, value)
30 if value == "0" then
31 luci.sys.call("/etc/init.d/adblock reload >/dev/null 2>&1")
32 else
33 luci.sys.call("/etc/init.d/adblock start >/dev/null 2>&1")
34 end
35 end
36 luci.http.redirect(luci.dispatcher.build_url("admin", "services", "adblock"))
37 end
38
39 -- Main adblock options
40
41 s = m:section(NamedSection, "global", "adblock")
42
43 local parse = json.parse(fs.readfile(adbinput) or "")
44 if parse then
45 status = parse.data.adblock_status
46 version = parse.data.adblock_version
47 domains = parse.data.blocked_domains
48 fetch = parse.data.fetch_utility
49 backend = parse.data.dns_backend
50 rundate = parse.data.last_rundate
51 end
52
53 o1 = s:option(Flag, "adb_enabled", translate("Enable Adblock"))
54 o1.default = o1.disabled
55 o1.rmempty = false
56
57 btn = s:option(Button, "", translate("Suspend / Resume Adblock"))
58 if parse and status == "enabled" then
59 btn.inputtitle = translate("Suspend")
60 btn.inputstyle = "reset"
61 btn.disabled = false
62 function btn.write()
63 luci.sys.call("/etc/init.d/adblock suspend >/dev/null 2>&1")
64 luci.http.redirect(luci.dispatcher.build_url("admin", "services", "adblock"))
65 end
66 elseif parse and status == "paused" then
67 btn.inputtitle = translate("Resume")
68 btn.inputstyle = "apply"
69 btn.disabled = false
70 function btn.write()
71 luci.sys.call("/etc/init.d/adblock resume >/dev/null 2>&1")
72 luci.http.redirect(luci.dispatcher.build_url("admin", "services", "adblock"))
73 end
74 else
75 btn.inputtitle = translate("-------")
76 btn.inputstyle = "button"
77 btn.disabled = true
78 end
79
80 o2 = s:option(ListValue, "adb_dns", translate("DNS Backend (DNS Directory)"),
81 translate("List of supported DNS backends with their default list export directory.<br />")
82 .. translate("To overwrite the default path use the 'DNS Directory' option in the extra section below."))
83 o2:value("dnsmasq", "dnsmasq (/tmp/dnsmasq.d)")
84 o2:value("unbound", "unbound (/var/lib/unbound)")
85 o2:value("named", "bind (/var/lib/bind)")
86 o2:value("kresd", "kresd (/etc/kresd)")
87 o2:value("dnscrypt-proxy","dnscrypt-proxy (/tmp)")
88 o2.default = "dnsmasq"
89 o2.rmempty = false
90
91 o3 = s:option(ListValue, "adb_trigger", translate("Startup Trigger"),
92 translate("List of available network interfaces. By default the startup will be triggered by the 'wan' interface.<br />")
93 .. translate("Choose 'none' to disable automatic startups, 'timed' to use a classic timeout (default 30 sec.) or select another trigger interface."))
94 o3:value("none")
95 o3:value("timed")
96 if dump then
97 local i, v
98 for i, v in ipairs(dump.interface) do
99 if v.interface ~= "loopback" then
100 o3:value(v.interface)
101 end
102 end
103 end
104 o3.rmempty = false
105
106 -- Runtime information
107
108 ds = s:option(DummyValue, "", translate("Runtime Information"))
109 ds.template = "cbi/nullsection"
110
111 dv1 = s:option(DummyValue, "", translate("Adblock Status"))
112 dv1.template = "adblock/runtime"
113 if parse == nil then
114 dv1.value = translate("n/a")
115 else
116 if status == "error" then
117 dv1.value = translate("error")
118 elseif status == "disabled" then
119 dv1.value = translate("disabled")
120 elseif status == "paused" then
121 dv1.value = translate("paused")
122 else
123 dv1.value = translate("enabled")
124 end
125 end
126
127 dv2 = s:option(DummyValue, "", translate("Adblock Version"))
128 dv2.template = "adblock/runtime"
129 if parse == nil then
130 dv2.value = translate("n/a")
131 else
132 dv2.value = version
133 end
134
135 dv3 = s:option(DummyValue, "", translate("Download Utility (SSL Library)"),
136 translate("For SSL protected blocklist sources you need a suitable SSL library, e.g. 'libustream-ssl' or the wget 'built-in'."))
137 dv3.template = "adblock/runtime"
138 if parse == nil then
139 dv3.value = translate("n/a")
140 else
141 dv3.value = fetch
142 end
143
144 dv4 = s:option(DummyValue, "", translate("DNS Backend (DNS Directory)"))
145 dv4.template = "adblock/runtime"
146 if parse == nil then
147 dv4.value = translate("n/a")
148 else
149 dv4.value = backend
150 end
151
152 dv5 = s:option(DummyValue, "", translate("Overall Blocked Domains"))
153 dv5.template = "adblock/runtime"
154 if parse == nil then
155 dv5.value = translate("n/a")
156 else
157 dv5.value = domains
158 end
159
160 dv6 = s:option(DummyValue, "", translate("Last Run"))
161 dv6.template = "adblock/runtime"
162 if parse == nil then
163 dv6.value = translate("n/a")
164 else
165 dv6.value = rundate
166 end
167
168 -- Blocklist table
169
170 bl = m:section(TypedSection, "source", translate("Blocklist Sources"),
171 translate("Available blocklist sources. ")
172 .. translate("List URLs and Shallalist category selections are configurable in the 'Advanced' section.<br />")
173 .. translate("Caution: Please don't select big lists or many lists at once on low memory devices to prevent OOM exceptions!"))
174 bl.template = "cbi/tblsection"
175
176 name = bl:option(Flag, "enabled", translate("Enabled"))
177 name.rmempty = false
178
179 ssl = bl:option(DummyValue, "adb_src", translate("SSL req."))
180 function ssl.cfgvalue(self, section)
181 local source = self.map:get(section, "adb_src")
182 if source and source:match("https://") then
183 return translate("Yes")
184 else
185 return translate("No")
186 end
187 end
188
189 des = bl:option(DummyValue, "adb_src_desc", translate("Description"))
190
191 -- Extra options
192
193 e = m:section(NamedSection, "global", "adblock", translate("Extra Options"),
194 translate("Options for further tweaking in case the defaults are not suitable for you."))
195
196 e1 = e:option(Flag, "adb_debug", translate("Verbose Debug Logging"),
197 translate("Enable verbose debug logging in case of any processing error."))
198 e1.default = e1.disabled
199 e1.rmempty = false
200
201 e2 = e:option(Flag, "adb_forcedns", translate("Force Local DNS"),
202 translate("Redirect all DNS queries from 'lan' zone to the local resolver."))
203 e2.default = e2.disabled
204 e2.rmempty = false
205
206 e3 = e:option(Flag, "adb_forcesrt", translate("Force Overall Sort"),
207 translate("Enable memory intense overall sort / duplicate removal on low memory devices (&lt; 64 MB RAM)"))
208 e3.default = e3.disabled
209 e3.rmempty = false
210
211 e4 = e:option(Flag, "adb_manmode", translate("Manual / Backup mode"),
212 translate("Do not automatically update blocklists during startup, use blocklist backups instead."))
213 e4.default = e4.disabled
214 e4.rmempty = false
215
216 e5 = e:option(Flag, "adb_backup", translate("Enable Blocklist Backup"),
217 translate("Create compressed blocklist backups, they will be used in case of download errors or during startup in manual mode."))
218 e5.default = e5.disabled
219 e5.rmempty = false
220
221 e6 = e:option(Value, "adb_backupdir", translate("Backup Directory"),
222 translate("Target directory for adblock backups. Please use only non-volatile disks, no ram/tmpfs drives."))
223 e6.datatype = "directory"
224 e6.default = "/mnt"
225 e6.rmempty = false
226
227 e7 = e:option(Value, "adb_dnsdir", translate("DNS Directory"),
228 translate("Target directory for the generated blocklist 'adb_list.overall'."))
229 e7.datatype = "directory"
230 e7.optional = true
231
232 e8 = e:option(Value, "adb_triggerdelay", translate("Trigger Delay"),
233 translate("Additional trigger delay in seconds before adblock processing begins."))
234 e8.datatype = "range(1,60)"
235 e8.optional = true
236
237 return m