summaryrefslogtreecommitdiff
path: root/15_Proxy.zsh
blob: 9b2111d62bf2f64c568d3af3c4126ab32449379e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

_set_proxy() {
	# set_proxy [http [https [ftp]]]
	if [ -z "$1" ]
	then
		unset http_proxy
		unset https_proxy
		unset ftp_proxy
		unset RSYNC_PROXY
	else
		export http_proxy="$1"
		export https_proxy="${2:-$1}"
		export ftp_proxy="${3:-$1}"
		export RSYNC_PROXY="${4:-${1#*://}}"
	fi
}

_get_proxy() {
	env | sed -n 's/^http_proxy=//p;T;q'
}
__get_proxy() {
	proxy=$(_get_proxy)
	[ -z "${proxy}" ] && return
	echo "┤$C_$_prompt_colors[proxy]${_C}PROXY${C_}$_prompt_colors[generic]$_C├"
}
PS1_TASKBAR+=(__get_proxy)

proxy_switch() {
	if [ ! -z "${PROXY}" ]
	then
		local proxy="$(_get_proxy)"
		if [ -z "${http_proxy}" ]
		then
			_set_proxy "${PROXY}"
		else
			_set_proxy
		fi
	fi
}

proxy_best() {
	if [ -n "${PROXY}" ]
	then
		local proxy_host="${${PROXY%:*}#*://}"
		if ping -q -c1 -w1 ${proxy_host} >/dev/null 2>&-
		then
			_set_proxy ${PROXY}
		else
			_set_proxy
		fi
	fi
}
periodic_functions+=( proxy_best )