1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-01 11:35:23 +00:00
v2fly/transport/internet/kcp/listener.go

291 lines
6.4 KiB
Go
Raw Normal View History

2016-06-17 14:51:41 +00:00
package kcp
import (
2016-09-30 14:53:40 +00:00
"crypto/tls"
2016-06-17 14:51:41 +00:00
"net"
"sync"
"time"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/serial"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet"
2016-11-27 07:58:31 +00:00
"v2ray.com/core/transport/internet/internal"
2016-09-30 14:53:40 +00:00
v2tls "v2ray.com/core/transport/internet/tls"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/transport/internet/udp"
2016-06-17 14:51:41 +00:00
)
2016-11-27 07:58:31 +00:00
type ConnectionId struct {
Remote v2net.Address
Port v2net.Port
Conv uint16
}
type ServerConnection struct {
id internal.ConnectionId
writer *Writer
local net.Addr
remote net.Addr
auth internet.Authenticator
input func([]byte)
}
func (o *ServerConnection) Read([]byte) (int, error) {
panic("KCP|ServerConnection: Read should not be called.")
}
func (o *ServerConnection) Write(b []byte) (int, error) {
return o.writer.Write(b)
}
func (o *ServerConnection) Close() error {
return o.writer.Close()
}
func (o *ServerConnection) Reset(auth internet.Authenticator, input func([]byte)) {
o.auth = auth
o.input = input
}
func (o *ServerConnection) Input(b *alloc.Buffer) {
defer b.Release()
if o.auth.Open(b) {
o.input(b.Value)
}
}
func (o *ServerConnection) LocalAddr() net.Addr {
return o.local
}
func (o *ServerConnection) RemoteAddr() net.Addr {
return o.remote
}
func (o *ServerConnection) SetDeadline(time.Time) error {
return nil
}
func (o *ServerConnection) SetReadDeadline(time.Time) error {
return nil
}
func (o *ServerConnection) SetWriteDeadline(time.Time) error {
return nil
}
func (o *ServerConnection) Id() internal.ConnectionId {
return o.id
}
2016-06-17 14:51:41 +00:00
// Listener defines a server listening for connections
type Listener struct {
sync.Mutex
running bool
2016-08-06 19:59:22 +00:00
authenticator internet.Authenticator
2016-11-27 07:58:31 +00:00
sessions map[ConnectionId]*Connection
2016-06-18 14:34:04 +00:00
awaitingConns chan *Connection
2016-06-17 14:51:41 +00:00
hub *udp.UDPHub
2016-09-30 14:53:40 +00:00
tlsConfig *tls.Config
2016-10-02 21:43:58 +00:00
config *Config
2016-06-17 14:51:41 +00:00
}
2016-09-30 14:53:40 +00:00
func NewListener(address v2net.Address, port v2net.Port, options internet.ListenOptions) (*Listener, error) {
2016-10-02 21:43:58 +00:00
networkSettings, err := options.Stream.GetEffectiveNetworkSettings()
if err != nil {
log.Error("KCP|Listener: Failed to get KCP settings: ", err)
return nil, err
}
kcpSettings := networkSettings.(*Config)
2016-11-27 08:02:27 +00:00
kcpSettings.ConnectionReuse = &ConnectionReuse{Enable: false}
2016-10-02 21:43:58 +00:00
auth, err := kcpSettings.GetAuthenticator()
2016-08-06 19:59:22 +00:00
if err != nil {
return nil, err
}
2016-06-17 14:51:41 +00:00
l := &Listener{
2016-08-06 19:59:22 +00:00
authenticator: auth,
2016-11-27 07:58:31 +00:00
sessions: make(map[ConnectionId]*Connection),
2016-06-18 14:34:04 +00:00
awaitingConns: make(chan *Connection, 64),
2016-08-15 20:20:16 +00:00
running: true,
2016-10-02 21:43:58 +00:00
config: kcpSettings,
2016-06-17 14:51:41 +00:00
}
2016-10-16 12:22:21 +00:00
if options.Stream != nil && options.Stream.HasSecuritySettings() {
2016-10-02 21:43:58 +00:00
securitySettings, err := options.Stream.GetEffectiveSecuritySettings()
if err != nil {
2016-10-16 12:22:21 +00:00
log.Error("KCP|Listener: Failed to get security settings: ", err)
2016-10-02 21:43:58 +00:00
return nil, err
}
2016-10-16 12:22:21 +00:00
switch securitySettings := securitySettings.(type) {
case *v2tls.Config:
l.tlsConfig = securitySettings.GetTLSConfig()
}
2016-09-30 14:53:40 +00:00
}
2016-11-18 20:30:03 +00:00
hub, err := udp.ListenUDP(address, port, udp.ListenOption{Callback: l.OnReceive, Concurrency: 2})
2016-06-17 14:51:41 +00:00
if err != nil {
return nil, err
}
l.hub = hub
2016-06-17 15:38:42 +00:00
log.Info("KCP|Listener: listening on ", address, ":", port)
2016-06-17 14:51:41 +00:00
return l, nil
}
2016-08-15 15:44:46 +00:00
func (this *Listener) OnReceive(payload *alloc.Buffer, session *proxy.SessionInfo) {
2016-06-17 14:51:41 +00:00
defer payload.Release()
2016-08-15 15:44:46 +00:00
src := session.Source
2016-08-06 19:59:22 +00:00
if valid := this.authenticator.Open(payload); !valid {
2016-06-17 15:38:42 +00:00
log.Info("KCP|Listener: discarding invalid payload from ", src)
2016-06-17 14:51:41 +00:00
return
}
if !this.running {
return
}
this.Lock()
defer this.Unlock()
if !this.running {
return
}
if payload.Len() < 4 {
return
}
2016-07-10 13:58:55 +00:00
conv := serial.BytesToUint16(payload.Value)
2016-07-14 20:10:37 +00:00
cmd := Command(payload.Value[2])
2016-11-27 07:58:31 +00:00
id := ConnectionId{
Remote: src.Address,
Port: src.Port,
Conv: conv,
}
conn, found := this.sessions[id]
2016-06-17 14:51:41 +00:00
if !found {
2016-07-14 20:10:37 +00:00
if cmd == CommandTerminate {
return
}
2016-06-17 14:51:41 +00:00
writer := &Writer{
2016-11-27 07:58:31 +00:00
id: id,
2016-06-17 14:51:41 +00:00
hub: this.hub,
dest: src,
listener: this,
}
2016-11-27 07:58:31 +00:00
remoteAddr := &net.UDPAddr{
2016-09-20 09:53:05 +00:00
IP: src.Address.IP(),
Port: int(src.Port),
2016-06-17 14:51:41 +00:00
}
2016-11-27 07:58:31 +00:00
localAddr := this.hub.Addr()
2016-10-02 21:43:58 +00:00
auth, err := this.config.GetAuthenticator()
2016-08-06 19:59:22 +00:00
if err != nil {
log.Error("KCP|Listener: Failed to create authenticator: ", err)
}
2016-11-27 07:58:31 +00:00
sConn := &ServerConnection{
id: internal.NewConnectionId(v2net.LocalHostIP, src),
local: localAddr,
remote: remoteAddr,
writer: writer,
}
conn = NewConnection(conv, sConn, this, auth, this.config)
2016-06-17 14:51:41 +00:00
select {
case this.awaitingConns <- conn:
case <-time.After(time.Second * 5):
conn.Close()
return
}
2016-11-27 07:58:31 +00:00
this.sessions[id] = conn
2016-06-17 14:51:41 +00:00
}
2016-07-05 21:02:52 +00:00
conn.Input(payload.Value)
2016-06-17 14:51:41 +00:00
}
2016-11-27 07:58:31 +00:00
func (this *Listener) Remove(id ConnectionId) {
2016-06-17 14:51:41 +00:00
if !this.running {
return
}
this.Lock()
defer this.Unlock()
if !this.running {
return
}
2016-11-27 07:58:31 +00:00
delete(this.sessions, id)
2016-06-17 14:51:41 +00:00
}
// Accept implements the Accept method in the Listener interface; it waits for the next call and returns a generic Conn.
func (this *Listener) Accept() (internet.Connection, error) {
for {
if !this.running {
2016-08-07 13:31:24 +00:00
return nil, ErrClosedListener
2016-06-17 14:51:41 +00:00
}
select {
2016-10-11 10:24:19 +00:00
case conn, open := <-this.awaitingConns:
if !open {
break
}
2016-09-30 14:53:40 +00:00
if this.tlsConfig != nil {
tlsConn := tls.Server(conn, this.tlsConfig)
return v2tls.NewConnection(tlsConn), nil
}
2016-06-17 14:51:41 +00:00
return conn, nil
case <-time.After(time.Second):
}
}
}
// Close stops listening on the UDP address. Already Accepted connections are not closed.
func (this *Listener) Close() error {
if !this.running {
2016-08-07 13:31:24 +00:00
return ErrClosedListener
2016-06-17 14:51:41 +00:00
}
this.Lock()
defer this.Unlock()
this.running = false
close(this.awaitingConns)
2016-06-18 19:18:21 +00:00
for _, conn := range this.sessions {
go conn.Terminate()
}
2016-06-17 14:51:41 +00:00
this.hub.Close()
return nil
}
2016-07-12 21:54:54 +00:00
func (this *Listener) ActiveConnections() int {
this.Lock()
defer this.Unlock()
return len(this.sessions)
}
2016-06-17 14:51:41 +00:00
// Addr returns the listener's network address, The Addr returned is shared by all invocations of Addr, so do not modify it.
func (this *Listener) Addr() net.Addr {
2016-08-15 20:20:16 +00:00
return this.hub.Addr()
2016-06-17 14:51:41 +00:00
}
2016-11-27 07:58:31 +00:00
func (this *Listener) Put(internal.ConnectionId, net.Conn) {}
2016-06-17 14:51:41 +00:00
type Writer struct {
2016-11-27 07:58:31 +00:00
id ConnectionId
2016-06-17 14:51:41 +00:00
dest v2net.Destination
hub *udp.UDPHub
listener *Listener
}
func (this *Writer) Write(payload []byte) (int, error) {
return this.hub.WriteTo(payload, this.dest)
}
func (this *Writer) Close() error {
2016-07-12 16:27:14 +00:00
this.listener.Remove(this.id)
2016-06-17 14:51:41 +00:00
return nil
}
2016-09-30 14:53:40 +00:00
func ListenKCP(address v2net.Address, port v2net.Port, options internet.ListenOptions) (internet.Listener, error) {
return NewListener(address, port, options)
2016-06-17 14:51:41 +00:00
}
func init() {
internet.KCPListenFunc = ListenKCP
}