d594ec9ec3e120353e9ea737efa0fb444dde25d7
[project/luci.git] / applications / luci-app-adblock / htdocs / luci-static / resources / view / adblock / dnsreport.js
1 'use strict';
2 'require view';
3 'require fs';
4 'require ui';
5
6 /*
7 button handling
8 */
9 function handleAction(ev) {
10 if (ev.target && ev.target.getAttribute('name') === 'blacklist') {
11 L.ui.showModal(_('Add Blacklist Domain'), [
12 E('p', _('Add this (sub-)domain to your local blacklist.')),
13 E('div', { 'class': 'left', 'style': 'display:flex; flex-direction:column' }, [
14 E('label', { 'class': 'cbi-input-text', 'style': 'padding-top:.5em' }, [
15 E('input', { 'class': 'cbi-input-text', 'style': 'width:300px', 'spellcheck': 'false', 'id': 'blacklist', 'value': ev.target.getAttribute('value') }, [])
16 ])
17 ]),
18 E('div', { 'class': 'right' }, [
19 E('button', {
20 'class': 'btn cbi-button',
21 'click': L.hideModal
22 }, _('Cancel')),
23 ' ',
24 E('button', {
25 'class': 'btn cbi-button-action',
26 'click': ui.createHandlerFn(this, function(ev) {
27 L.resolveDefault(fs.read_direct('/etc/adblock/adblock.blacklist'), '')
28 .then(function(res) {
29 var domain = document.getElementById('blacklist').value.trim().toLowerCase().replace(/[^a-z0-9\.\-]/g,'');
30 var pattern = new RegExp('^' + domain.replace(/[\.]/g,'\\.') + '$', 'm');
31 if (res.search(pattern) === -1) {
32 var blacklist = res + domain + '\n';
33 fs.write('/etc/adblock/adblock.blacklist', blacklist);
34 ui.addNotification(null, E('p', _('Blacklist changes have been saved. Refresh your adblock lists that changes take effect.')), 'info');
35 }
36 L.hideModal();
37 });
38 })
39 }, _('Save'))
40 ])
41 ]);
42 document.getElementById('blacklist').focus();
43 }
44
45 if (ev.target && ev.target.getAttribute('name') === 'whitelist') {
46 L.ui.showModal(_('Add Whitelist Domain'), [
47 E('p', _('Add this (sub-)domain to your local whitelist.')),
48 E('div', { 'class': 'left', 'style': 'display:flex; flex-direction:column' }, [
49 E('label', { 'class': 'cbi-input-text', 'style': 'padding-top:.5em' }, [
50 E('input', { 'class': 'cbi-input-text', 'style': 'width:300px', 'spellcheck': 'false', 'id': 'whitelist', 'value': ev.target.getAttribute('value') }, [])
51 ])
52 ]),
53 E('div', { 'class': 'right' }, [
54 E('button', {
55 'class': 'btn cbi-button',
56 'click': L.hideModal
57 }, _('Cancel')),
58 ' ',
59 E('button', {
60 'class': 'btn cbi-button-action',
61 'click': ui.createHandlerFn(this, function(ev) {
62 L.resolveDefault(fs.read_direct('/etc/adblock/adblock.whitelist'), '')
63 .then(function(res) {
64 var domain = document.getElementById('whitelist').value.trim().toLowerCase().replace(/[^a-z0-9\.\-]/g,'');
65 var pattern = new RegExp('^' + domain.replace(/[\.]/g,'\\.') + '$', 'm');
66 if (res.search(pattern) === -1) {
67 var whitelist = res + domain + '\n';
68 fs.write('/etc/adblock/adblock.whitelist', whitelist);
69 ui.addNotification(null, E('p', _('Whitelist changes have been saved. Refresh your adblock lists that changes take effect.')), 'info');
70 }
71 L.hideModal();
72 });
73 })
74 }, _('Save'))
75 ])
76 ]);
77 document.getElementById('whitelist').focus();
78 }
79
80 if (ev === 'query') {
81 L.ui.showModal(_('Blocklist Query'), [
82 E('p', _('Query active blocklists and backups for a specific domain.')),
83 E('div', { 'class': 'left', 'style': 'display:flex; flex-direction:column' }, [
84 E('label', { 'style': 'padding-top:.5em', 'id': 'run' }, [
85 E('input', {
86 'class': 'cbi-input-text',
87 'placeholder': 'google.com',
88 'style': 'width:300px',
89 'spellcheck': 'false',
90 'id': 'search'
91 })
92 ])
93 ]),
94 E('div', { 'class': 'left', 'style': 'display:flex; flex-direction:column' }, [
95 '\xa0',
96 E('h5', _('Result')),
97 E('textarea', {
98 'id': 'result',
99 'style': 'width: 100% !important; padding: 5px; font-family: monospace',
100 'readonly': 'readonly',
101 'wrap': 'off',
102 'rows': 20
103 })
104 ]),
105 E('div', { 'class': 'right' }, [
106 E('button', {
107 'class': 'btn cbi-button',
108 'click': L.hideModal
109 }, _('Cancel')),
110 ' ',
111 E('button', {
112 'class': 'btn cbi-button-action',
113 'click': ui.createHandlerFn(this, function(ev) {
114 var domain = document.getElementById('search').value.trim().toLowerCase().replace(/[^a-z0-9\.\-]/g,'');
115 if (domain) {
116 document.getElementById('run').classList.add("spinning");
117 document.getElementById('search').value = domain;
118 document.getElementById('result').textContent = 'The query is running, please wait...';
119 L.resolveDefault(fs.exec_direct('/etc/init.d/adblock', ['query', domain])).then(function(res) {
120 var result = document.getElementById('result');
121 if (res) {
122 result.textContent = res.trim();
123 } else {
124 result.textContent = _('No Query results!');
125 }
126 document.getElementById('run').classList.remove("spinning");
127 document.getElementById('search').value = '';
128 })
129 }
130 document.getElementById('search').focus();
131 })
132 }, _('Query'))
133 ])
134 ]);
135 document.getElementById('search').focus();
136 }
137
138 if (ev === 'refresh') {
139 L.ui.showModal(_('Refresh DNS Report'), [
140 E('div', { 'class': 'left', 'style': 'display:flex; flex-direction:column' }, [
141 E('label', { 'class': 'cbi-input-select', 'style': 'padding-top:.5em' }, [
142 E('select', { 'class': 'cbi-input-select', 'id': 'count' }, [
143 E('option', { 'value': '50' }, '50'),
144 E('option', { 'value': '100' }, '100'),
145 E('option', { 'value': '150' }, '150'),
146 E('option', { 'value': '250' }, '250'),
147 E('option', { 'value': '500' }, '500')
148 ]),
149 '\xa0\xa0\xa0',
150 _('max. result set size')
151 ])
152 ]),
153 E('label', { 'class': 'cbi-input-text', 'style': 'padding-top:.5em' }, [
154 E('input', { 'class': 'cbi-input-text', 'spellcheck': 'false', 'id': 'search' }, [
155 ]),
156 '\xa0\xa0\xa0',
157 _('Filter criteria like date, domain or client (optional)')
158 ]),
159 E('div', { 'class': 'right' }, [
160 E('button', {
161 'class': 'btn cbi-button',
162 'click': L.hideModal
163 }, _('Cancel')),
164 ' ',
165 E('button', {
166 'class': 'btn cbi-button-action',
167 'id': 'refresh',
168 'click': ui.createHandlerFn(this, async function(ev) {
169 var count = document.getElementById('count').value;
170 var search = document.getElementById('search').value.trim().replace(/[^\w\.\-\:]/g,'') || '+';
171 L.resolveDefault(fs.exec_direct('/etc/init.d/adblock', ['report', 'gen', count, search]),'');
172 var running = 1;
173 while (running === 1) {
174 await new Promise(r => setTimeout(r, 1000));
175 L.resolveDefault(fs.read_direct('/var/run/adblock.pid')).then(function(res) {
176 if (!res) {
177 running = 0;
178 }
179 })
180 }
181 L.hideModal();
182 location.reload();
183 })
184 }, _('Refresh'))
185 ])
186 ]);
187 document.getElementById('refresh').focus();
188 }
189 }
190
191 return view.extend({
192 load: function() {
193 return L.resolveDefault(fs.exec_direct('/etc/init.d/adblock', ['report', 'json', '50', '+']),'');
194 },
195
196 render: function(dnsreport) {
197 if (!dnsreport) {
198 dnsreport = '{}';
199 };
200 var content;
201 content = JSON.parse(dnsreport);
202
203 var rows_top = [];
204 var tbl_top = E('table', { 'class': 'table', 'id': 'top_10' }, [
205 E('tr', { 'class': 'tr table-titles' }, [
206 E('th', { 'class': 'th right' }, _('Count')),
207 E('th', { 'class': 'th' }, _('Clients')),
208 E('th', { 'class': 'th right' }, _('Count')),
209 E('th', { 'class': 'th' }, _('Domains')),
210 E('th', { 'class': 'th right' }, _('Count')),
211 E('th', { 'class': 'th' }, _('Blocked Domains'))
212 ])
213 ]);
214
215 var max = 0;
216 if (content.top_clients && content.top_domains && content.top_blocked) {
217 max = Math.max(content.top_clients.length, content.top_domains.length, content.top_blocked.length);
218 }
219 for (var i = 0; i < max; i++) {
220 var a_cnt = '\xa0', a_addr = '\xa0', b_cnt = '\xa0', b_addr = '\xa0', c_cnt = '\xa0', c_addr = '\xa0';
221 if (content.top_clients[i]) {
222 a_cnt = content.top_clients[i].count;
223 }
224 if (content.top_clients[i]) {
225 a_addr = content.top_clients[i].address;
226 }
227 if (content.top_domains[i]) {
228 b_cnt = content.top_domains[i].count;
229 }
230 if (content.top_domains[i]) {
231 b_addr = '<a href="https://duckduckgo.com/?q=' + encodeURIComponent(content.top_domains[i].address) + '&amp;k1=-1&amp;km=l&amp;kh=1" target="_blank" rel="noreferrer noopener" title="Domain Lookup">' + content.top_domains[i].address + '</a>';
232 }
233 if (content.top_blocked[i]) {
234 c_cnt = content.top_blocked[i].count;
235 }
236 if (content.top_blocked[i]) {
237 c_addr = '<a href="https://duckduckgo.com/?q=' + encodeURIComponent(content.top_blocked[i].address) + '&amp;k1=-1&amp;km=l&amp;kh=1" target="_blank" rel="noreferrer noopener" title="Domain Lookup">' + content.top_blocked[i].address + '</a>';
238 }
239 rows_top.push([
240 a_cnt,
241 a_addr,
242 b_cnt,
243 b_addr,
244 c_cnt,
245 c_addr
246 ]);
247 }
248 cbi_update_table(tbl_top, rows_top);
249
250 var rows_requests = [];
251 var tbl_requests = E('table', { 'class': 'table', 'id': 'requests' }, [
252 E('tr', { 'class': 'tr table-titles' }, [
253 E('th', { 'class': 'th' }, _('Date')),
254 E('th', { 'class': 'th' }, _('Time')),
255 E('th', { 'class': 'th' }, _('Client')),
256 E('th', { 'class': 'th' }, _('Domain')),
257 E('th', { 'class': 'th' }, _('Answer')),
258 E('th', { 'class': 'th' }, _('Action'))
259 ])
260 ]);
261
262 max = 0;
263 if (content.requests) {
264 var button;
265 max = content.requests.length;
266 for (var i = 0; i < max; i++) {
267 if (content.requests[i].rc === 'NX') {
268 button = E('button', {
269 'class': 'btn cbi-button cbi-button-positive',
270 'style': 'word-break: inherit',
271 'name': 'whitelist',
272 'value': content.requests[i].domain,
273 'click': handleAction
274 }, [ _('Whitelist...') ]);
275 } else {
276 button = E('button', {
277 'class': 'btn cbi-button cbi-button-negative',
278 'style': 'word-break: inherit',
279 'name': 'blacklist',
280 'value': content.requests[i].domain,
281 'click': handleAction
282 }, [ _('Blacklist...') ]);
283 }
284 rows_requests.push([
285 content.requests[i].date,
286 content.requests[i].time,
287 content.requests[i].client,
288 '<a href="https://duckduckgo.com/?q=' + encodeURIComponent(content.requests[i].domain) + '&amp;k1=-1&amp;km=l&amp;kh=1" target="_blank" rel="noreferrer noopener" title="Domain Lookup">' + content.requests[i].domain + '</a>',
289 content.requests[i].rc,
290 button
291 ]);
292 }
293 }
294 cbi_update_table(tbl_requests, rows_requests);
295
296 return E('div', { 'class': 'cbi-map', 'id': 'map' }, [
297 E('div', { 'class': 'cbi-section' }, [
298 E('p', _('This tab shows the last generated DNS Report, press the \'Refresh\' button to get a current one.')),
299 E('p', '\xa0'),
300 E('div', { 'class': 'cbi-value' }, [
301 E('div', { 'class': 'cbi-value-title', 'style': 'float:left;width:230px' }, _('Start Timestamp')),
302 E('div', { 'class': 'cbi-value-title', 'id': 'start', 'style': 'float:left;color:#37c' }, (content.start_date || '-') + ', ' + (content.start_time || '-'))
303 ]),
304 E('div', { 'class': 'cbi-value' }, [
305 E('div', { 'class': 'cbi-value-title', 'style': 'float:left;width:230px' }, _('End Timestamp')),
306 E('div', { 'class': 'cbi-value-title', 'id': 'end', 'style': 'float:left;color:#37c' }, (content.end_date || '-') + ', ' + (content.end_time || '-'))
307 ]),
308 E('div', { 'class': 'cbi-value' }, [
309 E('div', { 'class': 'cbi-value-title', 'style': 'float:left;width:230px' }, _('Total DNS Requests')),
310 E('div', { 'class': 'cbi-value-title', 'id': 'total', 'style': 'float:left;color:#37c' }, content.total || '-')
311 ]),
312 E('div', { 'class': 'cbi-value' }, [
313 E('div', { 'class': 'cbi-value-title', 'style': 'float:left;width:230px' }, _('Blocked DNS Requests')),
314 E('div', { 'class': 'cbi-value-title', 'id': 'blocked', 'style': 'float:left;color:#37c' }, (content.blocked || '-') + ' (' + (content.percent || '-') + ')')
315 ]),
316 E('div', { 'class': 'right' }, [
317 E('button', {
318 'class': 'btn cbi-button cbi-button-apply',
319 'click': ui.createHandlerFn(this, function() {
320 return handleAction('query');
321 })
322 }, [ _('Blocklist Query...') ]),
323 '\xa0\xa0\xa0',
324 E('button', {
325 'class': 'btn cbi-button cbi-button-positive',
326 'click': ui.createHandlerFn(this, function() {
327 return handleAction('refresh');
328 })
329 }, [ _('Refresh...') ])
330 ]),
331 ]),
332 E('div', { 'class': 'cbi-section' }, [
333 E('div', { 'class': 'left' }, [
334 E('h3', _('Top 10 Statistics')),
335 tbl_top
336 ])
337 ]),
338 E('br'),
339 E('div', { 'class': 'cbi-section' }, [
340 E('div', { 'class': 'left' }, [
341 E('h3', _('Latest DNS Requests')),
342 tbl_requests
343 ])
344 ])
345 ]);
346 },
347 handleSaveApply: null,
348 handleSave: null,
349 handleReset: null
350 });