HTTP(S) request from Perl over proxy with IO::Sock

2019-07-31 17:48发布

问题:

I have the Perl script where i do PUT request to HTTPS server and send a file. The request is done only with IO::Socket::SSL .

$socket = new IO::Socket::INET(PeerAddr => $host,PeerPort => $port,Proto    => 'tcp');
my $h ="PUT ".$path." HTTP/1.0\r\n";
    $h.="Accept: */*\r\n";
    ........
    $h.="\r\n";

print $socket $h;

But now i need to do this request over the proxy server

How is most easy way to do this? I can configure the prozy server to set different types of connections (if there are different types). What s best type?

How to do the request? As i understand, i have to connect to the proxy with IO::Socket and put something like CONNECT.

How does this look like?

Thanks

Update 1. I found the solution like this http://search.cpan.org/~oleg/IO-Socket-Socks-0.62/lib/IO/Socket/Socks.pm The module IO::Socket::Socks does what i need. But not for HTTPS. I tried to use IO::Socket::SecureSocks. But it fails all time

#!/usr/bin/perl

use IO::Socket::SecureSocks;


my $socket = IO::Socket::SecureSocks->new(
              ProxyAddr   => '127.0.0.1',
              ProxyPort   => 8876,  
              ConnectAddr => 'yahoo.com',
              ConnectPort => 443,
              Timeout     => 10
              ) or die "Error $!\n";

print $socket "GET / HTTP 1.1\r\n\r\n";
print join ('',<$socket>);

It shows error. What do i do wrong?

This proxy works fine with curl in PHP and https links. The proxy is SOCKS 5 proxy

回答1:

I found the solution myself. I used the module IO::Socket::Socks::Wrapper to redirect all requests to SOCKS5 proxy. I only added on the beginning of my script

use IO::Socket::Socks::Wrapper (
{
    ProxyAddr => 'proxy.addr',
    ProxyPort => 8876,
}
);


标签: perl proxy