#!/usr/bin/perl # #AutoProxyChecker1.0 # # # ##This Script check ProxyServer automaticaly. # port 8080,3128,80 #Result is written in allow/deny file. #Before Checking, compare allow/deny file & input host list. # #You must setup penv.cgi on other server. # #penv.cgi'source ##! /usr/local/bin/perl #print "Content-type: text/plain\n\n"; #print "kusi-ok-desu\n"; # foreach (sort keys %ENV){ # print "$_=$ENV{$_} \n"; # } #exit; #__END__ #If your server forbids the use of socket, this script don't work. #SET UP #nph-prox.cgi (755) #log.dat (666) #deny.dat (666) #allow.dat (666) #lock (directory:777) $|=1; #perl' magic word $cginame='nph-prox.cgi'; $method = 'POST'; $title = 'AutoProxyChecker'; $body=''; $checkmax =30;#one session checkking host's maximum $timeout = 15;#15sec, timeout $new_file = './log.dat';#input host data is written here(666) $deny_file = './deny.dat';#host data which is Access denied(666) $allow_file = './allow.dat';#host data which is Access allowed(666) #Important !!! $host = 'www.myhost.com';#Host name where This Script set up. $ip = '200.0.1.102';#IP address where This Script set up. $uri='http://www.foo.bar.com/~zzz/penv.cgi';#penv.cgi URL ## print "HTTP/1.0 200 OK\n"; #&jikan;#if you need.. (STOP 8:00AM- 10:00PM) &lock;#forbidden to work doubly. &decode; &html if($buffer eq '');#Input form HTML &html unless( ($ENV{'HTTP_REFERER'} eq '') || ($ENV{'HTTP_REFERER'} =~/$cginame/));#invald access ?? if ($] >= 5.0) { # socket use eval 'use Socket'; } else { eval 'require "sys/socket.ph"'; } #$printall = '0';#debug... Print all data $Referer = 'http://www.goo.ne.jp/'; $From = ''; $machine='';$port=''; $cmd = "GET $uri HTTP/1.0\r\n"; $cmd .= "User-Agent: Mozilla/4.04; (Neko/98; G3)\r\n"; $cmd .= "Pragma: no-cache\r\n"; $cmd .= "Referer: $Referer\r\n" if $Referer; $cmd .= "From: $From\r\n" if $From; $cmd .= "Accept: */*\r\n"; $cmd .= "\r\n"; &mainprog; &ended(); ################################### sub mainprog{ print "Content-type: text/html\n\n"; print <<"_HTML_"; $title $body $title
[Input Here][YouCanUse][Denied Host][Now Checking host list]

_HTML_ &readlog unless(@lines);#@lines is Input host list &readlog2;#@lines2 is allowed host list &readlog3;#@lines2 is denied host list #compare Input host list & allowed/denied host list unless($retry){ foreach $line3(@lines3){ $test=$line3;$test=~s/\n//g;($test, $value) = split(/:/,$test); $NOCHECK{$test}='deny'; } foreach $line2(@lines2){ $test=$line2;$test=~s/\n//g;($test, $value,$x) = split(/:/,$test); $NOCHECK{$test}="$value:$x"; } } foreach $line(@lines){ $line=~s/\n//g; if($NOCHECK{$line}){ print "Åü$line:$NOCHECK{$line}
\n"; }else{ $CHECK{$line}='1'; } } print "
\n"; undef @lines; undef @proxy; foreach (sort keys %CHECK){ push(@lines,"$_\n") if(length($_)>4); print "$_
\n"; } if(scalar(@lines)>$checkmax){ print '
Sorry... Reduce input host list
'; &ended(); } if(scalar(@lines)== 0){ print 'No new proxy server...
'; &ended(); } &writelog;#New host list(@lines) is written in input file print "New Server list made..
Valuation:
\n"; foreach $line(@lines){ next if(length($line)<3); print "\n$line
"; $line=~s/\n//g; open(LOG,">>$deny_file") || &ended('$deny_file write error'); eval 'flock(LOG,2);'; seek(LOG,0,0); print LOG "$line:checking\n"; eval 'flock(LOG,8);'; close(LOG); if($line=~/:/){#you can appoint a port number. ($line, $port) = split(/:/,$line); next if($port=~/\D/); $result=eval{ &hostcheck($line, $port);};$result='timeout' if $@; $end=&results($line,$port,$result); next if($end eq 'nxt'); }else{ $port='8080';#port number $result=eval{ &hostcheck($line, $port);}; $result='timeout' if $@; $end=&results($line,$port,$result); next if($end eq 'nxt'); $port='3128'; $result=eval{ &hostcheck($line, $port);};$result='timeout' if $@; $end=&results($line,$port,$result); next if($end eq 'nxt'); $port='80'; $result=eval{ &hostcheck($line, $port);};$result='timeout' if $@; $end=&results($line,$port,$result); next if($end eq 'nxt'); } #8080/3128/80 deny print "$line:$port:$result this host can't use.
\n"; open(LOG,">$deny_file") || &ended('$deny_file write error'); push(@lines3,"$line:$result\n"); eval 'flock(LOG,2);'; seek(LOG,0,0); print LOG @lines3; eval 'flock(LOG,8);'; close(LOG);next; }#foreach #All checking done, $new_file is initialized. open(LOG,">$new_file") || &ended('$allow_file write error'); close(LOG); open(LOG,">$deny_file") || &ended('$deny_file write error'); eval 'flock(LOG,2);'; seek(LOG,0,0); print LOG @lines3; eval 'flock(LOG,8);'; close(LOG); print "
Result:
" if(@proxy); print @proxy; print "
All done...\n\n"; &ended(); }#mainprog ################################### sub results{ local($line,$port,$result) = @_; print "$line:$port:$result
\n" if($printsome); if($result eq 'use'){ print "$line:$port:You can use
\n"; push(@proxy,"$line:$port:
"); open(LOG,">>$allow_file") || &ended('$allow_file write error'); eval 'flock(LOG,2);'; seek(LOG,0,0); print LOG "$line:$port\n"; eval 'flock(LOG,8);'; close(LOG);return "nxt"; }elsif($result eq 'use-mole'){ print "$line:$port:You can use. but Real IP forwarded.
\n"; push(@proxy,"$line:$port:X
"); open(LOG,">>$allow_file") || &ended('$allow_file write error'); eval 'flock(LOG,2);'; seek(LOG,0,0); print LOG "$line:$port:X\n";# eval 'flock(LOG,8);'; close(LOG);return "nxt"; }elsif($result eq 'forbid'){ print "$line:$port:Forbidden
\n"; open(LOG,">$deny_file") || &ended('$deny_file write error'); push(@lines3,"$line:$result\n"); eval 'flock(LOG,2);'; seek(LOG,0,0); print LOG @lines3; eval 'flock(LOG,8);'; close(LOG);return "nxt"; }elsif($result eq 'no'){ print "$line:$port:No response
\n"; open(LOG,">$deny_file") || &ended('$deny_file write error'); push(@lines3,"$line:$result\n"); eval 'flock(LOG,2);'; seek(LOG,0,0); print LOG @lines3; eval 'flock(LOG,8);'; close(LOG);return "nxt"; }elsif($result=~/BadHostname/){ print "$line:$port:hostname error(input ip addr)
\n"; open(LOG,">$deny_file") || &ended('$deny_file write error'); push(@lines3,"$line:$result\n"); eval 'flock(LOG,2);'; seek(LOG,0,0); print LOG @lines3; eval 'flock(LOG,8);'; close(LOG);return "nxt"; }elsif($result eq 'timeout'){ print "$line:$port:timeout..
\n"; open(LOG,">$deny_file") || &ended('$deny_file write error'); push(@lines3,"$line:$result\n"); eval 'flock(LOG,2);'; seek(LOG,0,0); print LOG @lines3; eval 'flock(LOG,8);'; close(LOG);return "nxt"; } return "moreport";# }#results ################################### sub hostcheck{ local($machine, $port) = @_; local($content, $mole,$buf); $connect=&connect_socket($machine, $port); if($connect ne 'OK'){#can't connect print "$machine:$port:$connect\n
" if ($printall); return $connect; } $content=''; $mole='';$forbid=''; print S $cmd; while ($buf = ) { print "$buf
" if ($printall); $content='1' if($buf=~/kusi-ok-desu/);#penv.cgi's original Key Word $mole='1' if($buf=~/$ip/);#Real IP found $mole='1' if($buf=~/$host/ && $uri!~/$host/);#Real Hostname found $forbid='1' if($buf=~/forbidden/i); $forbid='1' if($buf=~/Access.*Denied/i); } close(S); shutdown(S, 2) if !eof(S); $SIG{'ALRM'} = 'IGNORE'; alarm(0); if($content && $mole){ return "use-mole"; }elsif($content){ return "use"; }elsif($forbid){ return "forbid"; }else{ return "no"; } }#hostcheck ################################### sub connect_socket { my($machine, $port) = @_; my($sockaddr) = 'S n a4 x8'; eval '$Inet = &AF_INET' || ($Inet = 2); eval '$Stream = &SOCK_STREAM' || ($Stream = 1); chop($localhost = `hostname`); my($name, $aliases, $proto) = getprotobyname('tcp'); my($name, $aliases, $type, $len, $localaddr) = gethostbyname($localhost); #my($name, $aliases, $type, $len, $remaddr) = gethostbyname($machine); if ($machine =~ /^(\d+)+\.(\d+)\.(\d+)\.(\d+)$/) { $remaddr = pack('C4', $1, $2, $3, $4); } else { local(@temp) = gethostbyname($machine); unless (@temp) { return "BadHostname";#can't resolve hostname } $remaddr = $temp[4]; } my($local_pack) = pack($sockaddr, $Inet, 0, $localaddr); my($remote_pack) = pack($sockaddr, $Inet, $port, $remaddr); socket(S, $Inet, $Stream, $proto) || return "Socket: $!"; bind(S, $local_pack) || return "Bind: $!"; $SIG{'ALRM'} = 'timed_out'; alarm($timeout) connect(S, $remote_pack) || return "Connect: $!"; alarm($timeout); select(S); $| = 1; select(STDOUT); return "OK"; } ################################### sub readlog{ open(DB,"$new_file") || &ended('$new_file open error'); seek(DB,0,0); @lines = ; close(DB); }#readlog sub writelog{ open(DB,">$new_file") || &ended('$new_file write error'); seek(DB,0,0); print DB @lines; close(DB); }#readlog sub readlog2{ open(DB,"$allow_file") || &ended('$allow_file open error'); seek(DB,0,0); @lines2 = ; close(DB); }#readlog sub readlog3{ open(DB,"$deny_file") || &ended('$deny_file open error'); seek(DB,0,0); @lines3 = ; close(DB); }#readlog3 sub ended{ #error trap & lockfile unlock unlink($lockfile); print "\n\n $_[0] \n"if($_[0]); exit; }#ended sub timed_out { local($sig) = @_; die "\nConnection timed out. (SIG$sig)\n"; }#timed_out sub html { print "Content-type: text/html\n\n"; print <<"_HTML_"; $title $body
$title
[Use][Denied][Now checking]
Before use, You must check This site
Max10 hosts/15sec timeout
Input Example:(1 line/ 1 host)
www.test.com
prox.fin.com:10080
if no port,8080-3128-80 automaticaly


Option:
(for DEBUG)
show all response
check again. (No compare input hosts & allow/deny list)

Point:
_HTML_ &ended(); }#html END sub decode{ #decode if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } #else { $buffer = $ENV{'QUERY_STRING'}; } @pairs = split(/&/,$buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s/ /\n/g;$value =~ s/Å@/\n/g; $value =~ s/\r/\n/g;$value =~ s/\n\n/\n/g; $FORM{$name} = $value; } $url = $FORM{'host'};$printall = $FORM{'printall'};$retry = $FORM{'retry'}; $printsome = $FORM{'printsome'}; @lines = split(/\n/,$url); }#decode END sub lock{ $lockfile = './lock/lock-file.lock'; $filedata = -M "$lockfile"; unlink "$lockfile" if ($filedata > 0.01);#15 min, lock is unlocked $retry = 5; while (!symlink(".", $lockfile)) { if (--$retry <= 0) { print "Content-type: text/html\n\n"; print <<"_HTML_"; $title $body Someone use this script now [?])
Retry later

_HTML_ exit; } sleep(1); } }#lock END sub jikan{# $times = time; ($sec,$min,$hour,$mday,$month,$year,$wday,$yday,$isdst) = localtime($times); $min = "0$min" if ($min < 10); $month++;$year=$year+1900; $youbi = ('SUn','Mon','thu','Wed','?','Fri','Sat') [$wday]; $date = "$month/$mday($youbi)$hour:$min"; $month = "0$month" if ($month < 10); $mday = "0$mday" if ($mday < 10); $dateindex = "$year$month$mday"; if( $hour > 8 && $hour < 22){#Can't work 8:00AM-10:00PM print "Content-type: text/html\n\n"; print <<"_HTML_"; $title $body

$date now, service is stop


sorry _HTML_ exit; } }#jikan END __END__