Search code examples
perlwww-mechanize

Using a proxy with WWW::Mechanize


Im trying to perform tests on my servers upload function using several proxies. By the time I get my ip through api.ipify.org. The console outputs my true ip, not the proxies ip.

use strict; use warnings;
    use WWW::Mechanize;
    my $file = "proxies.txt";
    open (FH, "< $file") or die "Can't open $file for read: $!";
    my @lines = <FH>;
    close FH or die "Cannot close $file: $!";
    print "Loaded proxy list";
    my $m = WWW::Mechanize->new(
        autocheck => 1,
        agent_alias => 'Mozilla',
        cookie_jar => {},
        ssl_opts => {verify_hostname => 0},
        quiet => 0,
    );
    my $httpl = "http://";
    $m->no_proxy('localhost');
    my $ua = LWP::UserAgent->new;
    for(my $i=0; $i < 47; $i++)
    {
    $m->proxy('http', $httpl . '' . $lines[$i]);
    print "Connecting to proxy " . $lines[$i];
    $m->get("https://api.ipify.org?format=json");
    print $m->content;
    for(my $j = 0; $j <= 10; $j++){
    $m->get("http://example.com");
    system("node genran.js");
    $m->post('http://example.com/upload.php',
        Content_Type => "form-data",
        Content => [
            'password' => '',
            'public' => 'yes',
            'uploadContent' => [ 'spam.txt', 'Love pecons', 'Content_$
            file => [ 'x86.png', 'image_name', 'Content-Type' => 'ima$
        ]
    );

    print $m->content;
    }}

Solution

  • $m->proxy('http', $httpl . '' . $lines[$i]);
    print "Connecting to proxy " . $lines[$i];
    $m->get("https://api.ipify.org?format=json");
    

    You set only a proxy for http but make a https request. You need to set the proxy for https too like this:

    $m->proxy('https', ... put your https proxy here ...);
    

    Or to use the same proxy for multiple protocols:

    $m->proxy(['http','https'], ... );
    

    Also, make sure that you use at least version 6.06 of LWP::UserAgent and LWP::Protocol::https for proper support of proxies with https, i.e.

    use LWP::UserAgent;
    print LWP::UserAgent->VERSION,"\n";
    
    use LWP::Protocol::https;
    print LWP::Protocol::https->VERSION,"\n";