/* 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. */ // Define global variables /* global Chart */ var timeLineChart, queryTypeChart, forwardDestinationChart; var queryTypePieChart, forwardDestinationPieChart, clientsChart; function padNumber(num) { return ("00" + num).substr(-2,2); } // Helper function needed for converting the Objects to Arrays function objectToArray(p){ var keys = Object.keys(p); keys.sort(function(a, b) { return a - b; }); var arr = [], idx = []; for (var i = 0; i < keys.length; i++) { arr.push(p[keys[i]]); idx.push(keys[i]); } return [idx,arr]; } // Functions to update data in page var failures = 0; function updateQueriesOverTime() { $.getJSON("api.php?overTimeData10mins", function(data) { if("FTLnotrunning" in data) { return; } // convert received objects to arrays data.domains_over_time = objectToArray(data.domains_over_time); data.ads_over_time = objectToArray(data.ads_over_time); // remove last data point since it not representative data.ads_over_time[0].splice(-1,1); // Remove possibly already existing data timeLineChart.data.labels = []; timeLineChart.data.datasets[0].data = []; timeLineChart.data.datasets[1].data = []; // Add data for each hour that is available for (var hour in data.ads_over_time[0]) { if ({}.hasOwnProperty.call(data.ads_over_time[0], hour)) { var d,h; h = parseInt(data.domains_over_time[0][hour]); if(parseInt(data.ads_over_time[0][0]) < 1200) { // Fallback - old style d = new Date().setHours(Math.floor(h / 6), 10 * (h % 6), 0, 0); } else { // New style: Get Unix timestamps d = new Date(1000*h); } timeLineChart.data.labels.push(d); timeLineChart.data.datasets[0].data.push(data.domains_over_time[1][hour]); timeLineChart.data.datasets[1].data.push(data.ads_over_time[1][hour]); } } $("#queries-over-time .overlay").hide(); timeLineChart.update(); }).done(function() { // Reload graph after 10 minutes failures = 0; setTimeout(updateQueriesOverTime, 600000); }).fail(function() { failures++; if(failures < 5) { // Try again after 1 minute only if this has not failed more // than five times in a row setTimeout(updateQueriesOverTime, 60000); } }); } function updateQueryTypesOverTime() { $.getJSON("api.php?overTimeDataQueryTypes", function(data) { if("FTLnotrunning" in data) { return; } // convert received objects to arrays data.over_time = objectToArray(data.over_time); // remove last data point since it not representative data.over_time[0].splice(-1,1); var timestamps = data.over_time[0]; var plotdata = data.over_time[1]; // Remove possibly already existing data queryTypeChart.data.labels = []; queryTypeChart.data.datasets[0].data = []; queryTypeChart.data.datasets[1].data = []; var colors = []; $.each($.AdminLTE.options.colors, function(key, value) { colors.push(value); }); queryTypeChart.data.datasets[0].backgroundColor = colors[0]; queryTypeChart.data.datasets[1].backgroundColor = colors[1]; // Add data for each hour that is available for (var j in timestamps) { if ({}.hasOwnProperty.call(timestamps, j)) { var d,h; h = parseInt(timestamps[j]); // New style: Get Unix timestamps d = new Date(1000*h); queryTypeChart.data.labels.push(d); queryTypeChart.data.datasets[0].data.push(1e-2*plotdata[j][0]); queryTypeChart.data.datasets[1].data.push(1e-2*plotdata[j][1]); } } $("#query-types .overlay").hide(); queryTypeChart.update(); }).done(function() { // Reload graph after 10 minutes failures = 0; setTimeout(updateQueryTypesOverTime, 600000); }).fail(function() { failures++; if(failures < 5) { // Try again after 1 minute only if this has not failed more // than five times in a row setTimeout(updateQueryTypesOverTime, 60000); } }); } function updateQueryTypesPie() { $.getJSON("api.php?getQueryTypes", function(data) { if("FTLnotrunning" in data) { return; } var colors = []; // Get colors from AdminLTE $.each($.AdminLTE.options.colors, function(key, value) { colors.push(value); }); var v = [], c = [], k = [], iter; // Collect values and colors, and labels if(data.hasOwnProperty("querytypes")) { iter = data.querytypes; } else { iter = data; } $.each(iter, function(key , value) { v.push(value); c.push(colors.shift()); k.push(key); }); // Build a single dataset with the data to be pushed var dd = {data: v, backgroundColor: c}; // and push it at once queryTypePieChart.data.datasets[0] = dd; queryTypePieChart.data.labels = k; $("#query-types-pie .overlay").hide(); queryTypePieChart.update(); queryTypePieChart.chart.config.options.cutoutPercentage=50; queryTypePieChart.update(); // Don't use rotation animation for further updates queryTypePieChart.options.animation.duration=0; }).done(function() { // Reload graph after minute setTimeout(updateQueryTypesPie, 60000); }); } function updateForwardedOverTime() { $.getJSON("api.php?overTimeDataForwards&getForwardDestinationNames", function(data) { if("FTLnotrunning" in data) { return; } // convert received objects to arrays data.over_time = objectToArray(data.over_time); // remove last data point since it not representative data.over_time[0].splice(-1,1); var timestamps = data.over_time[0]; var plotdata = data.over_time[1]; var labels = []; var key, i, j; for (key in data.forward_destinations) { if (!{}.hasOwnProperty.call(data.forward_destinations, key)) continue; if(key.indexOf("|") > -1) { var idx = key.indexOf("|"); key = key.substr(0, idx); } labels.push(key); } // Get colors from AdminLTE var colors = []; $.each($.AdminLTE.options.colors, function(key, value) { colors.push(value); }); var v = [], c = [], k = []; // Remove possibly already existing data forwardDestinationChart.data.labels = []; forwardDestinationChart.data.datasets[0].data = []; for (i = 1; i < forwardDestinationChart.data.datasets.length; i++) { forwardDestinationChart.data.datasets[i].data = []; } // Collect values and colors, and labels forwardDestinationChart.data.datasets[0].backgroundColor = colors[0]; forwardDestinationChart.data.datasets[0].pointRadius = 0; forwardDestinationChart.data.datasets[0].pointHitRadius = 5; forwardDestinationChart.data.datasets[0].pointHoverRadius = 5; forwardDestinationChart.data.datasets[0].label = labels[0]; forwardDestinationChart.data.datasets[0].cubicInterpolationMode = "monotone"; for (i = forwardDestinationChart.data.datasets.length; i < plotdata[0].length; i++) { forwardDestinationChart.data.datasets.push({data: [], backgroundColor: colors[i], pointRadius: 0, pointHitRadius: 5, pointHoverRadius: 5, label: labels[i]}); } // Add data for each dataset that is available for (j in timestamps) { if (!{}.hasOwnProperty.call(timestamps, j)) continue; for (key in plotdata[j]) { if (!{}.hasOwnProperty.call(plotdata[j], key)) continue; forwardDestinationChart.data.datasets[key].data.push(1e-2*plotdata[j][key]); } var d = new Date(1000*parseInt(timestamps[j])); forwardDestinationChart.data.labels.push(d); } $("#forward-destinations .overlay").hide(); forwardDestinationChart.update(); }).done(function() { // Reload graph after 10 minutes failures = 0; setTimeout(updateForwardedOverTime, 600000); }).fail(function() { failures++; if(failures < 5) { // Try again after 1 minute only if this has not failed more // than five times in a row setTimeout(updateForwardedOverTime, 60000); } }); } function updateClientsOverTime() { $.getJSON("api.php?overTimeDataClients&getClientNames", function(data) { if("FTLnotrunning" in data) { return; } // convert received objects to arrays data.over_time = objectToArray(data.over_time); // remove last data point since it not representative data.over_time[0].splice(-1,1); var timestamps = data.over_time[0]; var plotdata = data.over_time[1]; var labels = []; var key, i, j; for (key in data.clients) { if (!{}.hasOwnProperty.call(data.clients, key)) continue; if(key.indexOf("|") > -1) { var idx = key.indexOf("|"); key = key.substr(0, idx); } labels.push(key); } // Get colors from AdminLTE var colors = []; $.each($.AdminLTE.options.colors, function(key, value) { colors.push(value); }); var v = [], c = [], k = []; // Remove possibly already existing data clientsChart.data.labels = []; clientsChart.data.datasets[0].data = []; for (i = 1; i < clientsChart.data.datasets.length; i++) { clientsChart.data.datasets[i].data = []; } // Collect values and colors, and labels clientsChart.data.datasets[0].backgroundColor = colors[0]; clientsChart.data.datasets[0].pointRadius = 0; clientsChart.data.datasets[0].pointHitRadius = 5; clientsChart.data.datasets[0].pointHoverRadius = 5; clientsChart.data.datasets[0].label = labels[0]; for (i = clientsChart.data.datasets.length; i < plotdata[0].length; i++) { clientsChart.data.datasets.push({data: [], backgroundColor: colors[i], pointRadius: 0, pointHitRadius: 5, pointHoverRadius: 5, label: labels[i], cubicInterpolationMode: "monotone" }); } // Add data for each dataset that is available for (j in timestamps) { if (!{}.hasOwnProperty.call(timestamps, j)) continue; for (key in plotdata[j]) { if (!{}.hasOwnProperty.call(plotdata[j], key)) continue; clientsChart.data.datasets[key].data.push(plotdata[j][key]); } var d = new Date(1000*parseInt(timestamps[j])); clientsChart.data.labels.push(d); } $("#clients .overlay").hide(); clientsChart.update(); }).done(function() { // Reload graph after 10 minutes failures = 0; setTimeout(updateClientsOverTime, 600000); }).fail(function() { failures++; if(failures < 5) { // Try again after 1 minute only if this has not failed more // than five times in a row setTimeout(updateClientsOverTime, 60000); } }); } function updateForwardDestinationsPie() { $.getJSON("api.php?getForwardDestinations=unsorted", function(data) { if("FTLnotrunning" in data) { return; } var colors = []; // Get colors from AdminLTE $.each($.AdminLTE.options.colors, function(key, value) { colors.push(value); }); var v = [], c = [], k = [], values = []; // Collect values and colors $.each(data.forward_destinations, function(key , value) { if(key.indexOf("|") > -1) { key = key.substr(0, key.indexOf("|")); } values.push([key, value, colors.shift()]); }); // Sort data ASC accorwing to 2nd column, keep already assigned labels and colors values = values.sort(function(a,b) { return b[1] - a[1]; }); // Split data into individual arrays for the graphs $.each(values, function(key , value) { k.push(value[0]); v.push(value[1]); c.push(value[2]); }); // Build a single dataset with the data to be pushed var dd = {data: v, backgroundColor: c}; // and push it at once forwardDestinationPieChart.data.labels = k; forwardDestinationPieChart.data.datasets[0] = dd; // and push it at once $("#forward-destinations-pie .overlay").hide(); forwardDestinationPieChart.update(); forwardDestinationPieChart.chart.config.options.cutoutPercentage=50; forwardDestinationPieChart.update(); // Don't use rotation animation for further updates forwardDestinationPieChart.options.animation.duration=0; }).done(function() { // Reload graph after one minute setTimeout(updateForwardDestinationsPie, 60000); }); } // 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]; }); } function updateTopClientsChart() { $.getJSON("api.php?summaryRaw&getQuerySources", function(data) { if("FTLnotrunning" in data) { return; } // Clear tables before filling them with data $("#client-frequency td").parent().remove(); var clienttable = $("#client-frequency").find("tbody:last"); var client, percentage, clientname, clientip; for (client in data.top_sources) { if ({}.hasOwnProperty.call(data.top_sources, client)){ // Sanitize client if(escapeHtml(client) !== client) { // Make a copy with the escaped index if necessary data.top_sources[escapeHtml(client)] = data.top_sources[client]; } client = escapeHtml(client); if(client.indexOf("|") > -1) { var idx = client.indexOf("|"); clientname = client.substr(0, idx); clientip = client.substr(idx+1, client.length-idx); } else { clientname = client; clientip = client; } var url = ""+clientname+""; percentage = data.top_sources[client] / data.dns_queries_today * 100; clienttable.append("