|
@@ -5,15 +5,18 @@ package kcp
|
|
|
import (
|
|
import (
|
|
|
"context"
|
|
"context"
|
|
|
"crypto/cipher"
|
|
"crypto/cipher"
|
|
|
- "crypto/tls"
|
|
|
|
|
|
|
+ gotls "crypto/tls"
|
|
|
"sync"
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
+ goxtls "github.com/xtls/go"
|
|
|
|
|
+
|
|
|
"v2ray.com/core/common"
|
|
"v2ray.com/core/common"
|
|
|
"v2ray.com/core/common/buf"
|
|
"v2ray.com/core/common/buf"
|
|
|
"v2ray.com/core/common/net"
|
|
"v2ray.com/core/common/net"
|
|
|
"v2ray.com/core/transport/internet"
|
|
"v2ray.com/core/transport/internet"
|
|
|
- v2tls "v2ray.com/core/transport/internet/tls"
|
|
|
|
|
|
|
+ "v2ray.com/core/transport/internet/tls"
|
|
|
"v2ray.com/core/transport/internet/udp"
|
|
"v2ray.com/core/transport/internet/udp"
|
|
|
|
|
+ "v2ray.com/core/transport/internet/xtls"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
type ConnectionID struct {
|
|
type ConnectionID struct {
|
|
@@ -25,14 +28,15 @@ type ConnectionID struct {
|
|
|
// Listener defines a server listening for connections
|
|
// Listener defines a server listening for connections
|
|
|
type Listener struct {
|
|
type Listener struct {
|
|
|
sync.Mutex
|
|
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
|
|
|
|
|
|
|
+ sessions map[ConnectionID]*Connection
|
|
|
|
|
+ hub *udp.Hub
|
|
|
|
|
+ tlsConfig *gotls.Config
|
|
|
|
|
+ xtlsConfig *goxtls.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) {
|
|
func NewListener(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig, addConn internet.ConnHandler) (*Listener, error) {
|
|
@@ -57,9 +61,12 @@ func NewListener(ctx context.Context, address net.Address, port net.Port, stream
|
|
|
addConn: addConn,
|
|
addConn: addConn,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if config := v2tls.ConfigFromStreamSettings(streamSettings); config != nil {
|
|
|
|
|
|
|
+ if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {
|
|
|
l.tlsConfig = config.GetTLSConfig()
|
|
l.tlsConfig = config.GetTLSConfig()
|
|
|
}
|
|
}
|
|
|
|
|
+ if config := xtls.ConfigFromStreamSettings(streamSettings); config != nil {
|
|
|
|
|
+ l.xtlsConfig = config.GetXTLSConfig()
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
hub, err := udp.ListenUDP(ctx, address, port, streamSettings, udp.HubCapacity(1024))
|
|
hub, err := udp.ListenUDP(ctx, address, port, streamSettings, udp.HubCapacity(1024))
|
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -131,8 +138,9 @@ func (l *Listener) OnReceive(payload *buf.Buffer, src net.Destination) {
|
|
|
}, writer, l.config)
|
|
}, writer, l.config)
|
|
|
var netConn internet.Connection = conn
|
|
var netConn internet.Connection = conn
|
|
|
if l.tlsConfig != nil {
|
|
if l.tlsConfig != nil {
|
|
|
- tlsConn := tls.Server(conn, l.tlsConfig)
|
|
|
|
|
- netConn = tlsConn
|
|
|
|
|
|
|
+ netConn = gotls.Server(conn, l.tlsConfig)
|
|
|
|
|
+ } else if l.xtlsConfig != nil {
|
|
|
|
|
+ netConn = goxtls.Server(conn, l.xtlsConfig)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
l.addConn(netConn)
|
|
l.addConn(netConn)
|