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!
- Install or compile ImageMagick to do the manipulation of the images.
- Enable web serving to serve the inverted images from the Mac running Squid
- 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
- Modify squid.conf to use the redirector
- 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:
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:
and then make a CRON job to run it regularly. I use CronniX.
Instructions from matschaffer.com, reproduced here:
First get a copy of libjpeg and build it as a shared library:
curl -O http://www.ijg.org/files/jpegsrc.v7.tar.gz
tar -zxf jpegsrc.v7.tar.gz
cd jpeg-7/
ln -s `which glibtool` ./libtool
./configure --enable-shared --prefix=/usr/local
make
sudo make install
Now get a copy of ImageMagick:
curl -O ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz
tar -zxf ImageMagick.tar.gz
cd ImageMagick-*/
./configure --prefix=/usr/local
make
sudo make install
Don't forget you need XCode installed.
In a burst of enthusiasm I decided to try to build and compile Squid on OSX.
First you need XCode and developer tools from either your DVD or the Apple web site.
Then download the source code from squid-cache.org. Current version is 3.1.10 released 22 Dec 2010. Current version are here. I downloaded the bz2, any will work. I moved it to the XCode folder on my hard disk, in the path
/XCode/squid
The run these commands:tar xjf squid-3.1.10.tar.bz2cd squid-3.1.10./configure --enable-sslmakesudo make installSome notes:- Don't have any spaces in the path to the source code else the build WILL fail!
- The only build option I used is to enable SSL so I can proxy https. For a full list of whats available you can do
./configure --help
for ease of use you can pipe this to a text file for reading:
./configure --help >build_options.txt
- It can take some time, do not worry
- You will get some errors, do not worry, as long as there is no space in the path!
- If you do get errors, likely XCode needs re-installing
This builds squid and put in in the default location which is/usr/local/squidIt also has the default modules, so you can play.
Next, chown the Squid's directory so it has write access to its logs and caches:
sudo chown -R nobody:wheel /usr/local/squidThen you need to create the cache directories:
sudo /usr/local/squid/sbin/squid -zThe default config file works OK! The only out of the box change I would make is to remove the image from the error file. In the file
/usr/local/squid/etc/errorpage.css
find the line
"background: url('http://www.squid-cache.org/Artwork/SN.png') no-repeat left;"
and delete it. Save the file.
Now, start Squid:
sudo /usr/local/squid/sbin/squidYou can point you local machine to 127.0.0.1:3128 and show its working.
if you decide to enable the cache, and then do
sudo /usr/local/squid/sbin/squid -k reconfigure
you might get
"2011/01/15 14:44:08| WARNING cache_mem is larger than total disk cache space!"
do not worry, its a spurious error. Things will still work OK.
See some other postings on Squid and OSX.
If you use Squid, you'll know that for the longest time it used Unix epoch system for its logs. There are various ways to convert to readable form such as using SARG, though thats not very elegant. With v2 there was an option to log in regular httpd format, by using the option "emulate_httpd_log", but that no longer works.
Now with Squid v3.x, you can choose your logging format, and you can log different detail to different logs. Great!
In your squid.conf file, add the following:
# log formats
access_log /usr/local/squid/var/logs/access.log squid
access_log /usr/local/squid/var/logs/access-GMTtime.log customformat
logformat customformat %tg.%03tu response:%tr %>a server:%Hs size:%
The line "# log formats" just tells the reader of the config file, most likely you but you never know, that this bit is to set log file options.
The next line tells squid to log in the old Squid manner, and default details.
The third line tells squid that you want to do something custom: we tell squid to also log to an additional log "access-localtime.log".
The final line defines the options we want. My example uses options chosen pretty much the old defaults details, but using GMT for human readable date / time, and I added some of the new options. For a full list consult the squid manual.
I needed to create a file and folder listing to check for duplicate and incorrectly named photos and other files.
Since OSX is based on unix, you can use the 'ls' command in terminal. Navigate to where you want to start from:
and then run
ls -R > picture.listing.txt
This command moves through all the folders within 'nikon' and sends the name of each folder and file to the file picture.listing.txt.
Well I have wondered, since its discussed on many Hackintosh sites. Well here it is:
DSDT = Differentiated System Description Table
The Galaxy Tab has a native screen resolution of 1024 x 600, and to have a wallpaper size that is correct for both orientations requires 1024 pixels wide x 1200 pixels high.
This Tabloid is different to previous Android devices in that the home screen autorotates even though there is no physical keyboard (like the G1).
Also the home screen uses the left hand side of the wallpaper by default. With this in mind I've created a new wallpaper template for the Galaxy Tab which is in Photoshop format. Get it here.
Also I've created some wallpapers which you can get / view www.fire-horse.co.uk/Galaxy_Tab_Wallpapers/index.html. Just so you know, not all the wallpapers are 100% safe for work ;-)
PS Just like setting wallpapers on other Android devices, the built in tool does weird stuff, so you'll need the free App from the Marketplace called "Wallpaper Set & Save".
More Galaxy Tab fun here.