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

972 lines
23 KiB
Go
Raw Normal View History

2016-01-28 07:40:00 -05:00
package scenarios
import (
"crypto/rand"
"sync"
2016-01-28 07:40:00 -05:00
"testing"
2017-01-29 03:02:19 -05:00
"time"
2016-12-09 06:08:25 -05:00
2018-10-23 16:27:18 -04:00
"v2ray.com/core/common"
"v2ray.com/core/common/compare"
"v2ray.com/core"
"v2ray.com/core/app/log"
"v2ray.com/core/app/proxyman"
2017-12-19 16:29:56 -05:00
clog "v2ray.com/core/common/log"
"v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/serial"
"v2ray.com/core/proxy/dokodemo"
"v2ray.com/core/proxy/freedom"
"v2ray.com/core/proxy/shadowsocks"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/testing/servers/tcp"
2017-05-02 16:29:02 -04:00
"v2ray.com/core/testing/servers/udp"
2017-10-26 15:44:22 -04:00
. "v2ray.com/ext/assert"
2016-01-28 07:40:00 -05:00
)
func TestShadowsocksAES256TCP(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2016-01-28 07:40:00 -05:00
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
defer tcpServer.Close()
account := serial.ToTypedMessage(&shadowsocks.Account{
Password: "shadowsocks-password",
CipherType: shadowsocks.CipherType_AES_256_CFB,
Ota: shadowsocks.Account_Enabled,
})
2018-03-01 05:53:39 -05:00
serverPort := tcp.PickPort()
serverConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&log.Config{
2017-12-19 16:29:56 -05:00
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),
}),
ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{
User: &protocol.User{
Account: account,
2017-03-31 14:56:30 -04:00
Level: 1,
},
2018-04-03 11:51:01 -04:00
Network: []net.Network{net.Network_TCP},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}
2018-03-01 05:53:39 -05:00
clientPort := tcp.PickPort()
clientConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&log.Config{
2017-12-19 16:29:56 -05:00
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(&shadowsocks.ClientConfig{
Server: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
Port: uint32(serverPort),
User: []*protocol.User{
{
Account: account,
},
},
},
},
}),
},
},
2016-01-28 07:40:00 -05:00
}
2017-05-17 18:39:30 -04:00
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
var wg sync.WaitGroup
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
payload := make([]byte, 10240*1024)
rand.Read(payload)
nBytes, err := conn.Write([]byte(payload))
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(nBytes, Equals, len(payload))
2017-03-31 11:24:39 -04:00
response := readFrom(conn, time.Second*20, 10240*1024)
2017-10-24 10:15:35 -04:00
assert(response, Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
2017-05-02 16:29:02 -04:00
wg.Done()
}()
}
wg.Wait()
2017-05-17 18:39:30 -04:00
CloseAllServers(servers)
2017-05-02 16:29:02 -04:00
}
func TestShadowsocksAES128UDP(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2017-05-02 16:29:02 -04:00
udpServer := udp.Server{
MsgProcessor: xor,
}
dest, err := udpServer.Start()
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-05-02 16:29:02 -04:00
defer udpServer.Close()
account := serial.ToTypedMessage(&shadowsocks.Account{
Password: "shadowsocks-password",
CipherType: shadowsocks.CipherType_AES_128_CFB,
Ota: shadowsocks.Account_Enabled,
})
2018-03-01 05:53:39 -05:00
serverPort := tcp.PickPort()
2017-05-02 16:29:02 -04:00
serverConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&log.Config{
2017-12-19 16:29:56 -05:00
ErrorLogLevel: clog.Severity_Debug,
ErrorLogType: log.LogType_Console,
}),
},
Inbound: []*core.InboundHandlerConfig{
2017-05-02 16:29:02 -04:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(serverPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
2017-05-02 16:29:02 -04:00
}),
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-05-02 16:29:02 -04:00
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-05-02 16:29:02 -04:00
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}
2018-03-01 05:53:39 -05:00
clientPort := tcp.PickPort()
clientConfig := &core.Config{
2017-05-02 16:29:02 -04:00
App: []*serial.TypedMessage{
serial.ToTypedMessage(&log.Config{
2017-12-19 16:29:56 -05:00
ErrorLogLevel: clog.Severity_Debug,
2017-05-02 16:29:02 -04:00
ErrorLogType: log.LogType_Console,
}),
},
Inbound: []*core.InboundHandlerConfig{
2017-05-02 16:29:02 -04:00
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: net.SinglePortRange(clientPort),
Listen: net.NewIPOrDomain(net.LocalHostIP),
2017-05-02 16:29:02 -04:00
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest.Address),
2017-05-02 16:29:02 -04:00
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_UDP},
2017-05-02 16:29:02 -04:00
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
2017-05-02 16:29:02 -04:00
{
ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{
Server: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
2017-05-02 16:29:02 -04:00
Port: uint32(serverPort),
User: []*protocol.User{
{
Account: account,
},
},
},
},
}),
},
},
}
2017-05-17 18:39:30 -04:00
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-05-02 16:29:02 -04:00
var wg sync.WaitGroup
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-05-02 16:29:02 -04:00
payload := make([]byte, 1024)
rand.Read(payload)
nBytes, err := conn.Write([]byte(payload))
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
assert(nBytes, Equals, len(payload))
2017-05-02 16:29:02 -04:00
response := readFrom(conn, time.Second*5, 1024)
2017-10-24 10:15:35 -04:00
assert(response, Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
wg.Done()
}()
}
wg.Wait()
2017-05-17 18:39:30 -04:00
CloseAllServers(servers)
}
func TestShadowsocksChacha20TCP(t *testing.T) {
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
2018-10-23 16:27:18 -04:00
common.Must(err)
2016-01-28 07:40:00 -05:00
defer tcpServer.Close()
account := serial.ToTypedMessage(&shadowsocks.Account{
Password: "shadowsocks-password",
CipherType: shadowsocks.CipherType_CHACHA20_IETF,
Ota: shadowsocks.Account_Enabled,
})
2018-03-01 05:53:39 -05:00
serverPort := tcp.PickPort()
serverConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&log.Config{
2017-12-19 16:29:56 -05:00
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),
}),
ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{
User: &protocol.User{
Account: account,
2017-03-31 14:56:30 -04:00
Level: 1,
},
2018-04-03 11:51:01 -04:00
Network: []net.Network{net.Network_TCP},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}
2018-03-01 05:53:39 -05:00
clientPort := tcp.PickPort()
clientConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&log.Config{
2017-12-19 16:29:56 -05:00
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(&shadowsocks.ClientConfig{
Server: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
Port: uint32(serverPort),
User: []*protocol.User{
{
Account: account,
},
},
},
},
}),
},
},
}
2017-05-17 18:39:30 -04:00
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
2018-10-23 16:27:18 -04:00
common.Must(err)
defer CloseAllServers(servers)
2016-11-04 20:00:54 -04:00
var wg sync.WaitGroup
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
2018-10-23 16:27:18 -04:00
defer wg.Done()
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
2018-10-23 16:27:18 -04:00
common.Must(err)
defer conn.Close()
2016-11-04 20:00:54 -04:00
payload := make([]byte, 10240*1024)
rand.Read(payload)
2016-11-04 20:00:54 -04:00
nBytes, err := conn.Write([]byte(payload))
2018-10-23 16:27:18 -04:00
common.Must(err)
if nBytes != len(payload) {
t.Error("only part of payload is written: ", nBytes)
}
2016-11-04 20:00:54 -04:00
2017-03-31 11:24:39 -04:00
response := readFrom(conn, time.Second*20, 10240*1024)
2018-10-23 16:27:18 -04:00
if err := compare.BytesEqualWithDetail(response, xor([]byte(payload))); err != nil {
t.Error(err)
}
}()
2016-11-04 20:00:54 -04:00
}
wg.Wait()
2018-10-23 16:27:18 -04:00
}
2016-02-23 12:16:13 -05:00
2018-10-23 16:27:18 -04:00
func TestShadowsocksChacha20Poly1305TCP(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_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{
Address: net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
},
}),
},
},
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 wg sync.WaitGroup
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
defer wg.Done()
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
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)
}
}()
}
wg.Wait()
2016-01-28 07:40:00 -05:00
}
2017-11-26 10:56:09 -05:00
func TestShadowsocksAES256GCMTCP(t *testing.T) {
assert := With(t)
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
assert(err, IsNil)
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{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&log.Config{
2017-12-19 16:29:56 -05:00
ErrorLogLevel: clog.Severity_Debug,
2017-11-26 10:56:09 -05:00
ErrorLogType: log.LogType_Console,
}),
},
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{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&log.Config{
2017-12-19 16:29:56 -05:00
ErrorLogLevel: clog.Severity_Debug,
2017-11-26 10:56:09 -05:00
ErrorLogType: log.LogType_Console,
}),
},
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{
Address: net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
},
}),
},
},
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)
assert(err, IsNil)
var wg sync.WaitGroup
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
assert(err, IsNil)
payload := make([]byte, 10240*1024)
rand.Read(payload)
nBytes, err := conn.Write([]byte(payload))
assert(err, IsNil)
assert(nBytes, Equals, len(payload))
response := readFrom(conn, time.Second*20, 10240*1024)
assert(response, Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
wg.Done()
}()
}
wg.Wait()
CloseAllServers(servers)
}
func TestShadowsocksAES128GCMUDP(t *testing.T) {
assert := With(t)
udpServer := udp.Server{
MsgProcessor: xor,
}
dest, err := udpServer.Start()
assert(err, IsNil)
defer udpServer.Close()
account := serial.ToTypedMessage(&shadowsocks.Account{
Password: "shadowsocks-password",
CipherType: shadowsocks.CipherType_AES_128_GCM,
})
2018-03-01 05:53:39 -05:00
serverPort := tcp.PickPort()
2017-11-26 10:56:09 -05:00
serverConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&log.Config{
2017-12-19 16:29:56 -05:00
ErrorLogLevel: clog.Severity_Debug,
2017-11-26 10:56:09 -05:00
ErrorLogType: log.LogType_Console,
}),
},
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{}),
},
},
}
2018-03-01 05:53:39 -05:00
clientPort := tcp.PickPort()
2017-11-26 10:56:09 -05:00
clientConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&log.Config{
2017-12-19 16:29:56 -05:00
ErrorLogLevel: clog.Severity_Debug,
2017-11-26 10:56:09 -05:00
ErrorLogType: log.LogType_Console,
}),
},
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{
Address: net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_UDP},
},
}),
},
},
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)
assert(err, IsNil)
2018-03-07 16:47:18 -05:00
var wg sync.WaitGroup
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
assert(err, IsNil)
payload := make([]byte, 1024)
rand.Read(payload)
nBytes, err := conn.Write([]byte(payload))
assert(err, IsNil)
assert(nBytes, Equals, len(payload))
response := readFrom(conn, time.Second*5, 1024)
assert(response, Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
wg.Done()
}()
}
wg.Wait()
CloseAllServers(servers)
}
func TestShadowsocksAES128GCMUDPMux(t *testing.T) {
assert := With(t)
udpServer := udp.Server{
MsgProcessor: xor,
}
dest, err := udpServer.Start()
assert(err, IsNil)
defer udpServer.Close()
account := serial.ToTypedMessage(&shadowsocks.Account{
Password: "shadowsocks-password",
CipherType: shadowsocks.CipherType_AES_128_GCM,
})
serverPort := tcp.PickPort()
serverConfig := &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(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 := 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_UDP},
},
}),
},
},
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)
assert(err, IsNil)
2017-11-26 10:56:09 -05:00
var wg sync.WaitGroup
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
assert(err, IsNil)
payload := make([]byte, 1024)
rand.Read(payload)
nBytes, err := conn.Write([]byte(payload))
assert(err, IsNil)
assert(nBytes, Equals, len(payload))
response := readFrom(conn, time.Second*5, 1024)
assert(response, Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
wg.Done()
}()
}
wg.Wait()
CloseAllServers(servers)
}
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,
Ota: shadowsocks.Account_Enabled,
})
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{
Address: net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
},
}),
},
},
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 wg sync.WaitGroup
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
defer wg.Done()
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
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)
}
}()
}
wg.Wait()
}