-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
listener.go
196 lines (165 loc) · 4.25 KB
/
listener.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
package kcp
import (
"context"
"crypto/cipher"
"crypto/tls"
"sync"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
"v2ray.com/core/transport/internet"
v2tls "v2ray.com/core/transport/internet/tls"
"v2ray.com/core/transport/internet/udp"
)
type ConnectionID struct {
Remote net.Address
Port net.Port
Conv uint16
}
// Listener defines a server listening for connections
type Listener struct {
sync.Mutex
sessions map[ConnectionID]*Connection
hub *udp.Hub
tlsConfig *tls.Config
config *Config
reader PacketReader
header internet.PacketHeader
security cipher.AEAD
addConn internet.ConnHandler
}
func NewListener(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig, addConn internet.ConnHandler) (*Listener, error) {
kcpSettings := streamSettings.ProtocolSettings.(*Config)
header, err := kcpSettings.GetPackerHeader()
if err != nil {
return nil, newError("failed to create packet header").Base(err).AtError()
}
security, err := kcpSettings.GetSecurity()
if err != nil {
return nil, newError("failed to create security").Base(err).AtError()
}
l := &Listener{
header: header,
security: security,
reader: &KCPPacketReader{
Header: header,
Security: security,
},
sessions: make(map[ConnectionID]*Connection),
config: kcpSettings,
addConn: addConn,
}
if config := v2tls.ConfigFromStreamSettings(streamSettings); config != nil {
l.tlsConfig = config.GetTLSConfig()
}
hub, err := udp.ListenUDP(ctx, address, port, streamSettings, udp.HubCapacity(1024))
if err != nil {
return nil, err
}
l.Lock()
l.hub = hub
l.Unlock()
newError("listening on ", address, ":", port).WriteToLog()
go l.handlePackets()
return l, nil
}
func (l *Listener) handlePackets() {
receive := l.hub.Receive()
for payload := range receive {
l.OnReceive(payload.Content, payload.Source)
}
}
func (l *Listener) OnReceive(payload *buf.Buffer, src net.Destination) {
segments := l.reader.Read(payload.Bytes())
payload.Release()
if len(segments) == 0 {
newError("discarding invalid payload from ", src).WriteToLog()
return
}
conv := segments[0].Conversation()
cmd := segments[0].Command()
id := ConnectionID{
Remote: src.Address,
Port: src.Port,
Conv: conv,
}
l.Lock()
defer l.Unlock()
conn, found := l.sessions[id]
if !found {
if cmd == CommandTerminate {
return
}
writer := &Writer{
id: id,
hub: l.hub,
dest: src,
listener: l,
}
remoteAddr := &net.UDPAddr{
IP: src.Address.IP(),
Port: int(src.Port),
}
localAddr := l.hub.Addr()
conn = NewConnection(ConnMetadata{
LocalAddr: localAddr,
RemoteAddr: remoteAddr,
Conversation: conv,
}, &KCPPacketWriter{
Header: l.header,
Security: l.security,
Writer: writer,
}, writer, l.config)
var netConn internet.Connection = conn
if l.tlsConfig != nil {
tlsConn := tls.Server(conn, l.tlsConfig)
netConn = tlsConn
}
l.addConn(netConn)
l.sessions[id] = conn
}
conn.Input(segments)
}
func (l *Listener) Remove(id ConnectionID) {
l.Lock()
delete(l.sessions, id)
l.Unlock()
}
// Close stops listening on the UDP address. Already Accepted connections are not closed.
func (l *Listener) Close() error {
l.hub.Close()
l.Lock()
defer l.Unlock()
for _, conn := range l.sessions {
go conn.Terminate()
}
return nil
}
func (l *Listener) ActiveConnections() int {
l.Lock()
defer l.Unlock()
return len(l.sessions)
}
// Addr returns the listener's network address, The Addr returned is shared by all invocations of Addr, so do not modify it.
func (l *Listener) Addr() net.Addr {
return l.hub.Addr()
}
type Writer struct {
id ConnectionID
dest net.Destination
hub *udp.Hub
listener *Listener
}
func (w *Writer) Write(payload []byte) (int, error) {
return w.hub.WriteTo(payload, w.dest)
}
func (w *Writer) Close() error {
w.listener.Remove(w.id)
return nil
}
func ListenKCP(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig, addConn internet.ConnHandler) (internet.Listener, error) {
return NewListener(ctx, address, port, streamSettings, addConn)
}
func init() {
common.Must(internet.RegisterTransportListener(protocolName, ListenKCP))
}