23 августа 2012 г.

squid redirector start page

#!/usr/bin/perl

use strict;

my $ref_time = 50400;
my $startp = "302:[url]http://mail.ru"[/url];
my $dat_dir = "/tmp";
my $match = "user"; # user / IP

local $| = 1;
$SIG{INT} = sub { die "Bye!"; };

while ( 1 ) {

  my $buff_in = <>;
  chomp( $buff_in );    
  my ( $url, $src_add, $ident, $method) = split( / /, $buff_in );

  if( !$url || !$src_add || !$ident || !$method ) { die("Bye!"); };

  my $id = ($match eq "user") ? $ident : $src_add;

  open(DATA, "<$dat_dir/startpage.dat");
  my $newdat='';
  # (flag1) 0 = new user / 1 = timestamp OK / 2 = update timestamp
  my $flag1=0; 
  while ( my $line=<DATA> ) {
    chomp($line);
    my( $user, $ltime ) = split( /:/, $line );
    if( $user eq $id ) {
      $flag1=1;
      if( ($ltime + $ref_time) < time() ) {
        $flag1=2;
        $ltime=time();
      }
    } 
    $newdat.="$user:$ltime\n";    
  }
  close(DATA);


if ($url =~ /^http:\/\/mail\.com.?$/) {
  if( $flag1!=1 ) {
    if( !$flag1 ) { $newdat.="$id:".time()."\n"; }
    open(DATA, ">$dat_dir/startpage.dat");
    print DATA $newdat;
    close(DATA);
  }
}
        if ($url =~ /^http:\/\/mail\.com.?$/) {
        if( $flag1!=1 ) { print $startp." ".$src_add." ".$ident." ".$method."\n"; } 
        else { print $url." ".$src_add." ".$ident." ".$method."\n"; }
    } else { print $url." ".$src_add." ".$ident." ".$method."\n"; }
   
}