-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
hub.go
137 lines (116 loc) · 3.03 KB
/
hub.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
package quic
import (
"context"
"time"
"v2ray.com/core/common"
"v2ray.com/core/common/net"
"v2ray.com/core/common/protocol/tls/cert"
"v2ray.com/core/common/signal/done"
quic "v2ray.com/core/external/github.com/lucas-clemente/quic-go"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/internet/tls"
)
// Listener is an internet.Listener that listens for TCP connections.
type Listener struct {
rawConn *sysConn
listener quic.Listener
done *done.Instance
addConn internet.ConnHandler
}
func (l *Listener) acceptStreams(session quic.Session) {
for {
stream, err := session.AcceptStream()
if err != nil {
newError("failed to accept stream").Base(err).WriteToLog()
select {
case <-session.Context().Done():
return
case <-l.done.Wait():
session.Close()
return
default:
time.Sleep(time.Second)
continue
}
}
conn := &interConn{
stream: stream,
local: session.LocalAddr(),
remote: session.RemoteAddr(),
}
l.addConn(conn)
}
}
func (l *Listener) keepAccepting() {
for {
conn, err := l.listener.Accept()
if err != nil {
newError("failed to accept QUIC sessions").Base(err).WriteToLog()
if l.done.Done() {
break
}
time.Sleep(time.Second)
continue
}
go l.acceptStreams(conn)
}
}
// Addr implements internet.Listener.Addr.
func (l *Listener) Addr() net.Addr {
return l.listener.Addr()
}
// Close implements internet.Listener.Close.
func (l *Listener) Close() error {
l.done.Close()
l.listener.Close()
l.rawConn.Close()
return nil
}
// Listen creates a new Listener based on configurations.
func Listen(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig, handler internet.ConnHandler) (internet.Listener, error) {
if address.Family().IsDomain() {
return nil, newError("domain address is not allows for listening quic")
}
tlsConfig := tls.ConfigFromStreamSettings(streamSettings)
if tlsConfig == nil {
tlsConfig = &tls.Config{
Certificate: []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil, cert.DNSNames(internalDomain), cert.CommonName(internalDomain)))},
}
}
config := streamSettings.ProtocolSettings.(*Config)
rawConn, err := internet.ListenSystemPacket(context.Background(), &net.UDPAddr{
IP: address.IP(),
Port: int(port),
}, streamSettings.SocketSettings)
if err != nil {
return nil, err
}
quicConfig := &quic.Config{
ConnectionIDLength: 12,
HandshakeTimeout: time.Second * 8,
IdleTimeout: time.Second * 45,
MaxIncomingStreams: 32,
MaxIncomingUniStreams: -1,
}
conn, err := wrapSysConn(rawConn, config)
if err != nil {
conn.Close()
return nil, err
}
qListener, err := quic.Listen(conn, tlsConfig.GetTLSConfig(), quicConfig)
if err != nil {
conn.Close()
return nil, err
}
listener := &Listener{
done: done.New(),
rawConn: conn,
listener: qListener,
addConn: handler,
}
go listener.keepAccepting()
return listener, nil
}
func init() {
common.Must(internet.RegisterTransportListener(protocolName, Listen))
}