2016-06-17 10:51:41 -04:00
|
|
|
package kcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2016-06-29 04:34:34 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/serial"
|
2016-06-17 10:51:41 -04:00
|
|
|
"github.com/v2ray/v2ray-core/transport/internet"
|
|
|
|
"github.com/v2ray/v2ray-core/transport/internet/udp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Listener defines a server listening for connections
|
|
|
|
type Listener struct {
|
|
|
|
sync.Mutex
|
|
|
|
running bool
|
2016-08-06 15:59:22 -04:00
|
|
|
authenticator internet.Authenticator
|
2016-06-18 10:34:04 -04:00
|
|
|
sessions map[string]*Connection
|
|
|
|
awaitingConns chan *Connection
|
2016-06-17 10:51:41 -04:00
|
|
|
hub *udp.UDPHub
|
|
|
|
localAddr *net.UDPAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewListener(address v2net.Address, port v2net.Port) (*Listener, error) {
|
2016-08-06 15:59:22 -04:00
|
|
|
auth, err := effectiveConfig.GetAuthenticator()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-17 10:51:41 -04:00
|
|
|
l := &Listener{
|
2016-08-06 15:59:22 -04:00
|
|
|
authenticator: auth,
|
2016-06-18 10:34:04 -04:00
|
|
|
sessions: make(map[string]*Connection),
|
|
|
|
awaitingConns: make(chan *Connection, 64),
|
2016-06-17 10:51:41 -04:00
|
|
|
localAddr: &net.UDPAddr{
|
|
|
|
IP: address.IP(),
|
|
|
|
Port: int(port),
|
|
|
|
},
|
|
|
|
running: true,
|
|
|
|
}
|
|
|
|
hub, err := udp.ListenUDP(address, port, l.OnReceive)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
l.hub = hub
|
2016-06-17 11:38:42 -04:00
|
|
|
log.Info("KCP|Listener: listening on ", address, ":", port)
|
2016-06-17 10:51:41 -04:00
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Listener) OnReceive(payload *alloc.Buffer, src v2net.Destination) {
|
|
|
|
defer payload.Release()
|
|
|
|
|
2016-08-06 15:59:22 -04:00
|
|
|
if valid := this.authenticator.Open(payload); !valid {
|
2016-06-17 11:38:42 -04:00
|
|
|
log.Info("KCP|Listener: discarding invalid payload from ", src)
|
2016-06-17 10:51:41 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !this.running {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.Lock()
|
|
|
|
defer this.Unlock()
|
|
|
|
if !this.running {
|
|
|
|
return
|
|
|
|
}
|
2016-07-14 06:57:14 -04:00
|
|
|
if payload.Len() < 4 {
|
|
|
|
return
|
|
|
|
}
|
2016-07-10 09:58:55 -04:00
|
|
|
conv := serial.BytesToUint16(payload.Value)
|
2016-07-14 16:10:37 -04:00
|
|
|
cmd := Command(payload.Value[2])
|
2016-07-10 09:58:55 -04:00
|
|
|
sourceId := src.NetAddr() + "|" + serial.Uint16ToString(conv)
|
|
|
|
conn, found := this.sessions[sourceId]
|
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-07-12 17:54:54 -04:00
|
|
|
log.Debug("KCP|Listener: Creating session with id(", sourceId, ") from ", src)
|
2016-06-17 10:51:41 -04:00
|
|
|
writer := &Writer{
|
2016-07-12 12:27:14 -04:00
|
|
|
id: sourceId,
|
2016-06-17 10:51:41 -04:00
|
|
|
hub: this.hub,
|
|
|
|
dest: src,
|
|
|
|
listener: this,
|
|
|
|
}
|
|
|
|
srcAddr := &net.UDPAddr{
|
|
|
|
IP: src.Address().IP(),
|
|
|
|
Port: int(src.Port()),
|
|
|
|
}
|
2016-08-06 15:59:22 -04:00
|
|
|
auth, err := effectiveConfig.GetAuthenticator()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("KCP|Listener: Failed to create authenticator: ", err)
|
|
|
|
}
|
|
|
|
conn = NewConnection(conv, writer, this.localAddr, srcAddr, auth)
|
2016-06-17 10:51:41 -04:00
|
|
|
select {
|
|
|
|
case this.awaitingConns <- conn:
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
2016-07-10 09:58:55 -04:00
|
|
|
this.sessions[sourceId] = conn
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
2016-07-05 17:02:52 -04:00
|
|
|
conn.Input(payload.Value)
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Listener) Remove(dest string) {
|
|
|
|
if !this.running {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.Lock()
|
|
|
|
defer this.Unlock()
|
|
|
|
if !this.running {
|
|
|
|
return
|
|
|
|
}
|
2016-07-12 17:54:54 -04:00
|
|
|
log.Debug("KCP|Listener: Removing session ", dest)
|
2016-06-17 10:51:41 -04:00
|
|
|
delete(this.sessions, dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 09:31:24 -04:00
|
|
|
return nil, ErrClosedListener
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
|
|
|
select {
|
|
|
|
case conn := <-this.awaitingConns:
|
|
|
|
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 09:31:24 -04:00
|
|
|
return ErrClosedListener
|
2016-06-17 10:51:41 -04:00
|
|
|
}
|
|
|
|
this.Lock()
|
|
|
|
defer this.Unlock()
|
|
|
|
|
|
|
|
this.running = false
|
|
|
|
close(this.awaitingConns)
|
2016-06-18 15:18:21 -04:00
|
|
|
for _, conn := range this.sessions {
|
|
|
|
go conn.Terminate()
|
|
|
|
}
|
2016-06-17 10:51:41 -04:00
|
|
|
this.hub.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-12 17:54:54 -04:00
|
|
|
func (this *Listener) ActiveConnections() int {
|
|
|
|
this.Lock()
|
|
|
|
defer this.Unlock()
|
|
|
|
|
|
|
|
return len(this.sessions)
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func (this *Listener) Addr() net.Addr {
|
|
|
|
return this.localAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
type Writer struct {
|
2016-07-12 12:27:14 -04:00
|
|
|
id string
|
2016-06-17 10:51:41 -04: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 12:27:14 -04:00
|
|
|
this.listener.Remove(this.id)
|
2016-06-17 10:51:41 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ListenKCP(address v2net.Address, port v2net.Port) (internet.Listener, error) {
|
|
|
|
return NewListener(address, port)
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
internet.KCPListenFunc = ListenKCP
|
|
|
|
}
|