-
-
Save filviu/07103fd18b5441b2f7db to your computer and use it in GitHub Desktop.
# /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"; | |
# Override the default proxy, DIRECT causes a direct connection to be used | |
Acquire::http::Proxy { | |
deb.opera.com DIRECT; | |
dl.google.com DIRECT; | |
}; |
#!/bin/bash | |
# - Returns a HTTP proxy which is available for use | |
# Author: Lekensteyn <[email protected]> | |
# 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 /usr/local/bin/detect-http-proxy.shy 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 |
Note: In some cases (I noticed this on bionic
) if the first entry in try_proxies
does not exist, the script will hang, ProxyAutoDetect
will (probably) time out and give a E: Sub-process ProxyAutoDetect returned an error code (100)
.
My solution was to add a -w
timeout to nc
, so the command does not hang forever. The only con of this is, if your proxy is really slow for some reason, it will be treated as non-existing, but probably if it cannot respond in for example 3 secs to nc
it does not worth using it...
Note: In some cases (I noticed this on
bionic
) if the first entry intry_proxies
does not exist, the script will hang,ProxyAutoDetect
will (probably) time out and give aE: Sub-process ProxyAutoDetect returned an error code (100)
.
My solution was to add a-w
timeout tonc
, so the command does not hang forever. The only con of this is, if your proxy is really slow for some reason, it will be treated as non-existing, but probably if it cannot respond in for example 3 secs tonc
it does not worth using it...
Thanks for tip I was trying figure out how to set the timeout, thanks again and thanks for the creator, nice script.
n.b. that apt option is now deprecated - I've added an updated answer to the askubuntu question which you linked to above...