simple/findproxy/findproxy

32 lines
709 B
Perl
Executable File

#!/usr/bin/perl
use warnings;
use strict;
use LWP::UserAgent;
use Time::HiRes qw(time);
my $verbose = 0;
my $ua = LWP::UserAgent->new();
my @proxies;
for my $proxy (@ARGV) {
print STDERR "trying proxy $proxy\n" if $verbose;
$ua->proxy('http', $proxy);
my $t0 = time();
my $response = $ua->get('http://www.hjp.at');
if ($response->is_success) {
my $dt = time() - $t0;
print STDERR "\tsucceded in $dt seconds\n" if $verbose;
push @proxies, [ $dt, $proxy ];
} else {
print STDERR "\tfailed\n" if $verbose;
}
}
if (@proxies) {
@proxies = sort { $a->[0] <=> $b->[0] } @proxies;
print $proxies[0]->[1], "\n";
} else {
print STDERR "no proxies found\n" if $verbose;
}