#!/usr/bin/perl -w
#
# proxytest.pl v0.6
# by bunker - http://rawlab.altervista.org 
# Fri Aug 19 16:05:34 CEST 2005 
# 
# It checks a list of proxies and makes 
# report with anonymous, transparent and 
# not working proxies (with time response)
#
use strict;
use LWP::UserAgent::ProxyAny;
use Tie::File;
use Time::HiRes qw(gettimeofday);
use Fcntl 'O_RDWR', 'O_CREAT', 'O_EXCL';

# if you change site remember to change IP matching!
my $url = "http://www.whatismyip.com/";
my $match = '([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3})';

my $timeout = "1.5";

# proxies and report files
my $list = "proxies.lst";
my $filenum = int(gettimeofday);
my $report = "report-$filenum.lst";
my $anonfile = "anon-$filenum.lst";

# files arrays
my (@lst_lines, @r_lines, @a_lines);

# read list
tie @lst_lines, 'Tie::File', "$list"
   or die "$list file problem: $!";
          
# LWP init
my $ua = LWP::UserAgent::ProxyAny->new;
$ua->agent("proxytest.pl by bunker [http://rawlab.altervista.org]");
$ua->timeout($timeout);

my (@anonymous, @anonreport, @transparent, @not_working);

sub my_ip {
   $ua->set_proxy_by_name("");
   $ua->no_proxy();
   my $request = $ua->get($url);
   my $content = $request->content;
	my $ip = $1
      if($content =~ m/$match/i);
   return $ip;
}
my $myip = &my_ip();

foreach my $proxy (@lst_lines) {
   $ua->set_proxy_by_name("$proxy");
   my $t0 = gettimeofday;
   my $request = $ua->get($url);
   my $t1 = gettimeofday;
   my $tdiff = sprintf "%0.3f", $t1-$t0;
   if ($request->is_success) {
      my $content = $request->content;
      if($content =~ m/$match/i) {
		   my $ret_ip = $1;         
         if ($proxy =~ m/^$myip:[0-9]/i) {
            print "$proxy is TRANSPARENT - Response time: $tdiff sec\n";
            push @transparent, "$proxy ($tdiff sec)";
         }
         else {
            print "$proxy is ANONYMOUS - Response time: $tdiff sec\n";
            push @anonymous, "$proxy ($tdiff sec)";
            push @anonreport, "$proxy";
         }
      } 
	   else {
	      print "Ooops!! Something wrong (Check matching string)!\n";
      }
   }
   else {
      my $msg = $request->status_line;
      print "$proxy ($msg)\n";
      push @not_working, "$proxy ($msg)";
   }
}

# make anonymous file
tie @a_lines, 'Tie::File', "$anonfile", mode => O_RDWR|O_CREAT|O_EXCL
   or die "$anonfile file problem: $!";
push @a_lines, @anonreport;
untie @a_lines;

# make report file
tie @r_lines, 'Tie::File', "$report", mode => O_RDWR|O_CREAT|O_EXCL
   or die "$report file problem: $!";

push @r_lines, "PROXYTEST.PL by bunker - http://rawlab.altervista.org\nReport $report\n";
push @r_lines, "\n[ANONYMOUS WORKING PROXIES]\n";
push @r_lines, @anonymous;
push @r_lines, "\n[TRANSPARENT WORKING PROXIES]\n";
push @r_lines, @transparent;
push @r_lines, "\n[NOT WORKING]\n";
push @r_lines, @not_working;
untie @r_lines;  
print "\nDone! Look at $report file for this report.\n";

# untie proxies.lst
untie @lst_lines;
