Tuesday, March 1, 2011

Squid on OSX: inverting images with redirect by content modification

Squid: inverting images with redirect by content modification.



I came across this script to invert images using Squid proxy, and I thought it would be a fun April fool joke for my family.

To get it working had a few dependencies:


0. Install XCode!
  1. Install or compile ImageMagick to do the manipulation of the images.
  2. Enable web serving to serve the inverted images from the Mac running Squid
  3. Modify the script to fetch the images and invert them because we need to also do this for png files, also the example is for linux and this is for OSX
  4. Modify squid.conf to use the redirector
  5. Bonus points for a CRON script that deletes the image files regularly

1. Follow the link above to install ImageMagick

2. To enable web sharing, go to System Preferences > Sharing and click the box next to Web Sharing.



The, I went into my home folder and found the Sites folder. This is where personal web sites are hosted. I created a folder call UpsideDown, which is where I download the images to, invert them, and the serve them back to the client.

I discovered that Squid, if it uses upstream proxy, which I do, likes to fetch from FQDN. Since I already run local DNS, I added an entry to the proxy, and made sure that the server was on the 'direct' list.

At this point, images in UpsideDown are being served by the URL

    http://server.domain.com/~user/UpsideDown/image.jpg


3. Next the script. First I added a section to handle png images such as the Google logo. Then I fixed up the paths.

Big thing! Squid runs as 'nobody' which means the images are downloaded and inverted as nobody. But the web server serves as the user. So thats why you need the 'chmod' to add everyone to be able to read the images.

    #!/usr/bin/perl
    $|=1;
    $count = 0;
    $pid = $$;
    while (<>) {
    chomp $_;
    if ($_ =~ /(.*\.jpg)/i) {
    $url = $1;
    system("/usr/local/bin/wget", "-q", "-O","/Users/user/Sites/UpsideDown/$pid-$count.jpg", "$url");
    system("/usr/local/bin/mogrify", "-flip","/Users/user/Sites/UpsideDown/$pid-$count.jpg");
    system("/bin/chmod", "774", "/Users/user/Sites/UpsideDown/$pid-$count.jpg");
    print "http://server.domain.com/~user/UpsideDown/$pid-$count.jpg\n";
    }

    elsif ($_ =~ /(.*\.gif)/i) {
    $url = $1;
    system("/usr/local/bin/wget", "-q", "-O","/Users/user/Sites/UpsideDown/$pid-$count.gif", "$url");
    system("/usr/local/bin/mogrify", "-flip","/Users/user/Sites/UpsideDown/$pid-$count.gif");
    system("/bin/chmod", "774", "/Users/user/Sites/UpsideDown/$pid-$count.gif");
    print "http://server.domain.com/~user/UpsideDown/$pid-$count.gif\n";

    }

    elsif ($_ =~ /(.*\.png)/i) {
    $url = $1;
    system("/usr/local/bin/wget", "-q", "-O","/Users/user/Sites/UpsideDown/$pid-$count.png", "$url");
    system("/usr/local/bin/mogrify", "-flip","/Users/user/Sites/UpsideDown/$pid-$count.png");
    system("/bin/chmod", "774", "/Users/user/Sites/UpsideDown/$pid-$count.png");
    print "http://server.domain.com/~user/UpsideDown/$pid-$count.png\n";

    }

    else {
    print "$_\n";;
    }
    $count++;
    }



When the script is done, save it as, say, redir.pl, and give it permissions:

    chmod o+x redir.pl

4. Modify squid. Quite straight forward, you need to add the line

    url_rewrite_program /usr/local/squid/etc/redir.pl

changing the path to suit where your redirect script is.


One thing to note is that not all images you get on the internet are .jpg etc. Google often serve in different formats.


5. You'll need to control the disk space used if this runs for any length of time. So create a script to delete the files in the UpsideDown directory:

    rm ~/Sites/UpsideDown/*

and then make a CRON job to run it regularly. I use CronniX.

No comments: