-
-
Notifications
You must be signed in to change notification settings - Fork 545
/
db_queries.js
486 lines (436 loc) · 15.4 KB
/
db_queries.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/* Pi-hole: A black hole for Internet advertisements
* (c) 2017 Pi-hole, LLC (https://pi-hole.net)
* Network-wide ad blocking via your own hardware.
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */
/* global moment:false, utils:false */
var start__ = moment().subtract(7, "days");
var from = moment(start__).utc().valueOf() / 1000;
var end__ = moment();
var until = moment(end__).utc().valueOf() / 1000;
var instantquery = false;
var daterange;
var timeoutWarning = $("#timeoutWarning");
var reloadBox = $(".reload-box");
var datepickerManuallySelected = false;
var dateformat = "MMMM Do YYYY, HH:mm";
var replyTypes = [
"N/A",
"NODATA",
"NXDOMAIN",
"CNAME",
"IP",
"DOMAIN",
"RRNAME",
"SERVFAIL",
"REFUSED",
"NOTIMP",
"upstream error",
"DNSSEC",
"NONE",
"BLOB",
];
// Do we want to filter queries?
var GETDict = {};
window.location.search
.substr(1)
.split("&")
.forEach(function (item) {
GETDict[item.split("=")[0]] = item.split("=")[1];
});
if ("from" in GETDict && "until" in GETDict) {
from = parseInt(GETDict.from, 10);
until = parseInt(GETDict.until, 10);
start__ = moment(1000 * from);
end__ = moment(1000 * until);
instantquery = true;
}
$(function () {
daterange = $("#querytime").daterangepicker(
{
timePicker: true,
timePickerIncrement: 15,
timePicker24Hour: true,
locale: { format: dateformat },
startDate: start__,
endDate: end__,
ranges: {
Today: [moment().startOf("day"), moment()],
Yesterday: [
moment().subtract(1, "days").startOf("day"),
moment().subtract(1, "days").endOf("day"),
],
"Last 7 Days": [moment().subtract(7, "days"), moment()],
"Last 30 Days": [moment().subtract(30, "days"), moment()],
"This Month": [moment().startOf("month"), moment()],
"Last Month": [
moment().subtract(1, "month").startOf("month"),
moment().subtract(1, "month").endOf("month"),
],
"This Year": [moment().startOf("year"), moment()],
"All Time": [moment(0), moment()],
},
opens: "center",
showDropdowns: true,
autoUpdateInput: false,
},
function (startt, endt) {
from = moment(startt).utc().valueOf() / 1000;
until = moment(endt).utc().valueOf() / 1000;
}
);
});
var tableApi, statistics;
function handleAjaxError(xhr, textStatus) {
if (textStatus === "timeout") {
alert("The server took too long to send the data.");
} else if (xhr.responseText.indexOf("Connection refused") === -1) {
alert(
"An unknown error occurred while loading the data.\n" +
xhr.responseText +
"\nCheck the server's log files (/var/log/lighttpd/error-pihole.log) for details.\n\nYou may need to increase PHP memory limit." +
"\n\nYou can find more info in pi-hole's FAQ:\nhttps://docs.pi-hole.net/main/faq/#error-while-loading-data-from-the-long-term-database"
);
} else {
alert("An error occurred while loading the data: Connection refused. Is FTL running?");
}
$("#all-queries_processing").hide();
tableApi.clear();
tableApi.draw();
}
function excludeStatusTypes() {
var statusType = [];
if ($("#type_gravity").prop("checked") === false) {
statusType.push(1);
}
if ($("#type_forwarded").prop("checked") === false) {
statusType.push([2, 14]);
}
if ($("#type_cached").prop("checked") === false) {
statusType.push(3);
}
if ($("#type_regex").prop("checked") === false) {
statusType.push(4);
}
if ($("#type_blacklist").prop("checked") === false) {
statusType.push(5);
}
if ($("#type_external").prop("checked") === false) {
// Multiple IDs correspond to this status
// We request queries with all of them
statusType.push([6, 7, 8]);
}
if ($("#type_gravity_CNAME").prop("checked") === false) {
statusType.push(9);
}
if ($("#type_regex_CNAME").prop("checked") === false) {
statusType.push(10);
}
if ($("#type_blacklist_CNAME").prop("checked") === false) {
statusType.push(11);
}
if ($("#type_retried").prop("checked") === false) {
// Multiple IDs correspond to this status
// We request queries with all of them
statusType.push([12, 13]);
}
// 14 is defined above
if ($("#type_dbbusy").prop("checked") === false) {
statusType.push(15);
}
if ($("#type_special_domain").prop("checked") === false) {
statusType.push(16);
}
if ($("#type_cached_stale").prop("checked") === false) {
statusType.push(17);
}
return statusType.join(",");
}
var reloadCallback = function () {
timeoutWarning.hide();
statistics = [0, 0, 0, 0];
var data = tableApi.rows().data();
for (var i = 0; i < data.length; i++) {
statistics[0]++; // TOTAL query
if (data[i][4] === 1 || (data[i][4] > 4 && ![10, 12, 13, 14, 17].includes(data[i][4]))) {
statistics[2]++; // EXACT blocked
} else if (data[i][4] === 3) {
statistics[1]++; // CACHE query
} else if (data[i][4] === 4 || data[i][4] === 10) {
statistics[3]++; // REGEX blocked
}
}
var formatter = new Intl.NumberFormat();
$("h3#dns_queries").text(formatter.format(statistics[0]));
$("h3#queries_blocked_exact").text(formatter.format(statistics[2]));
$("h3#queries_wildcard_blocked").text(formatter.format(statistics[3]));
var percent = 0;
if (statistics[2] + statistics[3] > 0) {
percent = (100 * (statistics[2] + statistics[3])) / statistics[0];
}
var percentage = formatter.format(Math.round(percent * 10) / 10);
$("h3#queries_percentage_today").text(percentage + " %");
};
function refreshTableData() {
timeoutWarning.show();
reloadBox.hide();
var APIstring = "api_db.php?getAllQueries&from=" + from + "&until=" + until;
// If status types should be excluded ("unchecked") we add them to the query
var statusType = excludeStatusTypes();
if (statusType.length > 0) {
APIstring += "&status=" + statusType;
}
statistics = [0, 0, 0];
tableApi.ajax.url(APIstring).load(reloadCallback);
}
$(function () {
var APIstring = instantquery
? "api_db.php?getAllQueries&from=" + from + "&until=" + until
: "api_db.php?getAllQueries=empty";
// If status types should be excluded ("unchecked") we add them to the query
var statusType = excludeStatusTypes();
if (statusType.length > 0) {
APIstring += "&status=" + statusType;
}
tableApi = $("#all-queries").DataTable({
rowCallback: function (row, data) {
var replyid = parseInt(data[6], 10);
var dnssecStatus;
switch (data[8]) {
case 1:
dnssecStatus = '<br><span class="text-green">SECURE';
break;
case 2:
dnssecStatus = '<br><span class="text-orange">INSECURE';
break;
case 3:
dnssecStatus = '<br><span class="text-red">BOGUS';
break;
case 4:
dnssecStatus = '<br><span class="text-red">ABANDONED';
break;
case 5:
dnssecStatus = '<br><span class="text-orange">UNKNOWN';
break;
default:
// No DNSSEC
dnssecStatus = "";
}
if (dnssecStatus.length > 0) {
if (replyid === 7) dnssecStatus += " (refused upstream)";
dnssecStatus += "</span>";
}
var fieldtext,
buttontext = "",
blocked = false;
switch (data[4]) {
case 1:
fieldtext = "<span class='text-red'>Blocked (gravity)</span>";
buttontext =
'<button type="button" class="btn btn-default btn-sm text-green btn-whitelist"><i class="fas fa-check"></i> Whitelist</button>';
blocked = true;
break;
case 2:
fieldtext =
replyid === 0
? "<span class='text-green'>OK</span> (sent to <br class='hidden-lg'>"
: "<span class='text-green'>OK</span> (answered by <br class='hidden-lg'>";
fieldtext += (data.length > 5 && data[5] !== "N/A" ? data[5] : "") + ")" + dnssecStatus;
buttontext =
'<button type="button" class="btn btn-default btn-sm text-red btn-blacklist"><i class="fa fa-ban"></i> Blacklist</button>';
break;
case 3:
fieldtext =
"<span class='text-green'>OK</span> <br class='hidden-lg'>(cache)" + dnssecStatus;
buttontext =
'<button type="button" class="btn btn-default btn-sm text-red btn-blacklist"><i class="fa fa-ban"></i> Blacklist</button>';
break;
case 4:
fieldtext = "<span class='text-red'>Blocked <br class='hidden-lg'>(regex blacklist)";
buttontext =
'<button type="button" class="btn btn-default btn-sm text-green btn-whitelist"><i class="fas fa-check"></i> Whitelist</button>';
blocked = true;
break;
case 5:
fieldtext = "<span class='text-red'>Blocked <br class='hidden-lg'>(exact blacklist)";
buttontext =
'<button type="button" class="btn btn-default btn-sm text-green btn-whitelist"><i class="fas fa-check"></i> Whitelist</button>';
blocked = true;
break;
case 6:
fieldtext = "<span class='text-red'>Blocked <br class='hidden-lg'>(external, IP)";
blocked = true;
break;
case 7:
fieldtext =
"<span class='text-red'>Blocked <br class='hidden-lg'>(external, NULL)</span>";
blocked = true;
break;
case 8:
fieldtext =
"<span class='text-red'>Blocked <br class='hidden-lg'>(external, NXRA)</span>";
blocked = true;
break;
case 9:
fieldtext =
"<span class='text-red'>Blocked <br class='hidden-lg'>(gravity, CNAME)</span>";
buttontext =
'<button type="button" class="btn btn-default btn-sm text-green btn-whitelist"><i class="fas fa-check"></i> Whitelist</button>';
blocked = true;
break;
case 10:
fieldtext =
"<span class='text-red'>Blocked <br class='hidden-lg'>(regex blacklist, CNAME)</span>";
buttontext =
'<button type="button" class="btn btn-default btn-sm text-green btn-whitelist"><i class="fas fa-check"></i> Whitelist</button>';
blocked = true;
break;
case 11:
fieldtext =
"<span class='text-red'>Blocked <br class='hidden-lg'>(exact blacklist, CNAME)</span>";
buttontext =
'<button type="button" class="btn btn-default btn-sm text-green btn-whitelist"><i class="fas fa-check"></i> Whitelist</button>';
blocked = true;
break;
case 12:
fieldtext = "<span class='text-green'>Retried</span>";
break;
case 13:
fieldtext = "<span class='text-green'>Retried</span> <br class='hidden-lg'>(ignored)";
break;
case 14:
fieldtext =
"<span class='text-green'>OK</span> <br class='hidden-lg'>(already forwarded)" +
dnssecStatus;
buttontext =
'<button type="button" class="btn btn-default btn-sm text-red btn-blacklist"><i class="fa fa-ban"></i> Blacklist</button>';
break;
case 15:
fieldtext =
"<span class='text-orange'>Blocked <br class='hidden-lg'>(database is busy)</span>";
blocked = true;
break;
case 16:
fieldtext =
"<span class='text-orange'>Blocked <br class='hidden-lg'>(special domain)</span>";
blocked = true;
break;
case 17:
fieldtext =
"<span class='text-orange'>OK</span> <br class='hidden-lg'>(stale cache)" +
dnssecStatus;
buttontext =
'<button type="button" class="btn btn-default btn-sm text-red btn-blacklist"><i class="fa fa-ban"></i> Blacklist</button>';
break;
default:
fieldtext = "Unknown (" + parseInt(data[4], 10) + ")";
}
// Cannot block internal queries of this type
if ((data[1] === "DNSKEY" || data[1] === "DS") && data[3] === "pi.hole") buttontext = "";
$(row).addClass(blocked === true ? "blocked-row" : "allowed-row");
if (localStorage && localStorage.getItem("colorfulQueryLog_chkbox") === "true") {
$(row).addClass(blocked === true ? "text-red" : "text-green");
}
// Check for existence of sixth column and display only if not Pi-holed
var replytext =
replyid >= 0 && replyid < replyTypes.length ? replyTypes[replyid] : "? (" + replyid + ")";
replytext += '<input type="hidden" name="id" value="' + replyid + '">';
$("td:eq(4)", row).html(fieldtext);
$("td:eq(5)", row).html(replytext);
$("td:eq(6)", row).html(buttontext);
// Show response time only when reply is not N/A
if (data.length > 7 && replyid !== 0) {
$("td:eq(5)", row).append(" (" + (1000 * data[7]).toFixed(1) + "ms)");
}
// Substitute domain by "." if empty
// This was introduced by https://github.com/pi-hole/AdminLTE/pull/1244 but is considered obsolete since
// https://github.com/pi-hole/FTL/pull/1413. However, we keep the conversion here to keep user's
// statistic accurat when they import older data with empty domain fields
var domain = data[2];
if (domain.length === 0) {
domain = ".";
}
$("td:eq(2)", row).text(domain);
},
dom:
"<'row'<'col-sm-12'f>>" +
"<'row'<'col-sm-4'l><'col-sm-8'p>>" +
"<'row'<'col-sm-12'<'table-responsive'tr>>>" +
"<'row'<'col-sm-5'i><'col-sm-7'p>>",
ajax: {
url: APIstring,
error: handleAjaxError,
dataSrc: function (data) {
var dataIndex = 0;
return data.data.map(function (x) {
x[0] = x[0] * 1e6 + dataIndex++;
return x;
});
},
},
autoWidth: false,
processing: true,
deferRender: true,
order: [[0, "desc"]],
columns: [
{
width: "12%",
render: function (data, type) {
if (type === "display") {
return moment
.unix(Math.floor(data / 1e6))
.format("Y-MM-DD [<br class='hidden-lg'>]HH:mm:ss z");
}
return data;
},
},
{ width: "9%" },
{ width: "36%" },
{ width: "10%", type: "ip-address" },
{ width: "15%" },
{ width: "9%" },
{ width: "9%" },
],
lengthMenu: [
[10, 25, 50, 100, -1],
[10, 25, 50, 100, "All"],
],
columnDefs: [
{
targets: -1,
data: null,
defaultContent: "",
},
{
targets: "_all",
render: $.fn.dataTable.render.text(),
},
],
initComplete: reloadCallback,
});
$("#all-queries tbody").on("click", "button", function () {
var data = tableApi.row($(this).parents("tr")).data();
if ([1, 4, 5, 9, 10, 11].indexOf(data[4]) === -1) {
utils.addFromQueryLog(data[2], "black");
} else {
utils.addFromQueryLog(data[2], "white");
}
});
if (instantquery) {
daterange.val(start__.format(dateformat) + " - " + end__.format(dateformat));
}
});
$("#querytime").on("apply.daterangepicker", function (ev, picker) {
$(this).val(picker.startDate.format(dateformat) + " to " + picker.endDate.format(dateformat));
datepickerManuallySelected = true;
refreshTableData();
});
$("input[id^=type]").on("change", function () {
if (datepickerManuallySelected) {
reloadBox.show();
}
});
$(".bt-reload").on("click", function () {
refreshTableData();
});