Skip to content

Commit

Permalink
Merge pull request #253 from pi-hole/clienthosts
Browse files Browse the repository at this point in the history
Improve API calls for Top Clients and Forward Destinations
  • Loading branch information
PromoFaux authored Dec 23, 2016
2 parents 8e3e9e3 + 41cd382 commit f92d138
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 40 deletions.
119 changes: 82 additions & 37 deletions data.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,36 @@ function getIpvType() {
return $queryTypes;
}

function resolveIPs(&$array) {
$hostarray = [];
foreach ($array as $key => $value)
{
$hostname = gethostbyaddr($key);
// If we found a hostname for the IP, replace it
if($hostname)
{
// Generate HOST entry
$hostarray[$hostname] = $value;
}
else
{
// Generate IP entry
$hostarray[$key] = $value;
}
}
$array = $hostarray;

// Sort new array
arsort($array);
}

function getForwardDestinations() {
global $log;
global $log, $setupVars;
$forwards = getForwards($log);
$destinations = array();
foreach ($forwards as $forward) {
$exploded = explode(" ", trim($forward));
$dest = hasHostName($exploded[count($exploded) - 1]);
$dest = $exploded[count($exploded) - 1];
if (isset($destinations[$dest])) {
$destinations[$dest]++;
}
Expand All @@ -122,17 +145,36 @@ function getForwardDestinations() {
}
}

if(istrue($setupVars["API_GET_UPSTREAM_DNS_HOSTNAME"]))
{
resolveIPs($destinations);
}

return $destinations;

}

// Check for existance of variable
// and test it only if it exists
function istrue(&$argument) {
$ret = false;
if(isset($argument))
{
if($argument)
{
$ret = true;
}
}
return $ret;
}

function getQuerySources() {
global $log;
global $log, $setupVars;
$dns_queries = getDnsQueries($log);
$sources = array();
foreach($dns_queries as $query) {
$exploded = explode(" ", $query);
$ip = hasHostName(trim($exploded[count($exploded)-1]));
$ip = trim($exploded[count($exploded)-1]);
if (isset($sources[$ip])) {
$sources[$ip]++;
}
Expand All @@ -149,6 +191,12 @@ function getQuerySources() {

arsort($sources);
$sources = array_slice($sources, 0, 10);

if(istrue($setupVars["API_GET_CLIENT_HOSTNAME"]))
{
resolveIPs($sources);
}

return Array(
'top_sources' => $sources
);
Expand Down Expand Up @@ -199,7 +247,7 @@ function setShowBlockedPermitted()
function getAllQueries($orderBy) {
global $log,$showBlocked,$showPermitted;
$allQueries = array("data" => array());
$dns_queries = getDnsQueriesAll($log);
$dns_queries = getDnsQueries($log);

// Create empty array for gravity
$gravity_domains = getGravity();
Expand All @@ -212,36 +260,33 @@ function getAllQueries($orderBy) {

setShowBlockedPermitted();

if (substr($tmp, 0, 5) == "query")
$status = isset($gravity_domains[$domain]) ? "Pi-holed" : "OK";
if(($status === "Pi-holed" && $showBlocked) || ($status === "OK" && $showPermitted))
{
$status = isset($gravity_domains[$domain]) ? "Pi-holed" : "OK";
if(($status === "Pi-holed" && $showBlocked) || ($status === "OK" && $showPermitted))
{
$type = substr($exploded[count($exploded)-4], 6, -1);
$client = $exploded[count($exploded)-1];

if($orderBy == "orderByClientDomainTime"){
$allQueries['data'][hasHostName($client)][$domain][$time->format('Y-m-d\TH:i:s')] = $status;
}elseif ($orderBy == "orderByClientTimeDomain"){
$allQueries['data'][hasHostName($client)][$time->format('Y-m-d\TH:i:s')][$domain] = $status;
}elseif ($orderBy == "orderByTimeClientDomain"){
$allQueries['data'][$time->format('Y-m-d\TH:i:s')][hasHostName($client)][$domain] = $status;
}elseif ($orderBy == "orderByTimeDomainClient"){
$allQueries['data'][$time->format('Y-m-d\TH:i:s')][$domain][hasHostName($client)] = $status;
}elseif ($orderBy == "orderByDomainClientTime"){
$allQueries['data'][$domain][hasHostName($client)][$time->format('Y-m-d\TH:i:s')] = $status;
}elseif ($orderBy == "orderByDomainTimeClient"){
$allQueries['data'][$domain][$time->format('Y-m-d\TH:i:s')][hasHostName($client)] = $status;
}else{
array_push($allQueries['data'], array(
$time->format('Y-m-d\TH:i:s'),
$type,
$domain,
hasHostName($client),
$status,
""
));
}
$type = substr($exploded[count($exploded)-4], 6, -1);
$client = $exploded[count($exploded)-1];

if($orderBy == "orderByClientDomainTime"){
$allQueries['data'][hasHostName($client)][$domain][$time->format('Y-m-d\TH:i:s')] = $status;
}elseif ($orderBy == "orderByClientTimeDomain"){
$allQueries['data'][hasHostName($client)][$time->format('Y-m-d\TH:i:s')][$domain] = $status;
}elseif ($orderBy == "orderByTimeClientDomain"){
$allQueries['data'][$time->format('Y-m-d\TH:i:s')][hasHostName($client)][$domain] = $status;
}elseif ($orderBy == "orderByTimeDomainClient"){
$allQueries['data'][$time->format('Y-m-d\TH:i:s')][$domain][hasHostName($client)] = $status;
}elseif ($orderBy == "orderByDomainClientTime"){
$allQueries['data'][$domain][hasHostName($client)][$time->format('Y-m-d\TH:i:s')] = $status;
}elseif ($orderBy == "orderByDomainTimeClient"){
$allQueries['data'][$domain][$time->format('Y-m-d\TH:i:s')][hasHostName($client)] = $status;
}else{
array_push($allQueries['data'], array(
$time->format('Y-m-d\TH:i:s'),
$type,
$domain,
hasHostName($client),
$status,
""
));
}
}
}
Expand All @@ -260,7 +305,7 @@ function getDnsQueries(\SplFileObject $log) {
$log->rewind();
$lines = [];
foreach ($log as $line) {
if(strpos($line, ": query[") !== false) {
if(strpos($line, ": query[A") !== false) {
$lines[] = $line;
}
}
Expand All @@ -269,14 +314,14 @@ function getDnsQueries(\SplFileObject $log) {

function countDnsQueries() {
global $logListName;
return exec("grep -c \": query\\[\" $logListName");
return exec("grep -c \": query\\[A\" $logListName");
}

function getDnsQueriesAll(\SplFileObject $log) {
$log->rewind();
$lines = [];
foreach ($log as $line) {
if(strpos($line, ": query[") || strpos($line, "gravity.list") || strpos($line, ": forwarded") !== false) {
if(strpos($line, ": query[A") || strpos($line, "gravity.list") || strpos($line, ": forwarded") !== false) {
$lines[] = $line;
}
}
Expand Down
32 changes: 32 additions & 0 deletions php/savesettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ function validIP($address){
return !filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false;
}

// Check for existance of variable
// and test it only if it exists
function istrue($argument) {
$ret = false;
if(isset($argument))
{
if($argument)
{
$ret = true;
}
}
return $ret;
}

// Credit: http://stackoverflow.com/a/4694816/2087442
function validDomain($domain_name)
{
Expand Down Expand Up @@ -216,6 +230,24 @@ function validDomain($domain_name)
$success .= "No entries will be shown in Query Log";
}

if(isset($_POST["resolve-forward"]))
{
exec("sudo pihole -a resolve forward true");
}
else
{
exec("sudo pihole -a resolve forward false");
}

if(isset($_POST["resolve-clients"]))
{
exec("sudo pihole -a resolve clients true");
}
else
{
exec("sudo pihole -a resolve clients false");
}

break;

case "webUI":
Expand Down
43 changes: 40 additions & 3 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,25 @@
} else {
$queryLog = "all";
}

if(istrue($setupVars["API_GET_UPSTREAM_DNS_HOSTNAME"]))
{
$resolveForward = true;
}
else
{
$resolveForward = false;
}

if(istrue($setupVars["API_GET_CLIENT_HOSTNAME"]))
{
$resolveClients = true;
}
else
{
$resolveClients = false;
}

?>
<div class="box box-success">
<div class="box-header with-border">
Expand All @@ -407,10 +426,28 @@
<textarea name="clients" class="form-control" rows="4" placeholder="Enter one IP address per line"><?php foreach ($excludedClients as $client) { echo $client."\n"; } ?></textarea>
</div>
</div>
<h4>Reverse DNS lookup</h4>
<p>Try to determine the domain name via querying the Pi-hole for</p>
<div class="col-lg-6">
<div class="form-group">
<div class="checkbox"><label><input type="checkbox" name="resolve-forward" <?php if($resolveForward){ ?>checked<?php } ?>> Forward Destinations</label></div>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<div class="checkbox"><label><input type="checkbox" name="resolve-clients" <?php if($resolveClients){ ?>checked<?php } ?>> Top Clients</label></div>
</div>
</div>
<h4>Query Log</h4>
<div class="form-group">
<div class="checkbox"><label><input type="checkbox" name="querylog-permitted" <?php if($queryLog === "permittedonly" || $queryLog === "all"){ ?>checked<?php } ?>> Show permitted queries</label></div>
<div class="checkbox"><label><input type="checkbox" name="querylog-blocked" <?php if($queryLog === "blockedonly" || $queryLog === "all"){ ?>checked<?php } ?>> Show blocked queries</label></div>
<div class="col-lg-6">
<div class="form-group">
<div class="checkbox"><label><input type="checkbox" name="querylog-permitted" <?php if($queryLog === "permittedonly" || $queryLog === "all"){ ?>checked<?php } ?>> Show permitted queries</label></div>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<div class="checkbox"><label><input type="checkbox" name="querylog-blocked" <?php if($queryLog === "blockedonly" || $queryLog === "all"){ ?>checked<?php } ?>> Show blocked queries</label></div>
</div>
</div>
</div>
<div class="box-footer">
Expand Down

0 comments on commit f92d138

Please sign in to comment.