2016-01-28 12:40:00 +00:00
|
|
|
package scenarios
|
|
|
|
|
|
|
|
import (
|
2017-03-31 15:06:16 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"sync"
|
2016-01-28 12:40:00 +00:00
|
|
|
"testing"
|
2017-01-29 08:02:19 +00:00
|
|
|
"time"
|
2016-12-09 11:08:25 +00:00
|
|
|
|
2017-03-31 15:06:16 +00:00
|
|
|
"v2ray.com/core"
|
|
|
|
"v2ray.com/core/app/log"
|
|
|
|
"v2ray.com/core/app/proxyman"
|
2017-08-29 10:56:57 +00:00
|
|
|
"v2ray.com/core/common/net"
|
2017-03-31 15:06:16 +00:00
|
|
|
"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 18:55:45 +00:00
|
|
|
"v2ray.com/core/testing/assert"
|
|
|
|
"v2ray.com/core/testing/servers/tcp"
|
2017-05-02 20:29:02 +00:00
|
|
|
"v2ray.com/core/testing/servers/udp"
|
2016-01-28 12:40:00 +00:00
|
|
|
)
|
|
|
|
|
2017-03-31 15:06:16 +00:00
|
|
|
func TestShadowsocksAES256TCP(t *testing.T) {
|
2016-05-24 19:55:46 +00:00
|
|
|
assert := assert.On(t)
|
2016-01-28 12:40:00 +00:00
|
|
|
|
2017-03-31 15:06:16 +00:00
|
|
|
tcpServer := tcp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
}
|
|
|
|
dest, err := tcpServer.Start()
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
defer tcpServer.Close()
|
|
|
|
|
|
|
|
account := serial.ToTypedMessage(&shadowsocks.Account{
|
|
|
|
Password: "shadowsocks-password",
|
|
|
|
CipherType: shadowsocks.CipherType_AES_256_CFB,
|
|
|
|
Ota: shadowsocks.Account_Enabled,
|
|
|
|
})
|
|
|
|
|
|
|
|
serverPort := pickPort()
|
|
|
|
serverConfig := &core.Config{
|
2017-10-20 21:30:36 +00:00
|
|
|
App: []*serial.TypedMessage{
|
|
|
|
serial.ToTypedMessage(&log.Config{
|
|
|
|
ErrorLogLevel: log.LogLevel_Debug,
|
|
|
|
ErrorLogType: log.LogType_Console,
|
|
|
|
}),
|
|
|
|
},
|
2017-03-31 15:06:16 +00:00
|
|
|
Inbound: []*proxyman.InboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 10:56:57 +00:00
|
|
|
PortRange: net.SinglePortRange(serverPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-03-31 15:06:16 +00:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{
|
|
|
|
User: &protocol.User{
|
|
|
|
Account: account,
|
2017-03-31 18:56:30 +00:00
|
|
|
Level: 1,
|
2017-03-31 15:06:16 +00:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Outbound: []*proxyman.OutboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
|
|
|
},
|
|
|
|
},
|
2017-10-20 21:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clientPort := pickPort()
|
|
|
|
clientConfig := &core.Config{
|
2017-03-31 15:06:16 +00:00
|
|
|
App: []*serial.TypedMessage{
|
|
|
|
serial.ToTypedMessage(&log.Config{
|
|
|
|
ErrorLogLevel: log.LogLevel_Debug,
|
|
|
|
ErrorLogType: log.LogType_Console,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Inbound: []*proxyman.InboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 10:56:57 +00:00
|
|
|
PortRange: net.SinglePortRange(clientPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-03-31 15:06:16 +00:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
|
2017-08-29 10:56:57 +00:00
|
|
|
Address: net.NewIPOrDomain(dest.Address),
|
2017-03-31 15:06:16 +00:00
|
|
|
Port: uint32(dest.Port),
|
2017-08-29 10:56:57 +00:00
|
|
|
NetworkList: &net.NetworkList{
|
|
|
|
Network: []net.Network{net.Network_TCP},
|
2017-03-31 15:06:16 +00:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Outbound: []*proxyman.OutboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{
|
|
|
|
Server: []*protocol.ServerEndpoint{
|
|
|
|
{
|
2017-08-29 10:56:57 +00:00
|
|
|
Address: net.NewIPOrDomain(net.LocalHostIP),
|
2017-03-31 15:06:16 +00:00
|
|
|
Port: uint32(serverPort),
|
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Account: account,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2016-01-28 12:40:00 +00:00
|
|
|
}
|
2017-03-31 15:06:16 +00:00
|
|
|
|
2017-05-17 22:39:30 +00:00
|
|
|
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
|
|
|
|
assert.Error(err).IsNil()
|
2017-03-31 15:06:16 +00:00
|
|
|
|
|
|
|
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.Error(err).IsNil()
|
|
|
|
|
|
|
|
payload := make([]byte, 10240*1024)
|
|
|
|
rand.Read(payload)
|
|
|
|
|
|
|
|
nBytes, err := conn.Write([]byte(payload))
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Int(nBytes).Equals(len(payload))
|
|
|
|
|
2017-03-31 15:24:39 +00:00
|
|
|
response := readFrom(conn, time.Second*20, 10240*1024)
|
2017-03-31 15:06:16 +00:00
|
|
|
assert.Bytes(response).Equals(xor([]byte(payload)))
|
|
|
|
assert.Error(conn.Close()).IsNil()
|
2017-05-02 20:29:02 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
2017-05-17 22:39:30 +00:00
|
|
|
CloseAllServers(servers)
|
2017-05-02 20:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShadowsocksAES128UDP(t *testing.T) {
|
|
|
|
assert := assert.On(t)
|
|
|
|
|
|
|
|
udpServer := udp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
}
|
|
|
|
dest, err := udpServer.Start()
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
defer udpServer.Close()
|
|
|
|
|
|
|
|
account := serial.ToTypedMessage(&shadowsocks.Account{
|
|
|
|
Password: "shadowsocks-password",
|
|
|
|
CipherType: shadowsocks.CipherType_AES_128_CFB,
|
|
|
|
Ota: shadowsocks.Account_Enabled,
|
|
|
|
})
|
|
|
|
|
|
|
|
serverPort := pickPort()
|
|
|
|
serverConfig := &core.Config{
|
2017-10-20 21:30:36 +00:00
|
|
|
App: []*serial.TypedMessage{
|
|
|
|
serial.ToTypedMessage(&log.Config{
|
|
|
|
ErrorLogLevel: log.LogLevel_Debug,
|
|
|
|
ErrorLogType: log.LogType_Console,
|
|
|
|
}),
|
|
|
|
},
|
2017-05-02 20:29:02 +00:00
|
|
|
Inbound: []*proxyman.InboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 10:56:57 +00:00
|
|
|
PortRange: net.SinglePortRange(serverPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-05-02 20:29:02 +00:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{
|
|
|
|
UdpEnabled: true,
|
|
|
|
User: &protocol.User{
|
|
|
|
Account: account,
|
|
|
|
Level: 1,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Outbound: []*proxyman.OutboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
|
|
|
},
|
|
|
|
},
|
2017-10-20 21:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clientPort := pickPort()
|
|
|
|
clientConfig := &core.Config{
|
2017-05-02 20:29:02 +00:00
|
|
|
App: []*serial.TypedMessage{
|
|
|
|
serial.ToTypedMessage(&log.Config{
|
|
|
|
ErrorLogLevel: log.LogLevel_Debug,
|
|
|
|
ErrorLogType: log.LogType_Console,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Inbound: []*proxyman.InboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 10:56:57 +00:00
|
|
|
PortRange: net.SinglePortRange(clientPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-05-02 20:29:02 +00:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
|
2017-08-29 10:56:57 +00:00
|
|
|
Address: net.NewIPOrDomain(dest.Address),
|
2017-05-02 20:29:02 +00:00
|
|
|
Port: uint32(dest.Port),
|
2017-08-29 10:56:57 +00:00
|
|
|
NetworkList: &net.NetworkList{
|
|
|
|
Network: []net.Network{net.Network_UDP},
|
2017-05-02 20:29:02 +00:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Outbound: []*proxyman.OutboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{
|
|
|
|
Server: []*protocol.ServerEndpoint{
|
|
|
|
{
|
2017-08-29 10:56:57 +00:00
|
|
|
Address: net.NewIPOrDomain(net.LocalHostIP),
|
2017-05-02 20:29:02 +00:00
|
|
|
Port: uint32(serverPort),
|
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Account: account,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-17 22:39:30 +00:00
|
|
|
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
|
|
|
|
assert.Error(err).IsNil()
|
2017-05-02 20:29:02 +00: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.Error(err).IsNil()
|
|
|
|
|
|
|
|
payload := make([]byte, 1024)
|
|
|
|
rand.Read(payload)
|
|
|
|
|
|
|
|
nBytes, err := conn.Write([]byte(payload))
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Int(nBytes).Equals(len(payload))
|
|
|
|
|
|
|
|
response := readFrom(conn, time.Second*5, 1024)
|
|
|
|
assert.Bytes(response).Equals(xor([]byte(payload)))
|
|
|
|
assert.Error(conn.Close()).IsNil()
|
2017-03-31 15:06:16 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
2017-05-17 22:39:30 +00:00
|
|
|
CloseAllServers(servers)
|
2017-03-31 15:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShadowsocksChacha20TCP(t *testing.T) {
|
|
|
|
assert := assert.On(t)
|
|
|
|
|
|
|
|
tcpServer := tcp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
}
|
|
|
|
dest, err := tcpServer.Start()
|
2016-01-28 12:40:00 +00:00
|
|
|
assert.Error(err).IsNil()
|
|
|
|
defer tcpServer.Close()
|
|
|
|
|
2017-03-31 15:06:16 +00:00
|
|
|
account := serial.ToTypedMessage(&shadowsocks.Account{
|
|
|
|
Password: "shadowsocks-password",
|
|
|
|
CipherType: shadowsocks.CipherType_CHACHA20_IETF,
|
|
|
|
Ota: shadowsocks.Account_Enabled,
|
|
|
|
})
|
|
|
|
|
|
|
|
serverPort := pickPort()
|
|
|
|
serverConfig := &core.Config{
|
2017-10-20 21:30:36 +00:00
|
|
|
App: []*serial.TypedMessage{
|
|
|
|
serial.ToTypedMessage(&log.Config{
|
|
|
|
ErrorLogLevel: log.LogLevel_Debug,
|
|
|
|
ErrorLogType: log.LogType_Console,
|
|
|
|
}),
|
|
|
|
},
|
2017-03-31 15:06:16 +00:00
|
|
|
Inbound: []*proxyman.InboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 10:56:57 +00:00
|
|
|
PortRange: net.SinglePortRange(serverPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-03-31 15:06:16 +00:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{
|
|
|
|
User: &protocol.User{
|
|
|
|
Account: account,
|
2017-03-31 18:56:30 +00:00
|
|
|
Level: 1,
|
2017-03-31 15:06:16 +00:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Outbound: []*proxyman.OutboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
|
|
|
},
|
|
|
|
},
|
2017-10-20 21:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clientPort := pickPort()
|
|
|
|
clientConfig := &core.Config{
|
2017-03-31 15:06:16 +00:00
|
|
|
App: []*serial.TypedMessage{
|
|
|
|
serial.ToTypedMessage(&log.Config{
|
|
|
|
ErrorLogLevel: log.LogLevel_Debug,
|
|
|
|
ErrorLogType: log.LogType_Console,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Inbound: []*proxyman.InboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 10:56:57 +00:00
|
|
|
PortRange: net.SinglePortRange(clientPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-03-31 15:06:16 +00:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
|
2017-08-29 10:56:57 +00:00
|
|
|
Address: net.NewIPOrDomain(dest.Address),
|
2017-03-31 15:06:16 +00:00
|
|
|
Port: uint32(dest.Port),
|
2017-08-29 10:56:57 +00:00
|
|
|
NetworkList: &net.NetworkList{
|
|
|
|
Network: []net.Network{net.Network_TCP},
|
2017-03-31 15:06:16 +00:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Outbound: []*proxyman.OutboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{
|
|
|
|
Server: []*protocol.ServerEndpoint{
|
|
|
|
{
|
2017-08-29 10:56:57 +00:00
|
|
|
Address: net.NewIPOrDomain(net.LocalHostIP),
|
2017-03-31 15:06:16 +00:00
|
|
|
Port: uint32(serverPort),
|
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Account: account,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-17 22:39:30 +00:00
|
|
|
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
|
|
|
|
assert.Error(err).IsNil()
|
2016-11-05 00:00:54 +00:00
|
|
|
|
2017-03-31 15:06:16 +00:00
|
|
|
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.Error(err).IsNil()
|
2016-11-05 00:00:54 +00:00
|
|
|
|
2017-03-31 15:06:16 +00:00
|
|
|
payload := make([]byte, 10240*1024)
|
|
|
|
rand.Read(payload)
|
2016-11-05 00:00:54 +00:00
|
|
|
|
2017-03-31 15:06:16 +00:00
|
|
|
nBytes, err := conn.Write([]byte(payload))
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Int(nBytes).Equals(len(payload))
|
2016-11-05 00:00:54 +00:00
|
|
|
|
2017-03-31 15:24:39 +00:00
|
|
|
response := readFrom(conn, time.Second*20, 10240*1024)
|
2017-03-31 15:06:16 +00:00
|
|
|
assert.Bytes(response).Equals(xor([]byte(payload)))
|
|
|
|
assert.Error(conn.Close()).IsNil()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
2016-11-05 00:00:54 +00:00
|
|
|
}
|
2017-03-31 15:06:16 +00:00
|
|
|
wg.Wait()
|
2016-02-23 17:16:13 +00:00
|
|
|
|
2017-05-17 22:39:30 +00:00
|
|
|
CloseAllServers(servers)
|
2016-01-28 12:40:00 +00:00
|
|
|
}
|