[squid-users] Weighted redundant proxy auto-config script.

From: Peter Smith <peter.smith@dont-contact.us>
Date: Mon, 02 Aug 2004 11:54:43 -0500

For those who are interested, I have crafted a dynamic
"Auto-Configuration" script for use in Apache using Perl. Based on a
random number, it will provide a user with a listing of proxies which
may be weighted to determine their allocation.

Please see http://www.squid-cache.org/Doc/FAQ/FAQ-5.html#ss5.4

My script consists, basically, of two files--"proxy.pac.pl" and "proxy";
the names are mostly arbitrary.

proxy.pac.pl:
#!/usr/bin/perl

if( $ENV{ "SCRIPT_NAME" } =~ /\.pac$/ ) {
 print "Content-type: application/x-ns-proxy-autoconfig\n";
} else {
 print "Content-type: text/html\n";
}

print "\n";

my %proxy = (), $total = 0;

open A, "<proxy" or die "Can not access proxy list.\n";

while( <A> ) {
 chomp;
 /^(\S+)\s+(\S+)$/;
 $proxy{ $1 } = $2;
}

foreach $p ( sort keys %proxy ) {
 $total += $proxy{ $p };
}

$val = rand() * $total;
$last = 0;

foreach $p ( sort keys %proxy ) {
 if( $val < ( $proxy{ $p } + $last ) && $val >=$last ) {
  $primary=$p;
  push @list, $p;
} elsif( $val < ( $proxy{ $p } + $last ) ) {
  push @list, $p;
 } elsif( $val >=$last ) {
  push @tail, $p;
 }
 $last += $proxy{ $p };
}

for( $i=0; $i<=$#tail; $i++ ) {
 push @list, $tail[ $i ];
}

print '
function FindProxyForURL(url, host)
    {
        if (isPlainHostName(host) ||
             dnsDomainIs(host, ".domain.com") &&
             !localHostOrDomainIs(host, "www.domain.com"))
            return "DIRECT";
        else
';
print ' return "';
for( $i=0; $i<=$#list; $i++ ) {
 print "PROXY ".$list[ $i ].":3128; "
}

print '
DIRECT";
}
';

proxy:
proxy1.domain.com:8080 25
proxy2.domain.com:8081 75

The 'proxy' file simply contains proxy entries with a weight seperated
by a space. The weight value is arbitrary, the weights are added
together to determine the value to be randomized against.

I hope this proves useful to someone. As has been mentioned before
there are many other scripts for doing proxy selection. Please see
http://www.squid-cache.org/Doc/FAQ/FAQ-5.html#ss5.5

Peter

Received on Mon Aug 02 2004 - 10:54:48 MDT

This archive was generated by hypermail pre-2.1.9 : Wed Sep 01 2004 - 12:00:01 MDT