Skip to content

Commit

Permalink
Add XTLS support to DomainSocket (v2fly#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
RPRX authored Sep 30, 2020
1 parent 6556557 commit cde63bf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
4 changes: 2 additions & 2 deletions infra/conf/transport_internet.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,8 @@ func (c *StreamConfig) Build() (*internet.StreamConfig, error) {
config.SecurityType = tm.Type
}
if strings.EqualFold(c.Security, "xtls") {
if config.ProtocolName != "tcp" {
return nil, newError("XTLS only supports TCP for now.")
if config.ProtocolName != "tcp" && config.ProtocolName != "domainsocket" {
return nil, newError("XTLS only supports TCP and DomainSocket for now.")
}
xtlsSettings := c.XTLSSettings
if xtlsSettings == nil {
Expand Down
3 changes: 3 additions & 0 deletions transport/internet/domainsocket/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"v2ray.com/core/common/net"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/internet/tls"
"v2ray.com/core/transport/internet/xtls"
)

func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
Expand All @@ -27,6 +28,8 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me

if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {
return tls.Client(conn, config.GetTLSConfig(tls.WithDestination(dest))), nil
} else if config := xtls.ConfigFromStreamSettings(streamSettings); config != nil {
return xtls.Client(conn, config.GetXTLSConfig(xtls.WithDestination(dest))), nil
}

return conn, nil
Expand Down
20 changes: 14 additions & 6 deletions transport/internet/domainsocket/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,25 @@ import (
"strings"

"github.com/pires/go-proxyproto"
goxtls "github.com/xtls/go"
"golang.org/x/sys/unix"

"v2ray.com/core/common"
"v2ray.com/core/common/net"
"v2ray.com/core/common/session"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/internet/tls"
"v2ray.com/core/transport/internet/xtls"
)

type Listener struct {
addr *net.UnixAddr
ln net.Listener
tlsConfig *gotls.Config
config *Config
addConn internet.ConnHandler
locker *fileLocker
addr *net.UnixAddr
ln net.Listener
tlsConfig *gotls.Config
xtlsConfig *goxtls.Config
config *Config
addConn internet.ConnHandler
locker *fileLocker
}

func Listen(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig, handler internet.ConnHandler) (internet.Listener, error) {
Expand Down Expand Up @@ -73,6 +76,9 @@ func Listen(ctx context.Context, address net.Address, port net.Port, streamSetti
if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {
ln.tlsConfig = config.GetTLSConfig()
}
if config := xtls.ConfigFromStreamSettings(streamSettings); config != nil {
ln.xtlsConfig = config.GetXTLSConfig()
}

go ln.run()

Expand Down Expand Up @@ -103,6 +109,8 @@ func (ln *Listener) run() {

if ln.tlsConfig != nil {
conn = tls.Server(conn, ln.tlsConfig)
} else if ln.xtlsConfig != nil {
conn = xtls.Server(conn, ln.xtlsConfig)
}

ln.addConn(internet.Connection(conn))
Expand Down

0 comments on commit cde63bf

Please sign in to comment.