1+ package main
2+
3+ import "net"
4+ import "fmt"
5+ import "bufio"
6+ import "os"
7+ import "strings"
8+ import "flag"
9+
10+ func main () {
11+
12+ // Catch flags
13+ hostPtr := flag .String ("host" , "127.0.0.1" , "host" )
14+ portPtr := flag .String ("port" , "9051" , "port" )
15+ secretPtr := flag .String ("secret" , "" , "secret" )
16+ cmdPtr := flag .String ("cmd" , "" , "cmd" )
17+
18+ // Parse flags
19+ flag .Parse ()
20+
21+ // Declare/initialize variables
22+ var text string
23+ console := bufio .NewReader (os .Stdin )
24+ conn , _ := net .Dial ("tcp" , * hostPtr + ":" + * portPtr )
25+
26+ // Authenticate connection to Tor
27+ fmt .Fprintf (conn , "authenticate \" " + * secretPtr + "\" \n " )
28+ text , _ = bufio .NewReader (conn ).ReadString ('\n' )
29+ if strings .TrimRight (text , "\r \n " ) != "250 OK" {os .Exit (0 )}
30+
31+ // Initialize function for receiving and handling command output
32+ receive := func () {
33+ // Handle responses
34+ text = ""
35+ tor := bufio .NewScanner (conn )
36+ for tor .Scan () {
37+ text = tor .Text ()
38+ if text == "250 OK" {
39+ break
40+ } else if text == "." {
41+ break
42+ } else if text == "250 closing connection" {
43+ os .Exit (0 )
44+ } else if strings .HasPrefix (text , "250 " ) {
45+ fmt .Println (strings .TrimPrefix (text , "250 " ))
46+ break
47+ } else if strings .HasPrefix (text , "251 " ) {
48+ fmt .Println (strings .TrimPrefix (text , "251 " ))
49+ break
50+ } else if strings .HasPrefix (text , "252 " ) {
51+ fmt .Println (strings .TrimPrefix (text , "252 " ))
52+ break
53+ } else if strings .HasPrefix (text , "451 " ) {
54+ fmt .Println (strings .TrimPrefix (text , "451 " ))
55+ break
56+ } else if strings .HasPrefix (text , "500 " ) {
57+ fmt .Println (strings .TrimPrefix (text , "500 " ))
58+ break
59+ } else if strings .HasPrefix (text , "510 " ) {
60+ fmt .Println (strings .TrimPrefix (text , "510 " ))
61+ break
62+ } else if strings .HasPrefix (text , "511 " ) {
63+ fmt .Println (strings .TrimPrefix (text , "511 " ))
64+ break
65+ } else if strings .HasPrefix (text , "512 " ) {
66+ fmt .Println (strings .TrimPrefix (text , "512 " ))
67+ break
68+ } else if strings .HasPrefix (text , "513 " ) {
69+ fmt .Println (strings .TrimPrefix (text , "513 " ))
70+ break
71+ } else if strings .HasPrefix (text , "514 " ) {
72+ fmt .Println (strings .TrimPrefix (text , "514 " ))
73+ break
74+ } else if strings .HasPrefix (text , "515 " ) {
75+ fmt .Println (strings .TrimPrefix (text , "515 " ))
76+ break
77+ } else if strings .HasPrefix (text , "550 " ) {
78+ fmt .Println (strings .TrimPrefix (text , "550 " ))
79+ break
80+ } else if strings .HasPrefix (text , "551 " ) {
81+ fmt .Println (strings .TrimPrefix (text , "551 " ))
82+ break
83+ } else if strings .HasPrefix (text , "552 " ) {
84+ fmt .Println (strings .TrimPrefix (text , "552 " ))
85+ break
86+ } else if strings .HasPrefix (text , "553 " ) {
87+ fmt .Println (strings .TrimPrefix (text , "553 " ))
88+ break
89+ } else if strings .HasPrefix (text , "554 " ) {
90+ fmt .Println (strings .TrimPrefix (text , "554 " ))
91+ break
92+ } else if strings .HasPrefix (text , "555 " ) {
93+ fmt .Println (strings .TrimPrefix (text , "555 " ))
94+ break
95+ } else if strings .HasPrefix (text , "650 " ) {
96+ fmt .Println (strings .TrimPrefix (text , "650 " ))
97+ break
98+ } else {
99+ fmt .Println (text )
100+ }
101+ }
102+ }
103+
104+ // Send non-interactive command and exit
105+ if * cmdPtr != "" {
106+ fmt .Fprintf (conn , * cmdPtr + "\n " )
107+ receive ()
108+ fmt .Fprintf (conn , "quit\n " )
109+ os .Exit (0 )
110+ }
111+
112+
113+ // Announce Tor version as banner
114+ fmt .Fprintf (conn , "getinfo version\n " )
115+ text , _ = bufio .NewReader (conn ).ReadString ('\n' )
116+ fmt .Print ("Tor " + strings .TrimPrefix (text , "250-version=" ))
117+
118+ for {
119+
120+ loop:
121+ text = ""
122+
123+ // Command prompt
124+ fmt .Print ("> " )
125+ text , _ = console .ReadString ('\n' )
126+
127+ // Internal TorMon commands
128+ switch strings .ToLower (strings .TrimRight (text , "\r \n " )) {
129+ case "help" :
130+ fmt .Print ("getinfo config/names\n getinfo signal/names\n getinfo info/names\n quit\n " )
131+ goto loop
132+ }
133+
134+ // Send Tor command
135+ fmt .Fprintf (conn , text )
136+
137+ // Handle special Tor commands
138+ switch strings .ToLower (strings .TrimRight (text , "\r \n " )) {
139+ case "signal shutdown" :
140+ os .Exit (0 )
141+ case "signal halt" :
142+ os .Exit (0 )
143+ case "signal int" :
144+ os .Exit (0 )
145+ case "signal term" :
146+ os .Exit (0 )
147+ }
148+
149+ // Receive command output
150+ receive ()
151+
152+ }
153+ }
0 commit comments