From 8080afadaccf0bbe409f3b2c6bdfe8f3d3d18ade Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 31 Dec 2016 15:36:47 +0000 Subject: [PATCH 01/13] Make ADD, SUB and GET compatible with the wildcard blocking list --- scripts/pi-hole/php/add.php | 3 ++ scripts/pi-hole/php/get.php | 56 +++++++++++++++++++++++++++++-------- scripts/pi-hole/php/sub.php | 3 ++ 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/scripts/pi-hole/php/add.php b/scripts/pi-hole/php/add.php index ffffccec..62c930ef 100644 --- a/scripts/pi-hole/php/add.php +++ b/scripts/pi-hole/php/add.php @@ -13,6 +13,9 @@ switch($type) { case "black": echo exec("sudo pihole -b -q ${_POST['domain']}"); break; + case "wild": + echo exec("sudo pihole -wild -q ${_POST['domain']}"); + break; } ?> diff --git a/scripts/pi-hole/php/get.php b/scripts/pi-hole/php/get.php index fada75b9..41bfe56b 100644 --- a/scripts/pi-hole/php/get.php +++ b/scripts/pi-hole/php/get.php @@ -1,21 +1,55 @@ = 0; $i--) { + if($list[$i] == "") + unset($list[$i]); + } + + return $list; + +} + +function getWildcardListContent() { + $rawList = file_get_contents(checkfile("/etc/dnsmasq.d/03-pihole-wildcard.conf")); + $wclist = explode("\n", $rawList); + $list = []; + + foreach ($wclist as $entry) { + $expl = explode("/", $entry); + if(count($expl) == 3) + { + array_push($list,"*${expl[1]}*"); + } + } + + return array_unique($list); -// Get rid of empty lines -for($i = sizeof($list)-1; $i >= 0; $i--) { - if($list[$i] == "") - unset($list[$i]); } function filterArray(&$inArray) { diff --git a/scripts/pi-hole/php/sub.php b/scripts/pi-hole/php/sub.php index fb670f3a..e09f2d0a 100644 --- a/scripts/pi-hole/php/sub.php +++ b/scripts/pi-hole/php/sub.php @@ -13,6 +13,9 @@ switch($type) { case "black": exec("sudo pihole -b -q -d ${_POST['domain']}"); break; + case "wild": + exec("sudo pihole -wild -q -d ${_POST['domain']}"); + break; } ?> From 754fa117ec0d627a758cac2dc30acd7f86892532 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 31 Dec 2016 15:45:12 +0000 Subject: [PATCH 02/13] Detect if wildcard blocking entry on Blacklists page --- scripts/pi-hole/js/list.js | 19 ++++++++++++++++--- scripts/pi-hole/php/get.php | 2 +- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/scripts/pi-hole/js/list.js b/scripts/pi-hole/js/list.js index de2692ad..62a84313 100644 --- a/scripts/pi-hole/js/list.js +++ b/scripts/pi-hole/js/list.js @@ -43,11 +43,24 @@ function refresh(fade) { } else { data.forEach(function (entry, index) { - list.append( + if(entry.substr(0,1) === "*") + { + // Wildcard entry + // remove leading * + entry = entry.substr(1, entry.length - 1); + list.append( "
  • " + entry + "
  • " - ); + ""); + } + else + { + // Normal entry + list.append( + "
  • " + entry + + "
  • "); + } // Handle button $("#list #"+index+"").on("click", "button", function() { diff --git a/scripts/pi-hole/php/get.php b/scripts/pi-hole/php/get.php index 41bfe56b..8bf28fb2 100644 --- a/scripts/pi-hole/php/get.php +++ b/scripts/pi-hole/php/get.php @@ -44,7 +44,7 @@ function getWildcardListContent() { $expl = explode("/", $entry); if(count($expl) == 3) { - array_push($list,"*${expl[1]}*"); + array_push($list,"*${expl[1]}"); } } From 05f2df720e106f8359274796b2c06200f1049e32 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 31 Dec 2016 16:06:21 +0000 Subject: [PATCH 03/13] Put wildcard blocking entries into their own table --- list.php | 5 +++++ scripts/pi-hole/js/list.js | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/list.php b/list.php index dc8097c1..266a5b7f 100644 --- a/list.php +++ b/list.php @@ -49,7 +49,12 @@ function getFullName() { +

    Exactl blocking

      + +

      Wildcard blocking

      +
        + " + entry + ""); From a33301911abb3b57c91927b462bb6544ac8eda67 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 31 Dec 2016 16:24:10 +0000 Subject: [PATCH 04/13] Made everything working --- list.php | 10 ++++++- scripts/pi-hole/js/list.js | 54 ++++++++++++++++++++++++++++++-------- 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/list.php b/list.php index 266a5b7f..9d030936 100644 --- a/list.php +++ b/list.php @@ -29,8 +29,14 @@ function getFullName() {
        + + + + + + }
        @@ -49,7 +55,9 @@ function getFullName() { -

        Exactl blocking

        + +

        Exact blocking

        +

          Wildcard blocking

          diff --git a/scripts/pi-hole/js/list.js b/scripts/pi-hole/js/list.js index fc3ef773..bbb5cb60 100644 --- a/scripts/pi-hole/js/list.js +++ b/scripts/pi-hole/js/list.js @@ -6,13 +6,18 @@ var token = $("#token").html(); var listType = $("#list-type").html(); var fullName = listType === "white" ? "Whitelist" : "Blacklist"; -function sub(index, entry) { +function sub(index, entry, arg) { var domain = $("#"+index); + var locallistType = listType; domain.hide("highlight"); + if(arg === "wild") + { + locallistType = "wild"; + } $.ajax({ url: "scripts/pi-hole/php/sub.php", method: "post", - data: {"domain":entry, "list":listType, "token":token}, + data: {"domain":entry, "list":locallistType, "token":token}, success: function(response) { if(response.length !== 0){ return; @@ -34,6 +39,10 @@ function refresh(fade) { } if(fade) { list.fadeOut(100); + if(listw) + { + listw.fadeOut(100); + } } $.ajax({ url: "scripts/pi-hole/php/get.php", @@ -41,6 +50,10 @@ function refresh(fade) { data: {"list":listType}, success: function(response) { list.html(""); + if(listw) + { + listw.html(""); + } var data = JSON.parse(response); if(data.length === 0) { @@ -57,6 +70,11 @@ function refresh(fade) { "
        • " + entry + "
        • "); + // Handle button + $("#list-wildcard #"+index+"").on("click", "button", function() { + sub(index, entry, "wild"); + console.log("wild rm"); + }); } else { @@ -65,15 +83,19 @@ function refresh(fade) { "
        • " + entry + "
        • "); + // Handle button + $("#list #"+index+"").on("click", "button", function() { + sub(index, entry, "exact"); + }); } - // Handle button - $("#list #"+index+"").on("click", "button", function() { - sub(index, entry); - }); }); } - list.fadeIn("fast"); + list.fadeIn(100); + if(listw) + { + listw.fadeIn(100); + } }, error: function(jqXHR, exception) { $("#alFailure").show(); @@ -83,11 +105,16 @@ function refresh(fade) { window.onload = refresh(false); -function add() { +function add(arg) { + var locallistType = listType; var domain = $("#domain"); if(domain.val().length === 0){ return; } + if(arg === "wild") + { + locallistType = "wild"; + } var alInfo = $("#alInfo"); var alSuccess = $("#alSuccess"); @@ -98,7 +125,7 @@ function add() { $.ajax({ url: "scripts/pi-hole/php/add.php", method: "post", - data: {"domain":domain.val(), "list":listType, "token":token}, + data: {"domain":domain.val(), "list":locallistType, "token":token}, success: function(response) { if (response.indexOf("not a valid argument") >= 0 || response.indexOf("is not a valid domain") >= 0) { @@ -139,14 +166,19 @@ function add() { $(document).keypress(function(e) { if(e.which === 13 && $("#domain").is(":focus")) { // Enter was pressed, and the input has focus - add(); + add("exact"); } }); // Handle buttons $("#btnAdd").on("click", function() { - add(); + add("exact"); }); + +$("#btnAddWildcard").on("click", function() { + add("wild"); +}); + $("#btnRefresh").on("click", function() { refresh(true); }); From c409cf20bfb79439c8a9dabe894325a1a58d2654 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 31 Dec 2016 16:35:16 +0000 Subject: [PATCH 05/13] Hide h3 headings if list is empty --- scripts/pi-hole/js/list.js | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/pi-hole/js/list.js b/scripts/pi-hole/js/list.js index bbb5cb60..b3b82955 100644 --- a/scripts/pi-hole/js/list.js +++ b/scripts/pi-hole/js/list.js @@ -57,6 +57,7 @@ function refresh(fade) { var data = JSON.parse(response); if(data.length === 0) { + $("h3").hide(); list.html("
          Your " + fullName + " is empty!
          "); } else { From 33714c540c8c10d7e62fa5c903e160869cc1fed9 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 31 Dec 2016 16:44:03 +0000 Subject: [PATCH 06/13] codacy fixes --- scripts/pi-hole/js/list.js | 10 ++++++++-- scripts/pi-hole/php/get.php | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/scripts/pi-hole/js/list.js b/scripts/pi-hole/js/list.js index b3b82955..ea9e5f44 100644 --- a/scripts/pi-hole/js/list.js +++ b/scripts/pi-hole/js/list.js @@ -58,7 +58,14 @@ function refresh(fade) { if(data.length === 0) { $("h3").hide(); - list.html("
          Your " + fullName + " is empty!
          "); + if(listw) + { + listw.html("
          Your " + fullName + " is empty!
          "); + } + else + { + list.html("
          Your " + fullName + " is empty!
          "); + } } else { data.forEach(function (entry, index) { @@ -74,7 +81,6 @@ function refresh(fade) { // Handle button $("#list-wildcard #"+index+"").on("click", "button", function() { sub(index, entry, "wild"); - console.log("wild rm"); }); } else diff --git a/scripts/pi-hole/php/get.php b/scripts/pi-hole/php/get.php index 8bf28fb2..043c8b87 100644 --- a/scripts/pi-hole/php/get.php +++ b/scripts/pi-hole/php/get.php @@ -22,7 +22,7 @@ switch ($listtype) { function getListContent($type) { - $rawList = file_get_contents(checkfile("/etc/pihole/${type}list.txt")); + $rawList = file_get_contents(checkfile("/etc/pihole/".$type."list.txt")); $list = explode("\n", $rawList); // Get rid of empty lines From 28bea42f8899f22a1cffd6df24f56af4fe0aac36 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 31 Dec 2016 17:21:50 +0000 Subject: [PATCH 07/13] Implemented wildcard blocking in Query Log --- scripts/pi-hole/js/queries.js | 4 ++- scripts/pi-hole/php/data.php | 54 ++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/scripts/pi-hole/js/queries.js b/scripts/pi-hole/js/queries.js index c68b412a..cfcd92b1 100644 --- a/scripts/pi-hole/js/queries.js +++ b/scripts/pi-hole/js/queries.js @@ -71,9 +71,11 @@ function add(domain,list) { } $(document).ready(function() { + var status; tableApi = $("#all-queries").DataTable( { "rowCallback": function( row, data, index ){ - if (data[4] === "Pi-holed") { + status = data[4]; + if (status.substr(0,2) === "Pi") { $(row).css("color","red"); $("td:eq(5)", row).html( "" ); } diff --git a/scripts/pi-hole/php/data.php b/scripts/pi-hole/php/data.php index ad24e57e..607e5169 100644 --- a/scripts/pi-hole/php/data.php +++ b/scripts/pi-hole/php/data.php @@ -283,6 +283,7 @@ // Create empty array for gravity $gravity_domains = getGravity(); + $wildcard_domains = getWildcardListContent(); setShowBlockedPermitted(); @@ -304,8 +305,36 @@ $domain = $exploded[count($exploded)-3]; $tmp = $exploded[count($exploded)-4]; - $status = isset($gravity_domains[$domain]) ? "Pi-holed" : "OK"; - if(($status === "Pi-holed" && $showBlocked) || ($status === "OK" && $showPermitted)) + $status = ""; + + if(isset($gravity_domains[$domain])) + { + if($gravity_domains[$domain] > 0) + { + // Exact matching gravity domain + $status = "Pi-holed (exact)"; + } + else + { + // Explicitly whitelisted + $status = "OK (whitelisted)"; + } + } + else + { + // Test for wildcard blocking + foreach ($wildcard_domains as $entry) { + if(strpos($domain, $entry) !== false) + { + $status = "Pi-holed (wildcard)"; + } + } + if(!strlen($status)) + { + $status = "OK"; + } + } + if((substr($status,0,2) === "Pi" && $showBlocked) || (substr($status,0,2) === "OK" && $showPermitted)) { $type = substr($exploded[count($exploded)-4], 6, -1); $client = $exploded[count($exploded)-1]; @@ -410,16 +439,33 @@ if($action && strlen($key) > 0) { // $action is true (we want to add) *and* key is not empty - $array[$key] = true; + $array[$key] = 1; } elseif(!$action && isset($array[$key])) { // $action is false (we want to remove) *and* key is set - unset($array[$key]); + $array[$key] = -1; } } } + function getWildcardListContent() { + $rawList = file_get_contents(checkfile("/etc/dnsmasq.d/03-pihole-wildcard.conf")); + $wclist = explode("\n", $rawList); + $list = []; + + foreach ($wclist as $entry) { + $expl = explode("/", $entry); + if(count($expl) == 3) + { + array_push($list,$expl[1]); + } + } + + return array_unique($list); + + } + function getGravity() { global $gravity,$whitelist,$blacklist; $domains = []; From fd3022abb3a4b93d669bc5222788e743199cae6a Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 31 Dec 2016 18:11:31 +0000 Subject: [PATCH 08/13] Fix check for white/blacklist action on Query Log --- scripts/pi-hole/js/queries.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/pi-hole/js/queries.js b/scripts/pi-hole/js/queries.js index cfcd92b1..a6b2a9fc 100644 --- a/scripts/pi-hole/js/queries.js +++ b/scripts/pi-hole/js/queries.js @@ -109,7 +109,8 @@ $(document).ready(function() { }); $("#all-queries tbody").on( "click", "button", function () { var data = tableApi.row( $(this).parents("tr") ).data(); - if (data[4] === "Pi-holed") + status = data[4]; + if (status.substr(0,2) === "Pi") { add(data[2],"white"); } From 2998e0b03abedfd9861a577e12f78a128c9e26b2 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 31 Dec 2016 21:10:02 +0000 Subject: [PATCH 09/13] Improved counting algorithm for blocked queries. Added couting of blocked DNS queries due to wildcard blocking --- scripts/pi-hole/php/data.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/scripts/pi-hole/php/data.php b/scripts/pi-hole/php/data.php index 607e5169..ab2de98c 100644 --- a/scripts/pi-hole/php/data.php +++ b/scripts/pi-hole/php/data.php @@ -527,7 +527,17 @@ function countBlockedQueries() { global $logListName; - return exec("grep \"gravity.list\" $logListName | grep -c \" is \""); + // Blocked due to gravity entries (ad lists + blacklist) + $gravityblocked = intval(exec("grep -c -e \"gravity\.list.*is\" $logListName")); + + // Blocked due to wildcard entries + $wildcard_domains = getWildcardListContent(); + $wildcardblocked = 0; + foreach ($wildcard_domains as $domain) { + $wildcardblocked += intval(exec("grep -c -e \"config.$domain is\" $logListName")); + } + + return $gravityblocked +$wildcardblocked; } function getForwards(\SplFileObject $log) { From 01d0f474a3a400cce91f0664981970f241d6b45d Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 31 Dec 2016 21:11:36 +0000 Subject: [PATCH 10/13] Minor improvement of couting regexp --- scripts/pi-hole/php/data.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/pi-hole/php/data.php b/scripts/pi-hole/php/data.php index ab2de98c..aa619dad 100644 --- a/scripts/pi-hole/php/data.php +++ b/scripts/pi-hole/php/data.php @@ -534,7 +534,7 @@ $wildcard_domains = getWildcardListContent(); $wildcardblocked = 0; foreach ($wildcard_domains as $domain) { - $wildcardblocked += intval(exec("grep -c -e \"config.$domain is\" $logListName")); + $wildcardblocked += intval(exec("grep -c -e \"config.*$domain is\" $logListName")); } return $gravityblocked +$wildcardblocked; From 3c0c92e8b1abe8a0485d8f6458159a67aeca4947 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Wed, 11 Jan 2017 12:41:35 +0100 Subject: [PATCH 11/13] Hide "Whitelist" button on wildcard blocked entries (Query Log page) --- scripts/pi-hole/js/queries.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/scripts/pi-hole/js/queries.js b/scripts/pi-hole/js/queries.js index 428006c7..13a34730 100644 --- a/scripts/pi-hole/js/queries.js +++ b/scripts/pi-hole/js/queries.js @@ -103,10 +103,14 @@ $(document).ready(function() { tableApi = $("#all-queries").DataTable( { "rowCallback": function( row, data, index ){ status = data[4]; - if (status.substr(0,2) === "Pi") { + if (status === "Pi-holed (exact)") { $(row).css("color","red"); $("td:eq(5)", row).html( "" ); } + else if (status === "Pi-holed (wildcard)") { + $(row).css("color","red"); + $("td:eq(5)", row).html( "" ); + } else{ $(row).css("color","green"); $("td:eq(5)", row).html( "" ); From b521a8de960036fbd6d2fbcb33a752de5c8fc86c Mon Sep 17 00:00:00 2001 From: DL6ER Date: Wed, 11 Jan 2017 16:50:46 +0100 Subject: [PATCH 12/13] Show notice that whitelisting of wildcard blocking is not possible --- list.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/list.php b/list.php index da146258..3f720e78 100644 --- a/list.php +++ b/list.php @@ -39,6 +39,8 @@ function getFullName() { } + +

          Note that whitelisting domains which are blocked using the wildcard method won't work.