|  | @@ -4,7 +4,16 @@ import (
 | 
	
		
			
				|  |  |  	"net"
 | 
	
		
			
				|  |  |  	"testing"
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +	"v2ray.com/core"
 | 
	
		
			
				|  |  |  	v2net "v2ray.com/core/common/net"
 | 
	
		
			
				|  |  | +	"v2ray.com/core/common/protocol"
 | 
	
		
			
				|  |  | +	"v2ray.com/core/common/serial"
 | 
	
		
			
				|  |  | +	"v2ray.com/core/common/uuid"
 | 
	
		
			
				|  |  | +	"v2ray.com/core/proxy/dokodemo"
 | 
	
		
			
				|  |  | +	"v2ray.com/core/proxy/freedom"
 | 
	
		
			
				|  |  | +	"v2ray.com/core/proxy/vmess"
 | 
	
		
			
				|  |  | +	"v2ray.com/core/proxy/vmess/inbound"
 | 
	
		
			
				|  |  | +	"v2ray.com/core/proxy/vmess/outbound"
 | 
	
		
			
				|  |  |  	"v2ray.com/core/testing/assert"
 | 
	
		
			
				|  |  |  	"v2ray.com/core/testing/servers/tcp"
 | 
	
		
			
				|  |  |  )
 | 
	
	
		
			
				|  | @@ -12,25 +21,79 @@ import (
 | 
	
		
			
				|  |  |  func TestDokodemoTCP(t *testing.T) {
 | 
	
		
			
				|  |  |  	assert := assert.On(t)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	tcpServer := &tcp.Server{
 | 
	
		
			
				|  |  | -		Port: v2net.Port(50016),
 | 
	
		
			
				|  |  | -		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,
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  | -	_, err := tcpServer.Start()
 | 
	
		
			
				|  |  | +	dest, err := tcpServer.Start()
 | 
	
		
			
				|  |  |  	assert.Error(err).IsNil()
 | 
	
		
			
				|  |  |  	defer tcpServer.Close()
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	assert.Error(InitializeServerSetOnce("test_2")).IsNil()
 | 
	
		
			
				|  |  | +	userID := protocol.NewID(uuid.New())
 | 
	
		
			
				|  |  | +	serverPort := pickPort()
 | 
	
		
			
				|  |  | +	serverConfig := &core.Config{
 | 
	
		
			
				|  |  | +		Inbound: []*core.InboundConnectionConfig{
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				PortRange: v2net.SinglePortRange(serverPort),
 | 
	
		
			
				|  |  | +				ListenOn:  v2net.NewIPOrDomain(v2net.LocalHostIP),
 | 
	
		
			
				|  |  | +				Settings: serial.ToTypedMessage(&inbound.Config{
 | 
	
		
			
				|  |  | +					User: []*protocol.User{
 | 
	
		
			
				|  |  | +						{
 | 
	
		
			
				|  |  | +							Account: serial.ToTypedMessage(&vmess.Account{
 | 
	
		
			
				|  |  | +								Id: userID.String(),
 | 
	
		
			
				|  |  | +							}),
 | 
	
		
			
				|  |  | +						},
 | 
	
		
			
				|  |  | +					},
 | 
	
		
			
				|  |  | +				}),
 | 
	
		
			
				|  |  | +			},
 | 
	
		
			
				|  |  | +		},
 | 
	
		
			
				|  |  | +		Outbound: []*core.OutboundConnectionConfig{
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				Settings: serial.ToTypedMessage(&freedom.Config{}),
 | 
	
		
			
				|  |  | +			},
 | 
	
		
			
				|  |  | +		},
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	dokodemoPortStart := v2net.Port(50011)
 | 
	
		
			
				|  |  | -	dokodemoPortEnd := v2net.Port(50015)
 | 
	
		
			
				|  |  | +	clientPort := uint32(pickPort())
 | 
	
		
			
				|  |  | +	clientPortRange := uint32(5)
 | 
	
		
			
				|  |  | +	clientConfig := &core.Config{
 | 
	
		
			
				|  |  | +		Inbound: []*core.InboundConnectionConfig{
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				PortRange: &v2net.PortRange{From: clientPort, To: clientPort + clientPortRange},
 | 
	
		
			
				|  |  | +				ListenOn:  v2net.NewIPOrDomain(v2net.LocalHostIP),
 | 
	
		
			
				|  |  | +				Settings: serial.ToTypedMessage(&dokodemo.Config{
 | 
	
		
			
				|  |  | +					Address: v2net.NewIPOrDomain(dest.Address),
 | 
	
		
			
				|  |  | +					Port:    uint32(dest.Port),
 | 
	
		
			
				|  |  | +					NetworkList: &v2net.NetworkList{
 | 
	
		
			
				|  |  | +						Network: []v2net.Network{v2net.Network_TCP},
 | 
	
		
			
				|  |  | +					},
 | 
	
		
			
				|  |  | +				}),
 | 
	
		
			
				|  |  | +			},
 | 
	
		
			
				|  |  | +		},
 | 
	
		
			
				|  |  | +		Outbound: []*core.OutboundConnectionConfig{
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				Settings: serial.ToTypedMessage(&outbound.Config{
 | 
	
		
			
				|  |  | +					Receiver: []*protocol.ServerEndpoint{
 | 
	
		
			
				|  |  | +						{
 | 
	
		
			
				|  |  | +							Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
 | 
	
		
			
				|  |  | +							Port:    uint32(serverPort),
 | 
	
		
			
				|  |  | +							User: []*protocol.User{
 | 
	
		
			
				|  |  | +								{
 | 
	
		
			
				|  |  | +									Account: serial.ToTypedMessage(&vmess.Account{
 | 
	
		
			
				|  |  | +										Id: userID.String(),
 | 
	
		
			
				|  |  | +									}),
 | 
	
		
			
				|  |  | +								},
 | 
	
		
			
				|  |  | +							},
 | 
	
		
			
				|  |  | +						},
 | 
	
		
			
				|  |  | +					},
 | 
	
		
			
				|  |  | +				}),
 | 
	
		
			
				|  |  | +			},
 | 
	
		
			
				|  |  | +		},
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	for port := dokodemoPortStart; port <= dokodemoPortEnd; port++ {
 | 
	
		
			
				|  |  | +	assert.Error(InitializeServerConfig(serverConfig)).IsNil()
 | 
	
		
			
				|  |  | +	assert.Error(InitializeServerConfig(clientConfig)).IsNil()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	for port := clientPort; port <= clientPort+clientPortRange; port++ {
 | 
	
		
			
				|  |  |  		conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
 | 
	
		
			
				|  |  |  			IP:   []byte{127, 0, 0, 1},
 | 
	
		
			
				|  |  |  			Port: int(port),
 | 
	
	
		
			
				|  | @@ -41,13 +104,11 @@ func TestDokodemoTCP(t *testing.T) {
 | 
	
		
			
				|  |  |  		assert.Error(err).IsNil()
 | 
	
		
			
				|  |  |  		assert.Int(nBytes).Equals(len(payload))
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -		conn.CloseWrite()
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  		response := make([]byte, 1024)
 | 
	
		
			
				|  |  |  		nBytes, err = conn.Read(response)
 | 
	
		
			
				|  |  |  		assert.Error(err).IsNil()
 | 
	
		
			
				|  |  | -		assert.String("Processed: " + payload).Equals(string(response[:nBytes]))
 | 
	
		
			
				|  |  | -		conn.Close()
 | 
	
		
			
				|  |  | +		assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload)))
 | 
	
		
			
				|  |  | +		assert.Error(conn.Close()).IsNil()
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	CloseAllServers()
 |