Skip to content

Commit c418b40

Browse files
author
hastmu
committed
finailized #3
1 parent c1fe165 commit c418b40

File tree

1 file changed

+95
-27
lines changed

1 file changed

+95
-27
lines changed

apt-proxy-detect.sh

Lines changed: 95 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@
55
# v1 ... pure detect
66
# v2 ... cache value with check
77
# 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
824

925
declare -i debug
1026
[ -z "${DEBUG_APT_PROXY_DETECT}" ] && debug=0 || debug=1
@@ -15,20 +31,38 @@ then
1531
:
1632
}
1733
else
34+
declare -i start_time=0
35+
start_time=$(date +%s%N)
1836
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
2038
}
2139
fi
22-
debug "INFO" "apt-proxy-detect"
40+
debug "INFO" "===--- apt-proxy-detect ---==="
2341

2442
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
2763
}
2864

29-
# cache file name
30-
cache_file_name=".apt-proxy-detect.$(id -un)"
31-
65+
# --- cache location ---
3266
# persistant cache file location
3367
# $HOME or special for system accounts
3468
declare -A CACHE_FILE_LOC
@@ -46,33 +80,57 @@ else
4680
fi
4781
fi
4882

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
5185

86+
# check ownership of cache file and fetch age.
5287
skip_cache=0
5388
if [ "$(stat -c %u "${cache_file}")" != "$(id -u)" ]
5489
then
5590
skip_cache=1
5691
echo "E: wrong owner of cache file ${cache_file}, remove or reown to $(id -un)" >&2
5792
fi
58-
service_name="_apt_proxy._tcp"
93+
94+
# eval testurl
5995
[ -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})"
61100

101+
# check out cached value
102+
now=$(date +%s)
62103
if [ -s "${cache_file}" ] && [ ${skip_cache} -eq 0 ]
63104
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 ]
68109
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}"
75112
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
76134
fi
77135
fi
78136

@@ -100,11 +158,6 @@ do
100158
if [ -z "${ret}" ]
101159
then
102160
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
108161
fi
109162
stat="OK"
110163
else
@@ -113,9 +166,24 @@ do
113166
printf "Service[%s][%s]@%s \n" "${stat}" "${name}" "${proxy}" >&2
114167
done
115168

116-
debug "PROXY" "return ${ret}"
169+
debug "PROXY" "return :${ret}:"
170+
117171
if [ -n "${ret}" ]
118172
then
119173
echo "${ret}"
174+
else
175+
ret="NONE"
120176
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+
121189
exit 0

0 commit comments

Comments
 (0)