blib/lib/IO/Stream/Proxy/SOCKSv4.pm

filter: Statementstmt 46.3% 38 / 82 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 70.5% 12 / 17 Podpod 33.3% 1 / 3 totaltotal 40.1% 51 / 127 SCAR 39.0
CC
12
Cov
36%
EVENT
90.0
new
12.0
PREPARE
6.0
CRAP 49.4
↑ Summary
1 package IO::Stream::Proxy::SOCKSv4;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use version; our $VERSION = qv('1.0.2'); # update POD & Changes & README
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # update DEPENDENCIES in POD & Makefile.PL & README
10 3 ▶use IO::Stream::const;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use IO::Stream::EV;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use constant VN => 0x04; # SOCKS protocol version number (4)
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use constant CD => 0x01; # SOCKS protocol command code (CONNECT)
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use constant REPLY_LEN=> 8; # SOCKS protocol reply length (bytes)
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use constant REPLY_VN => 0; # SOCKS protocol reply code version
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use constant REPLY_CD => 90;# SOCKS protocol reply code 'request granted'
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21 sub new {
22 0 ▶ my ($class, $opt) = @_;
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
23 croak '{host}+{port} required'
24 if !defined $opt->{host}
25 || !defined $opt->{port}
26 0 ▶ ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt->{"host"} and defined $opt->{"port"} 0 0 0
Condition0%0/3
defined $opt->{"host"} and defined $opt->{"port"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opt->{"host"} and defined $opt->{"port"}
defined $opt->{"host"} defined $opt->{"port"}
Truth table0%0/3
A and B
A= defined $opt->{"host"}B= defined $opt->{"port"}
ABresult
0X0
100
111
27 my $self = bless {
28 host => undef,
29 port => undef,
30 userid => q{},
31 0 %{$opt},
32 out_buf => q{}, # modified on: OUT
33 out_pos => undef, # modified on: OUT
34 out_bytes => 0, # modified on: OUT
35 in_buf => q{}, # modified on: IN
36 in_bytes => 0, # modified on: IN
37 ip => undef, # modified on: RESOLVED
38 is_eof => undef, # modified on: EOF
39 _want_write => undef,
40 }, $class;
41 0 return $self;
42 }
43  
44 sub PREPARE {
45 0 ▶ my ($self, $fh, $host, $port) = @_;
Subroutine PREPARE: not called CC=2 SCAR=17.9
Pod undocumented
46 0 ▶ croak '{fh} already connected'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 0 0 0
47 if !defined $host;
48 0 $self->{_slave}->PREPARE($fh, $self->{host}, $self->{port});
49 IO::Stream::EV::resolve($host, $self, sub {
50 0 ▶ my ($self, $ip) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 0 $self->{_master}{ip} = $ip;
52 $self->{out_buf} = pack 'C C n CCCC Z*',
53 0 VN, CD, $port, split(/[.]/xms, $ip), $self->{userid};
54 0 $self->{_slave}->WRITE();
55 0 });
56 0 return;
57 }
58  
59 sub WRITE {
60 0 ▶ my ($self) = @_;
Subroutine WRITE: not called CC=1 SCAR=6.9
61 0 $self->{_want_write} = 1;
62 0 return;
63 }
64  
65 sub EVENT {
66 0 ▶ my ($self, $e, $err) = @_;
Subroutine EVENT: not called CC=9 SCAR=45.0
Pod undocumented
67 0 my $m = $self->{_master};
68 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
69 0 $m->EVENT(0, $err);
70 }
71 0 ▶ if ($e & IN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e & 4) 0 0 0
72 0 ▶ if (length $self->{in_buf} < REPLY_LEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{'in_buf'} < 8) { } 0 0 0
73 0 $m->EVENT(0, 'socks v4 proxy: protocol error');
74 } else {
75 0 my ($vn, $cd) = unpack 'CC', $self->{in_buf};
76 0 substr $self->{in_buf}, 0, REPLY_LEN, q{};
77 0 ▶ if ($vn != REPLY_VN) {
Branch: 2 branches
BranchTrueFalseTotal
if ($vn != 0) { } 0 0 0
elsif ($cd != 90) { } 0 0 0
78 0 $m->EVENT(0, 'socks v4 proxy: unknown version of reply code');
79 }
80 elsif ($cd != REPLY_CD) {
81 0 $m->EVENT(0, 'socks v4 proxy: error '.$cd);
82 }
83 else {
84 0 $e = CONNECTED;
85 0 ▶ if (my $l = length $self->{in_buf}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $l = length $self->{"in_buf"}) 0 0 0
86 0 $e |= IN;
87 0 $m->{in_buf} .= $self->{in_buf};
88 0 $m->{in_bytes} += $l;
89 }
90 0 $m->EVENT($e);
91 0 $self->{_slave}->{_master} = $m;
92 0 weaken($self->{_slave}->{_master});
93 0 $m->{_slave} = $self->{_slave};
94 0 ▶ if ($self->{_want_write}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_want_write"}) 0 0 0
95 0 $self->{_slave}->WRITE();
96 }
97 }
98 }
99 }
100 0 ▶ if ($e & EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e & 16) 0 0 0
101 0 $m->{is_eof} = $self->{is_eof};
102 0 $m->EVENT(0, 'socks v4 proxy: unexpected EOF');
103 }
104 0 return;
105 }
106  
107  
108 3 1; # Magic true value required at end of module
109 __END__
110  
111 =head1 NAME
112  
113 IO::Stream::Proxy::SOCKSv4 - SOCKSv4 proxy plugin for IO::Stream
114  
115  
116 =head1 VERSION
117  
118 This document describes IO::Stream::Proxy::SOCKSv4 version 1.0.2
119  
120  
121 =head1 SYNOPSIS
122  
123 use IO::Stream;
124 use IO::Stream::Proxy::SOCKSv4;
125  
126 IO::Stream->new({
127 ...
128 plugin => [
129 ...
130 proxy => IO::Stream::Proxy::SOCKSv4->new({
131 host => 'my.proxy.com',
132 port => 3128,
133 }),
134 ...
135 ],
136 });
137  
138  
139 =head1 DESCRIPTION
140  
141 This module is plugin for L<IO::Stream> which allow you to route stream
142 through SOCKSv4 proxy.
143  
144 You may use several IO::Stream::Proxy::SOCKSv4 plugins for single IO::Stream
145 object, effectively creating proxy chain (first proxy plugin will define
146 last proxy in a chain).
147  
148 =head2 SECURITY
149  
150 Version 4 of SOCKS protocol doesn't support domain name resolving by proxy,
151 so resolving happens always on client side. This may result in leaking
152 client's DNS resolver IP address (usually it's client's address or client's
153 ISP address) and detecting the fact of using proxy.
154  
155 =head2 EVENTS
156  
157 When using this plugin event RESOLVED will never be delivered to user because
158 there may be two hosts to resolve (target host and proxy host) and it
159 isn't clear how to handle this case in right way.
160  
161 Event CONNECTED will be generated after SOCKS proxy successfully connects to
162 target {host} (and not when socket will connect to SOCKS proxy itself).
163  
164  
165 =head1 INTERFACE
166  
167 =over
168  
169 =item new({ host=>$host, port=>$port })
170  
171 =item new({ host=>$host, port=>$port, userid=>$ENV{USER} })
172  
173 Connect to proxy $host:$port, optionally using given userid (empty by default).
174  
175 =back
176  
177  
178 =head1 DIAGNOSTICS
179  
180 =over
181  
182 =item C<< {host}+{port} required >>
183  
184 You must provide both {host} and {port} to IO::Stream::Proxy::SOCKSv4->new().
185  
186 =item C<< {fh} already connected >>
187  
188 You have provided {fh} to IO::Stream->new(), but this is not supported by
189 this plugin. Either don't use this plugin or provide {host}+{port} to
190 IO::Stream->new() instead.
191  
192 =back
193  
194  
195  
196 =head1 CONFIGURATION AND ENVIRONMENT
197  
198 IO::Stream::Proxy::SOCKSv4 requires no configuration files or environment variables.
199  
200  
201 =head1 DEPENDENCIES
202  
203 L<IO::Stream>.
204  
205  
206 =head1 INCOMPATIBILITIES
207  
208 None reported.
209  
210  
211 =head1 BUGS AND LIMITATIONS
212  
213 SOCKS "BIND" request doesn't supported.
214  
215 No bugs have been reported.
216  
217 Please report any bugs or feature requests to
218 C<bug-io-stream-proxy-socksv4@rt.cpan.org>, or through the web interface at
219 L<http://rt.cpan.org>.
220  
221  
222 =head1 AUTHOR
223  
224 Alex Efros C<< <powerman-asdf@ya.ru> >>
225  
226  
227 =head1 LICENSE AND COPYRIGHT
228  
229 Copyright (c) 2009, Alex Efros C<< <powerman-asdf@ya.ru> >>. All rights reserved.
230  
231 This module is free software; you can redistribute it and/or
232 modify it under the same terms as Perl itself. See L<perlartistic>.
233  
234  
235 =head1 DISCLAIMER OF WARRANTY
236  
237 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
238 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
239 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
240 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
241 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
242 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
243 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
244 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
245 NECESSARY SERVICING, REPAIR, OR CORRECTION.
246  
247 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
248 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
249 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
250 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
251 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
252 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
253 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
254 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
255 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
256 SUCH DAMAGES.
↑ Summary