mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-22 05:06:07 +03:00
268 lines
6.6 KiB
Go
268 lines
6.6 KiB
Go
package scenarios
|
|
|
|
import (
|
|
"io"
|
|
"testing"
|
|
"time"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
"github.com/xtls/xray-core/v1/app/log"
|
|
"github.com/xtls/xray-core/v1/app/policy"
|
|
"github.com/xtls/xray-core/v1/app/proxyman"
|
|
"github.com/xtls/xray-core/v1/common"
|
|
clog "github.com/xtls/xray-core/v1/common/log"
|
|
"github.com/xtls/xray-core/v1/common/net"
|
|
"github.com/xtls/xray-core/v1/common/protocol"
|
|
"github.com/xtls/xray-core/v1/common/serial"
|
|
"github.com/xtls/xray-core/v1/common/uuid"
|
|
"github.com/xtls/xray-core/v1/core"
|
|
"github.com/xtls/xray-core/v1/proxy/dokodemo"
|
|
"github.com/xtls/xray-core/v1/proxy/freedom"
|
|
"github.com/xtls/xray-core/v1/proxy/vmess"
|
|
"github.com/xtls/xray-core/v1/proxy/vmess/inbound"
|
|
"github.com/xtls/xray-core/v1/proxy/vmess/outbound"
|
|
"github.com/xtls/xray-core/v1/testing/servers/tcp"
|
|
)
|
|
|
|
func startQuickClosingTCPServer() (net.Listener, error) {
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
go func() {
|
|
for {
|
|
conn, err := listener.Accept()
|
|
if err != nil {
|
|
break
|
|
}
|
|
b := make([]byte, 1024)
|
|
conn.Read(b)
|
|
conn.Close()
|
|
}
|
|
}()
|
|
return listener, nil
|
|
}
|
|
|
|
func TestVMessClosing(t *testing.T) {
|
|
tcpServer, err := startQuickClosingTCPServer()
|
|
common.Must(err)
|
|
defer tcpServer.Close()
|
|
|
|
dest := net.DestinationFromAddr(tcpServer.Addr())
|
|
|
|
userID := protocol.NewID(uuid.New())
|
|
serverPort := tcp.PickPort()
|
|
serverConfig := &core.Config{
|
|
App: []*serial.TypedMessage{
|
|
serial.ToTypedMessage(&policy.Config{
|
|
Level: map[uint32]*policy.Policy{
|
|
0: {
|
|
Timeout: &policy.Policy_Timeout{
|
|
UplinkOnly: &policy.Second{Value: 0},
|
|
DownlinkOnly: &policy.Second{Value: 0},
|
|
},
|
|
},
|
|
},
|
|
}),
|
|
},
|
|
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{}),
|
|
},
|
|
},
|
|
}
|
|
|
|
clientPort := tcp.PickPort()
|
|
clientConfig := &core.Config{
|
|
App: []*serial.TypedMessage{
|
|
serial.ToTypedMessage(&policy.Config{
|
|
Level: map[uint32]*policy.Policy{
|
|
0: {
|
|
Timeout: &policy.Policy_Timeout{
|
|
UplinkOnly: &policy.Second{Value: 0},
|
|
DownlinkOnly: &policy.Second{Value: 0},
|
|
},
|
|
},
|
|
},
|
|
}),
|
|
},
|
|
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(),
|
|
AlterId: 64,
|
|
SecuritySettings: &protocol.SecurityConfig{
|
|
Type: protocol.SecurityType_AES128_GCM,
|
|
},
|
|
}),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}),
|
|
},
|
|
},
|
|
}
|
|
|
|
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
|
|
common.Must(err)
|
|
defer CloseAllServers(servers)
|
|
|
|
if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != io.EOF {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestZeroBuffer(t *testing.T) {
|
|
tcpServer := tcp.Server{
|
|
MsgProcessor: xor,
|
|
}
|
|
dest, err := tcpServer.Start()
|
|
common.Must(err)
|
|
defer tcpServer.Close()
|
|
|
|
userID := protocol.NewID(uuid.New())
|
|
serverPort := tcp.PickPort()
|
|
serverConfig := &core.Config{
|
|
App: []*serial.TypedMessage{
|
|
serial.ToTypedMessage(&policy.Config{
|
|
Level: map[uint32]*policy.Policy{
|
|
0: {
|
|
Timeout: &policy.Policy_Timeout{
|
|
UplinkOnly: &policy.Second{Value: 0},
|
|
DownlinkOnly: &policy.Second{Value: 0},
|
|
},
|
|
Buffer: &policy.Policy_Buffer{
|
|
Connection: 0,
|
|
},
|
|
},
|
|
},
|
|
}),
|
|
},
|
|
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{}),
|
|
},
|
|
},
|
|
}
|
|
|
|
clientPort := tcp.PickPort()
|
|
clientConfig := &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(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(),
|
|
AlterId: 64,
|
|
SecuritySettings: &protocol.SecurityConfig{
|
|
Type: protocol.SecurityType_AES128_GCM,
|
|
},
|
|
}),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}),
|
|
},
|
|
},
|
|
}
|
|
|
|
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
|
|
common.Must(err)
|
|
defer CloseAllServers(servers)
|
|
|
|
var errg errgroup.Group
|
|
for i := 0; i < 10; i++ {
|
|
errg.Go(testTCPConn(clientPort, 10240*1024, time.Second*20))
|
|
}
|
|
if err := errg.Wait(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|