Skip to content

Commit 9b7c941

Browse files
author
hastmu
committed
first version
1 parent 1e65e9d commit 9b7c941

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

apt-proxy-detect.sh

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/bin/bash
2+
3+
# (c)opyright by [email protected]
4+
# checkout https://github.com/hastmu/apt-proxy-detect
5+
# v1 ... pure detect
6+
# v2 ... cache value with check
7+
8+
function check_proxy() {
9+
wget -e http_proxy=$1 -e https_proxy=$1 -qO - $2 >> /dev/null
10+
return $?
11+
}
12+
13+
cache_file="/tmp/.apt-proxy.$(id -un)"
14+
touch ${cache_file} >> /dev/null 2>&1
15+
skip_cache=0
16+
if [ "$(stat -c %u ${cache_file})" != "$(id -u)" ]
17+
then
18+
skip_cache=1
19+
echo "E: wrong owner of cache file ${cache_file}, remove or reown to $(id -un)" >&2
20+
fi
21+
service_name="_apt_proxy._tcp"
22+
[ -z "$1" ] && testurl="http://deb.debian.org/debian" || testurl="$1"
23+
24+
if [ -s "${cache_file}" ] && [ ${skip_cache} -eq 0 ]
25+
then
26+
proxy="$(cat ${cache_file})"
27+
if check_proxy "${proxy}" "${testurl}"
28+
then
29+
echo "${proxy}"
30+
exit 0
31+
else
32+
rm -f "${cache_file}"
33+
fi
34+
fi
35+
36+
T_FILE=$(mktemp)
37+
trap 'rm -f ${T_FILE}' EXIT
38+
avahi-browse -tcpr ${service_name} > ${T_FILE}
39+
if [ ! -s "${T_FILE}" ]
40+
then
41+
# non cached
42+
avahi-browse -tpr ${service_name} > ${T_FILE}
43+
fi
44+
45+
for service in $(cat ${T_FILE} | grep "^+;")
46+
do
47+
name="$(echo ${service} | cut -d\; -f4 | sed 's:\\032: :g')"
48+
namef="$(echo ${service} | cut -d\; -f4)"
49+
proxy="http://$(cat ${T_FILE} | grep "^=" | grep "$(echo ${namef} | sed 's:\\:\\\\:g')" | cut -d\; -f8,9 | tr ";" ":")"
50+
if check_proxy ${proxy} ${testurl}
51+
then
52+
# ok
53+
if [ -z "${ret}" ]
54+
then
55+
ret="${proxy}"
56+
if [ ${skip_cache} -eq 0 ]
57+
then
58+
echo "${proxy}" > ${cache_file}
59+
fi
60+
fi
61+
stat="OK"
62+
else
63+
stat="ER"
64+
fi
65+
printf "Service[%s][%s]@%s \n" "${stat}" "${name}" "${proxy}" >&2
66+
done
67+
68+
echo "${ret}"

0 commit comments

Comments
 (0)