fdd358038c804e5ca3c57fa2bd7712286ae15f47
[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',
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',
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',
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',
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', search, count, 'true', 'json']),'');
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', '+', '50', 'false', 'json']),'');
194 },
195
196 render: function(dnsreport) {
197 if (!dnsreport) {
198 dnsreport = '{ "data": "" }';
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' }, _('Name / IP Address')),
208 E('th', { 'class': 'th right' }, _('Count')),
209 E('th', { 'class': 'th' }, _('Domain')),
210 E('th', { 'class': 'th right' }, _('Count')),
211 E('th', { 'class': 'th' }, _('Blocked Domain'))
212 ])
213 ]);
214
215 var max = 0;
216 if (content.data.top_clients && content.data.top_domains && content.data.top_blocked) {
217 max = Math.max(content.data.top_clients.length, content.data.top_domains.length, content.data.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.data.top_clients[i]) {
222 a_cnt = content.data.top_clients[i].count;
223 }
224 if (content.data.top_clients[i]) {
225 a_addr = content.data.top_clients[i].address;
226 }
227 if (content.data.top_domains[i]) {
228 b_cnt = content.data.top_domains[i].count;
229 }
230 if (content.data.top_domains[i]) {
231 //[!CDATA[
232 b_addr = '<a href="https://duckduckgo.com/?q=' + encodeURIComponent(content.data.top_domains[i].address) + '&amp;k1=-1&amp;km=l&amp;kh=1" target="_blank" title="Search this domain">' + content.data.top_domains[i].address + '</a>';
233 //]]>
234 }
235 if (content.data.top_blocked[i]) {
236 c_cnt = content.data.top_blocked[i].count;
237 }
238 if (content.data.top_blocked[i]) {
239 //[!CDATA[
240 c_addr = '<a href="https://duckduckgo.com/?q=' + encodeURIComponent(content.data.top_blocked[i].address) + '&amp;k1=-1&amp;km=l&amp;kh=1" target="_blank" title="Search this domain">' + content.data.top_blocked[i].address + '</a>';
241 //]]>
242 }
243 rows_top.push([
244 a_cnt,
245 a_addr,
246 b_cnt,
247 b_addr,
248 c_cnt,
249 c_addr
250 ]);
251 }
252 cbi_update_table(tbl_top, rows_top);
253
254 var rows_requests = [];
255 var tbl_requests = E('table', { 'class': 'table', 'id': 'requests' }, [
256 E('tr', { 'class': 'tr table-titles' }, [
257 E('th', { 'class': 'th' }, _('Date')),
258 E('th', { 'class': 'th' }, _('Time')),
259 E('th', { 'class': 'th' }, _('Client')),
260 E('th', { 'class': 'th' }, _('Domain')),
261 E('th', { 'class': 'th' }, _('Answer')),
262 E('th', { 'class': 'th' }, _('Action'))
263 ])
264 ]);
265
266 max = 0;
267 if (content.data.requests) {
268 var button;
269 max = content.data.requests.length;
270 for (var i = 0; i < max; i++) {
271 if (content.data.requests[i].rc === 'NX') {
272 button = E('button', {
273 'class': 'cbi-button cbi-button-apply',
274 'style': 'word-break: inherit',
275 'name': 'whitelist',
276 'value': content.data.requests[i].domain,
277 'click': handleAction
278 }, [ _('Whitelist...') ]);
279 } else {
280 button = E('button', {
281 'class': 'cbi-button cbi-button-apply',
282 'style': 'word-break: inherit',
283 'name': 'blacklist',
284 'value': content.data.requests[i].domain,
285 'click': handleAction
286 }, [ _('Blacklist...') ]);
287 }
288 rows_requests.push([
289 content.data.requests[i].date,
290 content.data.requests[i].time,
291 content.data.requests[i].client,
292 //[!CDATA[
293 '<a href="https://duckduckgo.com/?q=' + encodeURIComponent(content.data.requests[i].domain) + '&amp;k1=-1&amp;km=l&amp;kh=1" target="_blank" title="Search this domain">' + content.data.requests[i].domain + '</a>',
294 //]]>
295 content.data.requests[i].rc,
296 button
297 ]);
298 }
299 }
300 cbi_update_table(tbl_requests, rows_requests);
301
302 return E('div', { 'class': 'cbi-map', 'id': 'map' }, [
303 E('div', { 'class': 'cbi-section' }, [
304 E('p', _('This shows the last generated DNS Report, press the refresh button to get a current one.')),
305 E('p', '\xa0'),
306 E('div', { 'class': 'cbi-value', 'style': 'margin-bottom:5px' }, [
307 E('label', { 'class': 'cbi-value-title', 'style': 'padding-top:0rem' }, _('Start Timestamp')),
308 E('div', { 'class': 'cbi-value-field', 'id': 'start', 'style': 'margin-bottom:5px;margin-left:200px;color:#37c' }, (content.data.start_date || '-') + ', ' + (content.data.start_time || '-'))]),
309 E('div', { 'class': 'cbi-value', 'style': 'margin-bottom:5px' }, [
310 E('label', { 'class': 'cbi-value-title', 'style': 'padding-top:0rem' }, _('End Timestamp')),
311 E('div', { 'class': 'cbi-value-field', 'id': 'end', 'style': 'margin-bottom:5px;margin-left:200px;color:#37c' }, (content.data.end_date || '-') + ', ' + (content.data.end_time || '-'))]),
312 E('div', { 'class': 'cbi-value', 'style': 'margin-bottom:5px' }, [
313 E('label', { 'class': 'cbi-value-title', 'style': 'padding-top:0rem' }, _('Total DNS Requests')),
314 E('div', { 'class': 'cbi-value-field', 'id': 'total', 'style': 'margin-bottom:5px;margin-left:200px;color:#37c' }, content.data.total || '-')]),
315 E('div', { 'class': 'cbi-value', 'style': 'margin-bottom:5px' }, [
316 E('label', { 'class': 'cbi-value-title', 'style': 'padding-top:0rem' }, _('Blocked DNS Requests')),
317 E('div', { 'class': 'cbi-value-field', 'id': 'blocked', 'style': 'margin-bottom:5px;margin-left:200px;color:#37c' }, (content.data.blocked || '-') + ' (' + (content.data.percent || '-') + ')')]),
318 E('div', { 'class': 'right' }, [
319 E('button', {
320 'class': 'cbi-button cbi-button-apply',
321 'click': ui.createHandlerFn(this, function() {
322 return handleAction('query');
323 })
324 }, [ _('Blocklist Query...') ]),
325 '\xa0\xa0\xa0',
326 E('button', {
327 'class': 'cbi-button cbi-button-apply',
328 'click': ui.createHandlerFn(this, function() {
329 return handleAction('refresh');
330 })
331 }, [ _('Refresh...') ])
332 ]),
333 ]),
334 E('div', { 'class': 'cbi-section' }, [
335 E('div', { 'class': 'left' }, [
336 E('h3', _('Top 10 Statistics')),
337 tbl_top
338 ])
339 ]),
340 E('br'),
341 E('div', { 'class': 'cbi-section' }, [
342 E('div', { 'class': 'left' }, [
343 E('h3', _('Latest DNS Requests')),
344 tbl_requests
345 ])
346 ])
347 ]);
348 },
349 handleSaveApply: null,
350 handleSave: null,
351 handleReset: null
352 });