#!/usr/bin/perl #use strict; use IO::Socket; use IO::Select; $progname = $0; $progname =~ s,.*[/\\],,; # use basename only $progname =~ s/\.\w*$//; # strip extension, if any $VERSION = sprintf("%d.%02d", q$Revision: 1.0 $ =~ /(\d+)\.(\d+)/); #toDo: scan common ports to determine the port the proxy is really #listening on if the user doesn't specify? $proxyPort=8080; $proxyStr=shift; if ($proxyStr) { ($proxyAddr, $proxyPort)=split(/:/, $proxyStr); $proxyPort=($proxyPort or 8080)} else { print "use: statProxy :\n"; my $DISTNAME = 'statProxy ' . $VERSION; die <<"EOT"; This is statProxy $VERSION ($DISTNAME) Author: wayne\@nym.alias.net This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. EOT }; $printHTML=0; # for me (to get the string for my proxy capabilities page) #make this an xml file eventually - then i can distribute updates easily %tests= ( 1, {type=>'CONNECT', host=>'panix.com', port=>22, check=>'', text=>'ssh'}, 2, {type=>'CONNECT', host=>'panix.com', port=>23, check=>'', text=>'telnet'}, 3, {type=>'CONNECT', host=>'smtp.panix.com', port=>25, check=>'', text=>'smtp'}, 4, {type=>'CONNECT', host=>'www.panix.com', port=>80, check=>'', text=>'http (website) via port 80'}, 5, {type=>'CONNECT', host=>'www.panix.com', port=>443, check=>'', text=>'secure http'}, 6, {type=>'CONNECT', host=>'news.panix.com', port=>119, check=>'', text=>'news'}, 7, {type=>'CONNECT', host=>'216.72.24.211', port=>1080, check=>'', text=>'socks'}, 8, {type=>'CONNECT', host=>'137.204.148.11', port=>3128, check=>'', text=>'http proxy via port 3128'}, 9, {type=>'CONNECT', host=>'irc.dalnet.com', port=>6667, check=>'', text=>'irc'}, 10, {type=>'CONNECT', host=>'139.13.25.160', port=>8080, check=>'', text=>'http proxy via port 8080'}, ); #check is a regex for response contents print "let me check how this sucker works\n"; print "please wait one or two minutes...\n"; $protocol = getprotobyname("tcp"); $proxyAgent=''; foreach $test (sort {$a <=> $b} keys(%tests)) { #ref to test object $pass=0; print STDOUT "$test "; &makeConnection(); #connect to the proxy ($type, $host, $port)=($tests{$test}->{type}, $tests{$test}->{host}, $tests{$test}->{port}); #there's a more elegant way ... map? # Send command to proxy: print PROXY "$type $host:$port HTTP/1.0\r\n\r\n"; #change \r\n to #$CRLF for Macs ($status) = (split(/\s+/,))[1]; if ($status) { $pass=1 if ( int($status/100) == 2 ); #good, got 2nn status code # Skip through remaining part of HTTP header (until blank line) # catch 'Proxy-Agent: ' on the way while () { last if ( /^[\r\n]+$/ ); unless ($proxyAgent) {($proxyAgent)= ($_ =~ /Proxy-Agent: (.*)$/i)}; # only get this on a #successful connect }; } else {$pass=0}; $tests{$test}->{result}=$pass; close PROXY; }; print STDOUT "\n"; unless ($proxyAgent) {$proxyAgent='unknown'}; print STDOUT "\nproxy $proxyAddr:$proxyPort results:\n"; $str=''; $goodStr=''; $badStr=''; $lastStat=-1; @tests=(); foreach $test (sort {$a <=> $b} keys(%tests)) { #ref to test object ($type, $host, $port, $result)=($tests{$test}->{type}, $tests{$test}->{host}, $tests{$test}->{port}, $tests{$test}->{result}); #there's a more elegant way ... map? if ($result) { $color="00FF00"; $goodStr.="$port, "; push(@tests, $test); } else { $color="FF0000"; $badStr.="$port, ";}; # do something different here, to get a simple list of good # and bad ports when $printHTML=0 $str.=(($result == $lastStat) ?"" :"").$port.", "; $lastStat=$result; }; for ($str, $goodStr, $badStr) {$_=~s/, $//}; print STDOUT "passed: $goodStr\n"; print STDOUT "failed: $badStr\n"; if ($printHTML) {print STDOUT "$str\n"}; print STDOUT "proxy type: $proxyAgent\n"; if (!@tests) {print "Can't use this proxy for anything special\n"; exit}; print STDOUT "You could run a local proxy (use localProxy.pl) for: \n"; $i=0; for (@tests) {print STDOUT "$tests[$i++]: ", $tests{$_}->{text}, " (eg ", $tests{$_}->{host}, ":", $tests{$_}->{port}, ")\n"}; print STDOUT "and maybe on other ports I haven't tested\n"; 1; sub makeConnection{ # connect to proxy server ... socket (PROXY, PF_INET, SOCK_STREAM, $protocol) or die("Failed to create socket:$!"); connect (PROXY, sockaddr_in($proxyPort,inet_aton($proxyAddr))) or # die("\nFailed to connect to $proxyAddr port $proxyPort: $!"); do {print "Failed to connect to $proxyAddr port $proxyPort, \n"; print "it's either dead, or your access is blocked\n"; exit}; select(\*PROXY); $|=1; #make it hot };