Cover V12, I02
feb2003.tar

Listing 1 Redirection script

Adfilter.pl

!/usr/bin/perl   
#################################################################
# adfilter
# Author:      Dustin Anders
# E-mail:    dustin@unixfun.com
# Description:  With the use of the hosts file provided to me
#        by www.smartin-designs.com/hosts_info.htm
#               and a regular expression filter file, this script
#        will filter out ads from webpages.  The script will
#        redirect the requests to a site provided in the filter
#        files.
#################################################################  

$filter_dir="/usr/local/squid/adfilter";
$hosts_file=$filter_dir."/hosts";
$filters_file=$filter_dir."/filters";
$log=$filter_dir."/log";

$counter=0;

# Filters structure
$filters = {
    $expression => "",
    $host => ""
};

$|=1;

# Load filters in memory
&loadfilters();

# Loop as a child in squid and wait for urls
while (<>) {
 
          chop;   
          ($url, @junk) = /(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/;

    # Check to make sure the url is for an image - these seem to be
    # the most common formats for banners
    if ($url =~ /.jpg|.png|.gif/i) {
        # Loop through filter array
        for ($i=0; $i<$counter; $i++) {
                        # Check to see if host or filter is specified
                        if ($url =~ /$filters[$i]{expression}/i) {
                # An ad has been found -- send host instead
                                print "http://".$filters[$i]{host}."/";
                
                # Terminate filter search
                last;
                        }
        }
    }
    print "\n";
}

#################################################################  
# loadfilters
# Description:  Loads filters into filters structure
#################################################################  
sub loadfilters {

    # Load regular expressions first then hosts to improve hit time
    $files[0]=$filters_file;
    $files[1]=$hosts_file;

    # Load files
    foreach $file (@files) {

            open (FILE, "<".$file);
            while (<FILE>) {
            $line=$_;
            $line =~ s/\r\n|\n//g;    
            
            # Ignore comments and blank lines
                    if ($line =~ /^#/ || $line eq "") {
                            # Do nothing - comment
                    } else {
                # Found entry -- load into struct
                            ($filters[$counter]{host},$filters[$counter] \
                             {expression}) = split(/\s+/, $line);
                $counter=$counter+1;
            }
        }
        close (FILE);
    }
}