Upside Down Ternet

From ArchWiki

This article explains how to create a transparent Squid proxy server using imagemagick's mogrify to flip the images upside down.

Installation

Install the squid, apache, wget and imagemagick packages.

Configuration

Create flip.pl, place it in your /usr/local/bin folder and make it executable:

/usr/local/bin/flip.pl
#!/usr/bin/perl
$|=1;
$count = 0;
$pid = $$;
while (<>) {
       @splitted=split(/ /,$_);
       chomp $_;
       if ($_ =~ /(.*\.jpg)/i) {
               $url = $1;
               system("/usr/bin/wget", "-q", "-O","/srv/http/images/$pid-$count.jpg", "$url");
               system("/usr/bin/mogrify", "-flip","/srv/http/images/$pid-$count.jpg");
               print "http://127.0.0.1/images/$pid-$count.jpg\n";
       }
       elsif ($_ =~ /(.*\.gif)/i) {
               $url = $1;
               system("/usr/bin/wget", "-q", "-O","/srv/http/images/$pid-$count.gif", "$url");
               system("/usr/bin/mogrify", "-flip","/srv/http/images/$pid-$count.gif");
               print "http://127.0.0.1/images/$pid-$count.gif\n";
       }
       elsif ($_ =~ /(.*\.png)/i) {
               $url = $1;
               system("/usr/bin/wget", "-q", "-O","/srv/http/images/$pid-$count.png", "$url");
               system("/usr/bin/mogrify", "-flip","/srv/http/images/$pid-$count.png");
               print "http://127.0.0.1/images/$pid-$count.png\n";
       }
       else {
               print "$splitted[0]\n";
       }
       $count++;
}

Next, while not necessary, does clean up the Squid configuration file a lot making it easier on the eyes

# sed -i "/^#/d;/^ *$/d" /etc/squid/squid.conf

Now, edit your squid.conf file and append this to the bottom

squid.conf
url_rewrite_program /usr/local/bin/flip.pl

Also find the line for http_port and make it now read

squid.conf
http_port 3128 intercept

Finally, we have to create the folders for the images to be flipped in and set their permissions

The directory where the images are to be stored must be owned by the proxy user.

# mkdir /srv/http/images
# chown proxy:proxy /srv/http/images
# chmod 755 /srv/http/images

Finally, add the http user to the proxy group

# usermod -aG proxy http

Verify that the http user is a member of the proxy group

# groups http

or

# id -Gn http

Router setup

You will need to edit the firewall on your router or gateway to redirect HTTP traffic to your proxy.

Starting

Start/enable httpd.service and squid.service.

See also