用perl写的http代理,大家帮忙指点!
#!/usr/bin/perl
use IO::Socket;
use threads;
require "cgilib.pl";
$sock_1=IO::Socket::INET->new(
LocalHost =>'10.21.2.226',
LocalPort =>3128,
Proto =>'tcp',
Listen =>3,
Reuse =>1
) or die "no socket:$!";
my $i=0;
while(1)
{
$sock_[$i]=$sock_1->accept();
$threads[$i]=threads->new(\&subthread,$sock_[$i]);
$threads[$i]->join;
$i++;
}
close $sock_1;
sub subthread{
my $sock_2=shift;
$len=sysread($sock_2,$data,2048);
#分析请求头
@a=split(/\r\n/,$data);
@b=();#初始化数组
for(my $j=0;$j<@a;$j++)
{
($b[$#b+1],$b[$#b+1])=split(/:/,$a[$j]);
}
my %http_head=@b;
$http_head{Host}=substr($http_head{Host},1);
print "$http_head{Host}:80\n$http_head{'GET http'}\n";
eval($sock_server = IO::Socket::INET->new("$http_head{Host}:80"));
if ($@ ne ""){ print "haha1$@\n";}
eval($len=syswrite($sock_server,$data,length($data)));
if ($@ ne ""){ print "haha3$@\n";}else{&writefile(\$data);}
while($flag)
{
eval($len=sysread($sock_server,$data,4096));
if ($@ ne ""){ print "haha4$@\n";}else{&writefile(\$data,\$i);}
print "read---------------$len-----$i---------\n";
if ($len<1){$flag=0;}
else{
eval($len=syswrite($sock_2,$data,$len));
if ($@ ne ""){ print "haha2[".$@."]\n";}#else{&writefile(\$data);}
print "write---------------$len-----$i---------\n";
unless($len){$flag=0;}
}
}
close $sock_2;
close $sock_server;
}
sub writefile{
local(*str,*i) = @_ if @_;
open(MYFILE,">>log");
binmode(MYFILE);
#print MYFILE &getTime . "sa".$i;
print MYFILE $str;
close(MYFILE);
}