| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Net::IPAddress::Filter::IPFilterDat; |
|
2
|
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
2822
|
use strict; |
|
|
2
|
|
|
|
|
6
|
|
|
|
2
|
|
|
|
|
76
|
|
|
4
|
2
|
|
|
2
|
|
13
|
use warnings; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
93
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: A fast IP address filter from ipfilter.dat |
|
7
|
|
|
|
|
|
|
our $VERSION = '20121119.02'; # VERSION |
|
8
|
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
22
|
use Scalar::Util (); |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
39
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
2345
|
use Net::IPAddress::Filter 20121117; |
|
|
2
|
|
|
|
|
31519
|
|
|
|
2
|
|
|
|
|
71
|
|
|
12
|
2
|
|
|
2
|
|
22
|
use base qw( Net::IPAddress::Filter ); |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
327
|
|
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
use constant { |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# 000.000.000.000 - 000.255.255.255 , 000 , invalid ip |
|
18
|
2
|
|
|
|
|
899
|
RULE_REGEX => qr{ |
|
19
|
|
|
|
|
|
|
\A \s* |
|
20
|
|
|
|
|
|
|
([0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3}) # Start IP address |
|
21
|
|
|
|
|
|
|
\s* - \s* |
|
22
|
|
|
|
|
|
|
([0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3}) # End IP address |
|
23
|
|
|
|
|
|
|
\s* , \s* |
|
24
|
|
|
|
|
|
|
(\d+) # Score |
|
25
|
|
|
|
|
|
|
\s* , \s* |
|
26
|
|
|
|
|
|
|
(.*?) # Label |
|
27
|
|
|
|
|
|
|
\s* \z |
|
28
|
|
|
|
|
|
|
}xms, |
|
29
|
2
|
|
|
2
|
|
11
|
}; |
|
|
2
|
|
|
|
|
4
|
|
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub load_file { |
|
33
|
2
|
|
|
2
|
1
|
1143
|
my $self = shift; |
|
34
|
2
|
|
50
|
|
|
9
|
my $file = shift || return; |
|
35
|
|
|
|
|
|
|
|
|
36
|
2
|
|
|
|
|
2
|
my $FH; |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# A filehandle can be a GLOB ref, or a blessed ref to one of the IO:: |
|
39
|
|
|
|
|
|
|
# packages. reftype() handles boths cases. |
|
40
|
2
|
100
|
33
|
|
|
20
|
if ( ref($file) |
|
|
|
|
66
|
|
|
|
|
|
41
|
|
|
|
|
|
|
&& ( Scalar::Util::reftype($file) eq 'GLOB' || Scalar::Util::reftype( \$file ) eq 'GLOB' ) ) |
|
42
|
|
|
|
|
|
|
{ |
|
43
|
1
|
|
|
|
|
2
|
$FH = $file; |
|
44
|
|
|
|
|
|
|
} |
|
45
|
|
|
|
|
|
|
else { |
|
46
|
1
|
50
|
|
|
|
79
|
open $FH, '<', $file |
|
47
|
|
|
|
|
|
|
or die __PACKAGE__ . "::load_file() unable to open $file for reading: $!"; |
|
48
|
|
|
|
|
|
|
} |
|
49
|
|
|
|
|
|
|
|
|
50
|
2
|
|
|
|
|
4
|
my $rules_added = 0; |
|
51
|
2
|
|
|
|
|
56
|
while ( my $line = <$FH> ) { |
|
52
|
5
|
100
|
|
|
|
14
|
$rules_added++ if $self->add_rule($line); |
|
53
|
|
|
|
|
|
|
} |
|
54
|
|
|
|
|
|
|
|
|
55
|
2
|
|
|
|
|
44
|
close $FH; |
|
56
|
|
|
|
|
|
|
|
|
57
|
2
|
|
|
|
|
9
|
return $rules_added; |
|
58
|
|
|
|
|
|
|
} |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub add_rule { |
|
62
|
7
|
|
|
7
|
1
|
23335
|
my $self = shift; |
|
63
|
7
|
|
50
|
|
|
25
|
my $rule = shift || return 0; |
|
64
|
|
|
|
|
|
|
|
|
65
|
7
|
100
|
|
|
|
15
|
if ( my $data = _parse_rule($rule) ) { |
|
66
|
5
|
|
|
|
|
27
|
$self->add_range_with_value( $data->{label}, $data->{start_ip}, $data->{end_ip} ); |
|
67
|
5
|
|
|
|
|
176
|
return 1; |
|
68
|
|
|
|
|
|
|
} |
|
69
|
|
|
|
|
|
|
|
|
70
|
2
|
|
|
|
|
20
|
return 0; |
|
71
|
|
|
|
|
|
|
} |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub _parse_rule { |
|
75
|
13
|
|
|
13
|
|
3998
|
my $rule = shift; |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# 000.000.000.000 - 000.255.255.255 , 000 , invalid ip |
|
78
|
13
|
100
|
|
|
|
98
|
if ( $rule =~ RULE_REGEX ) { |
|
79
|
|
|
|
|
|
|
return { |
|
80
|
9
|
|
|
|
|
87
|
start_ip => $1, |
|
81
|
|
|
|
|
|
|
end_ip => $2, |
|
82
|
|
|
|
|
|
|
score => $3, |
|
83
|
|
|
|
|
|
|
label => $4, |
|
84
|
|
|
|
|
|
|
}; |
|
85
|
|
|
|
|
|
|
} |
|
86
|
4
|
|
|
|
|
13
|
return; |
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
1; |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
__END__ |