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

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