1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 07:26:24 -05:00

scenario test for domain socket

This commit is contained in:
Darien Raymond 2018-04-09 20:45:23 +02:00
parent 38de82a5d3
commit b7fc1f0bd6
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
5 changed files with 145 additions and 1 deletions

View File

@ -18,6 +18,9 @@ func DestinationFromAddr(addr net.Addr) Destination {
return TCPDestination(IPAddress(addr.IP), Port(addr.Port)) return TCPDestination(IPAddress(addr.IP), Port(addr.Port))
case *net.UDPAddr: case *net.UDPAddr:
return UDPDestination(IPAddress(addr.IP), Port(addr.Port)) return UDPDestination(IPAddress(addr.IP), Port(addr.Port))
case *net.UnixAddr:
// TODO: deal with Unix domain socket
return TCPDestination(LocalHostIP, Port(9))
default: default:
panic("Net: Unknown address type.") panic("Net: Unknown address type.")
} }

View File

@ -32,6 +32,7 @@ import (
_ "v2ray.com/core/proxy/vmess/outbound" _ "v2ray.com/core/proxy/vmess/outbound"
// Transports // Transports
_ "v2ray.com/core/transport/internet/domainsocket"
_ "v2ray.com/core/transport/internet/http" _ "v2ray.com/core/transport/internet/http"
_ "v2ray.com/core/transport/internet/kcp" _ "v2ray.com/core/transport/internet/kcp"
_ "v2ray.com/core/transport/internet/tcp" _ "v2ray.com/core/transport/internet/tcp"

View File

@ -1,6 +1,7 @@
package scenarios package scenarios
import ( import (
"os"
"testing" "testing"
"time" "time"
@ -17,6 +18,7 @@ import (
"v2ray.com/core/proxy/vmess/outbound" "v2ray.com/core/proxy/vmess/outbound"
"v2ray.com/core/testing/servers/tcp" "v2ray.com/core/testing/servers/tcp"
"v2ray.com/core/transport/internet" "v2ray.com/core/transport/internet"
"v2ray.com/core/transport/internet/domainsocket"
"v2ray.com/core/transport/internet/headers/http" "v2ray.com/core/transport/internet/headers/http"
tcptransport "v2ray.com/core/transport/internet/tcp" tcptransport "v2ray.com/core/transport/internet/tcp"
. "v2ray.com/ext/assert" . "v2ray.com/ext/assert"
@ -139,3 +141,126 @@ func TestHttpConnectionHeader(t *testing.T) {
CloseAllServers(servers) 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)
}

View File

@ -1,6 +1,10 @@
package domainsocket 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) { func (c *Config) GetUnixAddr() (*net.UnixAddr, error) {
path := c.Path path := c.Path
@ -15,3 +19,9 @@ func (c *Config) GetUnixAddr() (*net.UnixAddr, error) {
Net: "unix", Net: "unix",
}, nil }, nil
} }
func init() {
common.Must(internet.RegisterProtocolConfigCreator(internet.TransportProtocol_DomainSocket, func() interface{} {
return new(Config)
}))
}

View File

@ -7,6 +7,7 @@ import (
"strings" "strings"
"syscall" "syscall"
"v2ray.com/core/common"
"v2ray.com/core/common/net" "v2ray.com/core/common/net"
"v2ray.com/core/transport/internet" "v2ray.com/core/transport/internet"
"v2ray.com/core/transport/internet/tls" "v2ray.com/core/transport/internet/tls"
@ -122,3 +123,7 @@ func (fl *fileLocker) Release() {
newError("failed to remove file: ", fl.path).Base(err).WriteToLog() newError("failed to remove file: ", fl.path).Base(err).WriteToLog()
} }
} }
func init() {
common.Must(internet.RegisterTransportListener(internet.TransportProtocol_DomainSocket, Listen))
}