本文原于在cu 论坛上的求助,最后自己解决了问题,贴子没有人关注,一直沉到海底,今找从cu 找回,以助有需要之士参考。
----
缘由:BOSS 向我提出了一个奇怪的问题,能否让客户在使用公司网络时,第一次打开ie 或其它浏览器都显示公司的主页。在学习了squid 发现squid 可以实现该功能。
1、首先需要在iptable/ipfw/pf 等防火墙上,将所有对外80 的请求重定向到squid。
2、在squid 启用重定向器
redirect_program /etc/pftable/red.pl
redirect_children 10
redirector_bypass on
3、red.pl 内容为:
# cat /etc/pftable/red.pl
#!/usr/bin/perl -w
# author fengyong 2008-01-22
use strict;
use DB_File;
use vars qw (%cache $uri $cachetime);
$|=1;
my $timeout=3600;#缓存超时时间
while (<>){
my ($client,$ident,$method)=();
($uri,$client,$ident,$method)=split;
if (check($client)){
next;
}else{
save($client);
$uri="301:[url]http://www.xxxx.cn[/url]";
}
}continue{
print $uri;
}
sub check {
my $client=shift;
# init cache time
my $time=time();
if (!$cachetime){
$cachetime=$time+$timeout;
}
if ($time > $cachetime){
%cache=();
$cachetime=();
}
return 1 if $cache{$client};
# reopen db
my %ip=();
tie %ip,"DB_File","/tmp/ip.db",O_CREAT|O_RDWR,0666;
%cache=%ip; #hard copy?
untie %ip;
$cache{$client} ? 1:0;
}
sub save {
my $client=shift;
my %ip=();
tie %ip,"DB_File","/tmp/ip.db",O_CREAT|O_RDWR,0666;
$ip{$client}=1;
untie %ip;
}
4、问题
程序虽然有缓存功能,但在客户端比较多时,不建议使用,因为会影响性能。