Squid URL_rewriter_program with Perl

Basic perl program for squid’s url_rewriter_program. You can add db connection for adapting blacklist or whitelist (text based or md5 based based,or etc.). Disable text logging!. My squid was   configured with Tproxy (not chaining client ip address).

Add this line to squid conf.

url_rewrite_program /usr/local/squid/perl_rewriter.pl

#!/usr/bin/perl -w

use strict;
use warnings;
use Digest::MD5 qw(md5 md5_hex md5_base64);

open(my $log, “>”, “/usr/local/squid/var/logs/url.log.txt”) || die(“Cannot open output file: $!”);
select $log; $| = 1;
select STDOUT; $| = 1;
while (<>)
my @parts = split;
my $url = $parts[0];
my $requestingip = $parts[1];
my $requestmethod = $parts[3];
my $now = localtime;
my($scheme, $authority, $path, $query, $fragment) = $url =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|;
my $md5_authority = md5_hex( $authority );
print $log “$now: URL:$url AUTHORITY:$authority MD5_AUTHORITY:$md5_authority REQIP:$requestingip REQMETHOD:$requestmethod\n”;
## can be redirected to any page but its good to tell the browser with 302 or you can just rewrite the url with squid’s error page etc
print “$url\n”;