privoxy-tor-toggle 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/bin/sh
  2. # A script to turn tor sock4a in privoxy on or off.
  3. CONFFILE=/etc/privoxy/config # privoxy config file.
  4. TOR_REG="forward.*localhost:9050" # Regular expression open the 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. Usage:
  11. privoxy.tor <-- No arguments. Switch the tor on or off.
  12. privoxy.tor [on|off] <-- Set the tor on or off.
  13. privoxy.tor !.*xia <-- Any argument other than above shows
  14. current status of the tor.
  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 "The 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 the 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 = --help ] || [ $1 = -h ] || [ $1 = "-?" ] ; then
  60. usage
  61. exit 0
  62. else
  63. get_status
  64. echo "Currently, the tor is $tor_status."
  65. fi