1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00
v2fly/testing/scenarios/reverse_test.go

396 lines
9.8 KiB
Go
Raw Normal View History

2018-10-28 02:27:07 -04:00
package scenarios
import (
"testing"
"time"
2019-01-07 05:37:06 -05:00
"golang.org/x/sync/errgroup"
2021-10-28 06:34:19 -04:00
"google.golang.org/protobuf/types/known/anypb"
2019-01-07 05:37:06 -05:00
core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/app/log"
"github.com/v2fly/v2ray-core/v5/app/policy"
"github.com/v2fly/v2ray-core/v5/app/proxyman"
"github.com/v2fly/v2ray-core/v5/app/reverse"
"github.com/v2fly/v2ray-core/v5/app/router"
"github.com/v2fly/v2ray-core/v5/app/router/routercommon"
"github.com/v2fly/v2ray-core/v5/common"
clog "github.com/v2fly/v2ray-core/v5/common/log"
"github.com/v2fly/v2ray-core/v5/common/net"
"github.com/v2fly/v2ray-core/v5/common/protocol"
"github.com/v2fly/v2ray-core/v5/common/serial"
"github.com/v2fly/v2ray-core/v5/common/uuid"
"github.com/v2fly/v2ray-core/v5/proxy/blackhole"
"github.com/v2fly/v2ray-core/v5/proxy/dokodemo"
"github.com/v2fly/v2ray-core/v5/proxy/freedom"
"github.com/v2fly/v2ray-core/v5/proxy/vmess"
"github.com/v2fly/v2ray-core/v5/proxy/vmess/inbound"
"github.com/v2fly/v2ray-core/v5/proxy/vmess/outbound"
"github.com/v2fly/v2ray-core/v5/testing/servers/tcp"
2018-10-28 02:27:07 -04:00
)
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{
2021-06-19 09:36:54 -04:00
App: []*anypb.Any{
2018-10-28 02:27:07 -04:00
serial.ToTypedMessage(&reverse.Config{
PortalConfig: []*reverse.PortalConfig{
{
Tag: "portal",
2021-02-16 15:31:50 -05:00
Domain: "test.v2fly.org",
2018-10-28 02:27:07 -04:00
},
},
}),
serial.ToTypedMessage(&router.Config{
Rule: []*router.RoutingRule{
{
2021-09-07 08:43:28 -04:00
Domain: []*routercommon.Domain{
{Type: routercommon.Domain_Full, Value: "test.v2fly.org"},
2018-10-28 02:27:07 -04:00
},
2018-11-07 19:01:53 -05:00
TargetTag: &router.RoutingRule_Tag{
Tag: "portal",
},
2018-10-28 02:27:07 -04:00
},
{
InboundTag: []string{"external"},
2018-11-07 19:01:53 -05:00
TargetTag: &router.RoutingRule_Tag{
Tag: "portal",
},
2018-10-28 02:27:07 -04:00
},
},
}),
},
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: 0,
2018-10-28 02:27:07 -04:00
}),
},
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&blackhole.Config{}),
},
},
}
clientPort := tcp.PickPort()
clientConfig := &core.Config{
2021-06-19 09:36:54 -04:00
App: []*anypb.Any{
2018-10-28 02:27:07 -04:00
serial.ToTypedMessage(&reverse.Config{
BridgeConfig: []*reverse.BridgeConfig{
{
Tag: "bridge",
2021-02-16 15:31:50 -05:00
Domain: "test.v2fly.org",
2018-10-28 02:27:07 -04:00
},
},
}),
serial.ToTypedMessage(&router.Config{
Rule: []*router.RoutingRule{
{
2021-09-07 08:43:28 -04:00
Domain: []*routercommon.Domain{
{Type: routercommon.Domain_Full, Value: "test.v2fly.org"},
2018-10-28 02:27:07 -04:00
},
2018-11-07 19:01:53 -05:00
TargetTag: &router.RoutingRule_Tag{
Tag: "reverse",
},
2018-10-28 02:27:07 -04:00
},
{
InboundTag: []string{"bridge"},
2018-11-07 19:01:53 -05:00
TargetTag: &router.RoutingRule_Tag{
Tag: "freedom",
},
2018-10-28 02:27:07 -04:00
},
},
}),
},
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: 0,
2018-10-28 02:27:07 -04:00
SecuritySettings: &protocol.SecurityConfig{
Type: protocol.SecurityType_AES128_GCM,
},
}),
},
},
},
},
}),
},
},
}
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
common.Must(err)
defer CloseAllServers(servers)
2019-01-07 05:37:06 -05:00
var errg errgroup.Group
2018-10-28 02:41:54 -04:00
for i := 0; i < 32; i++ {
2019-01-09 05:17:29 -05:00
errg.Go(testTCPConn(externalPort, 10240*1024, time.Second*40))
2019-01-07 05:37:06 -05:00
}
if err := errg.Wait(); err != nil {
t.Fatal(err)
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{
2021-06-19 09:36:54 -04:00
App: []*anypb.Any{
2018-10-28 04:08:43 -04:00
serial.ToTypedMessage(&log.Config{
2021-10-02 17:29:37 -04:00
Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: log.LogType_Console},
2018-10-28 04:08:43 -04:00
}),
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",
2021-02-16 15:31:50 -05:00
Domain: "test.v2fly.org",
2018-10-28 04:08:43 -04:00
},
},
}),
serial.ToTypedMessage(&router.Config{
Rule: []*router.RoutingRule{
{
2021-09-07 08:43:28 -04:00
Domain: []*routercommon.Domain{
{Type: routercommon.Domain_Full, Value: "test.v2fly.org"},
2018-10-28 04:08:43 -04:00
},
2018-11-07 19:01:53 -05:00
TargetTag: &router.RoutingRule_Tag{
Tag: "portal",
},
2018-10-28 04:08:43 -04:00
},
{
InboundTag: []string{"external"},
2018-11-07 19:01:53 -05:00
TargetTag: &router.RoutingRule_Tag{
Tag: "portal",
},
2018-10-28 04:08:43 -04:00
},
},
}),
},
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: 0,
2018-10-28 04:08:43 -04:00
}),
},
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&blackhole.Config{}),
},
},
}
clientPort := tcp.PickPort()
clientConfig := &core.Config{
2021-06-19 09:36:54 -04:00
App: []*anypb.Any{
2018-10-28 04:08:43 -04:00
serial.ToTypedMessage(&log.Config{
2021-10-02 17:29:37 -04:00
Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: log.LogType_Console},
2018-10-28 04:08:43 -04:00
}),
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",
2021-02-16 15:31:50 -05:00
Domain: "test.v2fly.org",
2018-10-28 04:08:43 -04:00
},
},
}),
serial.ToTypedMessage(&router.Config{
Rule: []*router.RoutingRule{
{
2021-09-07 08:43:28 -04:00
Domain: []*routercommon.Domain{
{Type: routercommon.Domain_Full, Value: "test.v2fly.org"},
2018-10-28 04:08:43 -04:00
},
2018-11-07 19:01:53 -05:00
TargetTag: &router.RoutingRule_Tag{
Tag: "reverse",
},
2018-10-28 04:08:43 -04:00
},
{
InboundTag: []string{"bridge"},
2018-11-07 19:01:53 -05:00
TargetTag: &router.RoutingRule_Tag{
Tag: "freedom",
},
2018-10-28 04:08:43 -04:00
},
},
}),
},
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: 0,
2018-10-28 04:08:43 -04:00
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++ {
2019-01-09 03:03:33 -05:00
if err := testTCPConn(externalPort, 1024, time.Second*20)(); err != nil {
2019-01-08 17:43:24 -05:00
t.Error(err)
2018-10-28 04:08:43 -04:00
}
}
}