| 1 | package Plack::Middleware::DNSBL; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use parent qw(Plack::Middleware); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | our $VERSION = '0.0304'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Plack::Util::Accessor qw(cache cache_time resolver blacklists blacklisted); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Net::DNS::Resolver; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | sub prepare_app { | ||||||||||||||||||||||||||
|
Subroutine
prepare_app: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 16 | 0 | ▶ | unless ($self->resolver) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 0 | $self->resolver( Net::DNS::Resolver->new ); | |||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | unless ($self->blacklisted && ref $self->blacklisted eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->blacklisted and ref $self->blacklisted eq "CODE"
MC/DC0%0/2
$self->blacklisted and ref $self->blacklisted eq "CODE"
$self->blacklisted
ref $self->blacklisted eq "CODE"
Truth table0%0/3
A and B
A= $self->blacklistedB= ref $self->blacklisted eq "CODE"
| |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | $self->blacklisted(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 22 | 0 | [ 500, [ 'Content-Type' => 'text/plain' ], [ '' ] ]; | |||||||||||||||||||||||||||
| 23 | 0 | }); | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | unless ($self->blacklists && ref $self->blacklists eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->blacklists and ref $self->blacklists eq "HASH"
MC/DC0%0/2
$self->blacklists and ref $self->blacklists eq "HASH"
$self->blacklists
ref $self->blacklists eq "HASH"
Truth table0%0/3
A and B
A= $self->blacklistsB= ref $self->blacklists eq "HASH"
| |||||||||||||||||||||||||||||
| 27 | 0 | Carp::carp("'blacklists' option must contain a HASHREF value"); | |||||||||||||||||||||||||||
| 28 | 0 | $self->blacklists(+{ }); | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | unless ($self->cache_time) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | $self->cache_time('86400'); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub query { | ||||||||||||||||||||||||||
|
Subroutine
query: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | 0 | my ($self, $address) = @_; | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | my $response = $self->resolver->send($address) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | or return; | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # A Record = black listed | ||||||||||||||||||||||||||||
| 42 | 0 | foreach my $record ($response->answer) { | |||||||||||||||||||||||||||
| 43 | 0 | ▶ | return 1 if $record->type eq 'A'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub is_blacklisted { | ||||||||||||||||||||||||||
|
Subroutine
is_blacklisted: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 0 | my ($self, $ip, $port) = @_; | |||||||||||||||||||||||||||
| 49 | 0 | my $reversed = _reverse_ip($ip); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # Check if we have a cached response | ||||||||||||||||||||||||||||
| 52 | 0 | ▶ | if ($self->cache && (my $cached = $self->cache->get("dnsbl:$reversed"))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->cache and my $cached = $self->cache->get("dnsbl:$reversed")
MC/DC0%0/2
$self->cache and my $cached = $self->cache->get("dnsbl:$reversed")
$self->cache
my $cached = $self->cache->get("dnsbl:$reversed")
Truth table0%0/3
A and B
A= $self->cacheB= my $cached = $self->cache->get("dnsbl:$reversed")
| |||||||||||||||||||||||||||||
| 53 | 0 | return @$cached; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | my ( $blacklisted, $blacklist ); | |||||||||||||||||||||||||||
| 57 | 0 | foreach (keys %{ $self->blacklists }) { | |||||||||||||||||||||||||||
| 58 | 0 | my $address = $self->blacklists->{$_}; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | $address =~ s/\$ip/$reversed/g; | |||||||||||||||||||||||||||
| 61 | 0 | $address =~ s/\$port/$port/g; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ($self->query($address)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | $blacklist = $_; | |||||||||||||||||||||||||||
| 65 | 0 | $blacklisted = 1; | |||||||||||||||||||||||||||
| 66 | 0 | last; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # Caches our result | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if ($self->cache) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | $self->cache->set("dnsbl:$reversed" => [ $blacklisted, $blacklist, 1 ], | |||||||||||||||||||||||||||
| 73 | $self->cache_time); | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | return ( $blacklisted, $blacklist, 0 ); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | sub call { | ||||||||||||||||||||||||||
|
Subroutine
call: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 0 | my ($self, $env) = @_; | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | if (_is_ipv4($env->{REMOTE_ADDR})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | # Check if this IP is blacklisted | ||||||||||||||||||||||||||||
| 83 | my ($blacklisted, $blacklist, $is_cached) | ||||||||||||||||||||||||||||
| 84 | 0 | = $self->is_blacklisted($env->{REMOTE_ADDR}, $env->{SERVER_PORT}); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # If it's blacklisted, call the callback and return it's return value | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | if ($blacklisted) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | $env->{DNSBL_BLACKLISTED} = 1; | |||||||||||||||||||||||||||
| 89 | 0 | $env->{DNSBL_BLACKLIST} = $blacklist; | |||||||||||||||||||||||||||
| 90 | 0 | $env->{DNSBL_IS_CACHED} = $is_cached; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | return $self->blacklisted->($env, $blacklist, $is_cached); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | return $self->app->($env); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # Helper functions | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub _is_ipv4 { shift =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/ } | ||||||||||||||||||||||||||
|
Subroutine
_is_ipv4: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | sub _reverse_ip { join '.', reverse split /\./, shift } | ||||||||||||||||||||||||||
|
Subroutine
_reverse_ip: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 1 | 1; | |||||||||||||||||||||||||||
| 105 | __END__ | ||||||||||||||||||||||||||||