forked from txthinking/brook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths5client.go
More file actions
125 lines (115 loc) · 2.43 KB
/
Copy paths5client.go
File metadata and controls
125 lines (115 loc) · 2.43 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package brook
import (
"crypto/aes"
"crypto/rand"
"io"
"log"
"net"
"time"
"github.com/txthinking/ant"
)
// S5Client is the client of socks5 encrypt protocol
type S5Client struct {
Address string
Password string
Server string
Timeout int
Deadline int
Dial Dialer
Listen net.Listener
}
// NewS5Client returns a new S5Client
func NewS5Client(address, server, password string, timeout, deadline int, dial Dialer) *S5Client {
if dial == nil {
dial = &DefaultDial{}
}
c := &S5Client{
Address: address,
Server: server,
Password: password,
Timeout: timeout,
Deadline: deadline,
Dial: dial,
}
return c
}
// ListenAndServe will let client start to listen and serve
func (c *S5Client) ListenAndServe() error {
ta, err := net.ResolveTCPAddr("tcp", c.Address)
if err != nil {
return err
}
l, err := net.ListenTCP("tcp", ta)
if err != nil {
return err
}
defer l.Close()
c.Listen = l
for {
conn, err := l.AcceptTCP()
if err != nil {
return err
}
go func(conn *net.TCPConn) {
if err := c.handle(conn); err != nil {
log.Println(err)
}
}(conn)
}
}
// Shutdown used to stop the client
func (c *S5Client) Shutdown() error {
if c.Listen == nil {
return nil
}
return c.Listen.Close()
}
func (c *S5Client) handle(conn *net.TCPConn) error {
defer conn.Close()
if c.Timeout != 0 {
if err := conn.SetKeepAlivePeriod(time.Duration(c.Timeout) * time.Second); err != nil {
return err
}
}
if c.Deadline != 0 {
if err := conn.SetDeadline(time.Now().Add(time.Duration(c.Deadline) * time.Second)); err != nil {
return err
}
}
rc, err := c.Dial.Dial("tcp", c.Server)
if err != nil {
return err
}
defer rc.Close()
if c.Timeout != 0 {
if rtc, ok := rc.(*net.TCPConn); ok {
if err := rtc.SetKeepAlivePeriod(time.Duration(c.Timeout) * time.Second); err != nil {
return err
}
}
}
if c.Deadline != 0 {
if err := rc.SetDeadline(time.Now().Add(time.Duration(c.Deadline) * time.Second)); err != nil {
return err
}
}
crc, err := c.wrapCipherConn(rc)
if err != nil {
return err
}
go func() {
_, _ = io.Copy(conn, crc)
}()
_, _ = io.Copy(crc, conn)
return nil
}
func (c *S5Client) wrapCipherConn(conn net.Conn) (*CipherConn, error) {
iv := make([]byte, aes.BlockSize)
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
if _, err := conn.Write(iv); err != nil {
return nil, err
}
return NewCipherConn(conn, []byte(ant.MD5(c.Password)), iv)
}