Skip to content

Commit c1fe165

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

File tree

3 files changed

+86
-131
lines changed

3 files changed

+86
-131
lines changed

README.md

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ Issues:
1111
* No check if the found proxy works for the requested target.
1212
* No longer active maintained (deprecated warnings all over the place)
1313

14+
What do you get?
15+
- [X] detect via avahi
16+
- [X] caching of URL specific working proxy (or none if none works)
17+
- [X] caching over reboot if possible
18+
1419
# How does it look?
1520

1621
Example output:
@@ -59,11 +64,17 @@ looks like:
5964
dev@dev:~$ curl -s https://raw.githubusercontent.com/hastmu/apt-proxy-detect/main/install.sh | sudo bash
6065
- check dependencies...
6166
- download latest to: /usr/local/bin/apt-proxy-detect.sh
67+
- BRANCH [main]
6268
- set permissions to a+rx
6369
- create/updating /etc/apt/apt.conf.d/30apt-proxy-detect.conf
6470
6571
```
6672

73+
if you like to specify a branch do it like:
74+
```
75+
curl -s https://raw.githubusercontent.com/hastmu/apt-proxy-detect/main/install.sh | sudo BRANCH=main bash
76+
```
77+
6778
or you just download the install.sh and run it locally.
6879

6980
# How does it work?
@@ -78,8 +89,8 @@ right or outdated for the new stuff to be checked out.
7889

7990
In case a proxy is not serving the URL it is drop for this dedicated URL.
8091

81-
The found (via avahi-browse _apt_proxy._tcp) proxies are cached under /tmp/.apt-proxy.$username
82-
in case this has the wrong owner it is ignored and a waring is issued.
92+
The found (via avahi-browse _apt_proxy._tcp) proxies are cached in case this has the
93+
wrong owner it is ignored and a waring is issued.
8394

8495
Thats it. Enjoy.
8596

@@ -103,41 +114,47 @@ looks like
103114
```
104115
dev@dev:~$ export DEBUG_APT_PROXY_DETECT=1
105116
dev@dev:~$ sudo apt update
106-
[ INFO]: apt-proxy-detect
107-
[ CACHE]: stored under: /tmp/.apt-proxy._apt
108-
[ TEST-URL]: URL: http://packages.microsoft.com/repos/code/dists/stable/InRelease
109-
[ AVAHI]: get cache entries for _apt_proxy._tcp
110-
[ AVAHI]: get non-cache entries for _apt_proxy._tcp
111-
[ CHECK]: Checking found proxy (http://192.168.0.27:3142) with testurl (http://packages.microsoft.com/repos/code/dists/stable/InRelease)
112-
Service[ER][apt-cacher-ng proxy on squid-deb-proxy]@http://192.168.0.27:3142
113-
[ CHECK]: Checking found proxy (http://192.168.0.27:8000) with testurl (http://packages.microsoft.com/repos/code/dists/stable/InRelease)
114-
[ CACHE]: Store (http://192.168.0.27:8000) in cache file (/tmp/.apt-proxy._apt)
115-
Service[OK][Squid deb proxy on squid-deb-proxy]@http://192.168.0.27:8000
116-
[ PROXY]: return http://192.168.0.27:8000
117-
[ INFO]: apt-proxy-detect
118-
[ CACHE]: stored under: /tmp/.apt-proxy._apt
119-
[ TEST-URL]: URL: http://download.proxmox.com/debian/pve/dists/bookworm/InRelease
120-
[ CHECK]: Checking cached proxy (http://192.168.0.27:8000) with testurl (http://download.proxmox.com/debian/pve/dists/bookworm/InRelease)
121-
[ WORKS]: give back cached proxy
122-
[ PROXY]: return http://192.168.0.27:8000
123-
[ INFO]: apt-proxy-detect
124-
[ CACHE]: stored under: /tmp/.apt-proxy._apt
125-
[ TEST-URL]: URL: http://local-repo.fritz.box/local-repo/dists/trunk/InRelease
126-
[ CHECK]: Checking cached proxy (http://192.168.0.27:8000) with testurl (http://local-repo.fritz.box/local-repo/dists/trunk/InRelease)
127-
[ WORKS]: give back cached proxy
128-
[ PROXY]: return http://192.168.0.27:8000
129-
[ INFO]: apt-proxy-detect
130-
[ CACHE]: stored under: /tmp/.apt-proxy._apt
131-
[ TEST-URL]: URL: http://security.debian.org/debian-security/dists/bookworm-security/InRelease
132-
[ CHECK]: Checking cached proxy (http://192.168.0.27:8000) with testurl (http://security.debian.org/debian-security/dists/bookworm-security/InRelease)
133-
[ WORKS]: give back cached proxy
134-
[ PROXY]: return http://192.168.0.27:8000
135-
[ INFO]: apt-proxy-detect
136-
[ CACHE]: stored under: /tmp/.apt-proxy._apt
137-
[ TEST-URL]: URL: http://deb.debian.org/debian/dists/bookworm/InRelease
138-
[ CHECK]: Checking cached proxy (http://192.168.0.27:8000) with testurl (http://deb.debian.org/debian/dists/bookworm/InRelease)
139-
[ WORKS]: give back cached proxy
140-
[ PROXY]: return http://192.168.0.27:8000
117+
# INFO-TAG MS : MESSAGE
118+
[ INFO][ 2]: ===--- apt-proxy-detect ---===
119+
[ TEST-URL][ 16]: URL: http://packages.microsoft.com/repos/code/dists/stable/InRelease
120+
[ HASH][ 25]: HASH: c0b917f192fa7cccb3f536f2c01b824d of (http://packages.microsoft.com)
121+
[ CACHE][ 30]: using stored under: /var/lib/apt/lists/auxfiles/.apt-proxy-detect._apt
122+
[ CACHE-AGE][ 33]: age: 687 sec
123+
[ CHECK-PROXY][ 35]: Checking proxy (http://192.168.0.27:8000) with testurl (http://packages.microsoft.com/repos/code/dists/stable/InRelease)
124+
[ WORKS][ 127]: give back cached proxy
125+
[ PROXY][ 131]: return http://192.168.0.27:8000
126+
[ INFO][ 3]: ===--- apt-proxy-detect ---===
127+
[ TEST-URL][ 20]: URL: http://download.proxmox.com/debian/pve/dists/bookworm/InRelease
128+
[ HASH][ 34]: HASH: 17b43db99b56eb6355d41861f4f304d0 of (http://download.proxmox.com)
129+
[ CACHE][ 41]: using stored under: /var/lib/apt/lists/auxfiles/.apt-proxy-detect._apt
130+
[ CACHE-AGE][ 44]: age: 687 sec
131+
[ CHECK-PROXY][ 47]: Checking proxy (http://192.168.0.27:8000) with testurl (http://download.proxmox.com/debian/pve/dists/bookworm/InRelease)
132+
[ WORKS][ 461]: give back cached proxy
133+
[ PROXY][ 464]: return http://192.168.0.27:8000
134+
[ INFO][ 3]: ===--- apt-proxy-detect ---===
135+
[ TEST-URL][ 17]: URL: http://local-repo.fritz.box/local-repo/dists/trunk/InRelease
136+
[ HASH][ 25]: HASH: 2bfbb1335aaf9d333a5c9498226eb208 of (http://local-repo.fritz.box)
137+
[ CACHE][ 32]: using stored under: /var/lib/apt/lists/auxfiles/.apt-proxy-detect._apt
138+
[ CACHE-AGE][ 37]: age: 687 sec
139+
[ CHECK-PROXY][ 39]: Checking proxy (http://192.168.0.27:8000) with testurl (http://local-repo.fritz.box/local-repo/dists/trunk/InRelease)
140+
[ WORKS][ 57]: give back cached proxy
141+
[ PROXY][ 60]: return http://192.168.0.27:8000
142+
[ INFO][ 2]: ===--- apt-proxy-detect ---===
143+
[ TEST-URL][ 19]: URL: http://security.debian.org/debian-security/dists/bookworm-security/InRelease
144+
[ HASH][ 28]: HASH: 3b68f7b6590a2da8625ff71f01d38ffb of (http://security.debian.org)
145+
[ CACHE][ 36]: using stored under: /var/lib/apt/lists/auxfiles/.apt-proxy-detect._apt
146+
[ CACHE-AGE][ 39]: age: 688 sec
147+
[ CHECK-PROXY][ 41]: Checking proxy (http://192.168.0.27:8000) with testurl (http://security.debian.org/debian-security/dists/bookworm-security/InRelease)
148+
[ WORKS][ 93]: give back cached proxy
149+
[ PROXY][ 95]: return http://192.168.0.27:8000
150+
[ INFO][ 2]: ===--- apt-proxy-detect ---===
151+
[ TEST-URL][ 24]: URL: http://deb.debian.org/debian/dists/bookworm/InRelease
152+
[ HASH][ 33]: HASH: efbfa0e2acaaa513c457b6698de83118 of (http://deb.debian.org)
153+
[ CACHE][ 41]: using stored under: /var/lib/apt/lists/auxfiles/.apt-proxy-detect._apt
154+
[ CACHE-AGE][ 44]: age: 687 sec
155+
[ CHECK-PROXY][ 47]: Checking proxy (http://192.168.0.27:8000) with testurl (http://deb.debian.org/debian/dists/bookworm/InRelease)
156+
[ WORKS][ 145]: give back cached proxy
157+
[ PROXY][ 147]: return http://192.168.0.27:8000
141158
Hit:1 http://local-repo.fritz.box/local-repo trunk InRelease
142159
Hit:2 http://deb.debian.org/debian bookworm InRelease
143160
Hit:3 http://security.debian.org/debian-security bookworm-security InRelease

apt-proxy-detect.sh

Lines changed: 27 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,6 @@
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-
# TODO: 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
248

259
declare -i debug
2610
[ -z "${DEBUG_APT_PROXY_DETECT}" ] && debug=0 || debug=1
@@ -31,38 +15,20 @@ then
3115
:
3216
}
3317
else
34-
declare -i start_time=0
35-
start_time=$(date +%s%N)
3618
function debug() {
37-
printf "[%12s][%4s]: %s\n" "$1" "$(( ($(date +%s%N) - start_time) / 1000000 ))" "$2" >&2
19+
printf "[%10s]: %s\n" "$1" "$2" >&2
3820
}
3921
fi
40-
debug "INFO" "===--- apt-proxy-detect ---==="
22+
debug "INFO" "apt-proxy-detect"
4123

4224
function check_proxy() {
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
25+
wget -e "http_proxy=$1" -e "https_proxy=$1" -qO - "$2" >> /dev/null
26+
return $?
6327
}
6428

65-
# --- cache location ---
29+
# cache file name
30+
cache_file_name=".apt-proxy-detect.$(id -un)"
31+
6632
# persistant cache file location
6733
# $HOME or special for system accounts
6834
declare -A CACHE_FILE_LOC
@@ -80,55 +46,33 @@ else
8046
fi
8147
fi
8248

83-
# check cache_file if not there create it.
84-
[ ! -e "${cache_file}" ] && touch "${cache_file}" >> /dev/null 2>&1
49+
# check cache_file
50+
touch "${cache_file}" >> /dev/null 2>&1
8551

86-
# check ownership of cache file and fetch age.
8752
skip_cache=0
8853
if [ "$(stat -c %u "${cache_file}")" != "$(id -u)" ]
8954
then
9055
skip_cache=1
9156
echo "E: wrong owner of cache file ${cache_file}, remove or reown to $(id -un)" >&2
9257
fi
93-
94-
# eval testurl
58+
service_name="_apt_proxy._tcp"
9559
[ -z "$1" ] && testurl="http://deb.debian.org/debian" || testurl="$1"
96-
debug "TEST-URL" "URL: ${testurl}"
97-
testurl_hash="$(echo "${testurl}" | md5sum)" ; testurl_hash="${testurl_hash%% *}"
98-
debug "HASH" "HASH: ${testurl_hash}"
60+
debug "TEST-URL" "URL: ${testurl}"
9961

100-
# check out cached value
10162
if [ -s "${cache_file}" ] && [ ${skip_cache} -eq 0 ]
10263
then
103-
# shellcheck disable=SC1090
104-
source "${cache_file}"
105-
# shellcheck disable=SC2181
106-
if [ $? -ne 0 ]
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}"
10768
then
108-
# something is wrong with the cache file remove it.
109-
debug "CACHE" "invalid cachefile (cleanup): ${cache_file}"
110-
rm -f "${cache_file}"
69+
debug "WORKS" "give back cached proxy"
70+
debug "PROXY" "return ${proxy}"
71+
echo "${proxy}"
72+
exit 0
11173
else
112-
debug "CACHE" "using stored under: ${cache_file}"
113-
fi
114-
cache_age=$(( $(date +%s) - ${CACHED_PROXIES_AGE[${testurl_hash}]:=0} ))
115-
debug "CACHE-AGE" "age: ${cache_age} sec"
116-
proxy="${CACHED_PROXIES[${testurl_hash}]}"
117-
if [ -n "${proxy}" ]
118-
then
119-
if check_proxy "${proxy}" "${testurl}"
120-
then
121-
debug "WORKS" "give back cached proxy"
122-
debug "PROXY" "return ${proxy}"
123-
[ "${proxy}" != "NONE" ] && echo "${proxy}"
124-
exit 0
125-
else
126-
debug "FAILED" "remove from cache file."
127-
# declare -p CACHED_PROXIES >&2
128-
unset 'CACHED_PROXIES["${testurl_hash}"]'
129-
unset 'CACHED_PROXIES_AGE["${testurl_hash}"]'
130-
# declare -p CACHED_PROXIES >&2
131-
fi
74+
debug "FAILED" "remove cache file."
75+
rm -f "${cache_file}"
13276
fi
13377
fi
13478

@@ -156,6 +100,11 @@ do
156100
if [ -z "${ret}" ]
157101
then
158102
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
159108
fi
160109
stat="OK"
161110
else
@@ -164,24 +113,9 @@ do
164113
printf "Service[%s][%s]@%s \n" "${stat}" "${name}" "${proxy}" >&2
165114
done
166115

167-
debug "PROXY" "return :${ret}:"
168-
116+
debug "PROXY" "return ${ret}"
169117
if [ -n "${ret}" ]
170118
then
171119
echo "${ret}"
172-
else
173-
ret="NONE"
174120
fi
175-
176-
# write back cachefile finally.
177-
if [ ${skip_cache} -eq 0 ]
178-
then
179-
debug "CACHE" "Store (${ret}) in cache file (${cache_file})"
180-
CACHED_PROXIES[${testurl_hash}]="${ret}"
181-
CACHED_PROXIES_AGE[${testurl_hash}]="$(date +%s)"
182-
debug "CACHE" "Update cachefile."
183-
declare -p CACHED_PROXIES > "${cache_file}"
184-
declare -p CACHED_PROXIES_AGE >> "${cache_file}"
185-
fi
186-
187121
exit 0

install.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ BINS["grep"]=0
1313
BINS["cut"]=0
1414
BINS["["]=0
1515
BINS["printf"]=0
16+
BINS["date"]=0
17+
BINS["md5sum"]=0
1618

1719
for pitem in ${PATH//:/ }
1820
do
@@ -42,7 +44,9 @@ TARGET="/usr/local/bin/apt-proxy-detect.sh"
4244
# download latest
4345
echo "- download latest to: ${TARGET}"
4446

45-
if ! wget -q -O "${TARGET}" https://raw.githubusercontent.com/hastmu/apt-proxy-detect/main/apt-proxy-detect.sh
47+
48+
echo "- BRANCH [${BRANCH:=main}]"
49+
if ! wget -q -O "${TARGET}" https://raw.githubusercontent.com/hastmu/apt-proxy-detect/${BRANCH}/apt-proxy-detect.sh
4650
then
4751
echo "- download failed."
4852
[ -x "${TARGET}" ] && rm -f "${TARGET}"

0 commit comments

Comments
 (0)