privoxy-tor-toggle 1.8 KB

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