#!/usr/bin/perl # Auto Proxy Env Checker 2.2 # Improved by ANONYMIZER dimdim ########## penv.cgi # # §«§‚§Ï§­€¡€ß€ˆ€´°º¿þˆ÷€µ€§€»§» Ã€µ€§€»§¨§§§§°£ # # #!/usr/local/bin/perl # # print "Content-type: text/plain\n\n"; # print "PROXY_CHECKER=dimdim\n"; # foreach (sort keys %ENV) { # print "$_=$ENV{$_}\n" if ( ($_ =~ /HTTP_/) || ($_ =~ /REMOTE_/) ); # } # exit; # # __END__ # ######## penv.cgi $| = 1; $cginame = 'nph-prxy.cgi'; # CGI §‘Ãæ¡ƒ $title = 'Auto Proxy Env Checker'; # TITLE €ø€ƒ $body = 'text=#ffffcc bgcolor=#000000 link=#ccccff alink=#ffffff vlink=#ccffcc'; # BODY €ø€ƒ $max = 30; # ƒÏ‰§À€¡€ß€ˆ€Ø§¼§Î†«¬Á€µ°º€­øÙ $timeout = 60; # $timeout Šˆ¥÷§« GET §«§‚§ §´§ˆ§ø§È°¢ª»Õ‹ŠŒ¾Ÿ§»§þ§ §¼ $penv = 'http://www.foo.com/penv.cgi'; # env €¡€ß€ˆ€´°º $chk_host = 'www.bar.com'; # ¿þˆ÷€µ°º€­ Hostname $ip_addrs = (gethostbyname($chk_host))[4]; # Host Addr ƒ™§Ø§‘§¨ÃÉ𧠧»§‚§À ‹¥¼§…§Þ§¼ $chk_addr = join('.', unpack("C4", $ip_addrs)); $warn_clr = 'red'; # ¦„§Ï§¨§¢§ˆ§ø§»§‚§‘…¼• FONT €ø€ƒ§‘øß $delword = "www.hoge.com"; # €Ì€ƒ§´§È戧…§§§‚§ø§§…Ë $no_list = './no.dat'; # NoProxy €¹€°€§€Î $anony_list = './anony.dat'; # Anony €¹€°€§€Î $via_list = './via.dat'; # Via €¹€°€§€Î $spill_list = './spill.dat'; # Spill €¹€°€§€Î $deny_list = './deny.dat'; # ŠŒ¾Ÿ€¹€°€§€Î $lockfile = './lock/nph-prxy.lock'; # €Ì€ˆ€Ø€¹€°€§€Î # # # # # # # # # # print "HTTP/1.0 200 OK\n"; # nph-xxx.cgi §«§¦Š¨Õ× if ($ARGV[0] eq '') { &lock; print <<"_HTML_"; Content-type: text/html $title

$title

[NoProxy][Anony][Via][Spill][Deny]

Max : $max
TimeOut : $timeout sec

www.foo.com
(8080 - 3128 - 80)
proxy.bar.com:10080


Option : Printing Detail Info

Ԭ͉¿°ß
  1. €Õ€ˆ€»€Ô°º€Ø‰§À±Û§§æÏ¼Á§ §Š§¦ª»Õ‹¾Ÿ«‡§«§¢§ˆ§§’€ø€§€ý€¢€¶€»§…§§…§Þ§§§Þ§¼°£
  2. €¡€ß€ˆ€Ø†‹§þ€Í€¼€»§»§‘滼Á§¦ƒÏ¿Ž¼Œ§§§Þ§ª§Û°£
  3. [$penv] §À€¢€Ø€ª€¼§«§‚§ §§æÏ¼Á§¦¿µ§…§§…ξ稿§È§Ï§Þ§ª§Û°£
ˆÌƒ¹ª–¼ý°ß
  1. §„§‘€µ°º€²€¼§¦§¢§ §øº´ø»§‘¿¹«§§À§§§§ª»Õ‹§…§¾º§µ§§°£
    §„§‘€µ°º€²€¼§ÚÕ¯Õ‹§¼§Îª–§À§Ë§Í»Ø¿½§¼§ÎƒÏ¿Ž§‘ŠŒÕ¯±×§À¬­§…§ª”§¦¿¹«§§ÚŠÈ§®§Þ§ª§Û°£
  2. §„§‘€µ°º€²€¼§Ú½¯‡¯‘Š¬Ø§À»ø§¼§ÎÃЉ§«ª»Õ‹§…§§¦§§§±§Þ§ª§Û°£
  3. §„§‘€µ°º€²€¼§¦Õ‡¼•§ §ØŸ”ªþ§¼§Î§„§»§¨§¢§Í§Þ§¼°£

Special Thanx ... Team CR[y]ACKER _HTML_ &ended; } elsif ($ARGV[0] eq 'chk') { 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/ //g; $value =~ s/°°//g; $value =~ s/\r/\n/g; $value =~ s/\n\n/\n/g; $FORM{$name} = $value; } @check_hostz = split(/\n/, $FORM{'host'}); &error("Too Many Hostz.") if (scalar(@check_hostz) > $max); $detail = '1' if ($FORM{'detail'} eq 'on'); if ($] >= 5.0) { eval `use Socket`; } else { eval `require "sys/socket.ph"`; } foreach $proxy (@check_hostz) { if ($proxy =~ /:/) { ($host, $port) = split(/:/, $proxy); $result = eval{ &hostcheck($host, $port); }; $result = 'timeout' if ($@); push(@checker, "$result


\n") if ($detail); &results($host, $port, $result); } else { @default_port = ('8080', '3128', '80'); foreach $default_port (@default_port) { $result = eval{ &hostcheck($proxy, $default_port); }; $result = 'timeout' if ($@); push(@checker, "$result
\n") if ($detail); &results($proxy, $default_port, $result); } } } print <<"_HTML_"; Content-type: text/html $title

_HTML_ print @print_result; print "
\n"; foreach $checker (@checker) { $checker =~ s/\r/\n/g; $checker =~ s/\n\n/\n/g; $checker =~ s/$delword/*** Delete ***/g; } print @checker; print <<"_HTML_";

... Ended. _HTML_ &ended; } ########## sub ########## sub hostcheck{ local($machine, $port) = @_; undef(@buf); undef($content); undef($delegate); undef($spill); undef($envz); undef($alias); undef($via_proxy); undef($anony); undef($forbid); undef($dflag); $connect = &connect_socket($machine, $port); return $connect if ($connect ne 'OK'); $cmd = "GET $penv HTTP/1.0\015\012"; $cmd .= "Pragma: No-Cache\015\012"; $cmd .= "User-Agent: Mozilla/4.5[en] (Win98; I)\015\012"; $cmd .= "Connection: Keep-Alive\015\012"; $cmd .= "\015\012"; print S $cmd; while ($buf = ) { $buf =~ s/<[^<>]*>//gi; push(@buf, $buf); } foreach $buf (@buf) { $content = '1' if ($buf =~ /PROXY_CHECKER=dimdim/); # $penv §À¥Þ§Þ§Ï§Îˆ Ã§  ½ª™‘Û§¨§¢§Ï§­ GET ¿Æ½˜ $delegate = '1' if ($buf =~ /^DeleGate/i); # DelegGate §´°© if ( ($buf =~ /$chk_host|$chk_addr/) && ($buf !~ /HTTP_HOST=/) ) { # ¦„§Ï§§ §§§´°© $spill = '1'; if ($detail) { $buf =~ s/$chk_host/*** Spill Host ***<\/FONT>/ig; $buf =~ s/$chk_addr/*** Spill Addr ***<\/FONT>/ig; } } if ($buf =~ /REMOTE_HOST=/) { if ($buf !~ /$machine/ig) { # REMOTE_HOST §¨ƒ¤§ §Î§´°© $alias = $buf; $alias =~ s/REMOTE_HOST=|\n//g; $alias = ":$alias"; } } if ($buf =~ /HTTP_/) { $via_proxy = '1' if ( ($buf =~ /HTTP_USER_AGENT=/) && ($buf =~ /via|squid|delegate|httpd|proxy|cache/i) ); # AGENT if ($buf !~ /HTTP_CONNECTION=|HTTP_USER_AGENT=|HTTP_HOST=|HTTP_PRAGMA=/) { # Ÿæ¿Ð¿Ð¬„ ‹øÙƒ „ƒ $via_proxy = '1'; if ($buf !~ /HTTP_PRAGMA=|HTTP_PROXY_CHECKER=dimdim|HTTP_VIA=|HTTP_PROXY_CONNECTION=|HTTP_X_FORWARDED_FOR=|HTTP_FORWARDED=|HTTP_IF_MODIFIED_SINCE=|HTTP_CACHE_CONTROL=|HTTP_HOST=/) { # Proxy »º´ ‹øÙ $envz .= "[$buf]"; $envz =~ s/\n//g; } } if ($buf =~ /HTTP_CONNECTION/) { $anony = '1'; # NoProxy §‘Õס«§¢§Í $anony = '0' if ($buf !~ /Keep-Alive/i); # close §»§´§«§¦ Anony } } $forbid = '1' if ($buf =~ /Forbidden/i); $forbid = '1' if ($buf =~ /Access.*Denied/i); $forbid = '1' if ($buf =~ /Not.*Found/i); $forbid = '1' if ($buf =~ /Fatal.*Error/i); $forbid = '1' if ($buf =~ /Proxy.*Error/i); push(@checker, $buf) if ($detail); } close (S); shutdown (S, 2) if ( !eof(S) ); $SIG{'ALRM'} = 'IGNORE'; alarm(0); if ($content eq '1') { $dflag = 'dg' if ($delegate eq '1'); if ($spill eq '1') { return "spill${dflag}"; # ª»Õ‹¾Ÿ«‡§¿§¨ IP §Ú‰¡§®§Î } else { if ($via_proxy ne '1') { if ($anony eq '1') { return "use:${dflag}:noproxy$alias"; } else { return "use:${dflag}:anony$alias"; } } else { return "use:${dflag}:via$alias $envz"; } } return 'use'; } elsif ($forbid) { return 'forbid'; } else { return 'no'; } } sub results { local($host, $port, $result) = @_; local($dlgt, $alias_host, $viaenv); if ($result =~ /^use/) { if ($result eq 'use') { $dlgt = ':?'; $write_file = $deny_list; } elsif ($result =~ /use:[^:]*:[^:]+:/) { $alias_host = $result; $alias_host =~ s/use:[^:]*:[^:]+:([^:]+)/$1/; $alias_host =~ s/\s//g; $alias_host = " ($alias_host)"; } $dlgt = ':D' if ($result =~ /:dg:/); if ($result =~ /^use:[^:]*:noproxy/) { # NoProxy $chk_res = 'NoProxy!'; $write_file = $no_list; } elsif ($result =~ /^use:[^:]*:anony/) { # Anony $chk_res = 'Anonymized.'; $write_file = $anony_list; } elsif ($result =~ /^use:[^:]*:via/) { # Via $chk_res = 'Via a Proxy.'; $viaenv = $result; $viaenv =~ s/^use:[^:]*:via (.*)/ $1/; $dlgt .= $viaenv; $write_file = $via_list; } push(@print_result, "$host:$port$dlgt$alias_host ... $chk_res
\n"); open(OUT, ">>$write_file") || &error("Write Error: $write_file"); flock(OUT, 2); print OUT "$host:$port$dlgt$alias_host\n"; flock(OUT, 8); close(OUT); } elsif ($result eq 'timeout') { push(@print_result, "$host:$port ... TimeOut!
\n"); open(OUT, ">>$deny_list") || &error("Write Error: $deny_list"); flock(OUT, 2); print OUT "$host:$port:TimeOut\n"; flock(OUT, 8); close(OUT); } elsif ($result =~ /spill/) { $dlgt = ':D' if ($result =~ /dg/); push(@print_result, "$host:$port$dlgt ... SPILL!!
\n"); open(OUT, ">>$spill_list") || &error("Write Error: $spill_list"); flock(OUT, 2); print OUT "$host:$port$dlgt\n"; flock(OUT, 8); close(OUT); } else { # ŠŒ¾Ÿ€Í€¼€»¼Œ§‚ if ($result eq 'forbid') { push(@print_result, "$host:$port ... Access Denied.
\n"); } elsif ($result eq 'no') { push(@print_result, "$host:$port ... No Responce.
\n"); } elsif ($result =~ /BadHostname/) { push(@print_result, "$host:$port ... Bad Hostname.
\n"); } else { push(@print_result, "$host:$port ... $result
\n"); } open(OUT, ">>$deny_list") || &error("Write Error: $deny_list"); flock(OUT, 2); print OUT "$host:$port:X\n"; flock(OUT, 8); close(OUT); } return; } sub connect_socket { local($machine, $port) = @_; local($sockaddr) = 'S n a4 x8'; eval '$Inet = &AF_INET' || ($Inet = 2); eval '$Stream = &SOCK_STREAM' || ($Stream = 1); chop($localhost = `hostname`); eval `$SIG{'ALRM'} = 'timed_out'`; alarm($timeout); local($name, $dmy, $proto) = getprotobyname('tcp'); local($dmy, $dmy, $dmy, $dmy, $localaddr) = gethostbyname($localhost); if ($machine =~ /^(\d+)+\.(\d+)\.(\d+)\.(\d+)$/) { $remaddr = pack('C4', $1, $2, $3, $4); } else { local(@temp) = gethostbyname($machine); return "BadHostname" unless (@temp); $remaddr = $temp[4]; } alarm($timeout); local($local_pack) = pack($sockaddr, $Inet, 0, $localaddr); local($remote_pack) = pack($sockaddr, $Inet, $port, $remaddr); socket(S, $Inet, $Stream, $proto) || return "SocketError: $!"; bind(S, $local_pack) || return "BindError: $!"; alarm($timeout); connect(S, $remote_pack) || return "ConnectError: $!"; alarm($timeout); select(S); $| = 1; select(STDOUT); return 'OK'; } sub lock { $filedata = -M "$lockfile"; unlink "$lockfile" if ($filedata > 0.01); # 15 ¨¥÷§«€Ì€ˆ€Ø€¹€°€§€Î§Ú†Ô‡¸ $retry = 5; while (!symlink(".", $lockfile)) { if (--$retry <= 0) { &error("Sorry, Busy!"); exit; } sleep(1); } } sub timed_out { local($sig) = @_; die "\nConnection timed out. (SIG$sig)\n"; } sub error { print <<"_HTML_"; Content-type: text/html Error

Error

$_[0]

_HTML_ exit; } sub ended { unlink($lockfile); exit; } __END__