| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "net/textproto" |
| 7 | "sync" |
| 8 | ) |
| 9 | |
| 10 | // Conn is the connection to the Tor control port. |
| 11 | type Conn struct { |
| 12 | // DebugWriter is the writer that debug logs for this library (not Tor |
| 13 | // itself) will be written to. If nil, no debug logs are generated/written. |
| 14 | DebugWriter io.Writer |
| 15 | |
| 16 | // This is the underlying connection. |
| 17 | conn *textproto.Conn |
| 18 | |
| 19 | // This is set lazily by ProtocolInfo(). |
| 20 | protocolInfo *ProtocolInfo |
| 21 | |
| 22 | // True if Authenticate has been called successfully. |
| 23 | Authenticated bool |
| 24 | |
| 25 | // The lock fot eventListeners |
| 26 | eventListenersLock sync.RWMutex |
| 27 | // The value slices can be traversed outside of lock, they are completely |
| 28 | // replaced on change, never mutated. But the map itself must be locked on |
| 29 | // when reading or writing. |
| 30 | eventListeners map[EventCode][]chan<- Event |
| 31 | |
| 32 | // This mutex is locked on when an entire response needs to be read. It |
| 33 | // helps synchronize accesses to the response by the asynchronous response |
| 34 | // listeners and the synchronous responses. |
| 35 | readLock sync.Mutex |
| 36 | } |
| 37 | |
| 38 | // NewConn creates a Conn from the given textproto connection. |
| 39 | func NewConn(conn *textproto.Conn) *Conn { |
| 40 | return &Conn{ |
| 41 | conn: conn, |
| 42 | eventListeners: map[EventCode][]chan<- Event{}, |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func (c *Conn) sendRequestIgnoreResponse(format string, args ...interface{}) error { |
| 47 | _, err := c.SendRequest(format, args...) |
| 48 | return err |
| 49 | } |
| 50 | |
| 51 | // SendRequest sends a synchronous request to Tor and awaits the response. If |
| 52 | // the response errors, the error result will be set, but the response will be |
| 53 | // set also. This is usually not directly used by callers, but instead called by |
| 54 | // higher-level methods. |
| 55 | func (c *Conn) SendRequest(format string, args ...interface{}) (*Response, error) { |
| 56 | if c.debugEnabled() { |
| 57 | c.debugf("Write line: %v", fmt.Sprintf(format, args...)) |
| 58 | } |
| 59 | id, err := c.conn.Cmd(format, args...) |
| 60 | if err != nil { |
| 61 | return nil, err |
| 62 | } |
| 63 | c.readLock.Lock() |
| 64 | defer c.readLock.Unlock() |
| 65 | c.conn.StartResponse(id) |
| 66 | defer c.conn.EndResponse(id) |
| 67 | // Get the first non-async response |
| 68 | var resp *Response |
| 69 | for { |
| 70 | if resp, err = c.ReadResponse(); err != nil || !resp.IsAsync() { |
| 71 | break |
| 72 | } |
| 73 | c.relayAsyncEvents(resp) |
| 74 | } |
| 75 | if err == nil && !resp.IsOk() { |
| 76 | err = resp.Err |
| 77 | } |
| 78 | return resp, err |
| 79 | } |
| 80 | |
| 81 | // Close sends a QUIT and closes the underlying Tor connection. This does not |
| 82 | // error if the QUIT is not accepted but does relay any error that occurs while |
| 83 | // closing the underlying connection. |
| 84 | func (c *Conn) Close() error { |
| 85 | // Ignore the response and ignore the error |
| 86 | c.Quit() |
| 87 | return c.conn.Close() |
| 88 | } |
| 89 | |
| 90 | func (c *Conn) debugEnabled() bool { |
| 91 | return c.DebugWriter != nil |
| 92 | } |
| 93 | |
| 94 | func (c *Conn) debugf(format string, args ...interface{}) { |
| 95 | if w := c.DebugWriter; w != nil { |
| 96 | fmt.Fprintf(w, format+"\n", args...) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func (*Conn) protoErr(format string, args ...interface{}) textproto.ProtocolError { |
| 101 | return textproto.ProtocolError(fmt.Sprintf(format, args...)) |
| 102 | } |