|  | @@ -1,51 +1,255 @@
 | 
	
		
			
				|  |  |  package scenarios
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import (
 | 
	
		
			
				|  |  | +	"crypto/rand"
 | 
	
		
			
				|  |  |  	"net"
 | 
	
		
			
				|  |  | +	"sync"
 | 
	
		
			
				|  |  |  	"testing"
 | 
	
		
			
				|  |  |  	"time"
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +	"v2ray.com/core"
 | 
	
		
			
				|  |  | +	"v2ray.com/core/app/log"
 | 
	
		
			
				|  |  | +	"v2ray.com/core/app/proxyman"
 | 
	
		
			
				|  |  |  	v2net "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"
 | 
	
		
			
				|  |  |  	"v2ray.com/core/testing/assert"
 | 
	
		
			
				|  |  |  	"v2ray.com/core/testing/servers/tcp"
 | 
	
		
			
				|  |  |  )
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -func TestShadowsocksTCP(t *testing.T) {
 | 
	
		
			
				|  |  | +func TestShadowsocksAES256TCP(t *testing.T) {
 | 
	
		
			
				|  |  |  	assert := assert.On(t)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	tcpServer := &tcp.Server{
 | 
	
		
			
				|  |  | -		Port: v2net.Port(50052),
 | 
	
		
			
				|  |  | -		MsgProcessor: func(data []byte) []byte {
 | 
	
		
			
				|  |  | -			buffer := make([]byte, 0, 2048)
 | 
	
		
			
				|  |  | -			buffer = append(buffer, []byte("Processed: ")...)
 | 
	
		
			
				|  |  | -			buffer = append(buffer, data...)
 | 
	
		
			
				|  |  | -			return buffer
 | 
	
		
			
				|  |  | +	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{
 | 
	
		
			
				|  |  | +		Inbound: []*proxyman.InboundHandlerConfig{
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
 | 
	
		
			
				|  |  | +					PortRange: v2net.SinglePortRange(serverPort),
 | 
	
		
			
				|  |  | +					Listen:    v2net.NewIPOrDomain(v2net.LocalHostIP),
 | 
	
		
			
				|  |  | +				}),
 | 
	
		
			
				|  |  | +				ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{
 | 
	
		
			
				|  |  | +					User: &protocol.User{
 | 
	
		
			
				|  |  | +						Account: account,
 | 
	
		
			
				|  |  | +					},
 | 
	
		
			
				|  |  | +				}),
 | 
	
		
			
				|  |  | +			},
 | 
	
		
			
				|  |  | +		},
 | 
	
		
			
				|  |  | +		Outbound: []*proxyman.OutboundHandlerConfig{
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
 | 
	
		
			
				|  |  | +			},
 | 
	
		
			
				|  |  | +		},
 | 
	
		
			
				|  |  | +		App: []*serial.TypedMessage{
 | 
	
		
			
				|  |  | +			serial.ToTypedMessage(&log.Config{
 | 
	
		
			
				|  |  | +				ErrorLogLevel: log.LogLevel_Debug,
 | 
	
		
			
				|  |  | +				ErrorLogType:  log.LogType_Console,
 | 
	
		
			
				|  |  | +			}),
 | 
	
		
			
				|  |  | +		},
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	clientPort := pickPort()
 | 
	
		
			
				|  |  | +	clientConfig := &core.Config{
 | 
	
		
			
				|  |  | +		Inbound: []*proxyman.InboundHandlerConfig{
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
 | 
	
		
			
				|  |  | +					PortRange: v2net.SinglePortRange(clientPort),
 | 
	
		
			
				|  |  | +					Listen:    v2net.NewIPOrDomain(v2net.LocalHostIP),
 | 
	
		
			
				|  |  | +				}),
 | 
	
		
			
				|  |  | +				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
 | 
	
		
			
				|  |  | +					Address: v2net.NewIPOrDomain(dest.Address),
 | 
	
		
			
				|  |  | +					Port:    uint32(dest.Port),
 | 
	
		
			
				|  |  | +					NetworkList: &v2net.NetworkList{
 | 
	
		
			
				|  |  | +						Network: []v2net.Network{v2net.Network_TCP},
 | 
	
		
			
				|  |  | +					},
 | 
	
		
			
				|  |  | +				}),
 | 
	
		
			
				|  |  | +			},
 | 
	
		
			
				|  |  | +		},
 | 
	
		
			
				|  |  | +		Outbound: []*proxyman.OutboundHandlerConfig{
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{
 | 
	
		
			
				|  |  | +					Server: []*protocol.ServerEndpoint{
 | 
	
		
			
				|  |  | +						{
 | 
	
		
			
				|  |  | +							Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
 | 
	
		
			
				|  |  | +							Port:    uint32(serverPort),
 | 
	
		
			
				|  |  | +							User: []*protocol.User{
 | 
	
		
			
				|  |  | +								{
 | 
	
		
			
				|  |  | +									Account: account,
 | 
	
		
			
				|  |  | +								},
 | 
	
		
			
				|  |  | +							},
 | 
	
		
			
				|  |  | +						},
 | 
	
		
			
				|  |  | +					},
 | 
	
		
			
				|  |  | +				}),
 | 
	
		
			
				|  |  | +			},
 | 
	
		
			
				|  |  | +		},
 | 
	
		
			
				|  |  | +		App: []*serial.TypedMessage{
 | 
	
		
			
				|  |  | +			serial.ToTypedMessage(&log.Config{
 | 
	
		
			
				|  |  | +				ErrorLogLevel: log.LogLevel_Debug,
 | 
	
		
			
				|  |  | +				ErrorLogType:  log.LogType_Console,
 | 
	
		
			
				|  |  | +			}),
 | 
	
		
			
				|  |  |  		},
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  | -	_, err := tcpServer.Start()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	assert.Error(InitializeServerConfig(serverConfig)).IsNil()
 | 
	
		
			
				|  |  | +	assert.Error(InitializeServerConfig(clientConfig)).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.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))
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			response := readFrom(conn, time.Second*10, 10240*1024)
 | 
	
		
			
				|  |  | +			assert.Bytes(response).Equals(xor([]byte(payload)))
 | 
	
		
			
				|  |  | +			assert.Error(conn.Close()).IsNil()
 | 
	
		
			
				|  |  | +			wg.Done()
 | 
	
		
			
				|  |  | +		}()
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	wg.Wait()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	CloseAllServers()
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +func TestShadowsocksChacha20TCP(t *testing.T) {
 | 
	
		
			
				|  |  | +	assert := assert.On(t)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	tcpServer := tcp.Server{
 | 
	
		
			
				|  |  | +		MsgProcessor: xor,
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	dest, err := tcpServer.Start()
 | 
	
		
			
				|  |  |  	assert.Error(err).IsNil()
 | 
	
		
			
				|  |  |  	defer tcpServer.Close()
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	assert.Error(InitializeServerSetOnce("test_6")).IsNil()
 | 
	
		
			
				|  |  | +	account := serial.ToTypedMessage(&shadowsocks.Account{
 | 
	
		
			
				|  |  | +		Password:   "shadowsocks-password",
 | 
	
		
			
				|  |  | +		CipherType: shadowsocks.CipherType_CHACHA20_IETF,
 | 
	
		
			
				|  |  | +		Ota:        shadowsocks.Account_Enabled,
 | 
	
		
			
				|  |  | +	})
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	serverPort := pickPort()
 | 
	
		
			
				|  |  | +	serverConfig := &core.Config{
 | 
	
		
			
				|  |  | +		Inbound: []*proxyman.InboundHandlerConfig{
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
 | 
	
		
			
				|  |  | +					PortRange: v2net.SinglePortRange(serverPort),
 | 
	
		
			
				|  |  | +					Listen:    v2net.NewIPOrDomain(v2net.LocalHostIP),
 | 
	
		
			
				|  |  | +				}),
 | 
	
		
			
				|  |  | +				ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{
 | 
	
		
			
				|  |  | +					User: &protocol.User{
 | 
	
		
			
				|  |  | +						Account: account,
 | 
	
		
			
				|  |  | +					},
 | 
	
		
			
				|  |  | +				}),
 | 
	
		
			
				|  |  | +			},
 | 
	
		
			
				|  |  | +		},
 | 
	
		
			
				|  |  | +		Outbound: []*proxyman.OutboundHandlerConfig{
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
 | 
	
		
			
				|  |  | +			},
 | 
	
		
			
				|  |  | +		},
 | 
	
		
			
				|  |  | +		App: []*serial.TypedMessage{
 | 
	
		
			
				|  |  | +			serial.ToTypedMessage(&log.Config{
 | 
	
		
			
				|  |  | +				ErrorLogLevel: log.LogLevel_Debug,
 | 
	
		
			
				|  |  | +				ErrorLogType:  log.LogType_Console,
 | 
	
		
			
				|  |  | +			}),
 | 
	
		
			
				|  |  | +		},
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	clientPort := pickPort()
 | 
	
		
			
				|  |  | +	clientConfig := &core.Config{
 | 
	
		
			
				|  |  | +		Inbound: []*proxyman.InboundHandlerConfig{
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
 | 
	
		
			
				|  |  | +					PortRange: v2net.SinglePortRange(clientPort),
 | 
	
		
			
				|  |  | +					Listen:    v2net.NewIPOrDomain(v2net.LocalHostIP),
 | 
	
		
			
				|  |  | +				}),
 | 
	
		
			
				|  |  | +				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
 | 
	
		
			
				|  |  | +					Address: v2net.NewIPOrDomain(dest.Address),
 | 
	
		
			
				|  |  | +					Port:    uint32(dest.Port),
 | 
	
		
			
				|  |  | +					NetworkList: &v2net.NetworkList{
 | 
	
		
			
				|  |  | +						Network: []v2net.Network{v2net.Network_TCP},
 | 
	
		
			
				|  |  | +					},
 | 
	
		
			
				|  |  | +				}),
 | 
	
		
			
				|  |  | +			},
 | 
	
		
			
				|  |  | +		},
 | 
	
		
			
				|  |  | +		Outbound: []*proxyman.OutboundHandlerConfig{
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				ProxySettings: serial.ToTypedMessage(&shadowsocks.ClientConfig{
 | 
	
		
			
				|  |  | +					Server: []*protocol.ServerEndpoint{
 | 
	
		
			
				|  |  | +						{
 | 
	
		
			
				|  |  | +							Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
 | 
	
		
			
				|  |  | +							Port:    uint32(serverPort),
 | 
	
		
			
				|  |  | +							User: []*protocol.User{
 | 
	
		
			
				|  |  | +								{
 | 
	
		
			
				|  |  | +									Account: account,
 | 
	
		
			
				|  |  | +								},
 | 
	
		
			
				|  |  | +							},
 | 
	
		
			
				|  |  | +						},
 | 
	
		
			
				|  |  | +					},
 | 
	
		
			
				|  |  | +				}),
 | 
	
		
			
				|  |  | +			},
 | 
	
		
			
				|  |  | +		},
 | 
	
		
			
				|  |  | +		App: []*serial.TypedMessage{
 | 
	
		
			
				|  |  | +			serial.ToTypedMessage(&log.Config{
 | 
	
		
			
				|  |  | +				ErrorLogLevel: log.LogLevel_Debug,
 | 
	
		
			
				|  |  | +				ErrorLogType:  log.LogType_Console,
 | 
	
		
			
				|  |  | +			}),
 | 
	
		
			
				|  |  | +		},
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	assert.Error(InitializeServerConfig(serverConfig)).IsNil()
 | 
	
		
			
				|  |  | +	assert.Error(InitializeServerConfig(clientConfig)).IsNil()
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	for i := 0; i < 1; i++ {
 | 
	
		
			
				|  |  | -		conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
 | 
	
		
			
				|  |  | -			IP:   []byte{127, 0, 0, 1},
 | 
	
		
			
				|  |  | -			Port: 50050,
 | 
	
		
			
				|  |  | -		})
 | 
	
		
			
				|  |  | -		assert.Error(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.Error(err).IsNil()
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -		payload := "dokodemo request."
 | 
	
		
			
				|  |  | -		nBytes, err := conn.Write([]byte(payload))
 | 
	
		
			
				|  |  | -		assert.Error(err).IsNil()
 | 
	
		
			
				|  |  | -		assert.Int(nBytes).Equals(len(payload))
 | 
	
		
			
				|  |  | +			payload := make([]byte, 10240*1024)
 | 
	
		
			
				|  |  | +			rand.Read(payload)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -		expectedResponse := "Processed: " + payload
 | 
	
		
			
				|  |  | -		response := readFrom(conn, time.Second, len(expectedResponse))
 | 
	
		
			
				|  |  | -		assert.String(string(response)).Equals(expectedResponse)
 | 
	
		
			
				|  |  | +			nBytes, err := conn.Write([]byte(payload))
 | 
	
		
			
				|  |  | +			assert.Error(err).IsNil()
 | 
	
		
			
				|  |  | +			assert.Int(nBytes).Equals(len(payload))
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -		conn.Close()
 | 
	
		
			
				|  |  | +			response := readFrom(conn, time.Second*10, 10240*1024)
 | 
	
		
			
				|  |  | +			assert.Bytes(response).Equals(xor([]byte(payload)))
 | 
	
		
			
				|  |  | +			assert.Error(conn.Close()).IsNil()
 | 
	
		
			
				|  |  | +			wg.Done()
 | 
	
		
			
				|  |  | +		}()
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  | +	wg.Wait()
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	CloseAllServers()
 | 
	
		
			
				|  |  |  }
 |