Skip to content

Commit bc5211c

Browse files
author
hastmu
committed
added global proxy registry and reuse in case of new url to check, including wget limit to only download 10Bytes to check
1 parent c9ff089 commit bc5211c

File tree

1 file changed

+56
-30
lines changed

1 file changed

+56
-30
lines changed

apt-proxy-detect.sh

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
# + timeout for check_proxy
1212
# + cache age per entry not per whole file.
1313
# + cache leading url not full, as i get called for all packages also.
14+
# v5 ... learn working in general and use them for new urls first
15+
# + limit wget check to 10 bytes
1416

1517

1618
# defaults
@@ -19,6 +21,7 @@ cache_file_name=".apt-proxy-detect.$(id -un)"
1921
cache_file_none_retry_timeout=60
2022
declare -i cache_age=0
2123

24+
declare -A WORKING_PROXIES
2225
declare -A CACHED_PROXIES
2326
declare -A CACHED_PROXIES_AGE
2427

@@ -51,14 +54,21 @@ function check_proxy() {
5154
return 0
5255
fi
5356
else
54-
debug "CHECK-PROXY" "Checking proxy (${1}) with testurl (${2})"
5557
if [ $debug -gt 1 ]
5658
then
57-
wget -v --tries=1 -T 1 -e "http_proxy=$1" -e "https_proxy=$1" -O - "$2" >&2
59+
wget -v --tries=1 -T 1 --header="Range: bytes=0-10" -e "http_proxy=$1" -e "https_proxy=$1" -O - "$2" >&2
5860
else
59-
wget -q --tries=1 -T 1 -e "http_proxy=$1" -e "https_proxy=$1" -O - "$2" >> /dev/null 2>&1
61+
wget -q --tries=1 -T 1 --header="Range: bytes=0-10" -e "http_proxy=$1" -e "https_proxy=$1" -O - "$2" >> /dev/null 2>&1
6062
fi
61-
return $?
63+
local stat=$?
64+
if [ $stat -eq 0 ]
65+
then
66+
debug "CHECK-PROXY" "Proxy (${1}) works with testurl (${2})."
67+
68+
else
69+
debug "CHECK-PROXY" "Proxy (${1}) failed with testurl (${2})"
70+
fi
71+
return ${stat}
6272
fi
6373
}
6474

@@ -134,38 +144,53 @@ then
134144
fi
135145
fi
136146

137-
T_FILE=$(mktemp)
138-
trap 'rm -f ${T_FILE}' EXIT
139-
debug "AVAHI" "get cache entries for ${service_name}"
140-
avahi-browse -tcpr ${service_name} > "${T_FILE}"
141-
if [ ! -s "${T_FILE}" ]
142-
then
143-
# non cached
144-
debug "AVAHI" "get non-cache entries for ${service_name}"
145-
avahi-browse -tpr ${service_name} > "${T_FILE}"
146-
fi
147-
148-
# shellcheck disable=SC2013
149-
for service in $(grep "^+;" "${T_FILE}")
147+
# check if there is a proxy of the past which works
148+
for proxy in "${!WORKING_PROXIES[@]}"
150149
do
151-
name="$(echo "${service}" | cut -d\; -f4 | sed 's:\\032: :g')"
152-
namef="$(echo "${service}" | cut -d\; -f4)"
153-
proxy="http://$(grep "^=" "${T_FILE}" | grep "${namef//\\/\\\\}" | cut -d\; -f8,9 | tr ";" ":")"
154-
debug "CHECK" "Checking found proxy (${proxy}) with testurl (${testurl})"
150+
debug "CHECK" "once working proxy: ${proxy} for ${testurl}"
155151
if check_proxy "${proxy}" "${testurl}"
156152
then
157-
# ok
158-
if [ -z "${ret}" ]
159-
then
160-
ret="${proxy}"
161-
fi
162-
stat="OK"
163-
else
164-
stat="ER"
153+
ret="${proxy}"
165154
fi
166-
printf "Service[%s][%s]@%s \n" "${stat}" "${name}" "${proxy}" >&2
167155
done
168156

157+
# search for proxies if none was found so far.
158+
if [ -z "${ret}" ]
159+
then
160+
T_FILE=$(mktemp)
161+
trap 'rm -f ${T_FILE}' EXIT
162+
debug "AVAHI" "get cache entries for ${service_name}"
163+
avahi-browse -tcpr ${service_name} > "${T_FILE}"
164+
if [ ! -s "${T_FILE}" ]
165+
then
166+
# non cached
167+
debug "AVAHI" "get non-cache entries for ${service_name}"
168+
avahi-browse -tpr ${service_name} > "${T_FILE}"
169+
fi
170+
171+
# shellcheck disable=SC2013
172+
for service in $(grep "^+;" "${T_FILE}")
173+
do
174+
name="$(echo "${service}" | cut -d\; -f4 | sed 's:\\032: :g')"
175+
namef="$(echo "${service}" | cut -d\; -f4)"
176+
proxy="http://$(grep "^=" "${T_FILE}" | grep "${namef//\\/\\\\}" | cut -d\; -f8,9 | tr ";" ":")"
177+
debug "CHECK" "Checking found proxy (${proxy}) with testurl (${testurl})"
178+
if check_proxy "${proxy}" "${testurl}"
179+
then
180+
# ok
181+
if [ -z "${ret}" ]
182+
then
183+
ret="${proxy}"
184+
fi
185+
stat="OK"
186+
debug "ADD" "add proxy to working proxy list."
187+
WORKING_PROXIES["${proxy}"]="${now}"
188+
else
189+
stat="ER"
190+
fi
191+
printf "Service[%s][%s]@%s \n" "${stat}" "${name}" "${proxy}" >&2
192+
done
193+
fi
169194
debug "PROXY" "return :${ret}:"
170195

171196
if [ -n "${ret}" ]
@@ -184,6 +209,7 @@ then
184209
debug "CACHE" "Update cachefile."
185210
declare -p CACHED_PROXIES > "${cache_file}"
186211
declare -p CACHED_PROXIES_AGE >> "${cache_file}"
212+
declare -p WORKING_PROXIES >> "${cache_file}"
187213
fi
188214

189215
exit 0

0 commit comments

Comments
 (0)