forked from txthinking/brook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbkserver.go
175 lines (165 loc) · 3.57 KB
/
bkserver.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package brook
import (
"bytes"
"crypto/aes"
"errors"
"io"
"log"
"net"
"time"
"github.com/txthinking/ant"
"github.com/txthinking/brook/music"
"github.com/txthinking/socks5"
)
// BKServer is the server of brook protocol
type BKServer struct {
Address string
Password string
Timeout int
Deadline int
Music music.Music
Listen net.Listener
}
// NewBKServer returns a new BKServer
func NewBKServer(address, password string, timeout, deadline int, m string) (*BKServer, error) {
msc, err := music.GetMusic(m)
if err != nil {
return nil, err
}
s := &BKServer{
Address: address,
Password: password,
Timeout: timeout,
Deadline: deadline,
Music: msc,
}
return s, nil
}
// ListenAndServe will let server start to listen and serve
func (s *BKServer) 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 *BKServer) Shutdown() error {
if s.Listen == nil {
return nil
}
return s.Listen.Close()
}
func (s *BKServer) 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
}
}
if s.Music != nil {
tmp := make([]byte, s.Music.Length())
if _, err := io.ReadFull(c, tmp); err != nil {
return err
}
if !bytes.Equal(tmp, s.Music.GetSong()) {
log.Println("Got a invalid song")
if _, err := c.Write(s.Music.GetResponse(tmp)); err != nil {
return err
}
return nil
}
}
cc, err := s.wrapCipherConn(c)
if err != nil {
return err
}
bb := make([]byte, 1)
if _, err := io.ReadFull(cc, bb); err != nil {
return err
}
var addr []byte
if bb[0] == socks5.ATYPIPv4 {
addr = make([]byte, 4)
if _, err := io.ReadFull(cc, addr); err != nil {
return err
}
} else if bb[0] == socks5.ATYPIPv6 {
addr = make([]byte, 16)
if _, err := io.ReadFull(cc, addr); err != nil {
return err
}
} else if bb[0] == socks5.ATYPDomain {
dal := make([]byte, 1)
if _, err := io.ReadFull(cc, dal); err != nil {
return err
}
if dal[0] == 0 {
return err
}
addr = make([]byte, int(dal[0]))
if _, err := io.ReadFull(cc, addr); err != nil {
return err
}
addr = append(dal, addr...)
} else {
return errors.New("Unknown address type")
}
port := make([]byte, 2)
if _, err := io.ReadFull(cc, port); err != nil {
return err
}
address := socks5.ToAddress(bb[0], addr, port)
ta, err := net.ResolveTCPAddr("tcp", address)
if err != nil {
return err
}
rc, err := net.DialTCP("tcp", nil, ta)
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 *BKServer) 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)
}