Allow CIDR notation in exclude file to skip line matching the given xxx.xxx.xxx.xxx/n network. Thanks to the MangOuste for the patch.

This commit is contained in:
Darold Gilles 2013-05-25 19:09:59 +02:00
parent 66d6d91bff
commit 63d2664447
5 changed files with 56 additions and 20 deletions

4
README
View File

@ -254,8 +254,12 @@ CONFIGURATION
the exclusion (USER, CLIENT or URI) and a space separated list of
valid regex.
You can also use the NETWORK type to define network address with
netmask using the CIDR notation: xxx.xxx.xxx.xxx/n
See example bellow:
NETWORK 192.168.1.0/24 10.10.0.0/16
CLIENT 192\.168\.1\.2
CLIENT 10\.169\.1\.\d+ 192\.168\.10\..*
USER myloginstr

View File

@ -230,22 +230,12 @@ sub parseFile
# Remove extra space character in username
$login =~ s/\%20//g;
my $found = 0;
my $id = $client_ip || '';
if ($login ne '-') {
$id = $login;
}
next if (!$id || !$bytes);
# check for client/user exclusion in old syntax
my $found = 0;
if (exists $self->{Exclude}{all}) {
foreach my $e (@{$self->{Exclude}{all}}) {
if ( ($client_ip =~ m#^$e$#i) || ($login =~ m#^$e$#i)) {
$found = 1;
last;
}
}
next if ($found);
}
# check for user exclusion
if (exists $self->{Exclude}{users}) {
foreach my $e (@{$self->{Exclude}{users}}) {
@ -276,6 +266,16 @@ sub parseFile
}
next if ($found);
}
# check for Network exclusion
if (exists $self->{Exclude}{networks}) {
foreach my $e (@{$self->{Exclude}{networks}}) {
if (&check_ip($client_ip, $e)) {
$found = 1;
last;
}
}
next if ($found);
}
# Anonymize all users
if ($self->{AnonymizeLogin} && ($client_ip ne $id)) {
if (!exists $self->{AnonymizedId}{$id}) {
@ -2551,7 +2551,6 @@ sub _print_top_domain_stat
$first = $4;
$last = $5;
}
$url =~ /(\.[^\.]+)$/;
if ($url !~ /\.\d+$/) {
if ($url =~ /([^\.]+)(\.[^\.]+)$/) {
$perdomain{$2}{hits} += $hits;
@ -2951,17 +2950,19 @@ sub parse_exclusion
chomp($l);
$i++;
next if (!$l || ($l =~ /^[\s\t]*#/));
if ($l =~ m#^(USER|CLIENT|URI)[\s\t]+(.*)#) {
# remove comments at end of line
$l =~ s/[\s\t]*#.*//;
if ($l =~ m#^(USER|CLIENT|URI|NETWORK)[\s\t]+(.*)#) {
my $lbl = lc($1) . 's';
my @rg = split(m#[\s\t]+#, $2);
foreach my $r (@rg) {
next if ($lbl eq 'networks');
&check_regex($r, "$file at line $i");
}
push(@{$exclusion{$lbl}}, @rg);
} else {
# backward compatibility
&check_regex($l, "$file at line $i");
push(@{$exclusion{all}}, $l);
# backward compatibility is not more supported
die "ERROR: wrong line format in file $file at line $i\n";
}
}
close(EXCLUDED);
@ -3332,6 +3333,25 @@ sub check_regex
}
}
sub check_ip
{
my ($ip, $block) = @_;
my @ip = split(/\./, $ip);
my $ip1 = $ip[0] * 2**24 + $ip[1] * 2**16 + $ip[2] * 2**8 + $ip[3];
my @submask = split(/\//, $block);
my $ip2 = $submask[0];
my $netmask = $submask[1];
my @ip2 = split(/\./, $ip2);
$ip2 = $ip2[0] * 2**24 + $ip2[1] * 2**16 + $ip2[2] * 2**8 + $ip2[3];
if ( $ip1 >> (32-$netmask) == $ip2 >> (32-$netmask)) {
return 1;
}
return 0;
}
1;
__END__

View File

@ -273,8 +273,12 @@ uri to exclude from report.
You can define one by line exclusion by specifying first the type of the
exclusion (USER, CLIENT or URI) and a space separated list of valid regex.
You can also use the NETWORK type to define network address with netmask
using the CIDR notation: xxx.xxx.xxx.xxx/n
See example bellow:
NETWORK 192.168.1.0/24 10.10.0.0/16
CLIENT 192\.168\.1\.2
CLIENT 10\.169\.1\.\d+ 192\.168\.10\..*
USER myloginstr

View File

@ -124,7 +124,7 @@
.\" ========================================================================
.\"
.IX Title "SQUIDANALYZER 1"
.TH SQUIDANALYZER 1 "2013-01-30" "perl v5.14.2" "User Contributed Perl Documentation"
.TH SQUIDANALYZER 1 "2013-05-25" "perl v5.14.2" "User Contributed Perl Documentation"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
@ -404,9 +404,13 @@ uri to exclude from report.
You can define one by line exclusion by specifying first the type of the
exclusion (\s-1USER\s0, \s-1CLIENT\s0 or \s-1URI\s0) and a space separated list of valid regex.
.Sp
You can also use the \s-1NETWORK\s0 type to define network address with netmask
using the \s-1CIDR\s0 notation: xxx.xxx.xxx.xxx/n
.Sp
See example bellow:
.Sp
.Vb 6
.Vb 7
\& NETWORK 192.168.1.0/24 10.10.0.0/16
\& CLIENT 192\e.168\e.1\e.2
\& CLIENT 10\e.169\e.1\e.\ed+ 192\e.168\e.10\e..*
\& USER myloginstr

View File

@ -1,11 +1,15 @@
#------------------------------------------------------------------------------
# File used to defined which client ip address, network regex address and auth
# login and URI to exclude from report.
# File used to defined which client ip address, network with netmask, network
# regex address, auth login and URI to exclude from the report.
#
# You can define one by line exclusion by specifying first the type of the
# exclusion (USER, CLIENT or URI) and a space separated list of valid regex.
# You can also use the NETWORK type to define network address with netmask
# using the CIDR notation: xxx.xxx.xxx.xxx/n
#
# See example bellow:
#------------------------------------------------------------------------------
#NETWORK 192.168.1.0/24 10.10.0.0/16
#CLIENT 192\.168\.1\.2
#CLIENT 10\.169\.1\.\d+ 192\.168\.10\..*
#USER myloginstr