-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
dialer.go
165 lines (136 loc) · 3.59 KB
/
dialer.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
package quic
import (
"context"
"sync"
"time"
quic "github.com/lucas-clemente/quic-go"
"v2ray.com/core/common"
"v2ray.com/core/common/net"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/internet/tls"
)
type sessionContext struct {
rawConn *sysConn
session quic.Session
}
type clientSessions struct {
access sync.Mutex
sessions map[net.Destination][]*sessionContext
}
func isActive(s quic.Session) bool {
select {
case <-s.Context().Done():
return false
default:
return true
}
}
func removeInactiveSessions(sessions []*sessionContext) []*sessionContext {
activeSessions := make([]*sessionContext, 0, len(sessions))
for _, s := range sessions {
if isActive(s.session) {
activeSessions = append(activeSessions, s)
} else {
s.rawConn.Close()
}
}
if len(activeSessions) < len(sessions) {
return activeSessions
}
return sessions
}
func openStream(sessions []*sessionContext) (quic.Stream, net.Addr) {
for _, s := range sessions {
if !isActive(s.session) {
continue
}
stream, err := s.session.OpenStream()
if err != nil {
newError("failed to create stream").Base(err).AtWarning().WriteToLog()
continue
}
return stream, s.session.LocalAddr()
}
return nil, nil
}
func (s *clientSessions) openConnection(destAddr net.Addr, config *Config, tlsConfig *tls.Config, sockopt *internet.SocketConfig) (internet.Connection, error) {
s.access.Lock()
defer s.access.Unlock()
if s.sessions == nil {
s.sessions = make(map[net.Destination][]*sessionContext)
}
dest := net.DestinationFromAddr(destAddr)
var sessions []*sessionContext
if s, found := s.sessions[dest]; found {
sessions = s
}
{
stream, local := openStream(sessions)
if stream != nil {
return &interConn{
stream: stream,
local: local,
remote: destAddr,
}, nil
}
}
sessions = removeInactiveSessions(sessions)
rawConn, err := internet.ListenSystemPacket(context.Background(), &net.UDPAddr{
IP: []byte{0, 0, 0, 0},
Port: 0,
}, sockopt)
if err != nil {
return nil, err
}
quicConfig := &quic.Config{
ConnectionIDLength: 8,
HandshakeTimeout: time.Second * 8,
IdleTimeout: time.Second * 30,
MaxReceiveStreamFlowControlWindow: 128 * 1024,
MaxReceiveConnectionFlowControlWindow: 2 * 1024 * 1024,
MaxIncomingUniStreams: -1,
MaxIncomingStreams: 32,
}
conn, err := wrapSysConn(rawConn, config)
if err != nil {
rawConn.Close()
return nil, err
}
session, err := quic.DialContext(context.Background(), conn, destAddr, "", tlsConfig.GetTLSConfig(tls.WithDestination(dest)), quicConfig)
if err != nil {
conn.Close()
return nil, err
}
s.sessions[dest] = append(sessions, &sessionContext{
session: session,
rawConn: conn,
})
stream, err := session.OpenStream()
if err != nil {
return nil, err
}
return &interConn{
stream: stream,
local: session.LocalAddr(),
remote: destAddr,
}, nil
}
var client clientSessions
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
tlsConfig := tls.ConfigFromStreamSettings(streamSettings)
if tlsConfig == nil {
tlsConfig = &tls.Config{
ServerName: internalDomain,
AllowInsecure: true,
}
}
destAddr, err := net.ResolveUDPAddr("udp", dest.NetAddr())
if err != nil {
return nil, err
}
config := streamSettings.ProtocolSettings.(*Config)
return client.openConnection(destAddr, config, tlsConfig, streamSettings.SocketSettings)
}
func init() {
common.Must(internet.RegisterTransportDialer(protocolName, Dial))
}