Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Following the instructions in Best way to cache apt downloads on a LAN?, I've set up a caching proxy in my local network. Since that machine is not always up, I'd like to be able to refresh the sources list and install packages without using that proxy if unavailable.

I've already read the Acquire group section in the manual page of apt.conf(5), but I couldn't find an option like "Silent-Fail".

At the moment, sudo apt-get update and related commands fail because no connection could be established. So how do I configure the client so that the proxy is ignored if it's not available?

share|improve this question
    
Are you using the zeroconf discovery or do you manually set the proxy on each client? –  Jorge Castro Jul 17 '11 at 20:19
    
I'm manually setting the proxy because of possible firewalls / disabled broadcasting. –  Lekensteyn Jul 17 '11 at 20:25

1 Answer 1

up vote 13 down vote accepted
+50

There is an undocumented setting, Acquire::http::ProxyAutoDetect. This setting should contains the full path to the binary and cannot have arguments. The command should output the proxy to be used (example: http://10.0.0.1:8000).

Given the above information, a script could be created that tries a proxy before setting it. If no proxy is available, a direct connection should be used.

Below is such a proxy detection script which tries the http://10.0.0.1:8000/ and http://10.0.0.2:8000 proxies.

Put the code in /etc/apt/detect-http-proxy:

#!/bin/bash
# detect-http-proxy - Returns a HTTP proxy which is available for use

# Author: Lekensteyn <lekensteyn@gmail.com>

# Supported since APT 0.7.25.3ubuntu1 (Lucid) and 0.7.26~exp1 (Debian Squeeze)
# Unsupported: Ubuntu Karmic and before, Debian Lenny and before

# Put this file in /etc/apt/detect-http-proxy and create and add the below
# configuration in /etc/apt/apt.conf.d/30detectproxy
#    Acquire::http::ProxyAutoDetect "/etc/apt/detect-http-proxy";

# APT calls this script for each host that should be connected to. Therefore
# you may see the proxy messages multiple times (LP 814130). If you find this
# annoying and wish to disable these messages, set show_proxy_messages to 0
show_proxy_messages=1

# on or more proxies can be specified. Note that each will introduce a routing
# delay and therefore its recommended to put the proxy which is most likely to
# be available on the top. If no proxy is available, a direct connection will
# be used
try_proxies=(
10.0.0.1:8000
10.0.0.2:8000
)

print_msg() {
    # \x0d clears the line so [Working] is hidden
    [ "$show_proxy_messages" = 1 ] && printf '\x0d%s\n' "$1" >&2
}

for proxy in "${try_proxies[@]}"; do
    # if the host machine / proxy is reachable...
    if nc -z ${proxy/:/ }; then
        proxy=http://$proxy
        print_msg "Proxy that will be used: $proxy"
        echo "$proxy"
        exit
    fi
done
print_msg "No proxy will be used"

# Workaround for Launchpad bug 654393 so it works with Debian Squeeze (<0.8.11)
echo DIRECT

Now, APT must be configured to use the above proxy detection script, so put the following code in /etc/apt/apt.conf.d/30detectproxy:

# Fail immediately if a file could not be retrieved. Comment if you have a bad
# Internet connection
Acquire::Retries 0;

# undocumented feature which was found in the source. It should be an absolute
# path to the program, no arguments are allowed. stdout contains the proxy
# server, stderr is shown (in stderr) but ignored by APT
Acquire::http::ProxyAutoDetect "/etc/apt/detect-http-proxy";

I've also put the next code to the file to prevent some host from being proxified.

# Override the default proxy, DIRECT causes a direct connection to be used
Acquire::http::Proxy {
    deb.opera.com DIRECT;
    dl.google.com DIRECT;
};

By default, the script outputs whether a proxy is used or not. To disable that, edit /etc/apt/detect-http-proxy and change show_proxy_messages=1 to show_proxy_messages=0.

share|improve this answer
    
Lekensteyn, could I have your permission to paste that into puppet-apt-cacher-ng as an option to apt-cacher-ng::client? –  Garth Kidd Apr 4 '12 at 12:04
1  
Permission granted, do whatever you want with it :) –  Lekensteyn Apr 4 '12 at 12:28
1  
I'm very happy I found this answer when I set up apt-cacher-ng a while back. I have apt-cacher-ng installed on one of my home-computers (192.168.0.2). The script felt a bit overkill for my case, so I wrote a simplified detect-http-proxy: if nc -w1 -z 192.168.0.2 3142; then printf http://192.168.0.2:3142; else printf DIRECT; fi. Let's just hope the undocumented feature gets removed :) –  geirha Apr 21 '12 at 17:28
1  
I documented the script extensively for your benefit, if you care about size you can make it even shorter with i=192.168.0.2;nc -zw1 $i 3142&&echo http://$i:3142/||echo DIRECT :p –  Lekensteyn Apr 21 '12 at 20:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.