2017-01-16 08:18:33 -05:00
|
|
|
package scenarios
|
|
|
|
|
|
|
|
import (
|
2021-06-19 09:36:54 -04:00
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
2018-04-09 14:45:23 -04:00
|
|
|
"os"
|
2018-07-28 12:16:19 -04:00
|
|
|
"runtime"
|
2017-01-16 08:18:33 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-01-08 17:27:02 -05:00
|
|
|
"golang.org/x/sync/errgroup"
|
2018-11-21 16:10:17 -05:00
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
core "github.com/v2fly/v2ray-core/v4"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/app/log"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/app/proxyman"
|
|
|
|
"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/dokodemo"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/proxy/freedom"
|
|
|
|
"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"
|
2021-09-26 19:16:07 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/testing/servers/udp"
|
2021-02-16 15:31:50 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/transport/internet"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/transport/internet/domainsocket"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/transport/internet/headers/http"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/transport/internet/headers/wechat"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/transport/internet/quic"
|
|
|
|
tcptransport "github.com/v2fly/v2ray-core/v4/transport/internet/tcp"
|
2017-01-16 08:18:33 -05:00
|
|
|
)
|
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
func TestHTTPConnectionHeader(t *testing.T) {
|
2017-01-16 08:18:33 -05:00
|
|
|
tcpServer := tcp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
}
|
|
|
|
dest, err := tcpServer.Start()
|
2019-01-08 17:27:02 -05:00
|
|
|
common.Must(err)
|
2017-01-16 08:18:33 -05:00
|
|
|
defer tcpServer.Close()
|
|
|
|
|
|
|
|
userID := protocol.NewID(uuid.New())
|
2018-03-01 05:53:39 -05:00
|
|
|
serverPort := tcp.PickPort()
|
2017-01-16 08:18:33 -05:00
|
|
|
serverConfig := &core.Config{
|
2018-01-10 06:22:37 -05:00
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
2017-01-16 08:18:33 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(serverPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-26 14:46:44 -05:00
|
|
|
StreamSettings: &internet.StreamConfig{
|
|
|
|
TransportSettings: []*internet.TransportConfig{
|
|
|
|
{
|
|
|
|
Protocol: internet.TransportProtocol_TCP,
|
|
|
|
Settings: serial.ToTypedMessage(&tcptransport.Config{
|
|
|
|
HeaderSettings: serial.ToTypedMessage(&http.Config{}),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&inbound.Config{
|
2017-01-16 08:18:33 -05:00
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Account: serial.ToTypedMessage(&vmess.Account{
|
|
|
|
Id: userID.String(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 06:22:37 -05:00
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
2017-01-16 08:18:33 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
2017-01-16 08:18:33 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-03-01 05:53:39 -05:00
|
|
|
clientPort := tcp.PickPort()
|
2017-01-16 08:18:33 -05:00
|
|
|
clientConfig := &core.Config{
|
2018-01-10 06:22:37 -05:00
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
2017-01-16 08:18:33 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(clientPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-26 14:46:44 -05:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(dest.Address),
|
2017-01-16 08:18:33 -05:00
|
|
|
Port: uint32(dest.Port),
|
2017-08-29 06:56:57 -04:00
|
|
|
NetworkList: &net.NetworkList{
|
|
|
|
Network: []net.Network{net.Network_TCP},
|
2017-01-16 08:18:33 -05:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 06:22:37 -05:00
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
2017-01-16 08:18:33 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ProxySettings: serial.ToTypedMessage(&outbound.Config{
|
2017-01-16 08:18:33 -05:00
|
|
|
Receiver: []*protocol.ServerEndpoint{
|
|
|
|
{
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-16 08:18:33 -05:00
|
|
|
Port: uint32(serverPort),
|
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Account: serial.ToTypedMessage(&vmess.Account{
|
|
|
|
Id: userID.String(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
2017-01-26 14:46:44 -05:00
|
|
|
SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
|
|
|
|
StreamSettings: &internet.StreamConfig{
|
|
|
|
TransportSettings: []*internet.TransportConfig{
|
|
|
|
{
|
|
|
|
Protocol: internet.TransportProtocol_TCP,
|
|
|
|
Settings: serial.ToTypedMessage(&tcptransport.Config{
|
|
|
|
HeaderSettings: serial.ToTypedMessage(&http.Config{}),
|
|
|
|
}),
|
|
|
|
},
|
2017-01-16 08:18:33 -05:00
|
|
|
},
|
|
|
|
},
|
2017-01-26 14:46:44 -05:00
|
|
|
}),
|
2017-01-16 08:18:33 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-17 18:39:30 -04:00
|
|
|
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
|
2019-01-08 17:27:02 -05:00
|
|
|
common.Must(err)
|
|
|
|
defer CloseAllServers(servers)
|
2017-01-16 08:18:33 -05:00
|
|
|
|
2019-01-08 17:27:02 -05:00
|
|
|
if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2017-01-16 08:18:33 -05:00
|
|
|
}
|
2018-04-09 14:45:23 -04:00
|
|
|
|
|
|
|
func TestDomainSocket(t *testing.T) {
|
2020-12-16 19:50:13 -05:00
|
|
|
if runtime.GOOS == "windows" || runtime.GOOS == "android" {
|
|
|
|
t.Skip("Not supported on windows and android")
|
2018-07-28 12:16:19 -04:00
|
|
|
return
|
|
|
|
}
|
2018-04-09 14:45:23 -04:00
|
|
|
tcpServer := tcp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
}
|
|
|
|
dest, err := tcpServer.Start()
|
2019-01-08 17:27:02 -05:00
|
|
|
common.Must(err)
|
2018-04-09 14:45:23 -04:00
|
|
|
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)
|
2019-01-08 17:27:02 -05:00
|
|
|
common.Must(err)
|
|
|
|
defer CloseAllServers(servers)
|
2018-04-09 14:45:23 -04:00
|
|
|
|
2019-01-08 17:27:02 -05:00
|
|
|
if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2018-04-09 14:45:23 -04:00
|
|
|
}
|
2018-11-21 16:08:21 -05:00
|
|
|
|
|
|
|
func TestVMessQuic(t *testing.T) {
|
|
|
|
tcpServer := tcp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
}
|
|
|
|
dest, err := tcpServer.Start()
|
2019-01-08 17:27:02 -05:00
|
|
|
common.Must(err)
|
2018-11-21 16:08:21 -05:00
|
|
|
defer tcpServer.Close()
|
|
|
|
|
|
|
|
userID := protocol.NewID(uuid.New())
|
2021-09-26 19:16:07 -04:00
|
|
|
serverPort := udp.PickPort()
|
2018-11-21 16:08:21 -05:00
|
|
|
serverConfig := &core.Config{
|
2021-06-19 09:36:54 -04:00
|
|
|
App: []*anypb.Any{
|
2018-11-21 16:08:21 -05:00
|
|
|
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),
|
|
|
|
StreamSettings: &internet.StreamConfig{
|
|
|
|
ProtocolName: "quic",
|
|
|
|
TransportSettings: []*internet.TransportConfig{
|
|
|
|
{
|
|
|
|
ProtocolName: "quic",
|
2018-11-21 16:10:17 -05:00
|
|
|
Settings: serial.ToTypedMessage(&quic.Config{
|
|
|
|
Header: serial.ToTypedMessage(&wechat.VideoConfig{}),
|
2018-11-22 17:13:52 -05:00
|
|
|
Security: &protocol.SecurityConfig{
|
|
|
|
Type: protocol.SecurityType_NONE,
|
|
|
|
},
|
2018-11-21 16:10:17 -05:00
|
|
|
}),
|
2018-11-21 16:08:21 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
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{
|
2021-06-19 09:36:54 -04:00
|
|
|
App: []*anypb.Any{
|
2018-11-21 16:08:21 -05:00
|
|
|
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{
|
|
|
|
{
|
|
|
|
SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
|
|
|
|
StreamSettings: &internet.StreamConfig{
|
|
|
|
ProtocolName: "quic",
|
|
|
|
TransportSettings: []*internet.TransportConfig{
|
|
|
|
{
|
|
|
|
ProtocolName: "quic",
|
2018-11-21 16:10:17 -05:00
|
|
|
Settings: serial.ToTypedMessage(&quic.Config{
|
|
|
|
Header: serial.ToTypedMessage(&wechat.VideoConfig{}),
|
2018-11-22 17:13:52 -05:00
|
|
|
Security: &protocol.SecurityConfig{
|
|
|
|
Type: protocol.SecurityType_NONE,
|
|
|
|
},
|
2018-11-21 16:10:17 -05:00
|
|
|
}),
|
2018-11-21 16:08:21 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to initialize all servers: ", err.Error())
|
|
|
|
}
|
|
|
|
defer CloseAllServers(servers)
|
|
|
|
|
2019-01-08 17:27:02 -05:00
|
|
|
var errg errgroup.Group
|
2018-11-21 16:08:21 -05:00
|
|
|
for i := 0; i < 10; i++ {
|
2019-01-08 17:27:02 -05:00
|
|
|
errg.Go(testTCPConn(clientPort, 10240*1024, time.Second*40))
|
|
|
|
}
|
2018-11-21 16:08:21 -05:00
|
|
|
|
2019-01-08 17:27:02 -05:00
|
|
|
if err := errg.Wait(); err != nil {
|
|
|
|
t.Error(err)
|
2018-11-21 16:08:21 -05:00
|
|
|
}
|
|
|
|
}
|