1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-30 05:56:54 -05:00
v2fly/testing/scenarios/feature_test.go

739 lines
19 KiB
Go
Raw Normal View History

2017-01-08 16:32:41 -05:00
package scenarios
import (
2018-02-20 18:18:53 -05:00
"context"
2017-05-17 18:39:30 -04:00
"io/ioutil"
2017-04-24 19:40:34 -04:00
"net/http"
"net/url"
2017-01-08 16:32:41 -05:00
"testing"
2017-01-27 08:10:36 -05:00
"time"
2017-01-08 16:32:41 -05:00
xproxy "golang.org/x/net/proxy"
2021-02-16 15:31:50 -05:00
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/app/dispatcher"
"github.com/v2fly/v2ray-core/v4/app/log"
"github.com/v2fly/v2ray-core/v4/app/proxyman"
_ "github.com/v2fly/v2ray-core/v4/app/proxyman/inbound"
_ "github.com/v2fly/v2ray-core/v4/app/proxyman/outbound"
"github.com/v2fly/v2ray-core/v4/app/router"
"github.com/v2fly/v2ray-core/v4/common"
clog "github.com/v2fly/v2ray-core/v4/common/log"
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/common/protocol"
"github.com/v2fly/v2ray-core/v4/common/serial"
"github.com/v2fly/v2ray-core/v4/common/uuid"
"github.com/v2fly/v2ray-core/v4/proxy/blackhole"
"github.com/v2fly/v2ray-core/v4/proxy/dokodemo"
"github.com/v2fly/v2ray-core/v4/proxy/freedom"
v2http "github.com/v2fly/v2ray-core/v4/proxy/http"
"github.com/v2fly/v2ray-core/v4/proxy/socks"
"github.com/v2fly/v2ray-core/v4/proxy/vmess"
"github.com/v2fly/v2ray-core/v4/proxy/vmess/inbound"
"github.com/v2fly/v2ray-core/v4/proxy/vmess/outbound"
"github.com/v2fly/v2ray-core/v4/testing/servers/tcp"
"github.com/v2fly/v2ray-core/v4/testing/servers/udp"
"github.com/v2fly/v2ray-core/v4/transport/internet"
2017-01-08 16:32:41 -05:00
)
func TestPassiveConnection(t *testing.T) {
tcpServer := tcp.Server{
MsgProcessor: xor,
SendFirst: []byte("send first"),
}
dest, err := tcpServer.Start()
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-01-08 16:32:41 -05:00
defer tcpServer.Close()
2018-03-01 05:53:39 -05:00
serverPort := tcp.PickPort()
2017-01-08 16:32:41 -05:00
serverConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-08 16:32:41 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest.Address),
2017-01-08 16:32:41 -05:00
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
2017-01-08 16:32:41 -05:00
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-08 16:32:41 -05:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
2017-01-08 16:32:41 -05:00
},
},
}
2017-05-17 18:39:30 -04:00
servers, err := InitializeServerConfigs(serverConfig)
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-03 13:46:53 -05:00
defer CloseAllServers(servers)
2017-01-08 16:32:41 -05:00
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(serverPort),
})
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-01-08 16:32:41 -05:00
{
response := make([]byte, 1024)
nBytes, err := conn.Read(response)
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-03 13:46:53 -05:00
if string(response[:nBytes]) != "send first" {
t.Error("unexpected first response: ", string(response[:nBytes]))
}
2017-01-08 16:32:41 -05:00
}
2019-02-03 13:46:53 -05:00
if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
t.Error(err)
2017-01-08 16:32:41 -05:00
}
}
2017-01-10 08:25:31 -05:00
func TestProxy(t *testing.T) {
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
2019-02-01 08:38:14 -05:00
common.Must(err)
2017-01-10 08:25:31 -05:00
defer tcpServer.Close()
serverUserID := protocol.NewID(uuid.New())
2018-03-01 05:53:39 -05:00
serverPort := tcp.PickPort()
2017-01-10 08:25:31 -05:00
serverConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-10 08:25:31 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&inbound.Config{
2017-01-10 08:25:31 -05:00
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: serverUserID.String(),
}),
},
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-10 08:25:31 -05:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
2017-01-10 08:25:31 -05:00
},
},
}
proxyUserID := protocol.NewID(uuid.New())
2018-03-01 05:53:39 -05:00
proxyPort := tcp.PickPort()
2017-01-10 08:25:31 -05:00
proxyConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-10 08:25:31 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(proxyPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&inbound.Config{
2017-01-10 08:25:31 -05:00
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: proxyUserID.String(),
}),
},
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-10 08:25:31 -05:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
2017-01-10 08:25:31 -05:00
},
},
}
2018-03-01 05:53:39 -05:00
clientPort := tcp.PickPort()
2017-01-10 08:25:31 -05:00
clientConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-10 08:25:31 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(clientPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest.Address),
2017-01-10 08:25:31 -05:00
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
2017-01-10 08:25:31 -05:00
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-10 08:25:31 -05:00
{
ProxySettings: serial.ToTypedMessage(&outbound.Config{
2017-01-10 08:25:31 -05:00
Receiver: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
2017-01-10 08:25:31 -05:00
Port: uint32(serverPort),
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: serverUserID.String(),
}),
},
},
},
},
}),
SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
ProxySettings: &internet.ProxyConfig{
Tag: "proxy",
},
}),
2017-01-10 08:25:31 -05:00
},
2017-01-12 06:59:57 -05:00
{
Tag: "proxy",
ProxySettings: serial.ToTypedMessage(&outbound.Config{
2017-01-12 06:59:57 -05:00
Receiver: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
2017-01-12 06:59:57 -05:00
Port: uint32(proxyPort),
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: proxyUserID.String(),
}),
},
},
},
},
}),
},
},
}
2017-05-17 18:39:30 -04:00
servers, err := InitializeServerConfigs(serverConfig, proxyConfig, clientConfig)
2019-02-01 08:38:14 -05:00
common.Must(err)
defer CloseAllServers(servers)
2017-01-12 06:59:57 -05:00
2019-02-01 08:38:14 -05:00
if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
2017-01-12 06:59:57 -05:00
}
func TestProxyOverKCP(t *testing.T) {
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-01-12 06:59:57 -05:00
defer tcpServer.Close()
serverUserID := protocol.NewID(uuid.New())
2018-03-01 05:53:39 -05:00
serverPort := tcp.PickPort()
2017-01-12 06:59:57 -05:00
serverConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-12 06:59:57 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
StreamSettings: &internet.StreamConfig{
Protocol: internet.TransportProtocol_MKCP,
},
}),
ProxySettings: serial.ToTypedMessage(&inbound.Config{
2017-01-12 06:59:57 -05:00
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: serverUserID.String(),
}),
},
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-12 06:59:57 -05:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
2017-01-12 06:59:57 -05:00
},
},
}
proxyUserID := protocol.NewID(uuid.New())
2018-03-01 05:53:39 -05:00
proxyPort := tcp.PickPort()
2017-01-12 06:59:57 -05:00
proxyConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-12 06:59:57 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(proxyPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&inbound.Config{
2017-01-12 06:59:57 -05:00
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: proxyUserID.String(),
}),
},
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-12 06:59:57 -05:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
StreamSettings: &internet.StreamConfig{
Protocol: internet.TransportProtocol_MKCP,
},
}),
2017-01-12 06:59:57 -05:00
},
},
}
2018-03-01 05:53:39 -05:00
clientPort := tcp.PickPort()
2017-01-12 06:59:57 -05:00
clientConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-12 06:59:57 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(clientPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest.Address),
2017-01-12 06:59:57 -05:00
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
2017-01-12 06:59:57 -05:00
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-12 06:59:57 -05:00
{
ProxySettings: serial.ToTypedMessage(&outbound.Config{
2017-01-12 06:59:57 -05:00
Receiver: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
2017-01-12 06:59:57 -05:00
Port: uint32(serverPort),
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: serverUserID.String(),
}),
},
},
},
},
}),
SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
ProxySettings: &internet.ProxyConfig{
Tag: "proxy",
},
StreamSettings: &internet.StreamConfig{
Protocol: internet.TransportProtocol_MKCP,
},
}),
2017-01-12 06:59:57 -05:00
},
2017-01-10 08:25:31 -05:00
{
Tag: "proxy",
ProxySettings: serial.ToTypedMessage(&outbound.Config{
2017-01-10 08:25:31 -05:00
Receiver: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
2017-01-10 08:25:31 -05:00
Port: uint32(proxyPort),
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: proxyUserID.String(),
}),
},
},
},
},
}),
},
},
}
2017-05-17 18:39:30 -04:00
servers, err := InitializeServerConfigs(serverConfig, proxyConfig, clientConfig)
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-03 13:46:53 -05:00
defer CloseAllServers(servers)
2017-01-10 08:25:31 -05:00
2019-02-03 13:46:53 -05:00
if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
2017-01-10 08:25:31 -05:00
}
func TestBlackhole(t *testing.T) {
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-01-10 08:25:31 -05:00
defer tcpServer.Close()
2017-01-10 18:09:33 -05:00
tcpServer2 := tcp.Server{
MsgProcessor: xor,
}
dest2, err := tcpServer2.Start()
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-01-10 18:09:33 -05:00
defer tcpServer2.Close()
2018-03-01 05:53:39 -05:00
serverPort := tcp.PickPort()
serverPort2 := tcp.PickPort()
2017-01-10 08:25:31 -05:00
serverConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-10 08:25:31 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest.Address),
2017-01-10 08:25:31 -05:00
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
2017-01-10 08:25:31 -05:00
},
}),
},
2017-01-10 18:09:33 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort2),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest2.Address),
2017-01-10 18:09:33 -05:00
Port: uint32(dest2.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
2017-01-10 18:09:33 -05:00
},
}),
},
2017-01-10 08:25:31 -05:00
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-10 08:25:31 -05:00
{
Tag: "direct",
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
2017-01-10 18:09:33 -05:00
},
{
Tag: "blocked",
ProxySettings: serial.ToTypedMessage(&blackhole.Config{}),
2017-01-10 08:25:31 -05:00
},
},
2017-01-10 18:09:33 -05:00
App: []*serial.TypedMessage{
serial.ToTypedMessage(&router.Config{
Rule: []*router.RoutingRule{
{
2018-11-07 19:01:53 -05:00
TargetTag: &router.RoutingRule_Tag{
Tag: "blocked",
},
PortRange: net.SinglePortRange(dest2.Port),
2017-01-10 18:09:33 -05:00
},
},
}),
},
2017-01-10 08:25:31 -05:00
}
2017-05-17 18:39:30 -04:00
servers, err := InitializeServerConfigs(serverConfig)
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-03 13:46:53 -05:00
defer CloseAllServers(servers)
2017-01-10 08:25:31 -05:00
2019-02-07 13:29:32 -05:00
if err := testTCPConn(serverPort2, 1024, time.Second*5)(); err == nil {
t.Error("nil error")
2017-01-10 08:25:31 -05:00
}
}
func TestForward(t *testing.T) {
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
2019-02-02 16:19:30 -05:00
common.Must(err)
defer tcpServer.Close()
2018-03-01 05:53:39 -05:00
serverPort := tcp.PickPort()
serverConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
AuthType: socks.AuthType_NO_AUTH,
Accounts: map[string]string{
"Test Account": "Test Password",
},
Address: net.NewIPOrDomain(net.LocalHostIP),
UdpEnabled: false,
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{
DestinationOverride: &freedom.DestinationOverride{
Server: &protocol.ServerEndpoint{
Address: net.NewIPOrDomain(net.LocalHostIP),
Port: uint32(dest.Port),
},
},
}),
},
},
}
2017-05-17 18:39:30 -04:00
servers, err := InitializeServerConfigs(serverConfig)
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-03 13:46:53 -05:00
defer CloseAllServers(servers)
{
noAuthDialer, err := xproxy.SOCKS5("tcp", net.TCPDestination(net.LocalHostIP, serverPort).NetAddr(), nil, xproxy.Direct)
2019-02-02 16:19:30 -05:00
common.Must(err)
conn, err := noAuthDialer.Dial("tcp", "google.com:80")
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-03 13:46:53 -05:00
defer conn.Close()
2019-02-03 13:46:53 -05:00
if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
}
}
2017-01-27 08:10:36 -05:00
func TestUDPConnection(t *testing.T) {
udpServer := udp.Server{
MsgProcessor: xor,
}
dest, err := udpServer.Start()
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-01-27 08:10:36 -05:00
defer udpServer.Close()
2018-03-01 05:53:39 -05:00
clientPort := tcp.PickPort()
2017-01-27 08:10:36 -05:00
clientConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-01-27 08:10:36 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(clientPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
2017-01-27 08:10:36 -05:00
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest.Address),
2017-01-27 08:10:36 -05:00
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_UDP},
2017-01-27 08:10:36 -05:00
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-01-27 08:10:36 -05:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}
2017-05-17 18:39:30 -04:00
servers, err := InitializeServerConfigs(clientConfig)
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-06 05:53:33 -05:00
defer CloseAllServers(servers)
2017-01-27 08:10:36 -05:00
2019-02-03 13:46:53 -05:00
if err := testUDPConn(clientPort, 1024, time.Second*5)(); err != nil {
t.Error(err)
2017-01-27 08:10:36 -05:00
}
time.Sleep(20 * time.Second)
2019-02-03 13:46:53 -05:00
if err := testUDPConn(clientPort, 1024, time.Second*5)(); err != nil {
t.Error(err)
2017-01-27 08:10:36 -05:00
}
}
2017-04-24 19:40:34 -04:00
func TestDomainSniffing(t *testing.T) {
2018-03-01 05:53:39 -05:00
sniffingPort := tcp.PickPort()
httpPort := tcp.PickPort()
2017-04-24 19:40:34 -04:00
serverConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
2017-04-24 19:40:34 -04:00
{
Tag: "snif",
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(sniffingPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
2017-04-24 19:40:34 -04:00
DomainOverride: []proxyman.KnownProtocols{
proxyman.KnownProtocols_TLS,
},
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(net.LocalHostIP),
2017-04-24 19:40:34 -04:00
Port: 443,
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
2017-04-24 19:40:34 -04:00
},
}),
},
{
Tag: "http",
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(httpPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
2017-04-24 19:40:34 -04:00
}),
ProxySettings: serial.ToTypedMessage(&v2http.ServerConfig{}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-04-24 19:40:34 -04:00
{
Tag: "redir",
ProxySettings: serial.ToTypedMessage(&freedom.Config{
DestinationOverride: &freedom.DestinationOverride{
Server: &protocol.ServerEndpoint{
Address: net.NewIPOrDomain(net.LocalHostIP),
2017-04-24 19:40:34 -04:00
Port: uint32(sniffingPort),
},
},
}),
},
{
Tag: "direct",
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
App: []*serial.TypedMessage{
serial.ToTypedMessage(&router.Config{
Rule: []*router.RoutingRule{
{
2018-11-07 19:01:53 -05:00
TargetTag: &router.RoutingRule_Tag{
Tag: "direct",
},
2017-04-24 19:40:34 -04:00
InboundTag: []string{"snif"},
}, {
2018-11-07 19:01:53 -05:00
TargetTag: &router.RoutingRule_Tag{
Tag: "redir",
},
2017-04-24 19:40:34 -04:00
InboundTag: []string{"http"},
},
},
}),
serial.ToTypedMessage(&log.Config{
2017-12-19 16:29:56 -05:00
ErrorLogLevel: clog.Severity_Debug,
2017-04-24 19:40:34 -04:00
ErrorLogType: log.LogType_Console,
}),
},
}
2017-05-17 18:39:30 -04:00
servers, err := InitializeServerConfigs(serverConfig)
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-03 13:46:53 -05:00
defer CloseAllServers(servers)
2017-04-24 19:40:34 -04:00
{
transport := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse("http://127.0.0.1:" + httpPort.String())
},
}
client := &http.Client{
Transport: transport,
}
resp, err := client.Get("https://www.github.com/")
2019-02-02 16:19:30 -05:00
common.Must(err)
defer resp.Body.Close()
2019-02-03 13:46:53 -05:00
if resp.StatusCode != 200 {
t.Error("unexpected status code: ", resp.StatusCode)
}
common.Must(resp.Write(ioutil.Discard))
2017-04-24 19:40:34 -04:00
}
}
2018-02-20 18:18:53 -05:00
func TestDialV2Ray(t *testing.T) {
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
2019-02-02 16:19:30 -05:00
common.Must(err)
2018-02-20 18:18:53 -05:00
defer tcpServer.Close()
userID := protocol.NewID(uuid.New())
2018-03-01 05:53:39 -05:00
serverPort := tcp.PickPort()
2018-02-20 18:18:53 -05:00
serverConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&log.Config{
ErrorLogLevel: clog.Severity_Debug,
ErrorLogType: log.LogType_Console,
}),
},
Inbound: []*core.InboundHandlerConfig{
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&inbound.Config{
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&vmess.Account{
Id: userID.String(),
AlterId: 64,
}),
},
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}
clientConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&dispatcher.Config{}),
serial.ToTypedMessage(&proxyman.InboundConfig{}),
serial.ToTypedMessage(&proxyman.OutboundConfig{}),
},
Inbound: []*core.InboundHandlerConfig{},
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(),
AlterId: 64,
SecuritySettings: &protocol.SecurityConfig{
Type: protocol.SecurityType_AES128_GCM,
},
}),
},
},
},
},
}),
},
},
}
servers, err := InitializeServerConfigs(serverConfig)
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-03 13:46:53 -05:00
defer CloseAllServers(servers)
2018-02-20 18:18:53 -05:00
client, err := core.New(clientConfig)
2019-02-02 16:19:30 -05:00
common.Must(err)
2018-02-20 18:18:53 -05:00
conn, err := core.Dial(context.Background(), client, dest)
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-03 13:46:53 -05:00
defer conn.Close()
2018-02-20 18:18:53 -05:00
2019-02-03 13:46:53 -05:00
if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
2018-02-20 18:18:53 -05:00
}