Skip to content

Commit 13bfb20

Browse files
author
hastmu
committed
added help #7 and services options for #1
1 parent b9319c0 commit 13bfb20

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

apt-proxy-detect.sh

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,84 @@
1313
# + cache leading url not full, as i get called for all packages also.
1414
# v5 ... learn working in general and use them for new urls first
1515
# + limit wget check to 10 bytes
16+
# v6 ... add default proxies
1617

1718

1819
# defaults
20+
defaults_location="/usr/local/etc/apt-proxy-detect.bash"
1921
service_name="_apt_proxy._tcp"
2022
cache_file_name=".apt-proxy-detect.$(id -un)"
2123
cache_file_none_retry_timeout=60
22-
declare -i cache_age=0
2324

25+
# help
26+
if [ -z "$1" ]
27+
then
28+
echo ""
29+
echo "usage: $0 <defaults|set-default|url for proxy>"
30+
echo ""
31+
echo " defaults ... list default proxies"
32+
echo " set-default <proxy1>,<proxy2> ... set default proxies"
33+
echo " url for proxy ... this is the url a proxy is tested for"
34+
echo ""
35+
exit 1
36+
fi
37+
38+
# service function
39+
if [ "$1" = "set-default" ]
40+
then
41+
declare -A WORKING_PROXIES
42+
echo "defining default proxies..."
43+
now=$(date +%s)
44+
for proxy in ${2//,/ }
45+
do
46+
echo "proxy: ${proxy}"
47+
WORKING_PROXIES["${proxy}"]=${now}
48+
done
49+
if [ -w "${defaults_location%/*}" ]
50+
then
51+
echo "saved."
52+
declare -p WORKING_PROXIES > "${defaults_location}"
53+
else
54+
echo "can not save under: ${defaults_location}"
55+
fi
56+
exit 0
57+
fi
58+
59+
if [ "$1" = "defaults" ]
60+
then
61+
declare -A WORKING_PROXIES
62+
if [ -r "${defaults_location}" ]
63+
then
64+
# shellcheck disable=SC1090
65+
if source "${defaults_location}"
66+
then
67+
for proxy in "${!WORKING_PROXIES[@]}"
68+
do
69+
echo "proxy: ${proxy}"
70+
done
71+
else
72+
echo "error reading default, please delete ${defaults_location}"
73+
exit 1
74+
fi
75+
else
76+
echo "no defaults found."
77+
fi
78+
exit 0
79+
fi
80+
81+
# runtime
82+
declare -i cache_age=0
2483
declare -A WORKING_PROXIES
2584
declare -A CACHED_PROXIES
2685
declare -A CACHED_PROXIES_AGE
2786

2887
declare -i debug
2988
[ -z "${DEBUG_APT_PROXY_DETECT}" ] && debug=0 || debug=1
3089

90+
function error() {
91+
printf "[%12s][%4s]: ERROR: %s\n" "$1" "$(( ($(date +%s%N) - start_time) / 1000000 ))" "$2" >&2
92+
}
93+
3194
if [ $debug -eq 0 ]
3295
then
3396
function debug() {
@@ -41,6 +104,18 @@ else
41104
}
42105
fi
43106
debug "INFO" "===--- apt-proxy-detect ---==="
107+
# source defaults
108+
if [ -r "${defaults_location}" ]
109+
then
110+
# shellcheck disable=SC1090
111+
if source "${defaults_location}"
112+
then
113+
debug "DEFAULTS" "loaded from ${defaults_location}"
114+
else
115+
error "DEFAULTS" "failed from ${defaults_location} please fix it."
116+
exit 1
117+
fi
118+
fi
44119

45120
function check_proxy() {
46121
if [ "$1" == "NONE" ]

0 commit comments

Comments
 (0)