From 349faac0482dbe569ff09b8727963c2026b83c7f Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 22 Apr 2017 16:09:56 +0200 Subject: [PATCH] Implemented also actions for audit log --- api_FTL.php | 11 ++++- auditlog.php | 9 ++-- scripts/pi-hole/js/auditlog.js | 80 ++++++++++++++++++++-------------- scripts/pi-hole/php/add.php | 26 +++++++++-- 4 files changed, 86 insertions(+), 40 deletions(-) diff --git a/api_FTL.php b/api_FTL.php index c367d153..04988f28 100644 --- a/api_FTL.php +++ b/api_FTL.php @@ -92,7 +92,11 @@ if (isset($_GET['topItems']) && $auth) $top_queries[$tmp[2]] = intval($tmp[1]); } - if(is_numeric($_GET['topItems'])) + if($_GET['topItems'] === "audit") + { + sendRequestFTL("top-ads for audit"); + } + else if(is_numeric($_GET['topItems'])) { sendRequestFTL("top-ads (".$_GET['topItems'].")"); } @@ -106,7 +110,10 @@ if (isset($_GET['topItems']) && $auth) foreach($return as $line) { $tmp = explode(" ",$line); - $top_ads[$tmp[2]] = intval($tmp[1]); + if(count($tmp) === 4) + $top_ads[$tmp[2]." (".$tmp[3].")"] = intval($tmp[1]); + else + $top_ads[$tmp[2]] = intval($tmp[1]); } $result = array('top_queries' => $top_queries, diff --git a/auditlog.php b/auditlog.php index d0ae4c44..96d74577 100644 --- a/auditlog.php +++ b/auditlog.php @@ -11,7 +11,7 @@
@@ -28,7 +28,7 @@ Domain Hits - Action + Actions @@ -55,7 +55,7 @@ Domain Hits - Action + Actions @@ -68,6 +68,9 @@
+
+

Important: Note that black- and whitelisted domains are not automatically applied on this page to avoid restarting the DNS service too often. Instead, go to Update Lists and run the update, to have the new settings become effective.

+
diff --git a/scripts/pi-hole/js/auditlog.js b/scripts/pi-hole/js/auditlog.js index 3cd2c680..85aee673 100644 --- a/scripts/pi-hole/js/auditlog.js +++ b/scripts/pi-hole/js/auditlog.js @@ -7,30 +7,6 @@ // Define global variables var timeLineChart, queryTypeChart, forwardDestinationChart; -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; - // Credit: http://stackoverflow.com/questions/1787322/htmlspecialchars-equivalent-in-javascript/4835406#4835406 function escapeHtml(text) { var map = { @@ -65,18 +41,29 @@ function updateTopLists() { url = ""+domain+""; percentage = data.top_queries[domain] / data.dns_queries_today * 100; domaintable.append(" " + url + - " " + data.top_queries[domain] + " Buttons ... "); + " " + data.top_queries[domain] + " "); } } for (domain in data.top_ads) { if ({}.hasOwnProperty.call(data.top_ads,domain)){ // Sanitize domain - domain = escapeHtml(domain); - url = ""+domain+""; - percentage = data.top_ads[domain] / data.ads_blocked_today * 100; - adtable.append(" " + url + - " " + data.top_ads[domain] + " Buttons ... "); + console.log(domain); + console.log(); + var input = domain.split(" "); + var printdomain = escapeHtml(input[0]); + if(input.length > 1) + { + url = ""+printdomain+" (wildcard blocked)"; + adtable.append(" " + url + + " " + data.top_ads[domain] + " "); + } + else + { + url = ""+printdomain+""; + adtable.append(" " + url + + " " + data.top_ads[domain] + " "); + } } } @@ -87,8 +74,37 @@ function updateTopLists() { }); } + +function add(domain,list) { + var token = $("#token").html(); + $.ajax({ + url: "scripts/pi-hole/php/add.php", + method: "post", + data: {"domain":domain, "list":list, "token":token, "auditlog":1}, + success: function(response) { + setTimeout(updateTopLists, 300); + } + }); +} + $(document).ready(function() { - // Pull in data via AJAX - updateTopLists(); + // Pull in data via AJAX + updateTopLists(); + + $("#domain-frequency tbody").on( "click", "button", function () { + var url = ($(this).parents("tr"))[0].innerText.split(" ")[0]; + if($(this).context.innerText === "Blacklist") + add(url,"black"); + else + add(url,"audit"); }); + + $("#ad-frequency tbody").on( "click", "button", function () { + var url = ($(this).parents("tr"))[0].innerText.split(" ")[0].split(" ")[0]; + if($(this).context.innerText === "Whitelist") + add(url,"white"); + else + add(url,"audit"); + }); +}); diff --git a/scripts/pi-hole/php/add.php b/scripts/pi-hole/php/add.php index d4ca64db..a19aae7a 100644 --- a/scripts/pi-hole/php/add.php +++ b/scripts/pi-hole/php/add.php @@ -16,13 +16,33 @@ list_verify($type); switch($type) { case "white": - echo exec("sudo pihole -w -q ${_POST['domain']}"); + if(!isset($_POST["auditlog"])) + echo exec("sudo pihole -w -q ${_POST['domain']}"); + else + { + echo exec("sudo pihole -w -q -n ${_POST['domain']}"); + echo exec("sudo pihole -a audit ${_POST['domain']}"); + } break; case "black": - echo exec("sudo pihole -b -q ${_POST['domain']}"); + if(!isset($_POST["auditlog"])) + echo exec("sudo pihole -b -q ${_POST['domain']}"); + else + { + echo exec("sudo pihole -b -q -n ${_POST['domain']}"); + echo exec("sudo pihole -a audit ${_POST['domain']}"); + } break; case "wild": - echo exec("sudo pihole -wild -q ${_POST['domain']}"); + if(!isset($_POST["auditlog"])) + echo exec("sudo pihole -wild -q ${_POST['domain']}"); + else + { + echo exec("sudo pihole -wild -q -n ${_POST['domain']}"); + echo exec("sudo pihole -a audit ${_POST['domain']}"); + } + case "audit": + echo exec("sudo pihole -a audit ${_POST['domain']}"); break; }