forked from v2fly/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialer.go
113 lines (91 loc) · 2.65 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
package quic
import (
"context"
"sync"
"time"
"v2ray.com/core/transport/internet/tls"
quic "github.com/lucas-clemente/quic-go"
"v2ray.com/core/common"
"v2ray.com/core/common/net"
"v2ray.com/core/transport/internet"
)
type clientSessions struct {
access sync.Mutex
sessions map[net.Destination]quic.Session
}
func (s *clientSessions) getSession(destAddr net.Addr, config *Config, tlsConfig *tls.Config, sockopt *internet.SocketConfig) (quic.Session, error) {
s.access.Lock()
defer s.access.Unlock()
if s.sessions == nil {
s.sessions = make(map[net.Destination]quic.Session)
}
dest := net.DestinationFromAddr(destAddr)
if session, found := s.sessions[dest]; found {
select {
case <-session.Context().Done():
// Session has been closed. Creating a new one.
default:
return session, nil
}
}
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{
Versions: []quic.VersionNumber{quic.VersionMilestone0_10_0},
ConnectionIDLength: 12,
KeepAlive: true,
HandshakeTimeout: time.Second * 4,
IdleTimeout: time.Second * 300,
MaxReceiveStreamFlowControlWindow: 128 * 1024,
MaxReceiveConnectionFlowControlWindow: 512 * 1024,
MaxIncomingUniStreams: -1,
}
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 {
rawConn.Close()
return nil, err
}
s.sessions[dest] = session
return session, 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)
session, err := client.getSession(destAddr, config, tlsConfig, streamSettings.SocketSettings)
if err != nil {
return nil, err
}
conn, err := session.OpenStreamSync()
if err != nil {
return nil, err
}
return &interConn{
stream: conn,
local: session.LocalAddr(),
remote: destAddr,
}, nil
}
func init() {
common.Must(internet.RegisterTransportDialer(protocolName, Dial))
}