1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-14 00:07:09 -05:00
v2fly/testing/scenarios/reverse_test.go

422 lines
10 KiB
Go
Raw Normal View History

2018-10-28 02:27:07 -04:00
package scenarios
import (
"crypto/rand"
2018-10-28 02:41:54 -04:00
"sync"
2018-10-28 02:27:07 -04:00
"testing"
"time"
"v2ray.com/core"
2018-10-28 04:08:43 -04:00
"v2ray.com/core/app/log"
"v2ray.com/core/app/policy"
2018-10-28 02:27:07 -04:00
"v2ray.com/core/app/proxyman"
2018-10-28 04:08:43 -04:00
"v2ray.com/core/app/reverse"
"v2ray.com/core/app/router"
2018-10-28 02:27:07 -04:00
"v2ray.com/core/common"
"v2ray.com/core/common/compare"
2018-10-28 04:08:43 -04:00
clog "v2ray.com/core/common/log"
2018-10-28 02:27:07 -04:00
"v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/serial"
"v2ray.com/core/common/uuid"
"v2ray.com/core/proxy/blackhole"
"v2ray.com/core/proxy/dokodemo"
"v2ray.com/core/proxy/freedom"
"v2ray.com/core/proxy/vmess"
"v2ray.com/core/proxy/vmess/inbound"
"v2ray.com/core/proxy/vmess/outbound"
"v2ray.com/core/testing/servers/tcp"
)
func TestReverseProxy(t *testing.T) {
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
common.Must(err)
defer tcpServer.Close()
userID := protocol.NewID(uuid.New())
externalPort := tcp.PickPort()
reversePort := tcp.PickPort()
serverConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&reverse.Config{
PortalConfig: []*reverse.PortalConfig{
{
Tag: "portal",
Domain: "test.v2ray.com",
},
},
}),
serial.ToTypedMessage(&router.Config{
Rule: []*router.RoutingRule{
{
Domain: []*router.Domain{
{Type: router.Domain_Full, Value: "test.v2ray.com"},
},
Tag: "portal",
},
{
InboundTag: []string{"external"},
Tag: "portal",
},
},
}),
},
Inbound: []*core.InboundHandlerConfig{
{
Tag: "external",
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(externalPort),
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},
},
}),
},
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(reversePort),
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(&blackhole.Config{}),
},
},
}
clientPort := tcp.PickPort()
clientConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&reverse.Config{
BridgeConfig: []*reverse.BridgeConfig{
{
Tag: "bridge",
Domain: "test.v2ray.com",
},
},
}),
serial.ToTypedMessage(&router.Config{
Rule: []*router.RoutingRule{
{
Domain: []*router.Domain{
{Type: router.Domain_Full, Value: "test.v2ray.com"},
},
Tag: "reverse",
},
{
InboundTag: []string{"bridge"},
Tag: "freedom",
},
},
}),
},
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{
{
Tag: "freedom",
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
{
Tag: "reverse",
ProxySettings: serial.ToTypedMessage(&outbound.Config{
Receiver: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
Port: uint32(reversePort),
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, clientConfig)
common.Must(err)
defer CloseAllServers(servers)
2018-10-28 02:41:54 -04:00
var wg sync.WaitGroup
2018-10-28 16:46:12 -04:00
wg.Add(32)
2018-10-28 02:41:54 -04:00
for i := 0; i < 32; i++ {
go func() {
defer wg.Done()
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(externalPort),
})
common.Must(err)
defer conn.Close()
payload := make([]byte, 10240*1024)
rand.Read(payload)
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
if nBytes != len(payload) {
t.Error("only part of payload is written: ", nBytes)
}
response := readFrom(conn, time.Second*20, 10240*1024)
if err := compare.BytesEqualWithDetail(response, xor([]byte(payload))); err != nil {
t.Error(err)
}
}()
2018-10-28 02:27:07 -04:00
}
2018-10-28 02:41:54 -04:00
wg.Wait()
2018-10-28 02:27:07 -04:00
}
2018-10-28 04:08:43 -04:00
func TestReverseProxyLongRunning(t *testing.T) {
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
common.Must(err)
defer tcpServer.Close()
userID := protocol.NewID(uuid.New())
externalPort := tcp.PickPort()
reversePort := tcp.PickPort()
serverConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&log.Config{
ErrorLogLevel: clog.Severity_Warning,
ErrorLogType: log.LogType_Console,
}),
serial.ToTypedMessage(&policy.Config{
Level: map[uint32]*policy.Policy{
0: {
Timeout: &policy.Policy_Timeout{
UplinkOnly: &policy.Second{Value: 0},
DownlinkOnly: &policy.Second{Value: 0},
},
},
},
}),
serial.ToTypedMessage(&reverse.Config{
PortalConfig: []*reverse.PortalConfig{
{
Tag: "portal",
Domain: "test.v2ray.com",
},
},
}),
serial.ToTypedMessage(&router.Config{
Rule: []*router.RoutingRule{
{
Domain: []*router.Domain{
{Type: router.Domain_Full, Value: "test.v2ray.com"},
},
Tag: "portal",
},
{
InboundTag: []string{"external"},
Tag: "portal",
},
},
}),
},
Inbound: []*core.InboundHandlerConfig{
{
Tag: "external",
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(externalPort),
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},
},
}),
},
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(reversePort),
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(&blackhole.Config{}),
},
},
}
clientPort := tcp.PickPort()
clientConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&log.Config{
ErrorLogLevel: clog.Severity_Warning,
ErrorLogType: log.LogType_Console,
}),
serial.ToTypedMessage(&policy.Config{
Level: map[uint32]*policy.Policy{
0: {
Timeout: &policy.Policy_Timeout{
UplinkOnly: &policy.Second{Value: 0},
DownlinkOnly: &policy.Second{Value: 0},
},
},
},
}),
serial.ToTypedMessage(&reverse.Config{
BridgeConfig: []*reverse.BridgeConfig{
{
Tag: "bridge",
Domain: "test.v2ray.com",
},
},
}),
serial.ToTypedMessage(&router.Config{
Rule: []*router.RoutingRule{
{
Domain: []*router.Domain{
{Type: router.Domain_Full, Value: "test.v2ray.com"},
},
Tag: "reverse",
},
{
InboundTag: []string{"bridge"},
Tag: "freedom",
},
},
}),
},
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{
{
Tag: "freedom",
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
{
Tag: "reverse",
ProxySettings: serial.ToTypedMessage(&outbound.Config{
Receiver: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
Port: uint32(reversePort),
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, clientConfig)
common.Must(err)
defer CloseAllServers(servers)
for i := 0; i < 4096; i++ {
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(externalPort),
})
common.Must(err)
payload := make([]byte, 1024)
rand.Read(payload)
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
if nBytes != len(payload) {
t.Error("only part of payload is written: ", nBytes)
}
response := readFrom(conn, time.Second*5, 1024)
if err := compare.BytesEqualWithDetail(response, xor([]byte(payload))); err != nil {
t.Error(err)
}
conn.Close()
}
}