|
5 | 5 | # v1 ... pure detect |
6 | 6 | # v2 ... cache value with check |
7 | 7 | # v3 ... add persistant caching for found proxies. |
| 8 | +# v4 ... extend caching to also cache no-found proxy state |
| 9 | +# and proxy checking based on testurl |
| 10 | +# + debug reveals ms execution time |
| 11 | +# + timeout for check_proxy |
| 12 | +# + cache age per entry not per whole file. |
| 13 | +# + cache leading url not full, as i get called for all packages also. |
| 14 | + |
| 15 | + |
| 16 | +# defaults |
| 17 | +service_name="_apt_proxy._tcp" |
| 18 | +cache_file_name=".apt-proxy-detect.$(id -un)" |
| 19 | +cache_file_none_retry_timeout=60 |
| 20 | +declare -i cache_age=0 |
| 21 | + |
| 22 | +declare -A CACHED_PROXIES |
| 23 | +declare -A CACHED_PROXIES_AGE |
8 | 24 |
|
9 | 25 | declare -i debug |
10 | 26 | [ -z "${DEBUG_APT_PROXY_DETECT}" ] && debug=0 || debug=1 |
|
15 | 31 | : |
16 | 32 | } |
17 | 33 | else |
| 34 | + declare -i start_time=0 |
| 35 | + start_time=$(date +%s%N) |
18 | 36 | function debug() { |
19 | | - printf "[%10s]: %s\n" "$1" "$2" >&2 |
| 37 | + printf "[%12s][%4s]: %s\n" "$1" "$(( ($(date +%s%N) - start_time) / 1000000 ))" "$2" >&2 |
20 | 38 | } |
21 | 39 | fi |
22 | | -debug "INFO" "apt-proxy-detect" |
| 40 | +debug "INFO" "===--- apt-proxy-detect ---===" |
23 | 41 |
|
24 | 42 | function check_proxy() { |
25 | | - wget -e "http_proxy=$1" -e "https_proxy=$1" -qO - "$2" >> /dev/null |
26 | | - return $? |
| 43 | + if [ "$1" == "NONE" ] |
| 44 | + then |
| 45 | + if [ ${cache_age} -gt ${cache_file_none_retry_timeout} ] |
| 46 | + then |
| 47 | + debug "CHECK-PROXY" "NONE-cached expired" |
| 48 | + return 1 |
| 49 | + else |
| 50 | + debug "CHECK-PROXY" "NONE-cached" |
| 51 | + return 0 |
| 52 | + fi |
| 53 | + else |
| 54 | + debug "CHECK-PROXY" "Checking proxy (${1}) with testurl (${2})" |
| 55 | + if [ $debug -gt 1 ] |
| 56 | + then |
| 57 | + wget -v --tries=1 -T 1 -e "http_proxy=$1" -e "https_proxy=$1" -O - "$2" >&2 |
| 58 | + else |
| 59 | + wget -q --tries=1 -T 1 -e "http_proxy=$1" -e "https_proxy=$1" -O - "$2" >> /dev/null 2>&1 |
| 60 | + fi |
| 61 | + return $? |
| 62 | + fi |
27 | 63 | } |
28 | 64 |
|
29 | | -# cache file name |
30 | | -cache_file_name=".apt-proxy-detect.$(id -un)" |
31 | | - |
| 65 | +# --- cache location --- |
32 | 66 | # persistant cache file location |
33 | 67 | # $HOME or special for system accounts |
34 | 68 | declare -A CACHE_FILE_LOC |
|
46 | 80 | fi |
47 | 81 | fi |
48 | 82 |
|
49 | | -# check cache_file |
50 | | -touch "${cache_file}" >> /dev/null 2>&1 |
| 83 | +# check cache_file if not there create it. |
| 84 | +[ ! -e "${cache_file}" ] && touch "${cache_file}" >> /dev/null 2>&1 |
51 | 85 |
|
| 86 | +# check ownership of cache file and fetch age. |
52 | 87 | skip_cache=0 |
53 | 88 | if [ "$(stat -c %u "${cache_file}")" != "$(id -u)" ] |
54 | 89 | then |
55 | 90 | skip_cache=1 |
56 | 91 | echo "E: wrong owner of cache file ${cache_file}, remove or reown to $(id -un)" >&2 |
57 | 92 | fi |
58 | | -service_name="_apt_proxy._tcp" |
| 93 | + |
| 94 | +# eval testurl |
59 | 95 | [ -z "$1" ] && testurl="http://deb.debian.org/debian" || testurl="$1" |
60 | | -debug "TEST-URL" "URL: ${testurl}" |
| 96 | +debug "TEST-URL" "URL: ${testurl}" |
| 97 | +testurl_hash_url="$(echo "${testurl}" | cut -d/ -f1-3)" |
| 98 | +testurl_hash="$(echo "${testurl_hash_url}" | md5sum)" ; testurl_hash="${testurl_hash%% *}" |
| 99 | +debug "HASH" "HASH: ${testurl_hash} of (${testurl_hash_url})" |
61 | 100 |
|
| 101 | +# check out cached value |
| 102 | +now=$(date +%s) |
62 | 103 | if [ -s "${cache_file}" ] && [ ${skip_cache} -eq 0 ] |
63 | 104 | then |
64 | | - debug "CACHE" "using stored under: ${cache_file}" |
65 | | - proxy="$(cat "${cache_file}")" |
66 | | - debug "CHECK" "Checking cached proxy (${proxy}) with testurl (${testurl})" |
67 | | - if check_proxy "${proxy}" "${testurl}" |
| 105 | + # shellcheck disable=SC1090 |
| 106 | + source "${cache_file}" |
| 107 | + # shellcheck disable=SC2181 |
| 108 | + if [ $? -ne 0 ] |
68 | 109 | then |
69 | | - debug "WORKS" "give back cached proxy" |
70 | | - debug "PROXY" "return ${proxy}" |
71 | | - echo "${proxy}" |
72 | | - exit 0 |
73 | | - else |
74 | | - debug "FAILED" "remove cache file." |
| 110 | + # something is wrong with the cache file remove it. |
| 111 | + debug "CACHE" "invalid cachefile (cleanup): ${cache_file}" |
75 | 112 | rm -f "${cache_file}" |
| 113 | + else |
| 114 | + debug "CACHE" "using stored under: ${cache_file}" |
| 115 | + fi |
| 116 | + cache_age=$(( now - ${CACHED_PROXIES_AGE[${testurl_hash}]:=0} )) |
| 117 | + debug "CACHE-AGE" "age: ${cache_age} sec" |
| 118 | + proxy="${CACHED_PROXIES[${testurl_hash}]}" |
| 119 | + if [ -n "${proxy}" ] |
| 120 | + then |
| 121 | + if check_proxy "${proxy}" "${testurl}" |
| 122 | + then |
| 123 | + debug "WORKS" "give back cached proxy" |
| 124 | + debug "PROXY" "return ${proxy}" |
| 125 | + [ "${proxy}" != "NONE" ] && echo "${proxy}" |
| 126 | + exit 0 |
| 127 | + else |
| 128 | + debug "FAILED" "remove from cache file." |
| 129 | + # declare -p CACHED_PROXIES >&2 |
| 130 | + unset 'CACHED_PROXIES["${testurl_hash}"]' |
| 131 | + unset 'CACHED_PROXIES_AGE["${testurl_hash}"]' |
| 132 | + # declare -p CACHED_PROXIES >&2 |
| 133 | + fi |
76 | 134 | fi |
77 | 135 | fi |
78 | 136 |
|
|
100 | 158 | if [ -z "${ret}" ] |
101 | 159 | then |
102 | 160 | ret="${proxy}" |
103 | | - if [ ${skip_cache} -eq 0 ] |
104 | | - then |
105 | | - debug "CACHE" "Store (${proxy}) in cache file (${cache_file})" |
106 | | - echo "${proxy}" > "${cache_file}" |
107 | | - fi |
108 | 161 | fi |
109 | 162 | stat="OK" |
110 | 163 | else |
|
113 | 166 | printf "Service[%s][%s]@%s \n" "${stat}" "${name}" "${proxy}" >&2 |
114 | 167 | done |
115 | 168 |
|
116 | | -debug "PROXY" "return ${ret}" |
| 169 | +debug "PROXY" "return :${ret}:" |
| 170 | + |
117 | 171 | if [ -n "${ret}" ] |
118 | 172 | then |
119 | 173 | echo "${ret}" |
| 174 | +else |
| 175 | + ret="NONE" |
120 | 176 | fi |
| 177 | + |
| 178 | +# write back cachefile finally. |
| 179 | +if [ ${skip_cache} -eq 0 ] |
| 180 | +then |
| 181 | + debug "CACHE" "Store (${ret}) in cache file (${cache_file})" |
| 182 | + CACHED_PROXIES[${testurl_hash}]="${ret}" |
| 183 | + CACHED_PROXIES_AGE[${testurl_hash}]="${now}" |
| 184 | + debug "CACHE" "Update cachefile." |
| 185 | + declare -p CACHED_PROXIES > "${cache_file}" |
| 186 | + declare -p CACHED_PROXIES_AGE >> "${cache_file}" |
| 187 | +fi |
| 188 | + |
121 | 189 | exit 0 |
0 commit comments