blib/lib/POE/Component/Client/SOCKS.pm

filter: Statementstmt 26.2% 49 / 187 Branchbran 12.9% 7 / 54 Conditioncond 0.0% 0 / 38 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 45.0% 9 / 20 Podpod 100% 5 / 5 totaltotal 21.4% 70 / 327 CC 48 SCAR 44.2
CC
48
Cov
20%
_command
272.0
_create
59.7
_conn_input
42.0
CRAP 82.7
↑ Summary
1 package POE::Component::Client::SOCKS;
2 2 $POE::Component::Client::SOCKS::VERSION = '1.02';
3 #ABSTRACT: SOCKS enable any POE Component
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use POE qw(Wheel::SocketFactory Filter::Stream Wheel::ReadWrite);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶sub spawn {
Subroutine spawn: called CC=1 SCAR=0.0
Pod documented
12 1 my $package = shift;
13 1 return $package->_create( 'spawn', @_ );
14 }
15  
16 0 ▶sub connect {
Subroutine connect: not called CC=4 SCAR=30.0
Pod documented
17 0 my $self;
18 0 eval {
19 0 ▶ if ( (ref $_[0]) && $_[0]->isa(__PACKAGE__) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and $_[0]->isa(__PACKAGE__)) 0 0 0
Condition0%0/3
ref $_[0] and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and $_[0]->isa(__PACKAGE__)
ref $_[0] $_[0]->isa(__PACKAGE__)
Truth table0%0/3
A and B
A= ref $_[0]B= $_[0]->isa(__PACKAGE__)
ABresult
0X0
100
111
20 0 $self = shift;
21 }
22 };
23 0 ▶ if ( $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self) 0 0 0
24 0 $poe_kernel->post( $self->{session_id}, 'connect', @_ );
25 0 return 1;
26 }
27 0 my $package = shift;
28 0 return $package->_create( 'connect', @_ );
29 }
30  
31 0 ▶sub bind {
Subroutine bind: not called CC=4 SCAR=30.0
Pod documented
32 0 my $self;
33 0 eval {
34 0 ▶ if ( (ref $_[0]) && $_[0]->isa(__PACKAGE__) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and $_[0]->isa(__PACKAGE__)) 0 0 0
Condition0%0/3
ref $_[0] and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and $_[0]->isa(__PACKAGE__)
ref $_[0] $_[0]->isa(__PACKAGE__)
Truth table0%0/3
A and B
A= ref $_[0]B= $_[0]->isa(__PACKAGE__)
ABresult
0X0
100
111
35 0 $self = shift;
36 }
37 };
38 0 ▶ if ( $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self) 0 0 0
39 0 $poe_kernel->post( $self->{session_id}, 'bind', @_ );
40 0 return 1;
41 }
42 0 my $package = shift;
43 0 return $package->_create( 'bind', @_ );
44 }
45  
46 1 ▶sub _create {
Subroutine _create: called CC=11 SCAR=40.9
47 1 my $package = shift;
48 1 my $command = shift;
49 1 my %opts = @_;
50 1 $opts{lc $_} = delete $opts{$_} for keys %opts;
51 1 my $options = delete $opts{options};
52 1 my $self = bless { }, $package;
53 1 ▶ if ( $command =~ /^(bind|connect)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command =~ /^(bind|connect)$/) 0 1 1
54 0 ▶ unless ( $opts{successevent} and $opts{failureevent} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"successevent"} and $opts{"failureevent"}) 0 0 0
Condition0%0/3
$opts{"successevent"} and $opts{"failureevent"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{"successevent"} and $opts{"failureevent"}
$opts{"successevent"} $opts{"failureevent"}
Truth table0%0/3
A and B
A= $opts{"successevent"}B= $opts{"failureevent"}
ABresult
0X0
100
111
55 0 warn "You must specify 'SuccessEvent' and 'FailureEvent' for '$command'\n";
56 0 return;
57 }
58 0 ▶ unless ( $opts{remoteaddress} and $opts{remoteport} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"remoteaddress"} and $opts{"remoteport"}) 0 0 0
Condition0%0/3
$opts{"remoteaddress"} and $opts{"remoteport"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{"remoteaddress"} and $opts{"remoteport"}
$opts{"remoteaddress"} $opts{"remoteport"}
Truth table0%0/3
A and B
A= $opts{"remoteaddress"}B= $opts{"remoteport"}
ABresult
0X0
100
111
59 0 warn "You must specify 'RemoteAddress' and 'RemotePort'\n";
60 0 return;
61 }
62 0 ▶ unless ( $opts{socksproxy} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"socksproxy"}) 0 0 0
63 0 warn "You must specify 'SocksProxy'\n";
64 0 return;
65 }
66 0 ▶ if ( $command eq 'bind' and $opts{remoteaddress} !~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "bind" and not $opts{"remoteaddress"} =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) 0 0 0
Condition0%0/3
$command eq "bind" and not $opts{"remoteaddress"} =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
!ll&&!rl&&r
000
MC/DC0%0/2
$command eq "bind" and not $opts{"remoteaddress"} =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
$command eq "bind" not $opts{"remoteaddress"} =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
Truth table0%0/3
A and not(B)
A= $command eq "bind"B= not $opts{"remoteaddress"} =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
ABresult
0X0
100
111
67 0 warn "You specified 'bind' but 'RemoteAddress' is not an IP address\n";
68 0 return;
69 }
70 }
71 1 ▶ $self->{session_id} = POE::Session->create(
Branch: 1 branch
BranchTrueFalseTotal
ref $options eq 'HASH' ? : 0 1 1
72 object_states => [
73 $self => { shutdown => '_shutdown',
74 connect => '_command',
75 bind => '_command',
76 },
77 $self => [qw(_start
78 _disconnect
79 _create_socket
80 _sock_failed
81 _sock_up
82 _conn_input
83 _conn_error) ],
84 ],
85 heap => $self,
86 args => [ $command, %opts ],
87 ( ref($options) eq 'HASH' ? ( options => $options ) : () ),
88 )->ID();
89 1 return $self;
90 }
91  
92 0 ▶sub session_id {
Subroutine session_id: not called CC=1 SCAR=6.9
Pod documented
93 0 return $_[0]->{session_id};
94 }
95  
96 0 ▶sub shutdown {
Subroutine shutdown: not called CC=1 SCAR=6.9
Pod documented
97 0 my $self = shift;
98 0 $poe_kernel->post( $self->{session_id}, 'shutdown' );
99 0 return 1;
100 }
101  
102 1 ▶sub _start {
Subroutine _start: called CC=6 SCAR=22.5
103 1 my ($kernel,$self,$sender,$command,@args) = @_[KERNEL,OBJECT,SENDER,ARG0..$#_];
104 1 $self->{session_id} = $_[SESSION]->ID();
105 1 $self->{filter} = POE::Filter::Stream->new();
106 1 ▶ if ( $command eq 'spawn' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "spawn") 1 0 1
107 1 my $opts = { @args };
108 1 $self->{$_} = $opts->{$_} for keys %{ $opts };
109 1 ▶ $kernel->alias_set($self->{alias}) if $self->{alias};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"alias"} 0 1 1
110 1 ▶ $kernel->refcount_increment($self->{session_id}, __PACKAGE__) unless $self->{alias};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"alias"} 1 0 1
111 1 return;
112 }
113 0 ▶ if ( $kernel == $sender ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kernel == $sender) 0 0 0
114 0 croak "'connect' and 'bind' should be called from another POE Session\n";
115 }
116 0 $self->{sender_id} = $sender->ID();
117 0 $kernel->refcount_increment( $self->{sender_id}, __PACKAGE__ );
118 0 $kernel->yield( $command, @args );
119 0 return;
120 }
121  
122 1 ▶sub _shutdown {
Subroutine _shutdown: called CC=6 SCAR=18.1
123 1 my ($kernel,$self) = @_[KERNEL,OBJECT];
124 1 ▶ unless ( $self->{sender_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"sender_id"}) 1 0 1
125 1 $kernel->alias_remove($_) for $kernel->alias_list();
126 1 ▶ $kernel->refcount_decrement($self->{session_id}, __PACKAGE__) unless $self->{alias};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"alias"} 1 0 1
127 }
128 1 $kernel->refcount_decrement( $_->{sender_id}, __PACKAGE__ ) for values %{ $self->{socks} };
129 1 $kernel->refcount_decrement( $_->{sender_id}, __PACKAGE__ ) for values %{ $self->{conns} };
130 1 delete $self->{socks};
131 1 delete $self->{conns};
132 1 return;
133 }
134  
135 0 ▶sub _command {
Subroutine _command: not called CC=16 SCAR=56.1
136 0 my ($kernel,$self,$state,$session,$sender) = @_[KERNEL,OBJECT,STATE,SESSION,SENDER];
137 0 my $args;
138 0 ▶ if ( ref $_[ARG0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[10] eq 'HASH') { } 0 0 0
139 0 $args = $_[ARG0];
140 }
141 else {
142 0 $args = { @_[ARG0..$#_] };
143 }
144 0 $args->{cmd} = $state;
145 0 ▶ if ( $session == $sender ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session == $sender) { } 0 0 0
146 0 $args->{sender_id} = $self->{sender_id};
147 }
148 else {
149 0 $args->{lc $_} = delete $args->{$_} for keys %{ $args };
150 0 $args->{sender_id} = $sender->ID();
151 0 ▶ unless ( $args->{successevent} and $args->{failureevent} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"successevent"} and $args->{"failureevent"}) 0 0 0
Condition0%0/3
$args->{"successevent"} and $args->{"failureevent"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"successevent"} and $args->{"failureevent"}
$args->{"successevent"} $args->{"failureevent"}
Truth table0%0/3
A and B
A= $args->{"successevent"}B= $args->{"failureevent"}
ABresult
0X0
100
111
152 0 warn "You must specify 'SuccessEvent' and 'FailureEvent'\n";
153 0 return;
154 }
155 0 ▶ unless ( $args->{remoteaddress} and $args->{remoteport} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"remoteaddress"} and $args->{"remoteport"}) 0 0 0
Condition0%0/3
$args->{"remoteaddress"} and $args->{"remoteport"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"remoteaddress"} and $args->{"remoteport"}
$args->{"remoteaddress"} $args->{"remoteport"}
Truth table0%0/3
A and B
A= $args->{"remoteaddress"}B= $args->{"remoteport"}
ABresult
0X0
100
111
156 0 warn "You must specify 'RemoteAddress' and 'RemotePort'\n";
157 0 return;
158 }
159 0 ▶ unless ( $args->{socksproxy} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"socksproxy"}) 0 0 0
160 0 warn "You must specify 'SocksProxy'\n";
161 0 return;
162 }
163 0 ▶ if ( $state eq 'bind' and $args->{remoteaddress} !~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "bind" and not $args->{"remoteaddress"} =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) 0 0 0
Condition0%0/3
$state eq "bind" and not $args->{"remoteaddress"} =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
!ll&&!rl&&r
000
MC/DC0%0/2
$state eq "bind" and not $args->{"remoteaddress"} =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
$state eq "bind" not $args->{"remoteaddress"} =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
Truth table0%0/3
A and not(B)
A= $state eq "bind"B= not $args->{"remoteaddress"} =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
ABresult
0X0
100
111
164 0 warn "You specified 'bind' but 'RemoteAddress' is not an IP address\n";
165 0 return;
166 }
167 0 $kernel->refcount_increment( $args->{sender_id}, __PACKAGE__ );
168 }
169 0 ▶ if ( $state eq 'connect' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq 'connect') { } 0 0 0
170 0 ▶ if ( $args->{remoteaddress} =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'remoteaddress'} =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { } 0 0 0
171 # SOCKS 4
172 $args->{packet} = pack ('CCn', 4, 1, $args->{remoteport}) .
173 0 ▶ inet_aton($args->{remoteaddress}) . ( $args->{socks_id} || '' ) . (pack 'x');
Condition0%0/2
$args->{'socks_id'} || ''
l!l
00
MC/DC0%0/1
$args->{'socks_id'} || ''
$args->{'socks_id'}
Truth table0%0/2
A
A= $args->{'socks_id'}
Aresult
11
00
174 }
175 else {
176 # SOCKS 4a
177 $args->{packet} = pack ('CCn', 4, 1, $args->{remoteport}) .
178 inet_aton('0.0.0.1') . ( $args->{socks_id} || '' ) . (pack 'x') .
179 0 ▶ $args->{remoteaddress} . (pack 'x');
Condition0%0/2
$args->{'socks_id'} || ''
l!l
00
MC/DC0%0/1
$args->{'socks_id'} || ''
$args->{'socks_id'}
Truth table0%0/2
A
A= $args->{'socks_id'}
Aresult
11
00
180 }
181 }
182 else {
183 $args->{packet} = pack ('CCn', 4, 2, $args->{remoteport}) .
184 0 ▶ inet_aton($args->{remoteaddress}) . ( $args->{socks_id} || '' ) . (pack 'x');
Condition0%0/2
$args->{'socks_id'} || ''
l!l
00
MC/DC0%0/1
$args->{'socks_id'} || ''
$args->{'socks_id'}
Truth table0%0/2
A
A= $args->{'socks_id'}
Aresult
11
00
185 }
186 0 $kernel->yield( '_create_socket', $args );
187 0 return;
188 }
189  
190 0 ▶sub _create_socket {
Subroutine _create_socket: not called CC=2 SCAR=17.9
191 0 my ($kernel,$self,$args) = @_[KERNEL,OBJECT,ARG0];
192 my $factory = POE::Wheel::SocketFactory->new(
193 SocketDomain => AF_INET,
194 SocketType => SOCK_STREAM,
195 SocketProtocol => 'tcp',
196 RemoteAddress => $args->{socksproxy},
197 0 ▶ RemotePort => $args->{socksport} || 1080,
Condition0%0/2
$args->{'socksport'} || 1080
l!l
00
MC/DC0%0/1
$args->{'socksport'} || 1080
$args->{'socksport'}
Truth table0%0/2
A
A= $args->{'socksport'}
Aresult
11
00
198 SuccessEvent => '_sock_up',
199 FailureEvent => '_sock_failed',
200 );
201 0 $args->{factory} = $factory;
202 0 $self->{socks}->{ $factory->ID } = $args;
203 0 return;
204 }
205  
206 0 ▶sub _sock_failed {
Subroutine _sock_failed: not called CC=1 SCAR=6.9
207 0 my ($kernel,$self,$operation,$errnum,$errstr,$factory_id) = @_[KERNEL,OBJECT,ARG0..ARG3];
208 0 my $args = delete $self->{socks}->{ $factory_id };
209 0 delete $args->{factory};
210 0 delete $args->{packet};
211 0 my $sender_id = delete $args->{sender_id};
212 0 $args->{sockerr} = [ $operation, $errnum, $errstr ];
213 0 $kernel->refcount_decrement( $sender_id, __PACKAGE__ );
214 0 $kernel->post( $sender_id, $args->{failureevent}, $args );
215 0 return;
216 }
217  
218 0 ▶sub _sock_up {
Subroutine _sock_up: not called CC=1 SCAR=6.9
219 0 my ($kernel,$self,$socket,$peeraddr,$peerport,$fact_id) = @_[KERNEL,OBJECT,ARG0..ARG3];
220 0 $peeraddr = inet_aton( $peeraddr );
221 0 my $args = delete $self->{socks}->{ $fact_id };
222 0 delete $args->{factory};
223 0 $args->{socket} = $socket;
224 my $wheel = POE::Wheel::ReadWrite->new(
225 Handle => $socket,
226 Filter => $self->{filter},
227 0 InputEvent => '_conn_input',
228 ErrorEvent => '_conn_error',
229 );
230 0 $args->{wheel} = $wheel;
231 0 $self->{conns}->{ $wheel->ID } = $args;
232 0 $wheel->put( $args->{packet} );
233 0 return;
234 }
235  
236 0 ▶sub _conn_input {
Subroutine _conn_input: not called CC=6 SCAR=37.4
237 0 my ($kernel,$self,$input,$wheel_id) = @_[KERNEL,OBJECT,ARG0,ARG1];
238 0 ▶ if ( length $input != 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $input != 8) 0 0 0
239 0 $kernel->yield( '_disconnect', $wheel_id, 'Mangled response from SOCKS proxy' );
240 0 return;
241 }
242 0 my @resp = unpack "CCnN", $input;
243 0 ▶ unless ( scalar @resp == 4 and $resp[0] eq '0' and $resp[1] =~ /^(90|91|92|93)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @resp == 4 and $resp[0] eq 0 and $resp[1] =~ /^(90|91|92|93)$/) 0 0 0
Condition0%0/6
scalar @resp == 4 and $resp[0] eq 0 and $resp[1] =~ /^(90|91|92|93)$/
!ll&&!rl&&r
000
scalar @resp == 4 and $resp[0] eq 0
!ll&&!rl&&r
000
MC/DC0%0/3
scalar @resp == 4 and $resp[0] eq 0 and $resp[1] =~ /^(90|91|92|93)$/
scalar @resp == 4 $resp[0] eq 0 $resp[1] =~ /^(90|91|92|93)$/
Truth table0%0/4
A and B and C
A= scalar @resp == 4B= $resp[0] eq 0C= $resp[1] =~ /^(90|91|92|93)$/
ABCresult
0XX0
10X0
1100
1111
244 0 $kernel->yield( '_disconnect', $wheel_id, 'Mangled response from SOCKS proxy' );
245 0 return;
246 }
247 0 my ($vn,$cd,$dstport,$dstip) = @resp;
248 0 my $args = delete $self->{conns}->{ $wheel_id };
249 0 delete $args->{wheel};
250 0 delete $args->{packet};
251 0 my $sender_id = delete $args->{sender_id};
252 0 ▶ unless ( $cd eq '90' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cd eq 90) 0 0 0
253 0 delete $args->{socket};
254 0 $args->{socks_error} = $cd;
255 0 $kernel->post( $sender_id, $args->{failureevent}, $args );
256 0 $kernel->refcount_decrement( $sender_id, __PACKAGE__ );
257 0 return;
258 }
259 0 $args->{socks_response} = [ $cd, inet_ntoa( pack "N", $dstip ), $dstport ];
260 0 $kernel->post( $sender_id, $args->{successevent}, $args );
261 0 $kernel->refcount_decrement( $sender_id, __PACKAGE__ );
262 0 return;
263 }
264  
265 0 ▶sub _conn_error {
Subroutine _conn_error: not called CC=1 SCAR=6.9
266 0 my ($kernel,$self,$operation,$errnum,$errstr,$wheel_id) = @_[KERNEL,OBJECT,ARG0..ARG3];
267 0 my $args = delete $self->{conns}->{ $wheel_id };
268 0 delete $args->{wheel};
269 0 delete $args->{socket};
270 0 delete $args->{packet};
271 0 my $sender_id = delete $args->{sender_id};
272 0 $args->{sockerr} = [ $operation, $errnum, $errstr ];
273 0 $kernel->post( $sender_id, $args->{failureevent}, $args );
274 0 $kernel->refcount_decrement( $sender_id, __PACKAGE__ );
275 0 return;
276 }
277  
278 0 ▶sub _disconnect {
Subroutine _disconnect: not called CC=1 SCAR=6.9
279 0 my ($kernel,$self,$wheel_id,$reason) = @_[KERNEL,OBJECT,ARG0,ARG1];
280 0 my $args = delete $self->{conns}->{ $wheel_id };
281 0 delete $args->{wheel};
282 0 delete $args->{socket};
283 0 delete $args->{packet};
284 0 my $sender_id = delete $args->{sender_id};
285 0 $args->{socks_unknown} = $reason;
286 0 $kernel->refcount_decrement( $sender_id, __PACKAGE__ );
287 0 $kernel->post( $sender_id, $args->{failureevent}, $args );
288 0 return;
289 }
290  
291 2 'SOCKS it to me!';
292  
293 __END__
↑ Summary