mirror of
https://github.com/pi-hole/web.git
synced 2026-04-21 01:10:19 +01:00
merge devel
This commit is contained in:
@@ -20,6 +20,15 @@
|
||||
$blackListFile = checkfile("/etc/pihole/blacklist.txt");
|
||||
$blacklist = new \SplFileObject($blackListFile);
|
||||
|
||||
if(isset($setupVars["API_PRIVACY_MODE"]))
|
||||
{
|
||||
$privacyMode = $setupVars["API_PRIVACY_MODE"];
|
||||
}
|
||||
else
|
||||
{
|
||||
$privacyMode = false;
|
||||
}
|
||||
|
||||
/******* Public Members ********/
|
||||
function getSummaryData() {
|
||||
$domains_being_blocked = gravityCount();
|
||||
@@ -45,6 +54,14 @@
|
||||
|
||||
$domains_over_time = overTime($dns_queries);
|
||||
$ads_over_time = overTime($ads_blocked);
|
||||
|
||||
// Provide a minimal valid array if there have are no blocked
|
||||
// queries at all. Otherwise the output of the API is inconsistent.
|
||||
if(count($ads_blocked) == 0)
|
||||
{
|
||||
$ads_over_time = [1 => 0];
|
||||
}
|
||||
|
||||
alignTimeArrays($ads_over_time, $domains_over_time);
|
||||
return Array(
|
||||
'domains_over_time' => $domains_over_time,
|
||||
@@ -59,6 +76,14 @@
|
||||
|
||||
$domains_over_time = overTime10mins($dns_queries);
|
||||
$ads_over_time = overTime10mins($ads_blocked);
|
||||
|
||||
// Provide a minimal valid array if there have are no blocked
|
||||
// queries at all. Otherwise the output of the API is inconsistent.
|
||||
if(count($ads_blocked) == 0)
|
||||
{
|
||||
$ads_over_time = [1 => 0];
|
||||
}
|
||||
|
||||
alignTimeArrays($ads_over_time, $domains_over_time);
|
||||
return Array(
|
||||
'domains_over_time' => $domains_over_time,
|
||||
@@ -67,12 +92,19 @@
|
||||
}
|
||||
|
||||
function getTopItems() {
|
||||
global $log;
|
||||
global $log,$privacyMode;
|
||||
$dns_queries = getDnsQueries($log);
|
||||
$ads_blocked = getBlockedQueries($log);
|
||||
|
||||
$topAds = topItems($ads_blocked);
|
||||
$topQueries = topItems($dns_queries, $topAds);
|
||||
if(!$privacyMode)
|
||||
{
|
||||
$topQueries = topItems($dns_queries, $topAds);
|
||||
}
|
||||
else
|
||||
{
|
||||
$topQueries = [];
|
||||
}
|
||||
|
||||
return Array(
|
||||
'top_queries' => $topQueries,
|
||||
@@ -107,13 +139,36 @@
|
||||
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|$key"] = $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]++;
|
||||
}
|
||||
@@ -122,17 +177,36 @@
|
||||
}
|
||||
}
|
||||
|
||||
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]++;
|
||||
}
|
||||
@@ -149,6 +223,12 @@
|
||||
|
||||
arsort($sources);
|
||||
$sources = array_slice($sources, 0, 10);
|
||||
|
||||
if(istrue($setupVars["API_GET_CLIENT_HOSTNAME"]))
|
||||
{
|
||||
resolveIPs($sources);
|
||||
}
|
||||
|
||||
return Array(
|
||||
'top_sources' => $sources
|
||||
);
|
||||
@@ -197,57 +277,93 @@
|
||||
}
|
||||
|
||||
function getAllQueries($orderBy) {
|
||||
global $log,$showBlocked,$showPermitted;
|
||||
global $log,$showBlocked,$showPermitted,$privacyMode;
|
||||
$allQueries = array("data" => array());
|
||||
$dns_queries = getDnsQueriesAll($log);
|
||||
$dns_queries = getDnsQueries($log);
|
||||
|
||||
// Create empty array for gravity
|
||||
$gravity_domains = getGravity();
|
||||
|
||||
setShowBlockedPermitted();
|
||||
|
||||
// Privacy mode?
|
||||
if($privacyMode)
|
||||
{
|
||||
$showPermitted = false;
|
||||
}
|
||||
|
||||
if(!$showBlocked && !$showPermitted)
|
||||
{
|
||||
// Nothing to do for us here
|
||||
return [];
|
||||
}
|
||||
|
||||
foreach ($dns_queries as $query) {
|
||||
$time = date_create(substr($query, 0, 16));
|
||||
$exploded = explode(" ", trim($query));
|
||||
$domain = $exploded[count($exploded)-3];
|
||||
$tmp = $exploded[count($exploded)-4];
|
||||
|
||||
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];
|
||||
$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,
|
||||
""
|
||||
));
|
||||
}
|
||||
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,
|
||||
""
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
return $allQueries;
|
||||
}
|
||||
|
||||
function tailPiholeLog($param) {
|
||||
// Not using SplFileObject here, since direct
|
||||
// usage of f-streams will be much faster for
|
||||
// files as large as the pihole.log
|
||||
global $logListName;
|
||||
$file = fopen($logListName,"r");
|
||||
$offset = intval($param);
|
||||
if($offset > 0)
|
||||
{
|
||||
// Seeks on the file pointer where we want to continue reading is known
|
||||
fseek($file, $offset);
|
||||
$lines = [];
|
||||
while (!feof($file)) {
|
||||
array_push($lines,fgets($file));
|
||||
}
|
||||
return ["offset" => ftell($file), "lines" => $lines];
|
||||
}
|
||||
else
|
||||
{
|
||||
// Locate the current position of the file read/write pointer
|
||||
fseek($file, -1, SEEK_END);
|
||||
// Add one to skip the very last "\n" in the log file
|
||||
return ["offset" => ftell($file)+1];
|
||||
}
|
||||
fclose($file);
|
||||
}
|
||||
|
||||
/******** Private Members ********/
|
||||
function gravityCount() {
|
||||
global $gravityListName,$blackListFile;
|
||||
@@ -260,7 +376,7 @@
|
||||
$log->rewind();
|
||||
$lines = [];
|
||||
foreach ($log as $line) {
|
||||
if(strpos($line, ": query[") !== false) {
|
||||
if(strpos($line, ": query[A") !== false) {
|
||||
$lines[] = $line;
|
||||
}
|
||||
}
|
||||
@@ -269,14 +385,14 @@
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -323,16 +439,39 @@
|
||||
function getBlockedQueries(\SplFileObject $log) {
|
||||
$log->rewind();
|
||||
$lines = [];
|
||||
$hostname = trim(file_get_contents("/etc/hostname"), "\x00..\x1F");
|
||||
foreach ($log as $line) {
|
||||
$line = preg_replace('/ {2,}/', ' ', $line);
|
||||
$exploded = explode(" ", $line);
|
||||
if(count($exploded) == 8) {
|
||||
$tmp = $exploded[count($exploded) - 4];
|
||||
$tmp2 = $exploded[count($exploded) - 5];
|
||||
$tmp3 = $exploded[count($exploded) - 3];
|
||||
//filter out bad names and host file reloads:
|
||||
if(substr($tmp, strlen($tmp) - 12, 12) == "gravity.list" && $tmp2 != "read" && $tmp3 != "pi.hole" && $tmp3 != $hostname) {
|
||||
if(count($exploded) == 8 || count($exploded) == 10) {
|
||||
// Structure of data is currently like:
|
||||
// Array
|
||||
// (
|
||||
// [0] => Dec
|
||||
// [1] => 19
|
||||
// [2] => 11:21:51
|
||||
// [3] => dnsmasq[2584]:
|
||||
// [4] => /etc/pihole/gravity.list
|
||||
// [5] => doubleclick.com
|
||||
// [6] => is
|
||||
// [7] => ip.of.pi.hole
|
||||
// )
|
||||
// with extra logging enabled
|
||||
// Array
|
||||
// (
|
||||
// [0] => Dec
|
||||
// [1] => 19
|
||||
// [2] => 11:21:51
|
||||
// [3] => dnsmasq[2584]:
|
||||
// [4] => 1 (identifier)
|
||||
// [5] => 1.2.3.4/12345
|
||||
// [6] => /etc/pihole/gravity.list
|
||||
// [7] => doubleclick.com
|
||||
// [8] => is
|
||||
// [9] => ip.of.pi.hole
|
||||
// )
|
||||
$list = $exploded[count($exploded)-4];
|
||||
$is = $exploded[count($exploded)-2];
|
||||
// Consider only gravity.list as DNS source (not e.g. hostname.list)
|
||||
if(substr($list, strlen($list) - 12, 12) === "gravity.list" && $is === "is") {
|
||||
$lines[] = $line;
|
||||
};
|
||||
}
|
||||
@@ -342,8 +481,7 @@
|
||||
|
||||
function countBlockedQueries() {
|
||||
global $logListName;
|
||||
$hostname = trim(file_get_contents("/etc/hostname"), "\x00..\x1F");
|
||||
return exec("grep \"gravity.list\" $logListName | grep -v \"pi.hole\" | grep -v \" read \" | grep -v -c \"".$hostname."\"");
|
||||
return exec("grep \"gravity.list\" $logListName | grep -c \" is \"");
|
||||
}
|
||||
|
||||
function getForwards(\SplFileObject $log) {
|
||||
|
||||
Reference in New Issue
Block a user