forked from txthinking/brook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths5server.go
More file actions
122 lines (113 loc) · 2.43 KB
/
Copy paths5server.go
File metadata and controls
122 lines (113 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
package brook
import (
"crypto/aes"
"io"
"log"
"net"
"time"
"github.com/txthinking/ant"
"github.com/txthinking/socks5"
)
// S5Server is the server of socks5 encrypt protocol
type S5Server struct {
Address string
Password string
Timeout int
Deadline int
Listen net.Listener
}
// NewS5Server returns a new S5Server
func NewS5Server(address, password string, timeout, deadline int) *S5Server {
s := &S5Server{
Address: address,
Password: password,
Timeout: timeout,
Deadline: deadline,
}
return s
}
// ListenAndServe will let server start to listen and serve
func (s *S5Server) ListenAndServe() error {
ta, err := net.ResolveTCPAddr("tcp", s.Address)
if err != nil {
return err
}
l, err := net.ListenTCP("tcp", ta)
if err != nil {
return err
}
defer l.Close()
s.Listen = l
for {
c, err := l.AcceptTCP()
if err != nil {
return err
}
go func(c *net.TCPConn) {
if err := s.handle(c); err != nil {
log.Println(err)
}
}(c)
}
}
// Shutdown used to stop the server
func (s *S5Server) Shutdown() error {
if s.Listen == nil {
return nil
}
return s.Listen.Close()
}
func (s *S5Server) handle(c *net.TCPConn) error {
defer c.Close()
if s.Timeout != 0 {
if err := c.SetKeepAlivePeriod(time.Duration(s.Timeout) * time.Second); err != nil {
return err
}
}
if s.Deadline != 0 {
if err := c.SetDeadline(time.Now().Add(time.Duration(s.Deadline) * time.Second)); err != nil {
return err
}
}
cc, err := s.wrapCipherConn(c)
if err != nil {
return err
}
s5s := socks5.NewClassicServer(cc)
if err := s5s.Negotiate(); err != nil {
return err
}
r, err := s5s.GetRequest()
if err != nil {
return err
}
if r.Cmd == socks5.CmdConnect { // this 'if' is not necessary when SupportedCommands only contain conect method
rc, err := r.Connect(cc)
if err != nil {
return err
}
defer rc.Close()
if s.Timeout != 0 {
if err := rc.SetKeepAlivePeriod(time.Duration(s.Timeout) * time.Second); err != nil {
return err
}
}
if s.Deadline != 0 {
if err := rc.SetDeadline(time.Now().Add(time.Duration(s.Deadline) * time.Second)); err != nil {
return err
}
}
go func() {
_, _ = io.Copy(cc, rc)
}()
_, _ = io.Copy(rc, cc)
}
return nil
}
func (s *S5Server) wrapCipherConn(conn net.Conn) (*CipherConn, error) {
iv := make([]byte, aes.BlockSize)
if _, err := io.ReadFull(conn, iv); err != nil {
return nil, err
}
return NewCipherConn(conn, []byte(ant.MD5(s.Password)), iv)
}