#!/usr/bin/perl # #AutoProxyChecker1.0 # #DZXNvgA #ProxyǐvzXgǕDŽLJA #ezXgNJǵǟ|[g8080/3128/80NJDžljDžB #LJǕAgp/st@CNJL^ǵDžB #xǵLJzXgNJǬǢǟAǵǼB # #GETDžt@CNJAtpenv.cgiǁǢljDžB({XNvg) # #socketgǶǻǢT[oNJuljǴǼB #lockfBNg(777)ǕpǵǟdNǕ~ǵǟǢDžB $|=1; #perlNJf[^obt@[NJLJDŽǼǻǢ $cginame='nph-prox.cgi'; $method = 'POST'; $title = 'AutoProxyChecker'; $body=''; $checkmax =30;#xNJ`FbNDžT[o $timeout = 15;#15bljGETljǴǻǩǡLJAgps $new_file = './log.dat';#ǵLJǢt@C(666) $deny_file = './deny.dat';#gpsǐǻǡLJzXgt@C(666) $allow_file = './allow.dat';#gpǐǻǡLJzXgt@C(666) $host = 'www.xxx.or.jp';#DZXNvgǕuDžzXgB $ip = '130.55.32.114';#DZXNvgǕuDžzXgIPAhXB $uri='http://www.zzz.or.jp/~zzz/penv.cgi';#GETDžURL(ljǴǑuTCgǐǁǢB) ## print "HTTP/1.0 200 OK\n";#nph-xxx.cgiljKv &jikan;#pǕDžB &lock;#dNǕ~DžLJbN@\ &decode; &html if($buffer eq '');#ljtH[ &html unless( ($ENV{'HTTP_REFERER'} eq '') || ($ENV{'HTTP_REFERER'} =~/$cginame/));#sANZXHH if ($] >= 5.0) { # socket ǕǬǩǧǮǏǻǢ eval 'use Socket'; } else { eval 'require "sys/socket.ph"'; } #$printall = '0';#GETDŽǟǴLJeǕDžǟ\DžǩǫǧǩB $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
[][gp][gps][`FbN]

_HTML_ &readlog unless(@lines);#XgǕ@linesNJ &readlog2;#(gp)Ǖ@lines2NJ &readlog3;#(gps)Ǖ@lines3NJ #L^NJ}b`DžzXgODžB 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 '
DžǼB`FbNDžT[oǕǵǟǂDŽǢB
'; &ended(); } if(scalar(@lines)== 0){ print 'VKǻT[oǁ LJǼ...
'; &ended(); } &writelog;#VKXg@linesǕǴ print "T[oXgǕǵǵLJ
]:
\n"; foreach $line(@lines){ next if(length($line)<3); print "\n$line
"; $line=~s/\n//g; #`FbNNJ~ǵLJA$deny_fileNJcheckingǐǵǟL^ǵǟǮǂB #FNǵLJAcheckinglj ǡLJzXg`FbNDŽǼB 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=~/:/){#|[gwǁ ǑAǪǤ[ǐDžB ($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';#|[g $result=eval{ &hostcheck($line, $port);}; $result='timeout' if $@;#timeoutǵLJAevalǁdieDžljA$@ǁB $end=&results($line,$port,$result);#$resulteNJǡǟAt@CǴEǕDžB 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 ǢAC|[gljs print "$line:$port:$result OǩgǶǼB
\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 #Džǟ`FbNIA$new_fileǕ 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 "
:
" if(@proxy); print @proxy; print "
IǵǵLJ...\n\n"; &ended(); }#mainprog ################################### sub results{ local($line,$port,$result) = @_; print "$line:$port:$result
\n" if($printsome); if($result eq 'use'){ print "$line:$port:OǩgǶDžB
\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:OǩgǶDžǁAIPǕ`ǶǟǢDžB
\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";#Rǁ ǑA:XǕtDžB eval 'flock(LOG,8);'; close(LOG);return "nxt"; }elsif($result eq 'forbid'){ print "$line:$port:Oǩ~DŽǟǢDžB
\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:OǩǵǼB
\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:zXgG[(IPAhXljDžǐǢljDžB)
\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:^CAEgǵǵLJB
\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'){#s 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.cgiNJǻǁ ǑGET $mole='1' if($buf=~/$ip/);#DZXNvgǕǩDžIPAhXǁ ǑARǟǢ $mole='1' if($buf=~/$host/ && $uri!~/$host/);#zXgB $forbid='1' if($buf=~/forbidden/i); $forbid='1' if($buf=~/Access.*Denied/i); } close(S); shutdown(S, 2) if !eof(S); # f?? $SIG{'ALRM'} = 'IGNORE';#A[ǁǡǟDŽǼ alarm(0);#timeoutpA[Ǖ~DžB if($content && $mole){ return "use-mole";#gp\ǁIPǕ`Ƕ }elsif($content){ return "use";#gp\ljIPǕ`ǶǻǢ }elsif($forbid){ return "forbid";#ǵǂ~DŽǟǢ }else{ return "no";#gǶǻǢHHH } }#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";#zXgǩIPǁoǼǻǢB } $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';#A[ǁǡLJǐǴǕ alarm($timeout);#$timeoutbNJA[ǕDžB #$timeoutbNJAǁljǴǻǢzXggǶǻǢǐǻDžB connect(S, $remote_pack) || return "Connect: $!"; alarm($timeout); select(S); $| = 1; select(STDOUT); return "OK"; } ################################### sub readlog{ #z@linesNJOǕ open(DB,"$new_file") || &ended('$new_file open error'); seek(DB,0,0); @lines = ; close(DB); }#readlog sub writelog{ #z@linesǕONJǴ open(DB,">$new_file") || &ended('$new_file write error'); seek(DB,0,0); print DB @lines; close(DB); }#readlog sub readlog2{ #z@lines2NJallowOǕ open(DB,"$allow_file") || &ended('$allow_file open error'); seek(DB,0,0); @lines2 = ; close(DB); }#readlog sub readlog3{ #z@lines3NJdenyOǕ open(DB,"$deny_file") || &ended('$deny_file open error'); seek(DB,0,0); @lines3 = ; close(DB); }#readlog3 sub ended{ #G[gbvǐexitǕsǧLJB unlink($lockfile);#bNt@CǕgpDžǻA#ǕDžB print "\n\n $_[0] \n"if($_[0]);#DZsljDžB 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
[gp][gps][`FbN]

gp

DZproxy checklǁInternet[UNJljljDžT[rXljDžB
DZT[rXǕpDžǕKvǁLDžB
  1. DZT[rX ǻLJgCNJǮǢǟgpǵǟDŽǢADZT[rXǕpDžNJDžsvNJǵǟCǕǼB
  2. DZT[rXǕNJDžIljgpǵǟǢǼB
  3. DZT[rX\\\\ǻǂ~DžDZǐǁLDžB
pDžONJA DZy[WǁǴǐǵǟǢDZǐǕmFǵǟǂDŽǢB
Max10zXg/15b^CAEgljDžB
:(sNJǬT[oǕǵDžB)
www.test.com
prox.fin.com:10080
portǕwǵǻǢǐ8080-3128-80NJ`FbNǵDžB


Option:
\\(for DEBUG)
T[oǕSǟ\\
`FbN(`FbNXgǐǕǵLJǂǻǢB)

_F
  1. lbg[NINJǢEǁǩǩT[oNJgp\lj ǡǟ^CAEgǵǟǵǢDžB
  2. |[g8080ljANZXDŽLJANJ|[g80lj\\\lj ǡǟDŽDžB
  3. |[gǕwǵLJA`FbNXgNJǡǟǢǟ`FbNDŽDžB
  4. `FbNDŽLJA`FbNXgNJǡǟǢǟAXgNJDŽǟǵǢDžB
  5. DZy[WNJANZXljǴǻǢǵǢǁǼB
_HTML_ &ended(); }#html END sub decode{ #IǻfR[h 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);#15ljbNt@CǕ $retry = 5; while (!symlink(".", $lockfile)) { if (--$retry <= 0) { print "Content-type: text/html\n\n"; print <<"_HTML_"; $title $body T[oǁGǵǟǢDžB(NǩǁgpǩǵǼB[?])
LJ@NJǮǢǵDžB

F
ǻLJǁgpNJrljǁLJA15pljǴǼB
DZACGIprNJbNt@CǕpǢǟǢLJANJIǵǻǩǡLJA bNt@CǁcLJljDžB15ǟǑAcǵLJbNt@CǁINJDŽDžljAp\ljDžB _HTML_ exit; } sleep(1); } #bNt@CDZDZlj }#lock END sub jikan{#$dateNJǕǵDžB $times = time;#18ǁ ǻA$times = time+18*60*60ǐDžB ($sec,$min,$hour,$mday,$month,$year,$wday,$yday,$isdst) = localtime($times); $min = "0$min" if ($min < 10); $month++;$year=$year+1900; $youbi = ('','','','','','','y') [$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){#9ǩ10ljgǶǻǢǧNJDžB print "Content-type: text/html\n\n"; print <<"_HTML_"; $title $body

$date AT[rXǕx~ǵǟǮDžB


ǵǩǽAǾǂDŽǢB _HTML_ exit; } }#jikan END __END__ # #GETDžLJpenv.cgi\[X ##! /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__