various fixes
[distro-setup] / filesystem / etc / apt / detect-http-proxy
1 #!/bin/bash
2 # based on
3 # http://askubuntu.com/questions/53443/how-do-i-ignore-a-proxy-if-not-available
4 # altered because testing a down proxy takes like 2 seconds,
5 # and we would do it like 100 times in a row. so cache a failed result
6 # locally for 10 minutes. Also, using newer option based on man apt.conf.
7 #
8 # Once when I was testing, it seemed I needed to have it output
9 # DIRECT after outputing the proxy url
10
11 # note, the url we are trying to fetch is $1
12
13 # note, if there is a problem, some ways to debug:
14 # first,
15 # edit /etc/apt/apt.conf.d/02proxy
16 # add, remove the autodetec
17 # Acquire::http::proxy "http://CacheServerIp:3142";
18 # see the /var/log/apt-cacher-ng logs
19 # read
20 # file:///usr/share/doc/apt-cacher-ng/html/index.html
21
22 proxy_host=faiserver
23 proxy_port=3142
24 proxy_url=http://$proxy_host:$proxy_port/
25 f=/tmp/apt_proxy_fail
26 if [[ ! -w /tmp ]]; then
27 echo DIRECT
28 exit 0
29 fi
30 modtime=$(stat -c %Y $f 2>/dev/null ) ||:
31 if [[ $modtime ]] && (( $(( EPOCHSECONDS - modtime )) < 60*10 )); then
32 echo DIRECT
33 exit 0
34 else
35 rm -f $f
36 fi
37 if getent hosts $proxy_host &>/dev/null && timeout 1 nc -z $proxy_host $proxy_port &>/dev/null; then
38 echo $proxy_url
39 else
40 if [[ ! -e $f || -w $f ]]; then
41 touch $f
42 fi
43 fi