-
Notifications
You must be signed in to change notification settings - Fork 0
/
torcontroller.go
92 lines (74 loc) · 1.65 KB
/
torcontroller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package torcontroller
import (
"fmt"
"net/textproto"
// "io/ioutils"
)
type ErrInvalidPassword string
type Client struct {
c *textproto.Conn
addr string
isConnected bool
}
func (self ErrInvalidPassword) Error() string {
return fmt.Sprintf("Invalid password %q. Password MUST BE encoded in hexadecimal OR surrounded by double quotes", self)
}
func (this *Client) Close() error {
return this.c.Close()
}
func (this *Client) setConnected(connected bool) error {
this.isConnected = connected
return nil
}
func (this *Client) readResponse(id uint, expectedCode int) error {
this.c.StartResponse(id)
defer this.c.EndResponse(id)
_, _, err := this.c.ReadCodeLine(expectedCode)
if err != nil {
return err
}
return nil
}
func (this *Client) Authenticate(pass string) error {
var id uint
var err error
if len(pass) == 0 {
id, err = this.c.Cmd("AUTHENTICATE")
} else {
id, err = this.c.Cmd("AUTHENTICATE %s", pass)
}
if err != nil {
return err
}
if err = this.readResponse(id, 250); err != nil {
this.setConnected(false)
this.Close()
return err
}
return nil
}
func (this *Client) send(format string, args ...interface{}) (uint, error) {
return this.c.Cmd(format, args)
}
func (this *Client) ReConnect() (*Client, error) {
if this.isConnected == false {
c, err := textproto.Dial("tcp", this.addr)
if err != nil {
return nil, err
}
this.c = c
this.setConnected(true)
}
return this, nil
}
func NewClient(addr string) (*Client, error) {
c, err := textproto.Dial("tcp", addr)
if err != nil {
return nil, err
}
client := &Client{
c: c,
addr: addr}
client.setConnected(true)
return client, nil
}