-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathserver.go
408 lines (375 loc) · 10.3 KB
/
server.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package reality
import (
"bufio"
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/ecdh"
"crypto/ed25519"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"io"
"net"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/cryptobyte"
)
type ServerConfig struct {
SNIAddr string `json:"sni_addr"`
ServerAddr string `json:"server_addr"`
SkipVerify bool `json:"skip_verify"`
PrivateKeyECDH string `json:"private_key_ecdh"`
PrivateKeySign string `json:"private_key_sign"`
ExpireSecond uint32 `json:"expire_second"`
Debug bool `json:"debug"`
ClientFingerPrint string `json:"finger_print,omitempty"`
privateKeyECDH *ecdh.PrivateKey
privateKeySign ed25519.PrivateKey
sniHost string
sniPort string
}
func NewServerConfig(sniAddr string, serverAddr string) (*ServerConfig, error) {
privateKeyECDH, err := ecdh.X25519().GenerateKey(rand.Reader)
if err != nil {
return nil, err
}
_, privateKeySign, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return nil, err
}
sniHost, sniPort, err := net.SplitHostPort(sniAddr)
if err != nil {
return nil, err
}
return &ServerConfig{
SNIAddr: sniAddr,
ServerAddr: serverAddr,
PrivateKeyECDH: base64.StdEncoding.EncodeToString(privateKeyECDH.Bytes()),
PrivateKeySign: base64.StdEncoding.EncodeToString(privateKeySign),
ExpireSecond: DefaultExpireSecond,
privateKeyECDH: privateKeyECDH,
privateKeySign: privateKeySign,
sniHost: sniHost,
sniPort: sniPort,
}, nil
}
func (c *ServerConfig) Validate() error {
if c.SNIAddr == "" {
return errors.New("SNI is required")
}
var err error
c.sniHost, c.sniPort, err = net.SplitHostPort(c.SNIAddr)
if err != nil {
return err
}
if c.ServerAddr == "" {
return errors.New("server address is required")
}
data, err := base64.StdEncoding.DecodeString(c.PrivateKeyECDH)
if err != nil {
return err
}
c.privateKeyECDH, err = ecdh.X25519().NewPrivateKey(data)
if err != nil {
return err
}
data, err = base64.StdEncoding.DecodeString(c.PrivateKeySign)
if err != nil {
return err
}
if len(data) != ed25519.PrivateKeySize {
return errors.New("private key sign length error")
}
c.privateKeySign = ed25519.PrivateKey(data)
if c.ExpireSecond == 0 {
c.ExpireSecond = DefaultExpireSecond
}
if c.ClientFingerPrint == "" {
c.ClientFingerPrint = "chrome"
}
return nil
}
func (c *ServerConfig) SNIHost() string {
return c.sniHost
}
func (c *ServerConfig) SNIPort() string {
return c.sniPort
}
func (s *ServerConfig) ToClientConfig(overlayData byte) *ClientConfig {
return &ClientConfig{
SNI: s.sniHost,
ServerAddr: s.ServerAddr,
SkipVerify: s.SkipVerify,
PublicKeyECDH: base64.StdEncoding.EncodeToString(s.privateKeyECDH.PublicKey().Bytes()),
PublicKeyVerify: base64.StdEncoding.EncodeToString(s.privateKeySign.Public().(ed25519.PublicKey)),
ExpireSecond: s.ExpireSecond,
Debug: s.Debug,
FingerPrint: s.ClientFingerPrint,
OverlayData: overlayData,
}
}
type Listener struct {
net.Listener
config *ServerConfig
chanConn chan net.Conn
chanErr chan error
logger logrus.FieldLogger
}
func Listen(laddr string, config *ServerConfig) (net.Listener, error) {
inner, err := net.Listen("tcp", laddr)
if err != nil {
return nil, err
}
l := &Listener{
Listener: inner,
config: config,
chanConn: make(chan net.Conn),
chanErr: make(chan error),
logger: GetLogger(config.Debug),
}
go func() {
for {
conn, err := l.Listener.Accept()
if err != nil {
l.chanErr <- err
close(l.chanConn)
return
}
go func() {
c, err := l.handshake(conn)
if err != nil {
if l.config.Debug {
l.logger.Warnln("handshake", conn.RemoteAddr(), err)
}
} else {
l.chanConn <- c
}
}()
}
}()
return l, nil
}
func (l *Listener) Accept() (net.Conn, error) {
if c, ok := <-l.chanConn; ok {
return c, nil
}
return nil, <-l.chanErr
}
// handshake 尝试处理私有握手,失败则进行客户端和代理目标转发,成功返回加密包装后的客户端连接
func (l *Listener) handshake(clientConn net.Conn) (net.Conn, error) {
logger := l.logger
targetConn, err := net.Dial("tcp", l.config.SNIAddr)
if err != nil {
return nil, errors.Join(ErrProxyDie, err)
}
// bufio.Reader是为了在读数据时,不是一个一个record读取,而是模仿一次性读取尽可能多的record
// io.TeeReader是为了在读数据时,同时互相转发
clientReader := bufio.NewReader(io.TeeReader(clientConn, targetConn))
targetReader := bufio.NewReader(io.TeeReader(targetConn, clientConn))
var aead cipher.AEAD
var plaintext []byte
readClientHello := func() error {
recordClientHello, err := readTlsRecord(clientReader)
if err != nil {
return err
}
var random, sessionId []byte
s := cryptobyte.String(recordClientHello.recordData)
if !s.Skip(6) || // skip type(1) length(3) version(2)
!s.ReadBytes(&random, 32) ||
!s.ReadUint8LengthPrefixed((*cryptobyte.String)(&sessionId)) ||
len(sessionId) != 32 {
return fmt.Errorf("invalid client hello: %x", hex.EncodeToString(recordClientHello.recordData))
}
logger.Debugf("random(public for ecdh): %x", random)
logger.Debugf("sessionId(ciphertext): %x", sessionId)
pub, err := ecdh.X25519().NewPublicKey(random)
if err != nil {
return err
}
sessionKey, err := l.config.privateKeyECDH.ECDH(pub)
if err != nil {
return err
}
logger.Debugf("sessionKey: %x", sessionKey)
block, err := aes.NewCipher(sessionKey)
if err != nil {
return err
}
aead, err = cipher.NewGCMWithNonceSize(block, 8)
if err != nil {
return err
}
nonce, err := generateNonce(aead.NonceSize(), sessionKey, l.config.ExpireSecond)
if err != nil {
return err
}
logger.Debugf("nonce: %x", nonce)
plaintext, err = aead.Open(nil, nonce, sessionId, nil)
if err != nil {
return err
}
logger.Debugf("plaintext: %x", plaintext)
if !bytes.HasPrefix(plaintext, Prefix) {
return fmt.Errorf("invalid prefix: %x", plaintext[:len(Prefix)])
}
logger.Debug("handshake ok")
return nil
}
if err = readClientHello(); err != nil {
go dup(clientConn, targetConn)
return nil, errors.Join(ErrVerifyFailed, err)
}
if _, err = serverOrder1.wait(targetReader, logger); err != nil {
go dup(clientConn, targetConn)
return nil, err
}
if _, err = clientOrder.wait(clientReader, logger); err != nil {
go dup(clientConn, targetConn)
return nil, err
}
records, err := serverOrder2.wait(targetReader, logger)
if err != nil {
go dup(clientConn, targetConn)
return nil, err
}
// 客户端和代理目标的tls握手已经完成,可以关闭目标的连接
targetConn.Close()
// 获取模拟目标的seq,如果有的话
seq := [8]byte{}
copy(seq[:], seqNumerOne[:])
if len(records) > 0 {
record := records[len(records)-1]
recordData := record.recordData
if len(recordData) > len(seq) {
copy(seq[:], recordData[:len(seq)])
}
}
logger.Debugf("seqNumer: %x", seq)
incSeq(seq[:])
// 读取客户端发送的附加内容
record, err := readTlsRecord(clientConn)
if err != nil {
return nil, err
}
overlayData := record.recordData[len(record.recordData)-1]
logger.Debugf("overlayData: %x", overlayData)
// 发送服务端签名
sign := ed25519.Sign(ed25519.PrivateKey(l.config.privateKeySign), plaintext)
logger.Debugf("sign: %x", sign)
record = newTLSRecord(
recordTypeApplicationData, versionTLS12,
generateRandomData(append(seq[:], sign...)), // record数据前缀模仿seq
)
if _, err = record.writeTo(clientConn); err != nil {
clientConn.Close()
return nil, err
}
return newWarpConn(clientConn, aead, overlayData, seq), nil
}
// dup 转发两个连接
func dup(clientConn net.Conn, proxyConn net.Conn) {
defer clientConn.Close()
defer proxyConn.Close()
go io.Copy(proxyConn, clientConn)
io.Copy(clientConn, proxyConn)
}
type recordOrders []struct {
recordType byte
handshakeType byte
optional bool
}
var serverOrder1 = recordOrders{
{
recordType: recordTypeHandshake,
handshakeType: typeServerHello,
},
{
recordType: recordTypeHandshake,
handshakeType: typeCertificate,
},
{
recordType: recordTypeHandshake,
handshakeType: typeServerKeyExchange,
},
{
recordType: recordTypeHandshake,
handshakeType: typeServerHelloDone,
},
}
var clientOrder = recordOrders{
{
recordType: recordTypeHandshake,
handshakeType: typeCertificate,
optional: true,
},
{
recordType: recordTypeHandshake,
handshakeType: typeClientKeyExchange,
},
{
recordType: recordTypeHandshake,
handshakeType: typeCertificateVerify,
optional: true,
},
{
recordType: recordTypeChangeCipherSpec,
},
{
recordType: recordTypeHandshake, // Encrypted Handshake Message(Finished)
},
}
var serverOrder2 = recordOrders{
{
recordType: recordTypeHandshake,
handshakeType: typeNewSessionTicket,
optional: true,
},
{
recordType: recordTypeChangeCipherSpec,
},
{
recordType: recordTypeHandshake, // Encrypted Handshake Message(Finished)
},
}
func (orders recordOrders) wait(reader io.Reader, logger logrus.FieldLogger) ([]*tlsRecord, error) {
records := make([]*tlsRecord, 0, len(orders))
orderPos := 0
for {
record, err := readTlsRecord(reader)
if err != nil {
return nil, err
}
records = append(records, record)
for pos := orderPos; pos < len(orders); pos++ {
o := orders[pos]
if o.handshakeType != 0 {
// 需要判断握手类型
if len(record.recordData) != 0 &&
record.recordData[0] == o.handshakeType {
orderPos = pos + 1
break
}
} else {
orderPos = pos + 1
break
}
if o.optional {
// 如果当前类型是可选的,继续向下查找
logger.Debugf("try optional record: %+v", record)
orderPos = pos + 1
continue
} else {
return nil, fmt.Errorf(
"invalid record, want %+v, got %d %x,",
o, record.recordType, record.recordData,
)
}
}
if orderPos == len(orders) {
return records, nil
}
}
}