IOLoop/Client/Paranoid.pm

filter: Statementstmt 70.4% 62 / 88 Branchbran 38.1% 16 / 42 Conditioncond 31.2% 5 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 72.2% 13 / 18 Podpod 100% 1 / 1 totaltotal 55.4% 97 / 175 CC 29 SCAR 26.2
CC
29
Cov
57%
connect
33.7
_connect
18.3
__ANON__
12.0
CRAP 13.8
↑ Summary UserAgent/Paranoid.pm »
1 package Mojo::IOLoop::Client::Paranoid 0.01;
2 2 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use IO::Socket::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use IO::Socket::UNIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Socket qw(IPPROTO_TCP SOCK_STREAM TCP_NODELAY);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Mojo::Base 'Mojo::IOLoop::Client';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Net::DNS::Paranoid; # does this handle IPv6 ??
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Mojo::IOLoop::Client::Paranoid - paranoid IOLoop proxy
16  
17 =head1 SYNOPSIS
18  
19 my $user_client = Mojo::IOLoop::Client::Paranoid->new(
20 paranoid_dns => Net::DNS::Paranoid->new(
21 blocked_hosts => [qr{\.dev.example\.com$}]
22 ),
23 );
24 $user_client->connect($host); # will not connect to internal hosts
25  
26 =head1 METHODS
27  
28 =head2 C<< ->new >>
29  
30 my $client = Mojo::IOLoop::Client::Paranoid->new(
31 paranoid_dns => Net::DNS::Paranoid->new(...),
32 )
33  
34 Creates a new paranoid client. The C<< ->connect >> method will check that the
35 given hostname does not resolve to any internal or blacklisted IP address.
36 See L<Net::DNS::Paranoid> for how to configure the white- and blacklists.
37  
38 =cut
39  
40 0 ▶use constant NNR => $ENV{MOJO_NO_NNR} ? 0 : !!eval { require Net::DNS::Native; Net::DNS::Native->VERSION('0.15'); 1 };
Subroutine BEGIN: called CC=2 SCAR=7.9
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'MOJO_NO_NNR'} ? : 0 2 2
41 2 our $NDN;
42  
43 0 ▶has 'paranoid_dns' => sub { Net::DNS::Paranoid->new() };
Subroutine __ANON__: not called CC=1 SCAR=1.2
44  
45 # We can't easily override the DNS resolver that Mojolicious uses as it is
46 # a lexical variable, and everything else would involve monkeypatching.
47 # So we copy the relevant code here.
48  
49 1 ▶sub _port { $_[0]{socks_port} || $_[0]{port} || ($_[0]{tls} ? 443 : 80) }
Subroutine _port: called CC=4 SCAR=20.7
Branch: 2 branches
BranchTrueFalseTotal
$_[0]{"socks_port"} or $_[0]{"port"} or $_[0]{"tls"} ? 443 : 80 1 0 1
$_[0]{'tls'} ? : 0 0 0
Condition33%1/3
$_[0]{"socks_port"} or $_[0]{"port"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$_[0]{"socks_port"} or $_[0]{"port"}
$_[0]{"socks_port"} $_[0]{"port"}
Truth table33%1/3
A or B
A= $_[0]{"socks_port"}B= $_[0]{"port"}
ABresult
1X1
011
000
50  
51 4 ▶sub connect( $self, @args) {
Subroutine connect: called CC=9 SCAR=35.2
Pod documented
52 4 ▶ my ($args) = (ref $args[0] ? $args[0] : {@args});
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 4 4
53 # Timeout
54 4 weaken $self;
55 4 my $reactor = $self->reactor;
56 4 my $r = $reactor;
57 0 ▶ $self->{timer} = $reactor->timer($args->{timeout} || 10, sub { $self->emit(error => 'Connect timeout') });
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition50%1/2
$args->{'timeout'} || 10
l!l
40
MC/DC0%0/1
$args->{'timeout'} || 10
$args->{'timeout'}
Truth table50%1/2
A
A= $args->{'timeout'}
Aresult
11
00
58 # Blocking name resolution
59 4 ▶ $_ && s/[[\]]//g for @$args{qw(address socks_address)};
Branch: 1 branch
BranchTrueFalseTotal
$_ and s/[[\]]//gu 4 4 8
60 4 ▶ my $address = $args->{socks_address} || ($args->{address} ||= '127.0.0.1');
Condition40%2/5
$args->{'socks_address'} || ($args->{'address'} ||= '127.0.0.1')
l!l&&r!l&&!r
040
$args->{"address"} ||= "127.0.0.1"
l!l
40
MC/DC0%0/2
$args->{'socks_address'} || ($args->{'address'} ||= '127.0.0.1')
$args->{'socks_address'} ($args->{'address'} ||= '127.0.0.1')
MC/DC0%0/1
$args->{"address"} ||= "127.0.0.1"
$args->{"address"}
Truth table33%1/3
A || B
A= $args->{'socks_address'}B= ($args->{'address'} ||= '127.0.0.1')
ABresult
1X1
011
000
Truth table50%1/2
A
A= $args->{"address"}
Aresult
11
00
61 4 ▶ return $reactor->next_tick(sub { $self && $self->_connect($args) }) if !NNR || $args->{handle} || $args->{path};
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->_connect($args) 4 0 4
62 # Non-blocking name resolution
63 0 my $paranoid_dns = $self->paranoid_dns;
64  
65 0 ▶ if( $paranoid_dns->_bad_host($args->{address})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paranoid_dns->_bad_host($args->{"address"})) 0 0 0
66 # We need to delay the error by one tick so the callback variables get
67 # initialized
68 # warn "Immediate bad host ($self)";
69 0 ▶ return $reactor->next_tick( sub { warn "Lost self" unless $self; $self && $self->emit(error => "Can't connect: Bad host '$args->{address}'"); undef $self });
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
unless $self 0 0 0
$self and $self->emit("error", qq[Can't connect: Bad host '$args->{"address"}']) 0 0 0
70 }
71  
72 0 ▶ $NDN //= Net::DNS::Native->new(pool => 5, extra_thread => 1);
Condition0%0/3
$Mojo::IOLoop::Client::Paranoid::NDN //= "Net::DNS::Native"->new("pool", 5, "extra_thread", 1)
l!l&&r!l&&!r
000
MC/DC0%0/2
$Mojo::IOLoop::Client::Paranoid::NDN //= "Net::DNS::Native"->new("pool", 5, "extra_thread", 1)
$Mojo::IOLoop::Client::Paranoid::NDN "Net::DNS::Native"->new("pool", 5, "extra_thread", 1)
Truth table0%0/3
A //= B
A= $Mojo::IOLoop::Client::Paranoid::NDNB= "Net::DNS::Native"->new("pool", 5, "extra_thread", 1)
ABresult
1X1
011
000
73 #warn "# Using Net::DNS::Native to resolve '$address'";
74 my $handle = $self->{dns}
75 0 = $NDN->getaddrinfo($address, _port($args), {protocol => IPPROTO_TCP, socktype => SOCK_STREAM});
76 $reactor->io(
77 0 ▶ $handle => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
78 0 my $reactor = shift;
79 0 $reactor->remove($self->{dns});
80 0 my ($err, @res) = $NDN->get_result(delete $self->{dns});
81 0 ▶ return $self->emit(error => "Can't resolve: $err") if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
82  
83 #use Data::Dumper;
84 #warn "# $address resolved via Net::DNS::Native: " . Dumper \@res;
85  
86 0 $args->{addr_info} = \@res;
87 0 $self->_connect($args);
88 }
89 0 )->watch($handle, 1, 0);
90 }
91  
92 0 ▶sub _to_address( $self, $addrinfo) {
Subroutine _to_address: not called CC=2 SCAR=17.9
93 return $addrinfo->{family} == AF_INET ?
94 inet_ntoa((unpack_sockaddr_in($addrinfo->{addr}))[1]) : # IPv4
95 0 ▶ Socket::inet_ntop(AF_INET6, (unpack_sockaddr_in6($addrinfo->{addr}))[1]); # IPv6
Branch: 1 branch
BranchTrueFalseTotal
$addrinfo->{'family'} == 2 ? : 0 0 0
96 }
97  
98 4 ▶sub _connect($self, $args) {
Subroutine _connect: called CC=12 SCAR=29.1
99 4 my $path = $args->{path};
100 4 my $handle = $self->{handle} = $args->{handle};
101 4 ▶ unless ($handle) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handle) 4 0 4
102 4 my $paranoid_dns = $self->paranoid_dns;
103 4 ▶ my $class = $path ? 'IO::Socket::UNIX' : 'IO::Socket::IP';
Branch: 1 branch
BranchTrueFalseTotal
$path ? : 0 4 4
104 4 my %options = (Blocking => 0);
105  
106 # UNIX domain socket
107 0 ▶ if ($path) { $options{Peer} = $path }
Branch: 1 branch
BranchTrueFalseTotal
if ($path) { } 0 4 4
108 # IP socket
109 else {
110 4 ▶ if(!$args->{addr_info}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"addr_info"}) 4 0 4
111 4 my ($resolved, $errmsg) = $paranoid_dns->resolve($args->{address});
112 4 ▶ if( $resolved ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resolved) { } 1 3 4
113 1 my $addr = $resolved->[0];
114 1 $options{PeerAddr} = $resolved;
115 } else {
116 3 return $self->emit(error => "Bad host: $errmsg");
117 }
118 }
119  
120 1 ▶ if (my $info = $args->{addr_info}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $info = $args->{'addr_info'}) { } 0 1 1
121 #use Data::Dumper; warn "Using pre-received addr_info " . Dumper $info;
122  
123 0 my $addr = $self->_to_address( $info->[0] );
124  
125 0 my ($resolved, $errmsg) = $paranoid_dns->_bad_host( $addr );
126 0 ▶ if( $errmsg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errmsg) 0 0 0
127 0 return $self->emit(error => "Bad host: $errmsg");
128 };
129 0 $options{PeerAddrInfo} = $info;
130  
131 } else {
132 1 ▶ $options{PeerAddr} = $args->{socks_address} || $args->{address};
Condition33%1/3
$args->{'socks_address'} || $args->{'address'}
l!l&&r!l&&!r
010
MC/DC0%0/2
$args->{'socks_address'} || $args->{'address'}
$args->{'socks_address'} $args->{'address'}
Truth table33%1/3
A || B
A= $args->{'socks_address'}B= $args->{'address'}
ABresult
1X1
011
000
133 1 $options{PeerPort} = _port($args);
134 }
135 1 ▶ @options{keys %{$args->{socket_options}}} = values %{$args->{socket_options}} if $args->{socket_options};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"socket_options"} 1 0 1
136 }
137 1 ▶ return $self->emit(error => "Can't connect: $@") unless $self->{handle} = $handle = $class->new(%options);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"handle"} = $handle = $class->new(%options) 0 1 1
138 }
139 1 $handle->blocking(0);
140 1 ▶ $path ? $self->_try_socks($args) : $self->_wait('_ready', $handle, $args);
Branch: 1 branch
BranchTrueFalseTotal
$path ? : 0 1 1
141 }
142  
143 2 1;
144  
145 =head1 SEE ALSO
146  
147 L<Net::DNS::Paranoid>
148  
149 L<Mojo::IOLoop::Client>
150  
151 =cut
152  
↑ Summary UserAgent/Paranoid.pm »