Saturday, April 6, 2013

Faster browsing aka Google Redirect Rewrite

Image courtesy of Open Clip Art Library
Once again I got annoyed by having to wait for Google to redirect me. I also think it's none of Google's business to know which sites I do visit especially if they can't be quick about it. So I decided to get rid of the delay.
I checked if there's a plugin for that and it seems there is. You can install Remove Google Redirects from Chrome Web Store. Being a paranoid weirdo like I am that still wasn't enough. I already had a Squid proxy that I had configured to make it so that you couldn't even know that some websites were blocked by my ISP(Sonera). It made sense to try and figure out if there was a trick that I could do with squid that allows me to get rid of the middle man(Google) in the redirect process.

And behold for the creators of Squid have indeed been so wise as to add a way to mangle the urls. All I had to do was to write a script that takes the url, checks if it's a google redirect url and if so, parses the url to get the actual url where we want to go and return that. The example on Squid feature page was a good place to start and this is what I came up with:

Perl Version


use URI;
use URI::QueryParam;

while (<>) {
    @X = split;
    $url = $X[1];
    #check if this is a google redirect url
    if ($url =~ /\/\/.*\.google\.[^\/]+\/url/) {
        my $uri = URI->new($url);
        $url = $uri->query_param("url");
        print $X[0]." 302:$url\n";
    } else {
        print $X[0]." \n";
I had to install a couple of Perl modules while I was trying to refamiliarize myself with Perl so I wanted make a PHP version of the same helper. With PHP I wouldn't have to install any extra modules when some day I decide to use this on some other machine. I usually have PHP installed everywhere.

PHP Version


function convertUrlQuery($query) {
    $queryParts = explode('&', $query);

    $params = array();
    foreach ($queryParts as $param) {
        $item = explode('=', $param);
        $params[$item[0]] = $item[1];

    return $params;
    $line = trim(fgets(STDIN)); // reads one line from STDIN
    $params = explode(" ", $line);
    $pattern = '/\/\/.*\.google\.[^\/]+\/url/';

    if (preg_match($pattern, $params[1], $matches, PREG_OFFSET_CAPTURE, 3)) {
        $parts = parse_url($params[1]);
        $query = convertUrlQuery($parts['query']);
        $url = urldecode($query['url']);
        echo $params[0]." 302:$url\n";
    } else {
        echo $params[0]." \n";

In the end I like that with Perl I didn't have to write any functions for simple things like url parsing, but unless I package this as an installable package I could not just drop it in and expect it to work since I had to install the extra stuff as modules.(Yes I could have written my own implementation, but I'm not that much into reinventing the wheel. Also I was a bit impatient to get the script ready so I could see the results) With the PHP version I could just drop it in and as long as I had PHP installed it would work.

I strongly recommend adding a line in squid.conf It really makes a difference.

url_rewrite_program /path/to/googleredirectrewriter

No comments:

Tip me if you like what you're reading