Setting Your Apt Proxy Depending on the Current Network

Setting Your Apt Proxy Depending on the Current Network

Dec 4, 2014
Linux, Debian, Ubuntu, Apt, Proxy, Netcat

Before I changed over to using squid3 for my transparent proxy I was using apt-cacher-ng to proxy my debian and kubuntu repositories. The issue I had was that at work there was no apt-cacher-ng proxy, just a transparent squid proxy. Remembering to change the details between home and work became too hard so I went in search of a solution. I really battled to find one, but did eventually come upon this article.

Note: The details below are stolen shamelessly from the article mentioned above so all credit must go to Claudio Mignanti…

I ended up with my 02proxy file in /etc/apt/apt.conf.d/ looking as follows:

Acquire::http::ProxyAutoDetect "/etc/apt/check_for_proxy.sh";

and the check_for_proxy.sh script in /etc/apt looking as follows:

#!/bin/bash

/bin/nc -z <ip of proxy server> <port of proxy server> && echo http://<ip of proxy server>:<port of proxy server>

The check_for_proxy.sh file needs to be executable.

chmod 755 /etc/apt/check_for_proxy.sh

When apt goes to look for a proxy it tries to connect using netcat to the ip and port specified. If it’s successfuly it returns the http string for the proxy. If it isn’t it returns nothing and the proxy setting is bypassed. Pretty neat.

Notes:

  • You need to have netcat installed which is in the netcat-openbsd package on both Debian and Ubuntu.
  • The ProxyAutoDetect option, as Claudio mentions, is an undocumented option of apt.