1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 18:06:15 -05:00
v2fly/testing/scenarios/shadowsocks_test.go

484 lines
12 KiB
Go
Raw Normal View History

2016-01-28 07:40:00 -05:00
package scenarios
import (
"testing"
2017-01-29 03:02:19 -05:00
"time"
2016-12-09 06:08:25 -05:00
2019-01-06 17:30:38 -05:00
"golang.org/x/sync/errgroup"
2021-10-28 06:34:19 -04:00
"google.golang.org/protobuf/types/known/anypb"
2018-10-23 16:27:18 -04:00
core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/app/log"
"github.com/v2fly/v2ray-core/v5/app/proxyman"
"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/proxy/dokodemo"
"github.com/v2fly/v2ray-core/v5/proxy/freedom"
"github.com/v2fly/v2ray-core/v5/proxy/shadowsocks"
"github.com/v2fly/v2ray-core/v5/testing/servers/tcp"
"github.com/v2fly/v2ray-core/v5/testing/servers/udp"
2016-01-28 07:40:00 -05:00
)
func TestShadowsocksChaCha20Poly1305TCP(t *testing.T) {
2018-10-23 16:27:18 -04:00
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
common.Must(err)
defer tcpServer.Close()
account := serial.ToTypedMessage(&shadowsocks.Account{
Password: "shadowsocks-password",
CipherType: shadowsocks.CipherType_CHACHA20_POLY1305,
})
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(&shadowsocks.ServerConfig{
User: &protocol.User{
Account: account,
Level: 1,
},
Network: []net.Network{net.Network_TCP},
}),
},
},
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{
2020-12-31 23:52:04 -05:00
Address: net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
Networks: []net.Network{net.Network_TCP},
2018-10-23 16:27:18 -04:00
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{
Server: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
Port: uint32(serverPort),
User: []*protocol.User{
{
Account: account,
},
},
},
},
}),
},
},
}
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
common.Must(err)
defer CloseAllServers(servers)
var errGroup errgroup.Group
2018-10-23 16:27:18 -04:00
for i := 0; i < 10; i++ {
errGroup.Go(testTCPConn(clientPort, 10240*1024, time.Second*20))
2019-01-07 17:27:59 -05:00
}
if err := errGroup.Wait(); err != nil {
2019-01-07 17:27:59 -05:00
t.Error(err)
2018-10-23 16:27:18 -04:00
}
2016-01-28 07:40:00 -05:00
}
2017-11-26 10:56:09 -05:00
func TestShadowsocksAES256GCMTCP(t *testing.T) {
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
2019-01-11 11:17:59 -05:00
common.Must(err)
2017-11-26 10:56:09 -05:00
defer tcpServer.Close()
account := serial.ToTypedMessage(&shadowsocks.Account{
Password: "shadowsocks-password",
CipherType: shadowsocks.CipherType_AES_256_GCM,
})
2018-03-01 05:53:39 -05:00
serverPort := tcp.PickPort()
2017-11-26 10:56:09 -05:00
serverConfig := &core.Config{
2021-06-19 09:36:54 -04:00
App: []*anypb.Any{
2017-11-26 10:56:09 -05:00
serial.ToTypedMessage(&log.Config{
2021-10-02 17:29:37 -04:00
Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: log.LogType_Console},
2017-11-26 10:56:09 -05:00
}),
},
Inbound: []*core.InboundHandlerConfig{
2017-11-26 10:56:09 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{
User: &protocol.User{
Account: account,
Level: 1,
},
2018-04-03 11:51:01 -04:00
Network: []net.Network{net.Network_TCP},
2017-11-26 10:56:09 -05:00
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-11-26 10:56:09 -05:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}
2018-03-01 05:53:39 -05:00
clientPort := tcp.PickPort()
2017-11-26 10:56:09 -05:00
clientConfig := &core.Config{
2021-06-19 09:36:54 -04:00
App: []*anypb.Any{
2017-11-26 10:56:09 -05:00
serial.ToTypedMessage(&log.Config{
2021-10-02 17:29:37 -04:00
Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: log.LogType_Console},
2017-11-26 10:56:09 -05:00
}),
},
Inbound: []*core.InboundHandlerConfig{
2017-11-26 10:56:09 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(clientPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
2020-12-31 23:52:04 -05:00
Address: net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
Networks: []net.Network{net.Network_TCP},
2017-11-26 10:56:09 -05:00
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-11-26 10:56:09 -05:00
{
ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{
Server: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
Port: uint32(serverPort),
User: []*protocol.User{
{
Account: account,
},
},
},
},
}),
},
},
}
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
2019-01-11 11:17:59 -05:00
common.Must(err)
defer CloseAllServers(servers)
2017-11-26 10:56:09 -05:00
var errGroup errgroup.Group
2017-11-26 10:56:09 -05:00
for i := 0; i < 10; i++ {
errGroup.Go(testTCPConn(clientPort, 10240*1024, time.Second*20))
2017-11-26 10:56:09 -05:00
}
if err := errGroup.Wait(); err != nil {
2019-01-11 11:17:59 -05:00
t.Error(err)
}
2017-11-26 10:56:09 -05:00
}
func TestShadowsocksAES128GCMUDP(t *testing.T) {
udpServer := udp.Server{
MsgProcessor: xor,
}
dest, err := udpServer.Start()
2019-02-02 16:19:30 -05:00
common.Must(err)
2017-11-26 10:56:09 -05:00
defer udpServer.Close()
account := serial.ToTypedMessage(&shadowsocks.Account{
Password: "shadowsocks-password",
CipherType: shadowsocks.CipherType_AES_128_GCM,
})
serverPort := udp.PickPort()
2017-11-26 10:56:09 -05:00
serverConfig := &core.Config{
2021-06-19 09:36:54 -04:00
App: []*anypb.Any{
2017-11-26 10:56:09 -05:00
serial.ToTypedMessage(&log.Config{
2021-10-02 17:29:37 -04:00
Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: log.LogType_Console},
2017-11-26 10:56:09 -05:00
}),
},
Inbound: []*core.InboundHandlerConfig{
2017-11-26 10:56:09 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{
User: &protocol.User{
Account: account,
Level: 1,
},
2018-04-03 11:51:01 -04:00
Network: []net.Network{net.Network_UDP},
2017-11-26 10:56:09 -05:00
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-11-26 10:56:09 -05:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}
clientPort := udp.PickPort()
2017-11-26 10:56:09 -05:00
clientConfig := &core.Config{
2021-06-19 09:36:54 -04:00
App: []*anypb.Any{
2017-11-26 10:56:09 -05:00
serial.ToTypedMessage(&log.Config{
2021-10-02 17:29:37 -04:00
Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: log.LogType_Console},
2017-11-26 10:56:09 -05:00
}),
},
Inbound: []*core.InboundHandlerConfig{
2017-11-26 10:56:09 -05:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(clientPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
2020-12-31 23:52:04 -05:00
Address: net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
Networks: []net.Network{net.Network_UDP},
2017-11-26 10:56:09 -05:00
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-11-26 10:56:09 -05:00
{
ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{
Server: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
Port: uint32(serverPort),
User: []*protocol.User{
{
Account: account,
},
},
},
},
}),
},
},
}
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-03 13:46:53 -05:00
defer CloseAllServers(servers)
2017-11-26 10:56:09 -05:00
var errGroup errgroup.Group
2018-03-07 16:47:18 -05:00
for i := 0; i < 10; i++ {
errGroup.Go(testUDPConn(clientPort, 1024, time.Second*5))
2019-02-03 13:46:53 -05:00
}
if err := errGroup.Wait(); err != nil {
2019-02-03 13:46:53 -05:00
t.Error(err)
2018-03-07 16:47:18 -05:00
}
}
func TestShadowsocksAES128GCMUDPMux(t *testing.T) {
udpServer := udp.Server{
MsgProcessor: xor,
}
dest, err := udpServer.Start()
2019-02-02 16:19:30 -05:00
common.Must(err)
2018-03-07 16:47:18 -05:00
defer udpServer.Close()
account := serial.ToTypedMessage(&shadowsocks.Account{
Password: "shadowsocks-password",
CipherType: shadowsocks.CipherType_AES_128_GCM,
})
serverPort := tcp.PickPort()
serverConfig := &core.Config{
2021-06-19 09:36:54 -04:00
App: []*anypb.Any{
2018-03-07 16:47:18 -05: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-03-07 16:47:18 -05:00
}),
},
Inbound: []*core.InboundHandlerConfig{
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{
User: &protocol.User{
Account: account,
Level: 1,
},
2018-04-03 11:51:01 -04:00
Network: []net.Network{net.Network_TCP},
2018-03-07 16:47:18 -05:00
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}
clientPort := udp.PickPort()
2018-03-07 16:47:18 -05:00
clientConfig := &core.Config{
2021-06-19 09:36:54 -04:00
App: []*anypb.Any{
2018-03-07 16:47:18 -05: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-03-07 16:47:18 -05:00
}),
},
Inbound: []*core.InboundHandlerConfig{
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(clientPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
2020-12-31 23:52:04 -05:00
Address: net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
Networks: []net.Network{net.Network_UDP},
2018-03-07 16:47:18 -05:00
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
MultiplexSettings: &proxyman.MultiplexingConfig{
Enabled: true,
Concurrency: 8,
},
}),
ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{
Server: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
Port: uint32(serverPort),
User: []*protocol.User{
{
Account: account,
},
},
},
},
}),
},
},
}
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
2019-02-02 16:19:30 -05:00
common.Must(err)
2019-02-03 13:46:53 -05:00
defer CloseAllServers(servers)
2018-03-07 16:47:18 -05:00
var errGroup errgroup.Group
2017-11-26 10:56:09 -05:00
for i := 0; i < 10; i++ {
errGroup.Go(testUDPConn(clientPort, 1024, time.Second*5))
2019-02-03 13:46:53 -05:00
}
if err := errGroup.Wait(); err != nil {
2019-02-03 13:46:53 -05:00
t.Error(err)
2017-11-26 10:56:09 -05:00
}
}
2018-10-23 16:27:18 -04:00
func TestShadowsocksNone(t *testing.T) {
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
common.Must(err)
defer tcpServer.Close()
account := serial.ToTypedMessage(&shadowsocks.Account{
Password: "shadowsocks-password",
CipherType: shadowsocks.CipherType_NONE,
})
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(&shadowsocks.ServerConfig{
User: &protocol.User{
Account: account,
Level: 1,
},
Network: []net.Network{net.Network_TCP},
}),
},
},
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{
2020-12-31 23:52:04 -05:00
Address: net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
Networks: []net.Network{net.Network_TCP},
2018-10-23 16:27:18 -04:00
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{
Server: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
Port: uint32(serverPort),
User: []*protocol.User{
{
Account: account,
},
},
},
},
}),
},
},
}
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
common.Must(err)
defer CloseAllServers(servers)
var errGroup errgroup.Group
2018-10-23 16:27:18 -04:00
for i := 0; i < 10; i++ {
errGroup.Go(testTCPConn(clientPort, 10240*1024, time.Second*20))
2019-01-06 17:30:38 -05:00
}
if err := errGroup.Wait(); err != nil {
2019-01-06 17:30:38 -05:00
t.Fatal(err)
2018-10-23 16:27:18 -04:00
}
}