Advertisement
Guest User

Untitled

a guest
Dec 9th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.94 KB | None | 0 0
  1. package LWP::Protocol::https::Socketeer;
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use vars qw(@ISA $VERSION %SOCKET_OPTS);
  7.  
  8. require LWP::Protocol::https;
  9.  
  10. @ISA = qw(LWP::Protocol::https);
  11.  
  12. $VERSION = 1.00;
  13.  
  14. our $MAX_CONNECT_ATTEMPTS = 1;
  15. our $DEBUG = 0;
  16.  
  17. sub _new_socket
  18. {
  19.     my($self, $host, $port, $timeout) = @_;
  20.  
  21.     my $conn_cache = $self->{ua}{conn_cache};
  22.     if ($conn_cache) {
  23.         if (my $sock = $conn_cache->withdraw("https", "$host:$port")) {
  24.             return $sock if $sock && !$sock->can_read(0);
  25.             # if the socket is readable, then either the peer has closed the
  26.             # connection or there are some garbage bytes on it.  In either
  27.             # case we abandon it.
  28.             $sock->close;
  29.         }
  30.     }
  31.  
  32.     local($^W) = 0;  # IO::Socket::INET can be noisy
  33.  
  34.     my $socket = $self->getSocket($host, $port, $timeout);
  35.  
  36.     unless ($socket) {
  37.         # IO::Socket::INET leaves additional error messages in $@
  38.         die "Can't connect to $host:$port ($@)";
  39.     }
  40.  
  41.     # perl 5.005's IO::Socket does not have the blocking method.
  42.     eval { $socket->blocking(0); };
  43.  
  44.     return $socket;
  45. }
  46.  
  47. sub getSocket
  48. {
  49.    my($self, $host, $port, $timeout) = @_;
  50.  
  51.    die "No proxy was specified!" unless exists $SOCKET_OPTS{proxy};
  52.    my $proxy = $SOCKET_OPTS{proxy};
  53.  
  54.    if ($DEBUG) {
  55.       print "Host: ",     $proxy->{host},     "\n",
  56.             "Port: ",     $proxy->{port},     "\n",
  57.             "Username: ", $proxy->{login},    "\n",
  58.             "Pass: ",     $proxy->{pass},     "\n";
  59.    }
  60.  
  61.    my $socket;
  62.    my $attempts = 0;
  63.    do {
  64.        print "Creating socket (attempt $attempts) to $host:$port via $proxy->{host}:$proxy->{port}\n" if $DEBUG;
  65.        $socket = $self->createSocket($proxy, $host, $port, $timeout);
  66.        $attempts++;
  67.    } while (!$socket && ($attempts <= $MAX_CONNECT_ATTEMPTS));
  68.  
  69.    return $socket;
  70. }
  71.  
  72. sub createSocket {
  73.     my ($self, $proxy, $host, $port, $timeout) = @_;
  74.    
  75.     my $socket = $self->socket_class->new(
  76.         ProxyAddr   => $proxy->{host},
  77.         ProxyPort   => $proxy->{port},
  78.  
  79.         AuthType    => 'userpass',
  80.         Username    => $proxy->{login},
  81.         Password    => $proxy->{pass},
  82.  
  83.         ConnectAddr => $host,
  84.         ConnectPort => $port,
  85.  
  86.         SocksDebug  => $DEBUG,
  87.         Timeout     => $timeout,
  88.     );
  89.    
  90.     return $socket;
  91. }
  92.  
  93. #-----------------------------------------------------------
  94. package LWP::Protocol::https::Socketeer::Socket;
  95.  
  96. use strict;
  97. use warnings;
  98. use vars qw( @ISA );
  99.  
  100. use IO::Socket::Socks;
  101. use IO::Socket::SSL;
  102.  
  103. @ISA = qw(IO::Socket::SSL LWP::Protocol::http::SocketMethods Net::HTTP::Methods);
  104.  
  105. sub new {
  106.     my $class = shift;
  107.     my %args = @_;
  108.     my $proxy_socket = IO::Socket::Socks->new(%args);
  109.     return undef unless $proxy_socket;
  110.  
  111.     my $ssl_socket = IO::Socket::SSL->start_SSL($proxy_socket);
  112.     return undef unless $ssl_socket;
  113.  
  114.     bless $ssl_socket, $class;
  115. }
  116.  
  117. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement