File Coverage

blib/lib/LWP/Protocol/socks.pm
Criterion Covered Total %
statement 3 54 5.5
branch 0 8 0.0
condition 0 3 0.0
subroutine 1 10 10.0
pod 1 1 100.0
total 5 76 6.5


line stmt bran cond sub pod time code
1             ##############################
2             package LWP::Protocol::http::socks;
3             require LWP::Protocol::http;
4             our @ISA = qw(LWP::Protocol::http);
5             our $VERSION = "1.7";
6             LWP::Protocol::implementor('http::socks' => 'LWP::Protocol::http::socks');
7              
8             sub new {
9 0     0     my $self = shift->SUPER::new(@_);
10 0           $self->{scheme} =~ s/::socks$//;
11 0           $self;
12             }
13              
14             sub _extra_sock_opts {
15 0     0     my $self = shift;
16 0           my($host, $port) = @_;
17 0           my @extra_sock_opts = $self->SUPER::_extra_sock_opts(@_);
18             #(@extra_sock_opts, SocksDebug =>1, @{$self->{proxy_sock_opts}});
19 0           (@extra_sock_opts, @{$self->{proxy_sock_opts}});
  0            
20             }
21              
22             ##############################
23             package LWP::Protocol::http::socks::Socket;
24             require LWP::Protocol::http;
25             require IO::Socket::Socks;
26             require Net::HTTP;
27             our @ISA = qw(LWP::Protocol::http::SocketMethods IO::Socket::Socks Net::HTTP);
28              
29             sub configure {
30 0     0     my $self = shift;
31 0           my $args = shift;
32              
33 0           my $connectAddr = $args->{ConnectAddr} = delete $args->{PeerAddr};
34 0           my $connectPort = $args->{ConnectPort} = delete $args->{PeerPort};
35              
36 0 0         $self->SUPER::configure($args) or return;
37 0           $self->http_configure($args);
38             }
39              
40             # hack out the connect so it doesn't reconnect
41             sub http_connect {
42 0     0     1;
43             }
44              
45             ##############################
46             package LWP::Protocol::https::socks;
47             require LWP::Protocol::https;
48             our @ISA = qw(LWP::Protocol::https);
49             LWP::Protocol::implementor('https::socks' => 'LWP::Protocol::https::socks');
50              
51             sub new {
52 0     0     my $self = shift->SUPER::new(@_);
53 0           $self->{scheme} =~ s/::socks$//;
54 0           $self;
55             }
56              
57             sub _extra_sock_opts {
58 0     0     my $self = shift;
59 0           my($host, $port) = @_;
60 0           my @extra_sock_opts = $self->SUPER::_extra_sock_opts(@_);
61 0           (@extra_sock_opts, @{$self->{proxy_sock_opts}});
  0            
62             #(@extra_sock_opts, @{$self->{proxy_sock_opts}});
63             }
64              
65             ##############################
66             package LWP::Protocol::https::socks::Socket;
67             require LWP::Protocol::https;
68             require IO::Socket::Socks;
69 1     1   20175 use IO::Socket::SSL;
  1         240510  
  1         12  
70             require Net::HTTPS;
71             our @ISA = qw(IO::Socket::SSL LWP::Protocol::https::Socket);
72              
73             sub new {
74 0     0     my $class = shift;
75 0           my %args = @_;
76 0           my $connectAddr = $args{ConnectAddr} = delete $args{PeerAddr};
77 0           my $connectPort = $args{ConnectPort} = delete $args{PeerPort};
78 0           my $socks = new IO::Socket::Socks(%args);
79 0           $args{PeerAddr} = $connectAddr;
80 0           $args{PeerPort} = $connectPort;
81 0           delete $args{ProxyAddr};
82 0           delete $args{ProxyPort};
83 0           delete $args{ConnectAddr};
84 0           delete $args{ConnectPort};
85            
86 0 0 0       unless ($socks && $class->start_SSL($socks, %args)) {
87 0           my $status = 'error while setting up ssl connection';
88 0 0         if ($@) {
89 0           $status .= " ($@)";
90             }
91 0           die($status);
92             }
93            
94 0           $socks->http_configure(\%args);
95 0           $socks;
96             }
97              
98             # hack out the connect so it doesn't reconnect
99             sub http_connect {
100 0     0     1;
101             }
102              
103             ##############################
104             package LWP::Protocol::socks;
105             require LWP::Protocol;
106             our @ISA = qw(LWP::Protocol);
107              
108             sub request {
109 0     0 1   my($self, $request, $proxy, $arg, $size, $timeout) = @_;
110 0           my $url = $request->uri;
111 0           my $scheme = $url->scheme;
112              
113 0           my $protocol = LWP::Protocol::create("$scheme\::socks", $self->{ua});
114 0           $protocol->{proxy_sock_opts} = [ProxyAddr => $proxy->host,
115             ProxyPort => $proxy->port,
116             ];
117              
118             # [RT 48172] Adding user/pass functionality
119 0 0         if ( $proxy->userinfo() ) {
120 0           push(@{$protocol->{proxy_sock_opts}},
  0            
121             AuthType => 'userpass',
122             Username => $proxy->user(),
123             Password => $proxy->pass(),
124             );
125             }
126              
127 0           $protocol->request($request, undef, $arg, $size, $timeout);
128             }
129              
130             1;
131              
132             __END__
133              
134             =head1 NAME
135              
136             LWP::Protocol::socks - adds support for the socks protocol and proxy facility
137              
138             =head1 SYNOPSIS
139              
140             use LWP::Protocol::socks;
141              
142             =head1 DESCRIPTION
143              
144             Use this package when you wish to use a socks proxy for your
145             connections.
146              
147             It provides some essential hooks into the LWP system to implement a
148             socks "scheme" similar to http for describing your socks connection,
149             and can be used to proxy either http or https connections.
150              
151             The use case is to use LWP::UserAgent's proxy method to register your
152             socks proxy like so:
153              
154             $ua->proxy([qw(http https)] => 'socks://socks.yahoo.com:1080');
155              
156             Then just use your $ua object as usual!
157              
158             =head1 EXAMPLES
159              
160             #!/usr/local/bin/perl
161             use strict;
162             use LWP::UserAgent;
163              
164             my $ua = new LWP::UserAgent(agent => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.5) Gecko/20060719 Firefox/1.5.0.5');
165             # for socks5, use socks like so:
166             $ua->proxy([qw(http https)] => 'socks://socks.yahoo.com:1080');
167             # for socks4, use socks4 like so:
168             $ua->proxy([qw(http https)] => 'socks4://socks.yahoo.com:1080');
169             my $response = $ua->get("http://www.freebsd.org");
170             print $response->code,' ', $response->message,"\n";
171             my $response = $ua->get("https://www.microsoft.com");
172             print $response->code,' ', $response->message,"\n";
173              
174             =head1 NOTES
175              
176             I don't have much time to contribute to this. If you'd like to
177             contribute, please fork https://github.com/scr/cpan and send me a pull
178             request.
179              
180             =head1 AUTHORS
181              
182             Sheridan C Rawlins E<lt>F<sheridan.rawlins@yahoo.com>E<gt>
183              
184             Oleg G E<lt>F<oleg@cpan.org>E<gt>