From b7fc1f0bd625a8d6ea90e0d91d6e487cddc0e250 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Mon, 9 Apr 2018 20:45:23 +0200 Subject: [PATCH] scenario test for domain socket --- common/net/destination.go | 3 + main/distro/all/all.go | 1 + testing/scenarios/transport_test.go | 125 ++++++++++++++++++++ transport/internet/domainsocket/config.go | 12 +- transport/internet/domainsocket/listener.go | 5 + 5 files changed, 145 insertions(+), 1 deletion(-) diff --git a/common/net/destination.go b/common/net/destination.go index 8cbfbccd1..99f243d4f 100644 --- a/common/net/destination.go +++ b/common/net/destination.go @@ -18,6 +18,9 @@ func DestinationFromAddr(addr net.Addr) Destination { return TCPDestination(IPAddress(addr.IP), Port(addr.Port)) case *net.UDPAddr: return UDPDestination(IPAddress(addr.IP), Port(addr.Port)) + case *net.UnixAddr: + // TODO: deal with Unix domain socket + return TCPDestination(LocalHostIP, Port(9)) default: panic("Net: Unknown address type.") } diff --git a/main/distro/all/all.go b/main/distro/all/all.go index 486c5be8b..9a402b091 100644 --- a/main/distro/all/all.go +++ b/main/distro/all/all.go @@ -32,6 +32,7 @@ import ( _ "v2ray.com/core/proxy/vmess/outbound" // Transports + _ "v2ray.com/core/transport/internet/domainsocket" _ "v2ray.com/core/transport/internet/http" _ "v2ray.com/core/transport/internet/kcp" _ "v2ray.com/core/transport/internet/tcp" diff --git a/testing/scenarios/transport_test.go b/testing/scenarios/transport_test.go index 0ce1462c3..6d5257290 100644 --- a/testing/scenarios/transport_test.go +++ b/testing/scenarios/transport_test.go @@ -1,6 +1,7 @@ package scenarios import ( + "os" "testing" "time" @@ -17,6 +18,7 @@ import ( "v2ray.com/core/proxy/vmess/outbound" "v2ray.com/core/testing/servers/tcp" "v2ray.com/core/transport/internet" + "v2ray.com/core/transport/internet/domainsocket" "v2ray.com/core/transport/internet/headers/http" tcptransport "v2ray.com/core/transport/internet/tcp" . "v2ray.com/ext/assert" @@ -139,3 +141,126 @@ func TestHttpConnectionHeader(t *testing.T) { CloseAllServers(servers) } + +func TestDomainSocket(t *testing.T) { + assert := With(t) + + tcpServer := tcp.Server{ + MsgProcessor: xor, + } + dest, err := tcpServer.Start() + assert(err, IsNil) + defer tcpServer.Close() + + const dsPath = "/tmp/ds_scenario" + os.Remove(dsPath) + + userID := protocol.NewID(uuid.New()) + serverPort := tcp.PickPort() + serverConfig := &core.Config{ + Inbound: []*core.InboundHandlerConfig{ + { + ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ + PortRange: net.SinglePortRange(serverPort), + Listen: net.NewIPOrDomain(net.LocalHostIP), + StreamSettings: &internet.StreamConfig{ + Protocol: internet.TransportProtocol_DomainSocket, + TransportSettings: []*internet.TransportConfig{ + { + Protocol: internet.TransportProtocol_DomainSocket, + Settings: serial.ToTypedMessage(&domainsocket.Config{ + Path: dsPath, + }), + }, + }, + }, + }), + ProxySettings: serial.ToTypedMessage(&inbound.Config{ + User: []*protocol.User{ + { + Account: serial.ToTypedMessage(&vmess.Account{ + Id: userID.String(), + }), + }, + }, + }), + }, + }, + Outbound: []*core.OutboundHandlerConfig{ + { + ProxySettings: serial.ToTypedMessage(&freedom.Config{}), + }, + }, + } + + clientPort := tcp.PickPort() + clientConfig := &core.Config{ + Inbound: []*core.InboundHandlerConfig{ + { + ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ + PortRange: net.SinglePortRange(clientPort), + Listen: net.NewIPOrDomain(net.LocalHostIP), + }), + ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ + Address: net.NewIPOrDomain(dest.Address), + Port: uint32(dest.Port), + NetworkList: &net.NetworkList{ + Network: []net.Network{net.Network_TCP}, + }, + }), + }, + }, + Outbound: []*core.OutboundHandlerConfig{ + { + ProxySettings: serial.ToTypedMessage(&outbound.Config{ + Receiver: []*protocol.ServerEndpoint{ + { + Address: net.NewIPOrDomain(net.LocalHostIP), + Port: uint32(serverPort), + User: []*protocol.User{ + { + Account: serial.ToTypedMessage(&vmess.Account{ + Id: userID.String(), + }), + }, + }, + }, + }, + }), + SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{ + StreamSettings: &internet.StreamConfig{ + Protocol: internet.TransportProtocol_DomainSocket, + TransportSettings: []*internet.TransportConfig{ + { + Protocol: internet.TransportProtocol_DomainSocket, + Settings: serial.ToTypedMessage(&domainsocket.Config{ + Path: dsPath, + }), + }, + }, + }, + }), + }, + }, + } + + servers, err := InitializeServerConfigs(serverConfig, clientConfig) + assert(err, IsNil) + + conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ + IP: []byte{127, 0, 0, 1}, + Port: int(clientPort), + }) + assert(err, IsNil) + + payload := "dokodemo request." + nBytes, err := conn.Write([]byte(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) + + response := readFrom(conn, time.Second*2, len(payload)) + assert(response, Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) + + CloseAllServers(servers) +} diff --git a/transport/internet/domainsocket/config.go b/transport/internet/domainsocket/config.go index 17339eb43..ae8cfbccc 100644 --- a/transport/internet/domainsocket/config.go +++ b/transport/internet/domainsocket/config.go @@ -1,6 +1,10 @@ package domainsocket -import "net" +import ( + "v2ray.com/core/common" + "v2ray.com/core/common/net" + "v2ray.com/core/transport/internet" +) func (c *Config) GetUnixAddr() (*net.UnixAddr, error) { path := c.Path @@ -15,3 +19,9 @@ func (c *Config) GetUnixAddr() (*net.UnixAddr, error) { Net: "unix", }, nil } + +func init() { + common.Must(internet.RegisterProtocolConfigCreator(internet.TransportProtocol_DomainSocket, func() interface{} { + return new(Config) + })) +} diff --git a/transport/internet/domainsocket/listener.go b/transport/internet/domainsocket/listener.go index be3eb256b..c11f1e4ff 100644 --- a/transport/internet/domainsocket/listener.go +++ b/transport/internet/domainsocket/listener.go @@ -7,6 +7,7 @@ import ( "strings" "syscall" + "v2ray.com/core/common" "v2ray.com/core/common/net" "v2ray.com/core/transport/internet" "v2ray.com/core/transport/internet/tls" @@ -122,3 +123,7 @@ func (fl *fileLocker) Release() { newError("failed to remove file: ", fl.path).Base(err).WriteToLog() } } + +func init() { + common.Must(internet.RegisterTransportListener(internet.TransportProtocol_DomainSocket, Listen)) +}