From f132122e5753e476e90e3c3122a2477d8b34cf8c Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 5 Mar 2018 09:18:37 +0100 Subject: [PATCH 1/4] Use better client host name caching for DB queries Signed-off-by: DL6ER --- api_db.php | 59 ++++++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/api_db.php b/api_db.php index 3e3fdf03..b4bd8cb3 100644 --- a/api_db.php +++ b/api_db.php @@ -16,6 +16,30 @@ check_cors(); ini_set("max_execution_time","600"); $data = array(); +$clients = []; +function resolveOnlyHostname($clientip) +{ + if(array_key_exists($clientip, $clients)) + { + // Entry already exists + $clientname = $clients[$clientip]; + return $clientname; + } + + if(filter_var($clientip, FILTER_VALIDATE_IP)) + { + // Get host name of client and convert to lower case + $clientname = strtolower(gethostbyaddr($clientip)); + } + else + { + // This is already a host name + $clientname = strtolower($clientip); + } + // Buffer result + $clients[$clientname] = $clientip; + return $clientname; +} // Get posible non-standard location of FTL's database $FTLsettings = parse_ini_file("/etc/pihole/pihole-FTL.conf"); @@ -78,27 +102,7 @@ if (isset($_GET['getAllQueries']) && $auth) if(!is_bool($results)) while ($row = $results->fetchArray()) { - $c = $row[3]; - if(array_key_exists($row[3], $clients)) - { - // Entry already exists - $c = $clients[$row[3]]; - } - else - { - if(filter_var($row[3], FILTER_VALIDATE_IP)) - { - // Get host name of client and convert to lower case - $c = strtolower(gethostbyaddr($row[3])); - } - else - { - // This is already a host name - $c = strtolower($row[3]); - } - // Buffer result - $clients[$row[3]] = $c; - } + $c = resolveOnlyHostname($row[3]); $allQueries[] = [$row[0],$row[1] == 1 ? "IPv4" : "IPv6",$row[2],$c,$row[4]]; } } @@ -132,16 +136,9 @@ if (isset($_GET['topClients']) && $auth) if(!is_bool($results)) while ($row = $results->fetchArray()) { - if(filter_var($row[0], FILTER_VALIDATE_IP)) - { - // Get host name of client and convert to lower case - $c = strtolower(gethostbyaddr($row[0])); - } - else - { - // This is already a host name - $c = strtolower($row[0]); - } + + $c = resolveOnlyHostname($row[0]); + if(array_key_exists($c, $clients)) { // Entry already exists, add to it (might appear multiple times due to mixed capitalization in the database) From 746ac1ea3f0fbf470da3f1728f6852915a41a44e Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 5 Mar 2018 15:45:22 +0100 Subject: [PATCH 2/4] Prevent confusion between arrays Signed-off-by: DL6ER --- api_db.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/api_db.php b/api_db.php index b4bd8cb3..7c126297 100644 --- a/api_db.php +++ b/api_db.php @@ -131,7 +131,7 @@ if (isset($_GET['topClients']) && $auth) $stmt->bindValue(":until", intval($_GET['until']), SQLITE3_INTEGER); $results = $stmt->execute(); - $clients = array(); + $clientnums = array(); if(!is_bool($results)) while ($row = $results->fetchArray()) @@ -139,25 +139,25 @@ if (isset($_GET['topClients']) && $auth) $c = resolveOnlyHostname($row[0]); - if(array_key_exists($c, $clients)) + if(array_key_exists($c, $clientnums)) { // Entry already exists, add to it (might appear multiple times due to mixed capitalization in the database) - $clients[$c] += intval($row[1]); + $clientnums[$c] += intval($row[1]); } else { // Entry does not yet exist - $clients[$c] = intval($row[1]); + $clientnums[$c] = intval($row[1]); } } // Sort by number of hits - arsort($clients); + arsort($clientnums); // Extract only the first ten entries - $clients = array_slice($clients, 0, 10); + $clientnums = array_slice($clientnums, 0, 10); - $result = array('top_sources' => $clients); + $result = array('top_sources' => $clientnums); $data = array_merge($data, $result); } From 57f0642c0f8afb4a6d450eb4e597b94b5f12e788 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 5 Mar 2018 15:46:23 +0100 Subject: [PATCH 3/4] Remove unused array Signed-off-by: DL6ER --- api_db.php | 1 - 1 file changed, 1 deletion(-) diff --git a/api_db.php b/api_db.php index 7c126297..0c3686f2 100644 --- a/api_db.php +++ b/api_db.php @@ -98,7 +98,6 @@ if (isset($_GET['getAllQueries']) && $auth) $stmt->bindValue(":from", intval($from), SQLITE3_INTEGER); $stmt->bindValue(":until", intval($until), SQLITE3_INTEGER); $results = $stmt->execute(); - $clients = array(); if(!is_bool($results)) while ($row = $results->fetchArray()) { From 322b87befe78e9f8b5ee8515dadea811ebd147c9 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 5 Mar 2018 16:48:05 +0100 Subject: [PATCH 4/4] Imporve resolveHostname() subroutine Signed-off-by: DL6ER --- api_db.php | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/api_db.php b/api_db.php index 0c3686f2..aee13616 100644 --- a/api_db.php +++ b/api_db.php @@ -16,28 +16,35 @@ check_cors(); ini_set("max_execution_time","600"); $data = array(); -$clients = []; -function resolveOnlyHostname($clientip) +$clients = array(); +function resolveHostname($clientip, $printIP) { + global $clients; + $ipaddr = strtolower($clientip); if(array_key_exists($clientip, $clients)) { // Entry already exists - $clientname = $clients[$clientip]; + $clientname = $clients[$ipaddr]; + if($printIP) + return $clientname."|".$clientip; return $clientname; } - if(filter_var($clientip, FILTER_VALIDATE_IP)) + else if(filter_var($clientip, FILTER_VALIDATE_IP)) { // Get host name of client and convert to lower case - $clientname = strtolower(gethostbyaddr($clientip)); + $clientname = strtolower(gethostbyaddr($ipaddr)); } else { // This is already a host name - $clientname = strtolower($clientip); + $clientname = $ipaddr; } // Buffer result - $clients[$clientname] = $clientip; + $clients[$ipaddr] = $clientname; + + if($printIP) + return $clientname."|".$clientip; return $clientname; } @@ -101,7 +108,7 @@ if (isset($_GET['getAllQueries']) && $auth) if(!is_bool($results)) while ($row = $results->fetchArray()) { - $c = resolveOnlyHostname($row[3]); + $c = resolveHostname($row[3],false); $allQueries[] = [$row[0],$row[1] == 1 ? "IPv4" : "IPv6",$row[2],$c,$row[4]]; } } @@ -136,7 +143,7 @@ if (isset($_GET['topClients']) && $auth) while ($row = $results->fetchArray()) { - $c = resolveOnlyHostname($row[0]); + $c = resolveHostname($row[0],false); if(array_key_exists($c, $clientnums)) {