Avoid spurious re-launch of first pending requested descriptor
[tor.git] / contrib / privoxy-tor-toggle
blob8f9cd51bd96e64af4d4840c53a8470fe1ed78995
1 #!/bin/sh
2 # A script to turn Tor SOCKS4a in Privoxy on or off.
4 CONFFILE=/etc/privoxy/config # privoxy config file.
5 TOR_REG="forward.*localhost:9050" # Regular expression to find Tor in privoxy
6 PRIVOXY="/etc/init.d/privoxy restart" # command to reload privoxy config file.
7 SED="/bin/sed" # sed command, of course.
8 GREP="/bin/grep" # grep command.
10 usage () {
11 echo "\
12 privoxy-tor-toggle: Change Privoxy's configuration to use/not use Tor.
13 Usage:
14 privoxy.tor <-- Switch Tor on or off.
15 privoxy.tor [on|off] <-- Set Tor on or off.
16 privoxy.tor status <-- Display Tor's current status.
17 privoxy.tor [-h|--help|-?] <-- Print usage.
21 # Find out the current status of tor. Set $tor_status
22 get_status () {
23 gret=`$GREP -l -e "^$TOR_REG" $CONFFILE`
24 if [ x$gret = x ] ; then
25 tor_status=off;
26 else
27 tor_status=on;
29 return
32 # Turn tor on/off according to $1
33 set_tor () {
34 tor_gate=$1
35 get_status
36 if [ $tor_status = $tor_gate ] ; then
37 echo "Tor is already $1."
38 return
39 elif [ $tor_gate = flip ] ; then
40 if [ $tor_status = on ] ; then
41 tor_gate=off
42 elif [ $tor_status = off ] ; then
43 tor_gate=on
46 echo "Turning Tor $tor_gate..."
47 if [ $tor_gate = on ] ; then
48 reg=s/^#\($TOR_REG\)/\\1/
49 $SED -i.bak -r "$reg" $CONFFILE
50 else
51 reg=s/^\($TOR_REG\)/#\\1/
52 $SED -i.bak -r "$reg" $CONFFILE
54 $PRIVOXY
55 return 0;
58 if [ x$1 = x ] ; then
59 set_tor flip
60 elif [ $1 = on ] ; then
61 set_tor on
62 elif [ $1 = off ] ; then
63 set_tor off
64 elif [ $1 = status ] ; then
65 get_status
66 echo "Tor is $tor_status"
67 elif [ $1 = --help ] || [ $1 = -h ] || [ $1 = "-?" ] ; then
68 usage
69 exit 0
70 else
71 echo "Unrecognized option: \"$1\""