Advertisement
Guest User

Untitled

a guest
Sep 10th, 2019
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # tor-ctrl is a commandline tool for executing commands on a Tor server via
  4. # the controlport. In order to get this to work, add "ControlPort 9051" and
  5. # "CookieAuthentication 1" to your torrc and reload Tor. Or - if you want a
  6. # fixed password - leave out "CookieAuthentication 1" and use the following
  7. # line to create the appropriate HashedControlPassword entry for your torrc
  8. # (you need to change yourpassword, of course):
  9. #
  10. # echo "HashedControlPassword $(tor --hash-password yourpassword | tail -n 1)"
  11. #
  12. # tor-ctrl will return 0 if it was successful and 1 if not, 2 will be returned
  13. # if something (telnet, xxd) is missing. 4 will be returned if it executed
  14. # several commands from a file.
  15. #
  16. # For setting the bandwidth for specific times of the day, I suggest calling
  17. # tor-ctrl via cron, e.g.:
  18. #
  19. # 0 22 * * * /path/to/tor-ctrl -c "SETCONF bandwidthrate=1mb"
  20. # 0 7 * * * /path/to/tor-ctrl -c "SETCONF bandwidthrate=100kb"
  21. #
  22. # This would set the bandwidth to 100kb at 07:00 and to 1mb at 22:00. You can
  23. # use notations like 1mb, 1kb or the number of bytes.
  24.  
  25. # Many, many other things are possible, see
  26. # https://gitweb.torproject.org/torspec.git/tree/control-spec.txt
  27.  
  28. # Copyright (c) 2007 by Stefan Behte
  29. # Portion Copyright (C) 2013 - 2019 ENCRYPTED SUPPORT LP <adrelanos@riseup.net>
  30. #
  31. # This program is free software: you can redistribute it and/or modify
  32. # it under the terms of the GNU General Public License as published by
  33. # the Free Software Foundation, either version 3 of the License, or
  34. # (at your option) any later version.
  35. #
  36. # This program is distributed in the hope that it will be useful,
  37. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. # GNU General Public License for more details.
  40. #
  41. # You should have received a copy of the GNU General Public License
  42. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  43. #
  44. # On Debian systems, the full text of the GNU General Public
  45. # License version 3 can be found in the file
  46. # `/usr/share/common-licenses/GPL-3'.
  47.  
  48. # tor-ctrl was originally written by Stefan Behte
  49. #
  50. # Please send bugs, comments, wishes, thanks and success stories to:
  51. # Stefan dot Behte at gmx dot net
  52. #
  53. # Also have a look at my page:
  54. # http://ge.mine.nu/
  55.  
  56. TORCTLIP=127.0.0.1
  57. TORCTLPORT=9051
  58. TOR_COOKIE="/run/tor/control.authcookie"
  59. TOR_CONTROL="/run/tor/control"
  60. SLEEP_AFTER_CMD=1
  61. VERBOSE=0
  62.  
  63. usage()
  64. {
  65. cat <<EOF
  66.  
  67. tor-ctrl was originally written by Stefan Behte (http://ge.mine.nu)
  68.  
  69. You should have a look at
  70. https://gitweb.torproject.org/torspec.git/tree/control-spec.txt
  71.  
  72. usage: tor-ctrl [-switch] [variable]
  73.  
  74. [-c] [command] = command to execute
  75. notice: always "quote" your command
  76.  
  77. [-f] [file] = file to execute commands from
  78. notice: only one command per line
  79.  
  80. [-a] [path] = path to Tor's control_auth_cookie
  81. default: /run/tor/control.authcookie
  82. notice: do not forget to adjust your torrc
  83.  
  84. [-s] [time] = sleep [var] seconds after each command sent
  85. default: 1 second
  86. notice: for GETCONF, you can use smaller pause times
  87. than for SETCONF; this is due to telnet's behavior.
  88.  
  89. [-p] [pwd] = Use password [var] instead of Tor's control_auth_cookie
  90. default: not used
  91. notice: do not forget to adjust your torrc
  92.  
  93. [-P] [port] = Tor ControlPort
  94. default: 9051
  95.  
  96. [-v] = verbose
  97. default: not set
  98. notice: the default output is the return code ;)
  99. You probably want to set -v when running manually
  100.  
  101. [-w] = wait instead of exiting after command
  102. default: not set
  103. notice: implies verbose. Useful with SETEVENTS.
  104.  
  105. Examples: tor-ctrl -c "SETCONF bandwidthrate=1mb"
  106. tor-ctrl -v -c "GETINFO version"
  107. tor-ctrl -v -s 0 -P 9051 -p foobar -c "GETCONF bandwidthrate"
  108.  
  109. EOF
  110. exit 2
  111. }
  112.  
  113. checkprogs()
  114. {
  115. local programs
  116. programs="telnet"
  117. if [ "$PASSWORD" = "" ]; then
  118. # you only need xxd when using control_auth_cookie
  119. programs="$programs xxd"
  120. fi
  121.  
  122. for p in $programs ; do
  123. command -v "$p" &>/dev/null # are you there?
  124. if [ "$?" != "0" ]; then
  125. echo "$p is missing."
  126. exit 2
  127. fi
  128. done
  129. }
  130.  
  131. sendcmd()
  132. {
  133. echo "$@"
  134. sleep "${SLEEP_AFTER_CMD}"
  135. }
  136.  
  137. login()
  138. {
  139. if [ "$PASSWORD" = "" ]; then
  140. sendcmd "AUTHENTICATE $(xxd -c 32 -g 0 "${TOR_COOKIE}" | awk '{print $2}')"
  141. else
  142. sendcmd "AUTHENTICATE \"${PASSWORD}\""
  143. fi
  144. }
  145.  
  146. cmdpipe()
  147. {
  148. login
  149. sendcmd "$@"
  150. if [ -z "$WAIT" ]; then
  151. sendcmd "QUIT"
  152. else
  153. while sleep 3600; do :; done
  154. fi
  155. }
  156.  
  157. vecho()
  158. {
  159. if [ "$VERBOSE" -ge 1 ]; then
  160. echo "$@"
  161. fi
  162. }
  163.  
  164. myecho()
  165. {
  166. local STR
  167. if [ -z "$WAIT" ]; then
  168. STR="$(cat)"
  169. vecho "$STR"
  170. echo "$STR" | if [ "$(grep -c ^"250 ")" = 3 ]; then
  171. exit 0
  172. else
  173. exit 1
  174. fi
  175. else
  176. cat
  177. fi
  178. }
  179.  
  180. filepipe()
  181. {
  182. login
  183. cat "$1" | while read line
  184. do
  185. sendcmd "$line"
  186. done
  187. sendcmd "QUIT"
  188. }
  189.  
  190. while getopts ":a:c:s:p:P:f:vwh" Option
  191. do
  192. case $Option in
  193. a) TOR_COOKIE="${OPTARG}";;
  194. c) CMD="${OPTARG}";;
  195. s) SLEEP_AFTER_CMD="${OPTARG}";;
  196. p) PASSWORD="${OPTARG}";;
  197. P) TORCTLPORT="${OPTARG}";;
  198. f) FILE="${OPTARG}";;
  199. v) VERBOSE=1;;
  200. w) WAIT=1;;
  201. h) usage;;
  202. *) usage;;
  203. esac
  204. done
  205.  
  206. if [ -e "$FILE" -o "$FILE" = "-" ]; then
  207. checkprogs
  208. #filepipe "$FILE" | telnet "$TORCTLIP" "$TORCTLPORT" 2>/dev/null | myecho
  209. filepipe "$FILE" | socat - UNIX-CONNECT:"$TOR_CONTROL" | myecho
  210. exit 4
  211. fi
  212.  
  213. if [ "$CMD" != "" ]; then
  214. checkprogs
  215. #cmdpipe "$CMD" | telnet "$TORCTLIP" "$TORCTLPORT" 2>/dev/null | myecho
  216. cmdpipe "$CMD" | socat - UNIX-CONNECT:"$TOR_CONTROL" | myecho
  217. else
  218. usage
  219. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement