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
signature.asc
Description: OpenPGP digital signature
