|
13 | 13 | # + cache leading url not full, as i get called for all packages also. |
14 | 14 | # v5 ... learn working in general and use them for new urls first |
15 | 15 | # + limit wget check to 10 bytes |
| 16 | +# v6 ... add default proxies |
16 | 17 |
|
17 | 18 |
|
18 | 19 | # defaults |
| 20 | +defaults_location="/usr/local/etc/apt-proxy-detect.bash" |
19 | 21 | service_name="_apt_proxy._tcp" |
20 | 22 | cache_file_name=".apt-proxy-detect.$(id -un)" |
21 | 23 | cache_file_none_retry_timeout=60 |
22 | | -declare -i cache_age=0 |
23 | 24 |
|
| 25 | +# help |
| 26 | +if [ -z "$1" ] |
| 27 | +then |
| 28 | + echo "" |
| 29 | + echo "usage: $0 <defaults|set-default|url for proxy>" |
| 30 | + echo "" |
| 31 | + echo " defaults ... list default proxies" |
| 32 | + echo " set-default <proxy1>,<proxy2> ... set default proxies" |
| 33 | + echo " url for proxy ... this is the url a proxy is tested for" |
| 34 | + echo "" |
| 35 | + exit 1 |
| 36 | +fi |
| 37 | + |
| 38 | +# service function |
| 39 | +if [ "$1" = "set-default" ] |
| 40 | +then |
| 41 | + declare -A WORKING_PROXIES |
| 42 | + echo "defining default proxies..." |
| 43 | + now=$(date +%s) |
| 44 | + for proxy in ${2//,/ } |
| 45 | + do |
| 46 | + echo "proxy: ${proxy}" |
| 47 | + WORKING_PROXIES["${proxy}"]=${now} |
| 48 | + done |
| 49 | + if [ -w "${defaults_location%/*}" ] |
| 50 | + then |
| 51 | + echo "saved." |
| 52 | + declare -p WORKING_PROXIES > "${defaults_location}" |
| 53 | + else |
| 54 | + echo "can not save under: ${defaults_location}" |
| 55 | + fi |
| 56 | + exit 0 |
| 57 | +fi |
| 58 | + |
| 59 | +if [ "$1" = "defaults" ] |
| 60 | +then |
| 61 | + declare -A WORKING_PROXIES |
| 62 | + if [ -r "${defaults_location}" ] |
| 63 | + then |
| 64 | + # shellcheck disable=SC1090 |
| 65 | + if source "${defaults_location}" |
| 66 | + then |
| 67 | + for proxy in "${!WORKING_PROXIES[@]}" |
| 68 | + do |
| 69 | + echo "proxy: ${proxy}" |
| 70 | + done |
| 71 | + else |
| 72 | + echo "error reading default, please delete ${defaults_location}" |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | + else |
| 76 | + echo "no defaults found." |
| 77 | + fi |
| 78 | + exit 0 |
| 79 | +fi |
| 80 | + |
| 81 | +# runtime |
| 82 | +declare -i cache_age=0 |
24 | 83 | declare -A WORKING_PROXIES |
25 | 84 | declare -A CACHED_PROXIES |
26 | 85 | declare -A CACHED_PROXIES_AGE |
27 | 86 |
|
28 | 87 | declare -i debug |
29 | 88 | [ -z "${DEBUG_APT_PROXY_DETECT}" ] && debug=0 || debug=1 |
30 | 89 |
|
| 90 | +function error() { |
| 91 | + printf "[%12s][%4s]: ERROR: %s\n" "$1" "$(( ($(date +%s%N) - start_time) / 1000000 ))" "$2" >&2 |
| 92 | +} |
| 93 | + |
31 | 94 | if [ $debug -eq 0 ] |
32 | 95 | then |
33 | 96 | function debug() { |
|
41 | 104 | } |
42 | 105 | fi |
43 | 106 | debug "INFO" "===--- apt-proxy-detect ---===" |
| 107 | +# source defaults |
| 108 | +if [ -r "${defaults_location}" ] |
| 109 | +then |
| 110 | + # shellcheck disable=SC1090 |
| 111 | + if source "${defaults_location}" |
| 112 | + then |
| 113 | + debug "DEFAULTS" "loaded from ${defaults_location}" |
| 114 | + else |
| 115 | + error "DEFAULTS" "failed from ${defaults_location} please fix it." |
| 116 | + exit 1 |
| 117 | + fi |
| 118 | +fi |
44 | 119 |
|
45 | 120 | function check_proxy() { |
46 | 121 | if [ "$1" == "NONE" ] |
|
0 commit comments