2016-06-17 10:51:41 -04:00
|
|
|
package kcp
|
|
|
|
|
|
|
|
import (
|
2017-02-23 19:05:16 -05:00
|
|
|
"context"
|
2017-01-13 18:27:45 -05:00
|
|
|
"crypto/cipher"
|
2016-09-30 10:53:40 -04:00
|
|
|
"crypto/tls"
|
2016-06-17 10:51:41 -04:00
|
|
|
"sync"
|
|
|
|
|
2017-01-03 09:16:48 -05:00
|
|
|
"v2ray.com/core/common"
|
2016-12-09 05:35:27 -05:00
|
|
|
"v2ray.com/core/common/buf"
|
2017-08-29 06:56:57 -04:00
|
|
|
"v2ray.com/core/common/net"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/transport/internet"
|
2016-09-30 10:53:40 -04:00
|
|
|
v2tls "v2ray.com/core/transport/internet/tls"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/transport/internet/udp"
|
2016-06-17 10:51:41 -04:00
|
|
|
)
|
|
|
|
|
2016-12-21 09:37:16 -05:00
|
|
|
type ConnectionID struct {
|
2017-08-29 06:56:57 -04:00
|
|
|
Remote net.Address
|
|
|
|
Port net.Port
|
2016-11-27 02:58:31 -05:00
|
|
|
Conv uint16
|
|
|
|
}
|
|
|
|
|
2016-06-17 10:51:41 -04:00
|
|
|
// Listener defines a server listening for connections
|
|
|
|
type Listener struct {
|
|
|
|
sync.Mutex
|
2017-02-26 08:38:41 -05:00
|
|
|
sessions map[ConnectionID]*Connection
|
|
|
|
hub *udp.Hub
|
|
|
|
tlsConfig *tls.Config
|
|
|
|
config *Config
|
|
|
|
reader PacketReader
|
|
|
|
header internet.PacketHeader
|
|
|
|
security cipher.AEAD
|
2018-02-08 09:39:46 -05:00
|
|
|
addConn internet.ConnHandler
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
|
|
|
|
2018-02-08 09:39:46 -05:00
|
|
|
func NewListener(ctx context.Context, address net.Address, port net.Port, addConn internet.ConnHandler) (*Listener, error) {
|
2017-02-23 19:05:16 -05:00
|
|
|
networkSettings := internet.TransportSettingsFromContext(ctx)
|
2016-10-02 17:43:58 -04:00
|
|
|
kcpSettings := networkSettings.(*Config)
|
|
|
|
|
2016-12-08 10:27:41 -05:00
|
|
|
header, err := kcpSettings.GetPackerHeader()
|
2016-08-06 15:59:22 -04:00
|
|
|
if err != nil {
|
2017-04-09 07:30:46 -04:00
|
|
|
return nil, newError("failed to create packet header").Base(err).AtError()
|
2016-12-08 10:27:41 -05:00
|
|
|
}
|
|
|
|
security, err := kcpSettings.GetSecurity()
|
|
|
|
if err != nil {
|
2017-04-09 07:30:46 -04:00
|
|
|
return nil, newError("failed to create security").Base(err).AtError()
|
2016-08-06 15:59:22 -04:00
|
|
|
}
|
2016-06-17 10:51:41 -04:00
|
|
|
l := &Listener{
|
2016-12-08 10:27:41 -05:00
|
|
|
header: header,
|
|
|
|
security: security,
|
|
|
|
reader: &KCPPacketReader{
|
|
|
|
Header: header,
|
|
|
|
Security: security,
|
|
|
|
},
|
2017-02-26 08:38:41 -05:00
|
|
|
sessions: make(map[ConnectionID]*Connection),
|
|
|
|
config: kcpSettings,
|
2017-05-08 18:01:15 -04:00
|
|
|
addConn: addConn,
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
2017-12-16 18:53:17 -05:00
|
|
|
|
|
|
|
if config := v2tls.ConfigFromContext(ctx); config != nil {
|
|
|
|
l.tlsConfig = config.GetTLSConfig()
|
2016-09-30 10:53:40 -04:00
|
|
|
}
|
2017-12-16 18:53:17 -05:00
|
|
|
|
2018-02-12 09:08:20 -05:00
|
|
|
hub, err := udp.ListenUDP(address, port, l.OnReceive, udp.HubCapacity(1024))
|
2016-06-17 10:51:41 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-17 18:04:25 -05:00
|
|
|
l.Lock()
|
2016-06-17 10:51:41 -04:00
|
|
|
l.hub = hub
|
2017-02-17 18:04:25 -05:00
|
|
|
l.Unlock()
|
2017-12-19 15:28:12 -05:00
|
|
|
newError("listening on ", address, ":", port).WriteToLog()
|
2016-06-17 10:51:41 -04:00
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
2018-01-17 11:36:14 -05:00
|
|
|
func (l *Listener) OnReceive(payload *buf.Buffer, src net.Destination, originalDest net.Destination) {
|
|
|
|
segments := l.reader.Read(payload.Bytes())
|
2018-02-24 18:02:31 -05:00
|
|
|
payload.Release()
|
|
|
|
|
2016-12-08 10:27:41 -05:00
|
|
|
if len(segments) == 0 {
|
2017-12-19 15:28:12 -05:00
|
|
|
newError("discarding invalid payload from ", src).WriteToLog()
|
2016-06-17 10:51:41 -04:00
|
|
|
return
|
|
|
|
}
|
2016-12-08 10:27:41 -05:00
|
|
|
|
2018-01-17 11:36:14 -05:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
2017-02-27 09:13:22 -05:00
|
|
|
|
2018-01-17 11:36:14 -05:00
|
|
|
if l.hub == nil {
|
2016-06-17 10:51:41 -04:00
|
|
|
return
|
|
|
|
}
|
2017-02-27 09:13:22 -05:00
|
|
|
|
2016-12-08 10:27:41 -05:00
|
|
|
conv := segments[0].Conversation()
|
|
|
|
cmd := segments[0].Command()
|
|
|
|
|
2016-12-21 09:37:16 -05:00
|
|
|
id := ConnectionID{
|
2016-11-27 02:58:31 -05:00
|
|
|
Remote: src.Address,
|
|
|
|
Port: src.Port,
|
|
|
|
Conv: conv,
|
|
|
|
}
|
2018-01-17 11:36:14 -05:00
|
|
|
conn, found := l.sessions[id]
|
2016-11-27 02:58:31 -05:00
|
|
|
|
2016-06-17 10:51:41 -04:00
|
|
|
if !found {
|
2016-07-14 16:10:37 -04:00
|
|
|
if cmd == CommandTerminate {
|
2016-07-14 06:57:14 -04:00
|
|
|
return
|
|
|
|
}
|
2016-06-17 10:51:41 -04:00
|
|
|
writer := &Writer{
|
2016-11-27 02:58:31 -05:00
|
|
|
id: id,
|
2018-01-17 11:36:14 -05:00
|
|
|
hub: l.hub,
|
2016-06-17 10:51:41 -04:00
|
|
|
dest: src,
|
2018-01-17 11:36:14 -05:00
|
|
|
listener: l,
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
2016-11-27 02:58:31 -05:00
|
|
|
remoteAddr := &net.UDPAddr{
|
2016-09-20 05:53:05 -04:00
|
|
|
IP: src.Address.IP(),
|
|
|
|
Port: int(src.Port),
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
2018-01-17 11:36:14 -05:00
|
|
|
localAddr := l.hub.Addr()
|
2017-12-14 17:24:40 -05:00
|
|
|
conn = NewConnection(ConnMetadata{
|
|
|
|
LocalAddr: localAddr,
|
|
|
|
RemoteAddr: remoteAddr,
|
|
|
|
Conversation: conv,
|
2017-12-03 15:29:27 -05:00
|
|
|
}, &KCPPacketWriter{
|
2018-01-17 11:36:14 -05:00
|
|
|
Header: l.header,
|
|
|
|
Security: l.security,
|
2017-12-03 15:29:27 -05:00
|
|
|
Writer: writer,
|
2018-01-17 11:36:14 -05:00
|
|
|
}, writer, l.config)
|
2017-02-26 08:38:41 -05:00
|
|
|
var netConn internet.Connection = conn
|
2018-01-17 11:36:14 -05:00
|
|
|
if l.tlsConfig != nil {
|
|
|
|
tlsConn := tls.Server(conn, l.tlsConfig)
|
2017-04-07 15:54:40 -04:00
|
|
|
netConn = tlsConn
|
2017-02-26 08:38:41 -05:00
|
|
|
}
|
|
|
|
|
2018-02-08 09:39:46 -05:00
|
|
|
l.addConn(netConn)
|
2018-01-17 11:36:14 -05:00
|
|
|
l.sessions[id] = conn
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
2016-12-08 10:27:41 -05:00
|
|
|
conn.Input(segments)
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
|
|
|
|
2018-01-17 11:36:14 -05:00
|
|
|
func (l *Listener) Remove(id ConnectionID) {
|
2018-02-08 09:39:46 -05:00
|
|
|
l.Lock()
|
|
|
|
delete(l.sessions, id)
|
|
|
|
l.Unlock()
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close stops listening on the UDP address. Already Accepted connections are not closed.
|
2018-01-17 11:36:14 -05:00
|
|
|
func (l *Listener) Close() error {
|
|
|
|
l.hub.Close()
|
2017-02-17 18:28:50 -05:00
|
|
|
|
2018-01-17 11:36:14 -05:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
2016-06-17 10:51:41 -04:00
|
|
|
|
2018-01-17 11:36:14 -05:00
|
|
|
for _, conn := range l.sessions {
|
2016-06-18 15:18:21 -04:00
|
|
|
go conn.Terminate()
|
|
|
|
}
|
2016-06-17 10:51:41 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-17 11:36:14 -05:00
|
|
|
func (l *Listener) ActiveConnections() int {
|
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
2016-07-12 17:54:54 -04:00
|
|
|
|
2018-01-17 11:36:14 -05:00
|
|
|
return len(l.sessions)
|
2016-07-12 17:54:54 -04:00
|
|
|
}
|
|
|
|
|
2016-06-17 10:51:41 -04:00
|
|
|
// Addr returns the listener's network address, The Addr returned is shared by all invocations of Addr, so do not modify it.
|
2018-01-17 11:36:14 -05:00
|
|
|
func (l *Listener) Addr() net.Addr {
|
|
|
|
return l.hub.Addr()
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Writer struct {
|
2016-12-21 09:37:16 -05:00
|
|
|
id ConnectionID
|
2017-08-29 06:56:57 -04:00
|
|
|
dest net.Destination
|
2016-12-21 09:48:39 -05:00
|
|
|
hub *udp.Hub
|
2016-06-17 10:51:41 -04:00
|
|
|
listener *Listener
|
|
|
|
}
|
|
|
|
|
2018-01-17 11:36:14 -05:00
|
|
|
func (w *Writer) Write(payload []byte) (int, error) {
|
|
|
|
return w.hub.WriteTo(payload, w.dest)
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
|
|
|
|
2018-01-17 11:36:14 -05:00
|
|
|
func (w *Writer) Close() error {
|
|
|
|
w.listener.Remove(w.id)
|
2016-06-17 10:51:41 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-08 09:39:46 -05:00
|
|
|
func ListenKCP(ctx context.Context, address net.Address, port net.Port, addConn internet.ConnHandler) (internet.Listener, error) {
|
2017-05-08 18:01:15 -04:00
|
|
|
return NewListener(ctx, address, port, addConn)
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2017-01-12 06:54:34 -05:00
|
|
|
common.Must(internet.RegisterTransportListener(internet.TransportProtocol_MKCP, ListenKCP))
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|