forked from txthinking/brook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamclient.go
233 lines (218 loc) · 5.23 KB
/
streamclient.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright (c) 2016-present Cloud <cloud@txthinking.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package brook
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
"errors"
"io"
"net"
"time"
"github.com/txthinking/socks5"
"github.com/txthinking/x"
"golang.org/x/crypto/hkdf"
)
type StreamClient struct {
Server net.Conn
cn []byte
ca cipher.AEAD
sn []byte
sa cipher.AEAD
RB []byte
WB []byte
Timeout int
network string
src string
dst string
}
func NewStreamClient(network string, password []byte, src string, server net.Conn, timeout int, dst []byte) (Exchanger, error) {
if timeout != 0 {
if err := server.SetDeadline(time.Now().Add(time.Duration(timeout) * time.Second)); err != nil {
return nil, err
}
}
if len(dst) > 2048-2-16-4-16 {
return nil, errors.New("dst too long")
}
c := &StreamClient{network: network, Server: server, Timeout: timeout, src: src, dst: socks5.ToAddress(dst[0], dst[1:len(dst)-2], dst[len(dst)-2:])}
c.cn = x.BP12.Get().([]byte)
if _, err := io.ReadFull(rand.Reader, c.cn); err != nil {
x.BP12.Put(c.cn)
return nil, err
}
ck := x.BP32.Get().([]byte)
if _, err := io.ReadFull(hkdf.New(sha256.New, password, c.cn, ClientHKDFInfo), ck); err != nil {
x.BP12.Put(c.cn)
x.BP32.Put(ck)
return nil, err
}
if _, err := c.Server.Write(c.cn); err != nil {
x.BP12.Put(c.cn)
x.BP32.Put(ck)
return nil, err
}
cb, err := aes.NewCipher(ck)
if err != nil {
x.BP12.Put(c.cn)
x.BP32.Put(ck)
return nil, err
}
x.BP32.Put(ck)
c.ca, err = cipher.NewGCM(cb)
if err != nil {
x.BP12.Put(c.cn)
return nil, err
}
c.WB = x.BP2048.Get().([]byte)
i := time.Now().Unix()
if c.network == "tcp" && i%2 != 0 {
i += 1
}
if c.network == "udp" && i%2 != 1 {
i += 1
}
binary.BigEndian.PutUint32(c.WB[2+16:2+16+4], uint32(i))
copy(c.WB[2+16+4:2+16+4+len(dst)], dst)
if err := c.Write(4 + len(dst)); err != nil {
x.BP12.Put(c.cn)
x.BP2048.Put(c.WB)
return nil, err
}
c.sn = x.BP12.Get().([]byte)
if _, err := io.ReadFull(c.Server, c.sn); err != nil {
x.BP12.Put(c.cn)
x.BP2048.Put(c.WB)
x.BP12.Put(c.sn)
return nil, err
}
sk := x.BP32.Get().([]byte)
if _, err := io.ReadFull(hkdf.New(sha256.New, password, c.sn, ServerHKDFInfo), sk); err != nil {
x.BP12.Put(c.cn)
x.BP2048.Put(c.WB)
x.BP12.Put(c.sn)
x.BP32.Put(sk)
return nil, err
}
sb, err := aes.NewCipher(sk)
if err != nil {
x.BP12.Put(c.cn)
x.BP2048.Put(c.WB)
x.BP12.Put(c.sn)
x.BP32.Put(sk)
return nil, err
}
x.BP32.Put(sk)
c.sa, err = cipher.NewGCM(sb)
if err != nil {
x.BP12.Put(c.cn)
x.BP2048.Put(c.WB)
x.BP12.Put(c.sn)
return nil, err
}
if c.network == "tcp" {
c.RB = x.BP2048.Get().([]byte)
}
if c.network == "udp" {
x.BP2048.Put(c.WB)
c.WB = x.BP65507.Get().([]byte)
c.RB = x.BP65507.Get().([]byte)
}
return ClientGate(c)
}
func (c *StreamClient) Exchange(local net.Conn) error {
go func() {
for {
if c.Timeout != 0 {
if err := c.Server.SetDeadline(time.Now().Add(time.Duration(c.Timeout) * time.Second)); err != nil {
return
}
}
l, err := c.Read()
if err != nil {
return
}
if _, err := local.Write(c.RB[2+16 : 2+16+l]); err != nil {
return
}
}
}()
for {
if c.Timeout != 0 {
if err := local.SetDeadline(time.Now().Add(time.Duration(c.Timeout) * time.Second)); err != nil {
return nil
}
}
l, err := local.Read(c.WB[2+16 : len(c.WB)-16])
if err != nil {
return nil
}
if err := c.Write(l); err != nil {
return nil
}
}
return nil
}
func (c *StreamClient) Write(l int) error {
binary.BigEndian.PutUint16(c.WB[:2], uint16(l))
c.ca.Seal(c.WB[:0], c.cn, c.WB[:2], nil)
NextNonce(c.cn)
c.ca.Seal(c.WB[:2+16], c.cn, c.WB[2+16:2+16+l], nil)
if _, err := c.Server.Write(c.WB[:2+16+l+16]); err != nil {
return err
}
NextNonce(c.cn)
return nil
}
func (c *StreamClient) Read() (int, error) {
if _, err := io.ReadFull(c.Server, c.RB[:2+16]); err != nil {
return 0, err
}
if _, err := c.sa.Open(c.RB[:0], c.sn, c.RB[:2+16], nil); err != nil {
return 0, err
}
l := int(binary.BigEndian.Uint16(c.RB[:2]))
if _, err := io.ReadFull(c.Server, c.RB[2+16:2+16+l+16]); err != nil {
return 0, err
}
NextNonce(c.sn)
if _, err := c.sa.Open(c.RB[:2+16], c.sn, c.RB[2+16:2+16+l+16], nil); err != nil {
return 0, err
}
NextNonce(c.sn)
return l, nil
}
func (c *StreamClient) Clean() {
x.BP12.Put(c.cn)
x.BP12.Put(c.sn)
if c.network == "tcp" {
x.BP2048.Put(c.WB)
x.BP2048.Put(c.RB)
}
if c.network == "udp" {
x.BP65507.Put(c.WB)
x.BP65507.Put(c.RB)
}
}
func (s *StreamClient) Network() string {
return s.network
}
func (s *StreamClient) Src() string {
return s.src
}
func (s *StreamClient) Dst() string {
return s.dst
}