From 5cbc20f4776e97d8fff894a572b114ce1adbdb1f Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 19 Dec 2016 11:36:58 +0100 Subject: [PATCH 1/7] Simplified function getBlockedQueries() --- data.php | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/data.php b/data.php index 2a145110..e02ee43e 100644 --- a/data.php +++ b/data.php @@ -285,11 +285,22 @@ $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) { + // 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 + // ) + $list = $exploded[4]; + $is = $exploded[6]; + // 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; }; } From 42e9644a3ff7980c389060616e293570151cba1e Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 19 Dec 2016 11:38:04 +0100 Subject: [PATCH 2/7] Greatly simplified function countBlockedQueries() --- data.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data.php b/data.php index e02ee43e..57d8aa4f 100644 --- a/data.php +++ b/data.php @@ -310,7 +310,7 @@ function countBlockedQueries() { $hostname = trim(file_get_contents("/etc/hostname"), "\x00..\x1F"); - return exec("grep \"gravity.list\" /var/log/pihole.log | grep -v \"pi.hole\" | grep -v \" read \" | grep -v -c \"".$hostname."\""); + return exec("grep \"gravity.list\" /var/log/pihole.log | grep -c \" is \""); } function getForwards(\SplFileObject $log) { From a30d8918c445e83e17bbc6e19580a7719792c49d Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 19 Dec 2016 11:43:59 +0100 Subject: [PATCH 3/7] Removed preg_replace --- data.php | 1 - 1 file changed, 1 deletion(-) diff --git a/data.php b/data.php index 57d8aa4f..b82dfa8f 100644 --- a/data.php +++ b/data.php @@ -282,7 +282,6 @@ $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) { // Structure of data is currently like: From 2d7ef8fd9a2be4aa619fc4854a5fd5f552965885 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 19 Dec 2016 11:46:25 +0100 Subject: [PATCH 4/7] == to === --- data.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data.php b/data.php index b82dfa8f..6b9acde9 100644 --- a/data.php +++ b/data.php @@ -299,7 +299,7 @@ $list = $exploded[4]; $is = $exploded[6]; // Consider only gravity.list as DNS source (not e.g. hostname.list) - if(substr($list, strlen($list) - 12, 12) == "gravity.list" && $is === "is") { + if(substr($list, strlen($list) - 12, 12) === "gravity.list" && $is === "is") { $lines[] = $line; }; } From c04c8f93a0d21586825301fc9cfefda1fefac46c Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 19 Dec 2016 11:56:06 +0100 Subject: [PATCH 5/7] Don't need to get $hostname anymore --- data.php | 1 - 1 file changed, 1 deletion(-) diff --git a/data.php b/data.php index 6b9acde9..1d163c67 100644 --- a/data.php +++ b/data.php @@ -280,7 +280,6 @@ function getBlockedQueries(\SplFileObject $log) { $log->rewind(); $lines = []; - $hostname = trim(file_get_contents("/etc/hostname"), "\x00..\x1F"); foreach ($log as $line) { $exploded = explode(" ", $line); if(count($exploded) == 8) { From 10ef50a6eb0fdf94d2a41b08cd186970a19a94d4 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 19 Dec 2016 11:59:09 +0100 Subject: [PATCH 6/7] Removed another $hostname = ... --- data.php | 1 - 1 file changed, 1 deletion(-) diff --git a/data.php b/data.php index 1d163c67..65d0a7aa 100644 --- a/data.php +++ b/data.php @@ -307,7 +307,6 @@ } function countBlockedQueries() { - $hostname = trim(file_get_contents("/etc/hostname"), "\x00..\x1F"); return exec("grep \"gravity.list\" /var/log/pihole.log | grep -c \" is \""); } From 815c4a7173fb83619d81a8a4c54347193060bd72 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 20 Dec 2016 23:23:41 +0100 Subject: [PATCH 7/7] make function getBlockedQueries(\SplFileObject $log) compatible with extra logging format of dnsmasq --- data.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/data.php b/data.php index 65d0a7aa..e08b1ac0 100644 --- a/data.php +++ b/data.php @@ -282,7 +282,7 @@ $lines = []; foreach ($log as $line) { $exploded = explode(" ", $line); - if(count($exploded) == 8) { + if(count($exploded) == 8 || count($exploded) == 10) { // Structure of data is currently like: // Array // ( @@ -295,8 +295,22 @@ // [6] => is // [7] => ip.of.pi.hole // ) - $list = $exploded[4]; - $is = $exploded[6]; + // 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;