APT and HTTP redirects

Recently, I was issued with a rather nice Thinkpad X230 for my new work laptop. Not being a huge fan of Windows 8, I decided to go for Debian as my operating system. And this presented me with a problem: my company has a local mirror of the Debian archive which I’d like to use in my /etc/apt/sources.list, as when I’m at the office, I have a fast network connection to it. But if I configure things that way, I’m out of luck outside the office if I want to install or update packages, as the private mirror isn’t visible to other networks.

Inspired by the Raspberry Pi’s mirror director (which takes advantage of the way APT follows HTTP redirects), I put together a solution to redirect me to the right mirror depending on where I am (based on a fairly naive reverse DNS lookup). Note that when you don’t have a private mirror around, you can use http.debian.net to apply the same redirection trick to point you at a nearby mirror based on geolocation of your IP address, etc.

It’s been running for a couple of weeks for my laptop’s use and seems to work. The only disappointment is that apt-get doesn’t print out the redirect chain, so you have to take it on faith that you really have been directed to the right place (of course, you can get a good idea of that by hitting up the redirector URL in a browser).

See Bitbucket for the code and deployment instructions. Let me know if you’re using it!