#!/bin/bash # I, Ian Kelling, follow the GNU license recommendations at # https://www.gnu.org/licenses/license-recommendations.en.html. They # recommend that small programs, < 300 lines, be licensed under the # Apache License 2.0. This file contains or is part of one or more small # programs. If a small program grows beyond 300 lines, I plan to switch # its license to GPL. # Copyright 2024 Ian Kelling # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # http://www.apache.org/licenses/LICENSE-2.0 # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # based on # http://askubuntu.com/questions/53443/how-do-i-ignore-a-proxy-if-not-available # altered because testing a down proxy takes like 2 seconds, # and we would do it like 100 times in a row. so cache a failed result # locally for 10 minutes. Also, using newer option based on man apt.conf. # # Once when I was testing, it seemed I needed to have it output # DIRECT after outputing the proxy url # note, the url we are trying to fetch is $1 # note, if there is a problem, some ways to debug: # first, # edit /etc/apt/apt.conf.d/02proxy # add, remove the autodetec # Acquire::http::proxy "http://CacheServerIp:3142"; # see the /var/log/apt-cacher-ng logs # read # file:///usr/share/doc/apt-cacher-ng/html/index.html proxy_host=faiserver proxy_port=3142 proxy_url=http://$proxy_host:$proxy_port/ f=/tmp/apt_proxy_fail if [[ ! -w /tmp ]]; then echo DIRECT exit 0 fi modtime=$(stat -c %Y $f 2>/dev/null ) ||: if [[ $modtime ]] && (( $(( EPOCHSECONDS - modtime )) < 60*10 )); then echo DIRECT exit 0 else rm -f $f fi if getent hosts $proxy_host &>/dev/null && timeout 1 nc -z $proxy_host $proxy_port &>/dev/null; then echo $proxy_url else if [[ ! -e $f || -w $f ]]; then touch $f fi fi