summaryrefslogtreecommitdiff
path: root/examples/proxyswitch.sh
blob: d21551775e837f364346e52c2dd1979da5d5a72c (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash

# Copyright 2012 Florian Bruhin <me@the-compiler.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>. 

declare -A urls

urls['ziproxy']="localhost:9001"
urls['polipo']="localhost:3129"
urls['ssh']='localhost:8123'
# ...

dwb_command() {
	if [[ "$DWB_FIFO" ]]; then # running inside DWB
		echo "$@" | sed 's/ *;; */\n/' >> "$DWB_FIFO"
	else
		if ! pgrep dwb >/dev/null; then
			error "dwb isn't running!"
		fi
		dwb -x "$@"
	fi
}

error() {
	if [[ "$DWB_FIFO" ]]; then # running inside DWB
		echo "js window.alert('$@');" >> "$DWB_FIFO"
	else
		echo "$@"
	fi
	exit 1
}

set_proxy() {
	proxy="${urls[$1]}"
	if [[ -z "$proxy" ]]; then
		error "Proxy $1 is unknown!"
	fi
	dwb_command "local_set proxy true ;; local_set proxy-url $proxy"
}

unset_proxy() {
	dwb_command "local_set proxy false ;; local_set proxy-url NULL"
}

if [[ "$DWB_FIFO" ]]; then
	# fills $1 with the argument in dwb's command line.
	set -- "$DWB_ARGUMENT" # quotes sic!
fi

if (( $# != 1 )); then
	error "Usage: $0 <proxyname>"
fi

case $1 in
	"off") unset_proxy ;;
	*) set_proxy "$1" ;;
esac