Re: [squid-users] Transparent Proxy w/ User ID's

From: Hans Juergen von Lengerke <lengerkeh@dont-contact.us>
Date: Wed, 3 Jul 2002 13:22:29 +0200 (CEST)

[WiZKiD] <wizkid@wizkid.org> on Jul 2, 2002:

> #!/usr/bin/perl
> $|=1;
>
> $accessfile = "/usr/local/squid/etc/ipguard.acc";
>
> while(<>) {
> $a = $_;
> #$u = URL $h = HOST $usr = USER/FQDN $met = METHOD
> ($u,$h,$usr,$met) = split(/ /,$a);
> ($ip,$fqdn) = split(/\//,$h);
>
> open (FILE,"<$accessfile");
> @a = <FILE>;
> close(FILE);
>
> foreach $a (@a) {
> chomp($a);
> ($alip,$alusr) = split(/:/,$a);
> if ($alip eq $ip) { $usr = $alusr; }
> }
>
> print STDOUT "$u $ip/$fqdn $usr $met\r";
> }

You don't have to read $accessfile for every request. Just check mtime
for every request and if it changes, recache $accessfile. Like this,
Perl may well be fast enough for what you want. I'd certainly give it a
try. Beware, code below is untested!

#!/usr/bin/perl
$|=1;

$accessfile = "/usr/local/squid/etc/ipguard.acc";
%accesscache = ();
$mtime = 0;

while(<>) {
         $a = $_;
         #$u = URL $h = HOST $usr = USER/FQDN $met = METHOD
         ($u,$h,$usr,$met) = split(/ /,$a);
         ($ip,$fqdn) = split(/\//,$h);

         $mtime = &cache_access_file() if (stat($accessfile))[9] <> $mtime;
         $usr = $accessfile{$ip} if exists $accessfile{$ip};

         print STDOUT "$u $ip/$fqdn $usr $met\r";
}

sub cache_access_file {
         %accesscache = (); # clear old cache
         open (FILE, "<$accessfile");
         while (<FILE>) {
                 my ($alip,$alusr) = split(/:/,$a);
                 $accesscache{$alip} = $alusr; # Assuming $alips are unique
         }
         close FILE;
         return stat($accessfile))[9];
}
Received on Wed Jul 03 2002 - 05:22:56 MDT

This archive was generated by hypermail pre-2.1.9 : Tue Dec 09 2003 - 17:09:00 MST