1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-04 16:37:12 -05:00

Add XTLS support to mKCP (#267)

This commit is contained in:
RPRX 2020-10-06 16:25:02 +00:00 committed by GitHub
parent 03fb762169
commit bcc7b78ff7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 20 deletions

View File

@ -496,8 +496,8 @@ func (c *StreamConfig) Build() (*internet.StreamConfig, error) {
config.SecurityType = tm.Type config.SecurityType = tm.Type
} }
if strings.EqualFold(c.Security, "xtls") { if strings.EqualFold(c.Security, "xtls") {
if config.ProtocolName != "tcp" && config.ProtocolName != "domainsocket" { if config.ProtocolName != "tcp" && config.ProtocolName != "mkcp" && config.ProtocolName != "domainsocket" {
return nil, newError("XTLS only supports TCP and DomainSocket for now.") return nil, newError("XTLS only supports TCP, mKCP and DomainSocket for now.")
} }
xtlsSettings := c.XTLSSettings xtlsSettings := c.XTLSSettings
if xtlsSettings == nil { if xtlsSettings == nil {

View File

@ -4,7 +4,6 @@ package kcp
import ( import (
"context" "context"
"crypto/tls"
"io" "io"
"sync/atomic" "sync/atomic"
@ -13,7 +12,8 @@ import (
"v2ray.com/core/common/dice" "v2ray.com/core/common/dice"
"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/xtls"
) )
var ( var (
@ -88,9 +88,10 @@ func DialKCP(ctx context.Context, dest net.Destination, streamSettings *internet
var iConn internet.Connection = session var iConn internet.Connection = session
if config := v2tls.ConfigFromStreamSettings(streamSettings); config != nil { if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {
tlsConn := tls.Client(iConn, config.GetTLSConfig(v2tls.WithDestination(dest))) iConn = tls.Client(iConn, config.GetTLSConfig(tls.WithDestination(dest)))
iConn = tlsConn } else if config := xtls.ConfigFromStreamSettings(streamSettings); config != nil {
iConn = xtls.Client(iConn, config.GetXTLSConfig(xtls.WithDestination(dest)))
} }
return iConn, nil return iConn, nil

View File

@ -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 {
@ -27,7 +30,8 @@ type Listener struct {
sync.Mutex sync.Mutex
sessions map[ConnectionID]*Connection sessions map[ConnectionID]*Connection
hub *udp.Hub hub *udp.Hub
tlsConfig *tls.Config tlsConfig *gotls.Config
xtlsConfig *goxtls.Config
config *Config config *Config
reader PacketReader reader PacketReader
header internet.PacketHeader header internet.PacketHeader
@ -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 = gotls.Server(conn, l.tlsConfig)
netConn = tlsConn } else if l.xtlsConfig != nil {
netConn = goxtls.Server(conn, l.xtlsConfig)
} }
l.addConn(netConn) l.addConn(netConn)