Skip to content

Commit bcc7b78

Browse files
authored
Add XTLS support to mKCP (#267)
1 parent 03fb762 commit bcc7b78

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

infra/conf/transport_internet.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,8 +496,8 @@ func (c *StreamConfig) Build() (*internet.StreamConfig, error) {
496496
config.SecurityType = tm.Type
497497
}
498498
if strings.EqualFold(c.Security, "xtls") {
499-
if config.ProtocolName != "tcp" && config.ProtocolName != "domainsocket" {
500-
return nil, newError("XTLS only supports TCP and DomainSocket for now.")
499+
if config.ProtocolName != "tcp" && config.ProtocolName != "mkcp" && config.ProtocolName != "domainsocket" {
500+
return nil, newError("XTLS only supports TCP, mKCP and DomainSocket for now.")
501501
}
502502
xtlsSettings := c.XTLSSettings
503503
if xtlsSettings == nil {

transport/internet/kcp/dialer.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package kcp
44

55
import (
66
"context"
7-
"crypto/tls"
87
"io"
98
"sync/atomic"
109

@@ -13,7 +12,8 @@ import (
1312
"v2ray.com/core/common/dice"
1413
"v2ray.com/core/common/net"
1514
"v2ray.com/core/transport/internet"
16-
v2tls "v2ray.com/core/transport/internet/tls"
15+
"v2ray.com/core/transport/internet/tls"
16+
"v2ray.com/core/transport/internet/xtls"
1717
)
1818

1919
var (
@@ -88,9 +88,10 @@ func DialKCP(ctx context.Context, dest net.Destination, streamSettings *internet
8888

8989
var iConn internet.Connection = session
9090

91-
if config := v2tls.ConfigFromStreamSettings(streamSettings); config != nil {
92-
tlsConn := tls.Client(iConn, config.GetTLSConfig(v2tls.WithDestination(dest)))
93-
iConn = tlsConn
91+
if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {
92+
iConn = tls.Client(iConn, config.GetTLSConfig(tls.WithDestination(dest)))
93+
} else if config := xtls.ConfigFromStreamSettings(streamSettings); config != nil {
94+
iConn = xtls.Client(iConn, config.GetXTLSConfig(xtls.WithDestination(dest)))
9495
}
9596

9697
return iConn, nil

transport/internet/kcp/listener.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ package kcp
55
import (
66
"context"
77
"crypto/cipher"
8-
"crypto/tls"
8+
gotls "crypto/tls"
99
"sync"
1010

11+
goxtls "github.com/xtls/go"
12+
1113
"v2ray.com/core/common"
1214
"v2ray.com/core/common/buf"
1315
"v2ray.com/core/common/net"
1416
"v2ray.com/core/transport/internet"
15-
v2tls "v2ray.com/core/transport/internet/tls"
17+
"v2ray.com/core/transport/internet/tls"
1618
"v2ray.com/core/transport/internet/udp"
19+
"v2ray.com/core/transport/internet/xtls"
1720
)
1821

1922
type ConnectionID struct {
@@ -25,14 +28,15 @@ type ConnectionID struct {
2528
// Listener defines a server listening for connections
2629
type Listener struct {
2730
sync.Mutex
28-
sessions map[ConnectionID]*Connection
29-
hub *udp.Hub
30-
tlsConfig *tls.Config
31-
config *Config
32-
reader PacketReader
33-
header internet.PacketHeader
34-
security cipher.AEAD
35-
addConn internet.ConnHandler
31+
sessions map[ConnectionID]*Connection
32+
hub *udp.Hub
33+
tlsConfig *gotls.Config
34+
xtlsConfig *goxtls.Config
35+
config *Config
36+
reader PacketReader
37+
header internet.PacketHeader
38+
security cipher.AEAD
39+
addConn internet.ConnHandler
3640
}
3741

3842
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
5761
addConn: addConn,
5862
}
5963

60-
if config := v2tls.ConfigFromStreamSettings(streamSettings); config != nil {
64+
if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {
6165
l.tlsConfig = config.GetTLSConfig()
6266
}
67+
if config := xtls.ConfigFromStreamSettings(streamSettings); config != nil {
68+
l.xtlsConfig = config.GetXTLSConfig()
69+
}
6370

6471
hub, err := udp.ListenUDP(ctx, address, port, streamSettings, udp.HubCapacity(1024))
6572
if err != nil {
@@ -131,8 +138,9 @@ func (l *Listener) OnReceive(payload *buf.Buffer, src net.Destination) {
131138
}, writer, l.config)
132139
var netConn internet.Connection = conn
133140
if l.tlsConfig != nil {
134-
tlsConn := tls.Server(conn, l.tlsConfig)
135-
netConn = tlsConn
141+
netConn = gotls.Server(conn, l.tlsConfig)
142+
} else if l.xtlsConfig != nil {
143+
netConn = goxtls.Server(conn, l.xtlsConfig)
136144
}
137145

138146
l.addConn(netConn)

0 commit comments

Comments
 (0)