line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::Middleware::IPAddressFilter; |
2
|
2
|
|
|
2
|
|
1469
|
use parent qw(Plack::Middleware); |
|
2
|
|
|
|
|
279
|
|
|
2
|
|
|
|
|
16
|
|
3
|
|
|
|
|
|
|
|
4
|
2
|
|
|
2
|
|
21835
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
51
|
|
5
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
76
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
1673
|
use Net::IP::AddrRanges; |
|
2
|
|
|
|
|
5015
|
|
|
2
|
|
|
|
|
72
|
|
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
15
|
use Plack::Util::Accessor qw( rules ); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
20
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub prepare_app { |
13
|
2
|
|
|
2
|
1
|
1350
|
my $self = shift; |
14
|
|
|
|
|
|
|
|
15
|
2
|
|
|
|
|
9
|
my $rules = $self->rules; |
16
|
2
|
50
|
|
|
|
98
|
if(ref $rules ne 'ARRAY') { |
17
|
0
|
|
|
|
|
0
|
die 'rules should be an array reference'; |
18
|
|
|
|
|
|
|
} |
19
|
2
|
|
|
|
|
14
|
my $ranges = Net::IP::AddrRanges->new(); |
20
|
2
|
|
|
|
|
18
|
for(@$rules) { |
21
|
2
|
|
|
|
|
11
|
s/^([\+\-])? *//; |
22
|
2
|
50
|
33
|
|
|
23
|
if(not defined $1 or $1 eq '+') { |
23
|
2
|
|
|
|
|
9
|
$ranges->add($_); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
else { |
26
|
0
|
|
|
|
|
0
|
$ranges->subtract($_); |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
} |
29
|
2
|
|
|
|
|
171
|
$self->rules($ranges); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub call { |
33
|
2
|
|
|
2
|
1
|
29308
|
my($self, $env) = @_; |
34
|
|
|
|
|
|
|
|
35
|
2
|
50
|
|
|
|
11
|
my $remote_address = $env->{REMOTE_ADDR} |
36
|
|
|
|
|
|
|
or return $self->forbidden; |
37
|
|
|
|
|
|
|
|
38
|
2
|
100
|
|
|
|
11
|
if($self->rules->find($remote_address)) { |
39
|
1
|
|
|
|
|
77
|
return $self->app->($env); |
40
|
|
|
|
|
|
|
} |
41
|
1
|
|
|
|
|
37
|
return $self->forbidden; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub forbidden { |
45
|
1
|
|
|
1
|
0
|
2
|
my $self = shift; |
46
|
1
|
|
|
|
|
4
|
my $body = "You don't have permission to access this resouce."; |
47
|
|
|
|
|
|
|
return [ |
48
|
1
|
|
|
|
|
10
|
403, |
49
|
|
|
|
|
|
|
[ 'Content-Type' => 'text/plain', |
50
|
|
|
|
|
|
|
'Content-Length' => length $body ], |
51
|
|
|
|
|
|
|
[ $body ], |
52
|
|
|
|
|
|
|
]; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
1; |
56
|
|
|
|
|
|
|
__END__ |