Merge pull request #312 from pi-hole/wildcard_blacklisting

Add blacklisting wildcard support
This commit is contained in:
DL6ER
2017-01-12 00:14:04 +01:00
committed by GitHub
7 changed files with 209 additions and 33 deletions
+15
View File
@@ -29,10 +29,18 @@ function getFullName() {
<div class="form-group input-group">
<input id="domain" type="text" class="form-control" placeholder="Add a domain (example.com or sub.example.com)">
<span class="input-group-btn">
<?php if($list === "black") { ?>
<button id="btnAdd" class="btn btn-default" type="button">Add (exact)</button>
<button id="btnAddWildcard" class="btn btn-default" type="button">Add (wildcard)</button>
<?php }else{ ?>
<button id="btnAdd" class="btn btn-default" type="button">Add</button>
<?php } ?>
<button id="btnRefresh" class="btn btn-default" type="button">Refresh</button>
}
</span>
</div>
<?php if($list === "white") { ?>
<p>Note that whitelisting domains which are blocked using the wildcard method won't work.</p><?php } ?>
<!-- Alerts -->
<div id="alInfo" class="alert alert-info alert-dismissible fade in" role="alert" hidden="true">
@@ -49,7 +57,14 @@ function getFullName() {
</div>
<!-- Domain List -->
<?php if($list === "black") { ?>
<h3>Exact blocking</h3>
<?php } ?>
<ul class="list-group" id="list"></ul>
<?php if($list === "black") { ?>
<h3>Wildcard blocking</h3>
<ul class="list-group" id="list-wildcard"></ul>
<?php } ?>
<?php
require "scripts/pi-hole/php/footer.php";
+73 -15
View File
@@ -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;
@@ -27,9 +32,18 @@ function sub(index, entry) {
}
function refresh(fade) {
var listw;
var list = $("#list");
if(listType === "black")
{
listw = $("#list-wildcard");
}
if(fade) {
list.fadeOut(100);
if(listw)
{
listw.fadeOut(100);
}
}
$.ajax({
url: "scripts/pi-hole/php/get.php",
@@ -37,26 +51,60 @@ function refresh(fade) {
data: {"list":listType},
success: function(response) {
list.html("");
if(listw)
{
listw.html("");
}
var data = JSON.parse(response);
if(data.length === 0) {
list.html("<div class=\"alert alert-info\" role=\"alert\">Your " + fullName + " is empty!</div>");
$("h3").hide();
if(listw)
{
listw.html("<div class=\"alert alert-info\" role=\"alert\">Your " + fullName + " is empty!</div>");
}
else
{
list.html("<div class=\"alert alert-info\" role=\"alert\">Your " + fullName + " is empty!</div>");
}
}
else {
$("h3").show();
data.forEach(function (entry, index) {
list.append(
if(entry.substr(0,1) === "*")
{
// Wildcard entry
// remove leading *
entry = entry.substr(1, entry.length - 1);
listw.append(
"<li id=\"" + index + "\" class=\"list-group-item clearfix\">" + entry +
"<button class=\"btn btn-danger btn-xs pull-right\" type=\"button\">" +
"<span class=\"glyphicon glyphicon-trash\"></span></button></li>"
);
"<span class=\"glyphicon glyphicon-trash\"></span></button></li>");
// Handle button
$("#list-wildcard #"+index+"").on("click", "button", function() {
sub(index, entry, "wild");
});
}
else
{
// Normal entry
list.append(
"<li id=\"" + index + "\" class=\"list-group-item clearfix\">" + entry +
"<button class=\"btn btn-danger btn-xs pull-right\" type=\"button\">" +
"<span class=\"glyphicon glyphicon-trash\"></span></button></li>");
// 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();
@@ -66,11 +114,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");
@@ -82,7 +135,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) {
@@ -125,14 +178,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);
});
+9 -2
View File
@@ -85,6 +85,7 @@ function handleAjaxError( xhr, textStatus, error ) {
}
$(document).ready(function() {
var status;
// Do we want to filter queries?
var GETDict = {};
@@ -104,10 +105,15 @@ $(document).ready(function() {
tableApi = $("#all-queries").DataTable( {
"rowCallback": function( row, data, index ){
if (data[4] === "Pi-holed") {
status = data[4];
if (status === "Pi-holed (exact)") {
$(row).css("color","red");
$("td:eq(5)", row).html( "<button style=\"color:green; white-space: nowrap;\"><i class=\"fa fa-pencil-square-o\"></i> Whitelist</button>" );
}
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( "<button style=\"color:red; white-space: nowrap;\"><i class=\"fa fa-ban\"></i> Blacklist</button>" );
@@ -139,7 +145,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");
}
+3
View File
@@ -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;
}
?>
+61 -5
View File
@@ -333,6 +333,7 @@
// Create empty array for gravity
$gravity_domains = getGravity();
$wildcard_domains = getWildcardListContent();
if(isset($_GET["from"]))
{
@@ -381,8 +382,36 @@
$exploded = explode(" ", trim($query));
$domain = $exploded[count($exploded)-3];
$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];
@@ -505,16 +534,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 = [];
@@ -576,7 +622,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) {
+45 -11
View File
@@ -1,21 +1,55 @@
<?php
if(!isset($_GET['list']))
die();
die("Missing parameter");
$type = $_GET['list'];
if($type !== "white" && $type !== "black")
die("Invalid list parameter");
$listtype = $_GET['list'];
require "func.php";
$rawList = file_get_contents(checkfile("/etc/pihole/${type}list.txt"));
$list = explode("\n", $rawList);
switch ($listtype) {
case "white":
$list = getListContent("white");
break;
case "black":
$list = array_merge(getListContent("black"),getWildcardListContent());
break;
default:
die("Invalid list parameter");
break;
}
function getListContent($type) {
$rawList = file_get_contents(checkfile("/etc/pihole/".$type."list.txt"));
$list = explode("\n", $rawList);
// Get rid of empty lines
for($i = sizeof($list)-1; $i >= 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) {
+3
View File
@@ -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;
}
?>