forked from ginuerzh/gost
-
Notifications
You must be signed in to change notification settings - Fork 6
/
quic.go
378 lines (325 loc) · 8.65 KB
/
quic.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
package gost
import (
"context"
"crypto/tls"
"errors"
"net"
"sync"
"time"
"github.com/apernet/hysteria/core/v2/congestion"
"github.com/apernet/hysteria/extras/v2/correctnet"
"github.com/apernet/quic-go"
"github.com/go-log/log"
"golang.org/x/crypto/blake2s"
)
type quicSession struct {
session quic.EarlyConnection
}
func (session *quicSession) GetConn() (*quicConn, error) {
stream, err := session.session.OpenStream()
if err != nil {
return nil, err
}
return &quicConn{
Stream: stream,
laddr: session.session.LocalAddr(),
raddr: session.session.RemoteAddr(),
}, nil
}
func (session *quicSession) Close() error {
return session.session.CloseWithError(quic.ApplicationErrorCode(0), "closed")
}
type quicTransporter struct {
config *QUICConfig
sessionMutex sync.Mutex
sessions map[string]*quicSession
}
// QUICTransporter creates a Transporter that is used by QUIC proxy client.
func QUICTransporter(config *QUICConfig) Transporter {
if config == nil {
config = &QUICConfig{}
}
if config.ReceiveWindowConn == 0 {
config.ReceiveWindowConn = DefaultReceiveWindowConn
}
if config.ReceiveWindow == 0 {
config.ReceiveWindow = DefaultReceiveWindow
}
return &quicTransporter{
config: config,
sessions: make(map[string]*quicSession),
}
}
func (tr *quicTransporter) Dial(addr string, options ...DialOption) (conn net.Conn, err error) {
opts := &DialOptions{}
for _, option := range options {
option(opts)
}
tr.sessionMutex.Lock()
defer tr.sessionMutex.Unlock()
session, ok := tr.sessions[addr]
if ok {
conn, err = session.GetConn()
if err != nil {
session.Close()
delete(tr.sessions, addr)
ok = false
}
}
if !ok {
var udpAddr net.Addr
udpAddr, err = net.ResolveUDPAddr("udp", addr)
if err != nil {
return
}
var pc net.PacketConn
pc, err = net.ListenUDP("udp", nil)
if err != nil {
return
}
if tr.config != nil && tr.config.Key != nil {
keyCapacity := len(tr.config.Key) + cipherSuffixLen
pc = &quicCipherConn{
PacketConn: pc,
keyR: append(make([]byte, 0, keyCapacity), tr.config.Key...),
keyW: append(make([]byte, 0, keyCapacity), tr.config.Key...),
}
}
session, err = tr.initSession(udpAddr, pc)
if err != nil {
pc.Close()
return nil, err
}
conn, err = session.GetConn()
if err != nil {
session.Close()
pc.Close()
return nil, err
}
tr.sessions[addr] = session
}
return conn, nil
}
func (tr *quicTransporter) Handshake(conn net.Conn, options ...HandshakeOption) (net.Conn, error) {
return conn, nil
}
func (tr *quicTransporter) initSession(addr net.Addr, conn net.PacketConn) (*quicSession, error) {
config := tr.config
if config == nil {
config = &QUICConfig{}
}
if config.TLSConfig == nil {
config.TLSConfig = &tls.Config{InsecureSkipVerify: true}
}
quicConfig := &quic.Config{
HandshakeIdleTimeout: config.Timeout,
MaxIdleTimeout: config.IdleTimeout,
KeepAlivePeriod: config.KeepAlivePeriod,
Versions: []quic.Version{
quic.Version1,
quic.Version2,
},
InitialStreamReceiveWindow: config.ReceiveWindowConn,
MaxStreamReceiveWindow: config.ReceiveWindowConn,
InitialConnectionReceiveWindow: config.ReceiveWindow,
MaxConnectionReceiveWindow: config.ReceiveWindow,
}
session, err := quic.DialEarly(context.Background(), conn, addr, tlsConfigQUICALPN(config.TLSConfig), quicConfig)
if err != nil {
log.Logf("quic dial %s: %v", addr, err)
return nil, err
}
if config.SendBps > 0 {
congestion.UseBrutal(session, config.SendBps)
} else {
congestion.UseBBR(session)
}
return &quicSession{session: session}, nil
}
func (tr *quicTransporter) Multiplex() bool {
return true
}
// QUICConfig is the config for QUIC client and server
type QUICConfig struct {
TLSConfig *tls.Config
Timeout time.Duration
KeepAlive bool
KeepAlivePeriod time.Duration
IdleTimeout time.Duration
Key []byte
SendBps uint64
ReceiveWindowConn uint64
ReceiveWindow uint64
MaxConnClient int64
}
const (
MbpsToBps = 1024 * 1024 / 8 // Mbit/Byte
DefaultReceiveWindowConn = 8 * 1024 * 1024 // 8MB
DefaultReceiveWindow = 20 * 1024 * 1024 // 20MB
DefaultMaxConnClient = 1024
cipherSuffixLen = 4
)
type quicListener struct {
ln quic.EarlyListener
connChan chan net.Conn
errChan chan error
}
// QUICListener creates a Listener for QUIC proxy server.
func QUICListener(addr string, config *QUICConfig) (Listener, error) {
if config == nil {
config = &QUICConfig{}
}
if config.ReceiveWindowConn == 0 {
config.ReceiveWindowConn = DefaultReceiveWindowConn
}
if config.ReceiveWindow == 0 {
config.ReceiveWindow = DefaultReceiveWindow
}
if config.MaxConnClient == 0 {
config.MaxConnClient = DefaultMaxConnClient
}
quicConfig := &quic.Config{
HandshakeIdleTimeout: config.Timeout,
KeepAlivePeriod: config.KeepAlivePeriod,
MaxIdleTimeout: config.IdleTimeout,
Versions: []quic.Version{
quic.Version1,
quic.Version2,
},
InitialStreamReceiveWindow: config.ReceiveWindowConn,
MaxStreamReceiveWindow: config.ReceiveWindowConn,
InitialConnectionReceiveWindow: config.ReceiveWindow,
MaxConnectionReceiveWindow: config.ReceiveWindow,
MaxIncomingStreams: config.MaxConnClient,
}
tlsConfig := config.TLSConfig
if tlsConfig == nil {
tlsConfig = DefaultTLSConfig
}
var conn net.PacketConn
udpAddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return nil, err
}
conn, err = correctnet.ListenUDP("udp", udpAddr)
if err != nil {
return nil, err
}
if config.Key != nil {
keyCapacity := len(config.Key) + cipherSuffixLen
conn = &quicCipherConn{
PacketConn: conn,
keyR: append(make([]byte, 0, keyCapacity), config.Key...),
keyW: append(make([]byte, 0, keyCapacity), config.Key...),
}
}
ln, err := quic.ListenEarly(conn, tlsConfigQUICALPN(tlsConfig), quicConfig)
if err != nil {
return nil, err
}
l := &quicListener{
ln: *ln,
connChan: make(chan net.Conn, 1024),
errChan: make(chan error, 1),
}
go l.listenLoop(config.SendBps)
return l, nil
}
func (l *quicListener) listenLoop(bps uint64) {
for {
session, err := l.ln.Accept(context.Background())
if err != nil {
log.Log("[quic] accept:", err)
l.errChan <- err
close(l.errChan)
return
}
if bps > 0 {
congestion.UseBrutal(session, bps)
} else {
congestion.UseBBR(session)
}
go l.sessionLoop(session)
}
}
func (l *quicListener) sessionLoop(session quic.Connection) {
log.Logf("[quic] %s <-> %s", session.RemoteAddr(), session.LocalAddr())
defer log.Logf("[quic] %s >-< %s", session.RemoteAddr(), session.LocalAddr())
for {
stream, err := session.AcceptStream(context.Background())
if err != nil {
log.Log("[quic] accept stream:", err)
session.CloseWithError(quic.ApplicationErrorCode(0), "closed")
return
}
cc := &quicConn{Stream: stream, laddr: session.LocalAddr(), raddr: session.RemoteAddr()}
select {
case l.connChan <- cc:
default:
cc.Close()
log.Logf("[quic] %s - %s: connection queue is full", session.RemoteAddr(), session.LocalAddr())
}
}
}
func (l *quicListener) Accept() (conn net.Conn, err error) {
var ok bool
select {
case conn = <-l.connChan:
case err, ok = <-l.errChan:
if !ok {
err = errors.New("accpet on closed listener")
}
}
return
}
func (l *quicListener) Addr() net.Addr {
return l.ln.Addr()
}
func (l *quicListener) Close() error {
return l.ln.Close()
}
type quicConn struct {
quic.Stream
laddr net.Addr
raddr net.Addr
}
func (c *quicConn) LocalAddr() net.Addr {
return c.laddr
}
func (c *quicConn) RemoteAddr() net.Addr {
return c.raddr
}
func (c *quicConn) Close() error {
c.Stream.CancelRead(0)
return c.Stream.Close()
}
type quicCipherConn struct {
net.PacketConn
keyR, keyW []byte
}
func (conn *quicCipherConn) ReadFrom(data []byte) (n int, addr net.Addr, err error) {
if n, addr, err = conn.PacketConn.ReadFrom(data); n > cipherSuffixLen {
key := blake2s.Sum256(append(conn.keyR, data[n-cipherSuffixLen:n]...))
for i, c := range data[:n-cipherSuffixLen] {
data[i] = c ^ key[i%blake2s.Size]
}
}
return n, addr, err
}
func (conn *quicCipherConn) WriteTo(data []byte, addr net.Addr) (n int, err error) {
if n := len(data); n > cipherSuffixLen {
key := blake2s.Sum256(append(conn.keyW, data[n-cipherSuffixLen:n]...))
for i, c := range data[:n-cipherSuffixLen] {
data[i] = c ^ key[i%blake2s.Size]
}
}
return conn.PacketConn.WriteTo(data, addr)
}
func tlsConfigQUICALPN(tlsConfig *tls.Config) *tls.Config {
if tlsConfig == nil {
panic("quic: tlsconfig is nil")
}
tlsConfigQUIC := tlsConfig.Clone()
tlsConfigQUIC.NextProtos = []string{"http/3", "quic/v1"}
return tlsConfigQUIC
}