From 57c262a704e69bbebb257f7c85beb9a35b70f8e4 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 21 Nov 2016 17:32:51 +0100 Subject: [PATCH 01/11] Be able to call queries.js with preset filters from the main page --- js/pihole/index.js | 17 ++++++++++++++--- js/pihole/queries.js | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/js/pihole/index.js b/js/pihole/index.js index f67bf18a..809631e7 100644 --- a/js/pihole/index.js +++ b/js/pihole/index.js @@ -224,7 +224,8 @@ function updateTopClientsChart() { $.getJSON("api.php?summaryRaw&getQuerySources", function(data) { var clienttable = $('#client-frequency').find('tbody:last'); for (domain in data.top_sources) { - clienttable.append(' ' + domain + + var url = ''+domain+''; + clienttable.append(' ' + url + ' ' + data.top_sources[domain] + '
'); } @@ -260,14 +261,24 @@ function updateTopLists() { $.getJSON("api.php?summaryRaw&topItems", function(data) { var domaintable = $('#domain-frequency').find('tbody:last'); var adtable = $('#ad-frequency').find('tbody:last'); + var url; for (domain in data.top_queries) { - domaintable.append(' ' + domain + + if(domain !== "pi.hole") + { + url = ''+domain+''; + } + else + { + url = domain; + } + domaintable.append(' ' + url + ' ' + data.top_queries[domain] + '
'); } for (domain in data.top_ads) { - adtable.append(' ' + domain + + url = ''+domain+''; + adtable.append(' ' + url + ' ' + data.top_ads[domain] + '
'); } diff --git a/js/pihole/queries.js b/js/pihole/queries.js index 29d388a1..ec55a7f1 100644 --- a/js/pihole/queries.js +++ b/js/pihole/queries.js @@ -40,6 +40,33 @@ $(document).ready(function() { add(data[2],"black"); } } ); + + // Do we want to filter queries? + var GETDict = {} + location.search.substr(1).split("&").forEach(function(item) {GETDict[item.split("=")[0]] = item.split("=")[1]}) + if("client" in GETDict) + { + if(GETDict["client"] == "localhost(127.0.0.1)") + { + // Have to use normal search, as regexp of DataTable is broken + // It should be fixed in next release which might come up next + // year. In the meantime, we work around using normal search. + tableApi.column(3).search("localhost"); + } + else + { + // Search in third column (zero indexed) + // Use regular expression to only show exact matches, i.e. + // don't show 192.168.0.100 when searching for 192.168.0.1 + // true = use regex, false = don't use smart search + tableApi.column(3).search("^"+GETDict["client"]+"$",true,false); + } + } + if("domain" in GETDict) + { + // Search in second column (zero indexed) + tableApi.column(2).search("^"+GETDict["domain"]+"$",true,false); + } } ); function refreshData() { From 214b96fea2c1f974742bee65e0192e1329842625 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 21 Nov 2016 17:58:20 +0100 Subject: [PATCH 02/11] Fixed some small errors --- js/pihole/index.js | 16 ++++++++-------- js/pihole/queries.js | 9 +++++---- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/js/pihole/index.js b/js/pihole/index.js index 809631e7..de41e492 100644 --- a/js/pihole/index.js +++ b/js/pihole/index.js @@ -223,9 +223,9 @@ function updateQueryTypes() { function updateTopClientsChart() { $.getJSON("api.php?summaryRaw&getQuerySources", function(data) { var clienttable = $('#client-frequency').find('tbody:last'); - for (domain in data.top_sources) { - var url = ''+domain+''; - clienttable.append(' ' + url + + for (var domain in data.top_sources) { + var url = url = ""+domain+""; + clienttable.append(" " + url + ' ' + data.top_sources[domain] + '
'); } @@ -263,22 +263,22 @@ function updateTopLists() { var adtable = $('#ad-frequency').find('tbody:last'); var url; - for (domain in data.top_queries) { + for (var domain in data.top_queries) { if(domain !== "pi.hole") { - url = ''+domain+''; + url = ""+domain+""; } else { url = domain; } - domaintable.append(' ' + url + + domaintable.append(" " + url + ' ' + data.top_queries[domain] + '
'); } for (domain in data.top_ads) { - url = ''+domain+''; - adtable.append(' ' + url + + url = ""+domain+""; + adtable.append(" " + url + ' ' + data.top_ads[domain] + '
'); } diff --git a/js/pihole/queries.js b/js/pihole/queries.js index ec55a7f1..ff6a6694 100644 --- a/js/pihole/queries.js +++ b/js/pihole/queries.js @@ -1,4 +1,5 @@ -$(document).ready(function() { +var tableApi; +(document).ready(function() { tableApi = $('#all-queries').DataTable( { "rowCallback": function( row, data, index ){ if (data[4] == "Pi-holed") { @@ -42,11 +43,11 @@ $(document).ready(function() { } ); // Do we want to filter queries? - var GETDict = {} - location.search.substr(1).split("&").forEach(function(item) {GETDict[item.split("=")[0]] = item.split("=")[1]}) + var GETDict = {}; + location.search.substr(1).split("&").forEach(function(item) {GETDict[item.split("=")[0]] = item.split("=")[1]}); if("client" in GETDict) { - if(GETDict["client"] == "localhost(127.0.0.1)") + if(GETDict["client"] === "localhost(127.0.0.1)") { // Have to use normal search, as regexp of DataTable is broken // It should be fixed in next release which might come up next From e67a16a54c18a2b04826654f81c39908b0aa50b9 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 21 Nov 2016 18:29:21 +0100 Subject: [PATCH 03/11] Another minor change --- js/pihole/index.js | 2 +- js/pihole/queries.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/js/pihole/index.js b/js/pihole/index.js index de41e492..4d873b11 100644 --- a/js/pihole/index.js +++ b/js/pihole/index.js @@ -276,7 +276,7 @@ function updateTopLists() { ' ' + data.top_queries[domain] + '
'); } - for (domain in data.top_ads) { + for (var domain in data.top_ads) { url = ""+domain+""; adtable.append(" " + url + ' ' + data.top_ads[domain] + '
Date: Mon, 21 Nov 2016 18:33:27 +0100 Subject: [PATCH 04/11] Lost $ sign --- js/pihole/queries.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/pihole/queries.js b/js/pihole/queries.js index 796d26be..cf2a9321 100644 --- a/js/pihole/queries.js +++ b/js/pihole/queries.js @@ -1,5 +1,5 @@ var tableApi; -(document).ready(function() { +$(document).ready(function() { tableApi = $('#all-queries').DataTable( { "rowCallback": function( row, data, index ){ if (data[4] == "Pi-holed") { @@ -44,7 +44,7 @@ var tableApi; // Do we want to filter queries? var GETDict = {}; - location.search.substr(1).split("&").forEach(function(item) {GETDict[item.split("=")[0]] = item.split("=")[1];}); + location.search.substr(1).split("&").forEach(function(item) {GETDict[item.split("=")[0]] = item.split("=")[1]}); if("client" in GETDict) { if(GETDict["client"] === "localhost(127.0.0.1)") From d4aa4fce9701ed0a067c50d295c050b023719467 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 21 Nov 2016 18:34:19 +0100 Subject: [PATCH 05/11] Another semicolon --- js/pihole/queries.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/pihole/queries.js b/js/pihole/queries.js index cf2a9321..04a299a7 100644 --- a/js/pihole/queries.js +++ b/js/pihole/queries.js @@ -44,7 +44,7 @@ $(document).ready(function() { // Do we want to filter queries? var GETDict = {}; - location.search.substr(1).split("&").forEach(function(item) {GETDict[item.split("=")[0]] = item.split("=")[1]}); + location.search.substr(1).split("&").forEach(function(item) {GETDict[item.split("=")[0]] = item.split("=")[1];}); if("client" in GETDict) { if(GETDict["client"] === "localhost(127.0.0.1)") From e94a2be01664f4effdf06e0e581db5287ceda90b Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 21 Nov 2016 18:58:04 +0100 Subject: [PATCH 06/11] More codacy complaints --- js/pihole/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/js/pihole/index.js b/js/pihole/index.js index 4d873b11..d620e5d9 100644 --- a/js/pihole/index.js +++ b/js/pihole/index.js @@ -261,9 +261,9 @@ function updateTopLists() { $.getJSON("api.php?summaryRaw&topItems", function(data) { var domaintable = $('#domain-frequency').find('tbody:last'); var adtable = $('#ad-frequency').find('tbody:last'); - var url; + var url, domain; - for (var domain in data.top_queries) { + for (domain in data.top_queries) { if(domain !== "pi.hole") { url = ""+domain+""; @@ -276,7 +276,7 @@ function updateTopLists() { ' ' + data.top_queries[domain] + '
'); } - for (var domain in data.top_ads) { + for (domain in data.top_ads) { url = ""+domain+""; adtable.append(" " + url + ' ' + data.top_ads[domain] + '
"']/g, function(m) { return map[m]; }); +} + function updateTopClientsChart() { $.getJSON("api.php?summaryRaw&getQuerySources", function(data) { var clienttable = $('#client-frequency').find('tbody:last'); for (var domain in data.top_sources) { - var url = url = ""+domain+""; + // Sanitize domain + domain = escapeHtml(domain); + var url = ""+domain+""; clienttable.append(" " + url + ' ' + data.top_sources[domain] + '
'); @@ -264,6 +279,8 @@ function updateTopLists() { var url, domain; for (domain in data.top_queries) { + // Sanitize domain + domain = escapeHtml(domain); if(domain !== "pi.hole") { url = ""+domain+""; @@ -277,6 +294,8 @@ function updateTopLists() { data.top_queries[domain] / data.dns_queries_today * 100 + '%">
'); } for (domain in data.top_ads) { + // Sanitize domain + domain = escapeHtml(domain); url = ""+domain+""; adtable.append(" " + url + ' ' + data.top_ads[domain] + '
Date: Wed, 23 Nov 2016 12:00:59 +0100 Subject: [PATCH 09/11] codacy: Strings must use doublequote. --- js/pihole/queries.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/js/pihole/queries.js b/js/pihole/queries.js index ac11b3de..e8af78a8 100644 --- a/js/pihole/queries.js +++ b/js/pihole/queries.js @@ -2,9 +2,9 @@ var tableApi; function escapeRegex(text) { var map = { - '(': '\\(', - ')': '\\)', - '.': '\\.', + "(": "\\(", + ")": "\\)", + ".": "\\.", }; return text.replace(/[().]/g, function(m) { return map[m]; }); } From cfc76cc9346867c7e1099b27dd7247ac33924aa6 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Wed, 23 Nov 2016 12:02:18 +0100 Subject: [PATCH 10/11] codacy: Strings must use doublequote. --- js/pihole/index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/js/pihole/index.js b/js/pihole/index.js index 92a11fad..03845dc3 100644 --- a/js/pihole/index.js +++ b/js/pihole/index.js @@ -223,11 +223,11 @@ function updateQueryTypes() { // Credit: http://stackoverflow.com/questions/1787322/htmlspecialchars-equivalent-in-javascript/4835406#4835406 function escapeHtml(text) { var map = { - '&': '&', - '<': '<', - '>': '>', - '"': '"', - "'": ''' + "&": "&", + "<": "<", + ">": ">", + "\"": """, + "\'": "'" }; return text.replace(/[&<>"']/g, function(m) { return map[m]; }); From 623ae2c56e5009cd5d67216de29eba1f9298f036 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Wed, 23 Nov 2016 12:02:48 +0100 Subject: [PATCH 11/11] Moved definition of var domain outside of the for-in loop --- js/pihole/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/js/pihole/index.js b/js/pihole/index.js index 03845dc3..5d22efdd 100644 --- a/js/pihole/index.js +++ b/js/pihole/index.js @@ -236,7 +236,8 @@ function escapeHtml(text) { function updateTopClientsChart() { $.getJSON("api.php?summaryRaw&getQuerySources", function(data) { var clienttable = $('#client-frequency').find('tbody:last'); - for (var domain in data.top_sources) { + var domain; + for (domain in data.top_sources) { // Sanitize domain domain = escapeHtml(domain); var url = ""+domain+"";