Guest User

Untitled

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