จะใช้ squid ทำตัวเป็น redirector โดยให้ดูจาก rule ที่กำหนด
#vi /etc/squid/squid.conf
url_rewrite_program /home/kitti/squid_redirect.pl
#vi /home/kitti/squid_redirect.pl
#!/usr/bin/perl
$|=1;
sub print_log
{
local($msg) = @_;
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
$time=sprintf "%4d-%02d-%02d %02d:%02d:%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec;
open(LOG,"+>>$log_file");
print LOG "$time : $msg";
close(LOG);
}
$rules_file = "/etc/squid/rules.conf";
$log_file = "/var/log/squid/redirect.log";
open (RULE,$rules_file);
&print_log("-- redirector start --\n");
while (<>) {
@X = split;
seek(RULE,0,0);
$url = $X[0];
($ip,$fqdn) = split(/\//,$X[1]);
$number=1;
$flag=0;
$line_no=0;
$go_url = $url;
while(
chomp;
$line = $_;
if((!($line =~ /^[\#\;\t ]/))&&($line)){
($src_ip,$dst_url,$action,$redirect) = split(/[ |\t]+/,$line);
if(($url =~ /$redirect/)&&($ip =~ /$src_ip/)) {
$flag = 1;
$go_url = $url;
$action = "direct-to";
}elsif (($url =~ /$dst_url/)&&($ip =~ /$src_ip/)) {
$line_no = $number;
if($action eq 'bypass'){
$go_url = $url;
$flag = 1;
}elsif ($action eq 'redirect-to'){
$go_url = "302:$redirect";
$flag = 1;
}
}
}
$number++;
if($flag){ last; }
}
print "$go_url\n";
#&print_log("squid request: @X\n");
&print_log("ip:$ip, url:$url, rule:$line_no, action:$action, redirect:$go_url\n");
}
close(RULE);
&print_log("-- redirector exit-- \n");
exit(0);
#===============================================================
#vi /etc/squid/rules.conf
# description
;src-ip url action
1.1.1.1 www.domain1.com bypass
127.0.0.1 www.domain2.co.th bypass
.* www.domain3.com bypass
.* domain4.co.th bypass
.* .* redirect-to http://www.default-domain.co.th
Thursday, May 24, 2007
[Linux] Squid จะต้องการให้มีการ redirect url โดยดูจาก source ip
at 12:00 AM
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment