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:
parent
38de82a5d3
commit
b7fc1f0bd6
@ -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.")
|
||||
}
|
||||
|
@ -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"
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}))
|
||||
}
|
||||
|
@ -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))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user