View RSS Feed

Duke5A

Squid Proxy and Youtube for Schools

Rate this Entry
by , 14th December 2012 at 05:24 PM (9394 Views)
Getting Youtube for Schools to work is something that is either easy, or an extreme PITA; which is entirely dependent upon what you use for a proxy. If you're reading this, then you know Squid falls in the ladder of those two scenarios.

There are two ways to go about doing it: adding a custom HTTP header, or rewriting the URL on the fly. The cleanest way is using the custom HTTP header, but as of Squid 3.1 it doesn't natively support adding custom headers. You can only change existing ones. To do this you need to setup an ICAP server and it seemed a bit overkill for this simple task, so I elected to go for the URL rewriting. The basic gist of this is that your school's filter ID needs to be appended onto the end of every URL. This is accomplished by means of either a perl or PHP script that Squid calls out to do the rewrite. Seeing as how I'm a VB monkey and couldn't wrap my head around Perl in the time allotted for this task so I cheated.

Following Youtube's guidelines I wrote the script in VB to give me the desired output and used a converter to go to Perl. It needed some tweaks, but it seems to work. Just replace the filter ID with your own in the script and you should be set.

Code:
#!/usr/bin/perl
$|=1;

$strSchoolID = 'edufilter=xxxxxxxxxxxxxxxxxx';

while (<>) {
	@X = split;
	$strURL = $X[0];

if ((index($strURL, 'youtube.com') + 1)) {
    if ((index($strURL, 'edufilter') + 1)) {
        print $strURL, "\n";
    }
    elsif ((index($strURL, '.css') + 1)) {
        print $strURL, "\n";
    }
    elsif ((index($strURL, '.gif') + 1)) {
        print $strURL, "\n";
    }
    elsif ((index($strURL, '.png') + 1)) {
        print $strURL, "\n";
    }
    elsif ((index($strURL, 'gif') + 1)) {
        print $strURL, "\n";
    }
    elsif ((index($strURL, '.js') + 1)) {
        print $strURL, "\n";
    }
    elsif ((index($strURL, '.xml') + 1)) {
        print $strURL, "\n";
    }
    elsif ((index($strURL, '?') + 1)) {
        $strURL = $strURL . '&' . $strSchoolID;
        print "$strURL\n";
    }
    else {
        $strURL = $strURL . '?' . $strSchoolID;
        print "$strURL\n";
    }
}
else {
    print "$strURL\n";
}
}
The Squid configuration should look something like this:

Code:
url_rewrite_program /etc/squid3/helper/urlrewrite3.pl
redirect_rewrites_host_header on
redirect_children 30
acl RedirectYoutube dstdomain .youtube.com
redirector_access allow RedirectYoutube
redirector_access deny all
By default Squid will pass every outgoing connection through the script, so I set an ACL to only pass connections going to Youtube. And don't forget to set execute permission on the script file.

I haven't pushed this out to an entire building yet, but it works with my test group so far. If you find any bugs with this drop me a line. I hope this helps!

Updated 14th December 2012 at 05:40 PM by Duke5A

Categories
Uncategorized

Comments

Trackbacks

Total Trackbacks 0
Trackback URL: