|  | @@ -4,61 +4,37 @@ import (
 | 
											
												
													
														|  |  	"net"
 |  |  	"net"
 | 
											
												
													
														|  |  	"testing"
 |  |  	"testing"
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +	testdispatcher "github.com/v2ray/v2ray-core/app/dispatcher/testing"
 | 
											
												
													
														|  | 
 |  | +	v2net "github.com/v2ray/v2ray-core/common/net"
 | 
											
												
													
														|  |  	v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
 |  |  	v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
 | 
											
												
													
														|  | -	_ "github.com/v2ray/v2ray-core/proxy/freedom"
 |  | 
 | 
											
												
													
														|  | -	"github.com/v2ray/v2ray-core/shell/point"
 |  | 
 | 
											
												
													
														|  | 
 |  | +	netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
 | 
											
												
													
														|  | 
 |  | +	. "github.com/v2ray/v2ray-core/proxy/dokodemo"
 | 
											
												
													
														|  |  	v2testing "github.com/v2ray/v2ray-core/testing"
 |  |  	v2testing "github.com/v2ray/v2ray-core/testing"
 | 
											
												
													
														|  |  	"github.com/v2ray/v2ray-core/testing/assert"
 |  |  	"github.com/v2ray/v2ray-core/testing/assert"
 | 
											
												
													
														|  | -	"github.com/v2ray/v2ray-core/testing/servers/tcp"
 |  | 
 | 
											
												
													
														|  | -	"github.com/v2ray/v2ray-core/testing/servers/udp"
 |  | 
 | 
											
												
													
														|  |  )
 |  |  )
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  func TestDokodemoTCP(t *testing.T) {
 |  |  func TestDokodemoTCP(t *testing.T) {
 | 
											
												
													
														|  |  	v2testing.Current(t)
 |  |  	v2testing.Current(t)
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -	port := v2nettesting.PickPort()
 |  | 
 | 
											
												
													
														|  | 
 |  | +	testPacketDispatcher := &testdispatcher.TestPacketDispatcher{}
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  	data2Send := "Data to be sent to remote."
 |  |  	data2Send := "Data to be sent to remote."
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -	tcpServer := &tcp.Server{
 |  | 
 | 
											
												
													
														|  | -		Port: port,
 |  | 
 | 
											
												
													
														|  | -		MsgProcessor: func(data []byte) []byte {
 |  | 
 | 
											
												
													
														|  | -			buffer := make([]byte, 0, 2048)
 |  | 
 | 
											
												
													
														|  | -			buffer = append(buffer, []byte("Processed: ")...)
 |  | 
 | 
											
												
													
														|  | -			buffer = append(buffer, data...)
 |  | 
 | 
											
												
													
														|  | -			return buffer
 |  | 
 | 
											
												
													
														|  | -		},
 |  | 
 | 
											
												
													
														|  | -	}
 |  | 
 | 
											
												
													
														|  | -	_, err := tcpServer.Start()
 |  | 
 | 
											
												
													
														|  | -	assert.Error(err).IsNil()
 |  | 
 | 
											
												
													
														|  | 
 |  | +	dokodemo := NewDokodemoDoor(&Config{
 | 
											
												
													
														|  | 
 |  | +		Address: v2net.IPAddress([]byte{1, 2, 3, 4}),
 | 
											
												
													
														|  | 
 |  | +		Port:    128,
 | 
											
												
													
														|  | 
 |  | +		Network: v2net.TCPNetwork.AsList(),
 | 
											
												
													
														|  | 
 |  | +		Timeout: 600,
 | 
											
												
													
														|  | 
 |  | +	}, testPacketDispatcher)
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -	pointPort := v2nettesting.PickPort()
 |  | 
 | 
											
												
													
														|  | -	config := &point.Config{
 |  | 
 | 
											
												
													
														|  | -		Port: pointPort,
 |  | 
 | 
											
												
													
														|  | -		InboundConfig: &point.ConnectionConfig{
 |  | 
 | 
											
												
													
														|  | -			Protocol: "dokodemo-door",
 |  | 
 | 
											
												
													
														|  | -			Settings: []byte(`{
 |  | 
 | 
											
												
													
														|  | -        "address": "127.0.0.1",
 |  | 
 | 
											
												
													
														|  | -        "port": ` + port.String() + `,
 |  | 
 | 
											
												
													
														|  | -        "network": "tcp",
 |  | 
 | 
											
												
													
														|  | -        "timeout": 0
 |  | 
 | 
											
												
													
														|  | -      }`),
 |  | 
 | 
											
												
													
														|  | -		},
 |  | 
 | 
											
												
													
														|  | -		OutboundConfig: &point.ConnectionConfig{
 |  | 
 | 
											
												
													
														|  | -			Protocol: "freedom",
 |  | 
 | 
											
												
													
														|  | -			Settings: nil,
 |  | 
 | 
											
												
													
														|  | -		},
 |  | 
 | 
											
												
													
														|  | -	}
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -	point, err := point.NewPoint(config)
 |  | 
 | 
											
												
													
														|  | -	assert.Error(err).IsNil()
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -	err = point.Start()
 |  | 
 | 
											
												
													
														|  | 
 |  | +	port := v2nettesting.PickPort()
 | 
											
												
													
														|  | 
 |  | +	err := dokodemo.Listen(port)
 | 
											
												
													
														|  |  	assert.Error(err).IsNil()
 |  |  	assert.Error(err).IsNil()
 | 
											
												
													
														|  | 
 |  | +	netassert.Port(port).Equals(dokodemo.Port())
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  	tcpClient, err := net.DialTCP("tcp", nil, &net.TCPAddr{
 |  |  	tcpClient, err := net.DialTCP("tcp", nil, &net.TCPAddr{
 | 
											
												
													
														|  |  		IP:   []byte{127, 0, 0, 1},
 |  |  		IP:   []byte{127, 0, 0, 1},
 | 
											
												
													
														|  | -		Port: int(pointPort),
 |  | 
 | 
											
												
													
														|  | 
 |  | +		Port: int(port),
 | 
											
												
													
														|  |  		Zone: "",
 |  |  		Zone: "",
 | 
											
												
													
														|  |  	})
 |  |  	})
 | 
											
												
													
														|  |  	assert.Error(err).IsNil()
 |  |  	assert.Error(err).IsNil()
 | 
											
										
											
												
													
														|  | @@ -72,54 +48,33 @@ func TestDokodemoTCP(t *testing.T) {
 | 
											
												
													
														|  |  	tcpClient.Close()
 |  |  	tcpClient.Close()
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  	assert.StringLiteral("Processed: " + data2Send).Equals(string(response[:nBytes]))
 |  |  	assert.StringLiteral("Processed: " + data2Send).Equals(string(response[:nBytes]))
 | 
											
												
													
														|  | 
 |  | +	assert.Bool(testPacketDispatcher.LastPacket.Destination().IsTCP()).IsTrue()
 | 
											
												
													
														|  | 
 |  | +	netassert.Address(testPacketDispatcher.LastPacket.Destination().Address()).Equals(v2net.IPAddress([]byte{1, 2, 3, 4}))
 | 
											
												
													
														|  | 
 |  | +	netassert.Port(testPacketDispatcher.LastPacket.Destination().Port()).Equals(128)
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  func TestDokodemoUDP(t *testing.T) {
 |  |  func TestDokodemoUDP(t *testing.T) {
 | 
											
												
													
														|  |  	v2testing.Current(t)
 |  |  	v2testing.Current(t)
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -	port := v2nettesting.PickPort()
 |  | 
 | 
											
												
													
														|  | 
 |  | +	testPacketDispatcher := &testdispatcher.TestPacketDispatcher{}
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  	data2Send := "Data to be sent to remote."
 |  |  	data2Send := "Data to be sent to remote."
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -	udpServer := &udp.Server{
 |  | 
 | 
											
												
													
														|  | -		Port: port,
 |  | 
 | 
											
												
													
														|  | -		MsgProcessor: func(data []byte) []byte {
 |  | 
 | 
											
												
													
														|  | -			buffer := make([]byte, 0, 2048)
 |  | 
 | 
											
												
													
														|  | -			buffer = append(buffer, []byte("Processed: ")...)
 |  | 
 | 
											
												
													
														|  | -			buffer = append(buffer, data...)
 |  | 
 | 
											
												
													
														|  | -			return buffer
 |  | 
 | 
											
												
													
														|  | -		},
 |  | 
 | 
											
												
													
														|  | -	}
 |  | 
 | 
											
												
													
														|  | -	_, err := udpServer.Start()
 |  | 
 | 
											
												
													
														|  | -	assert.Error(err).IsNil()
 |  | 
 | 
											
												
													
														|  | 
 |  | +	dokodemo := NewDokodemoDoor(&Config{
 | 
											
												
													
														|  | 
 |  | +		Address: v2net.IPAddress([]byte{5, 6, 7, 8}),
 | 
											
												
													
														|  | 
 |  | +		Port:    256,
 | 
											
												
													
														|  | 
 |  | +		Network: v2net.UDPNetwork.AsList(),
 | 
											
												
													
														|  | 
 |  | +		Timeout: 600,
 | 
											
												
													
														|  | 
 |  | +	}, testPacketDispatcher)
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -	pointPort := v2nettesting.PickPort()
 |  | 
 | 
											
												
													
														|  | -	config := &point.Config{
 |  | 
 | 
											
												
													
														|  | -		Port: pointPort,
 |  | 
 | 
											
												
													
														|  | -		InboundConfig: &point.ConnectionConfig{
 |  | 
 | 
											
												
													
														|  | -			Protocol: "dokodemo-door",
 |  | 
 | 
											
												
													
														|  | -			Settings: []byte(`{
 |  | 
 | 
											
												
													
														|  | -        "address": "127.0.0.1",
 |  | 
 | 
											
												
													
														|  | -        "port": ` + port.String() + `,
 |  | 
 | 
											
												
													
														|  | -        "network": "udp",
 |  | 
 | 
											
												
													
														|  | -        "timeout": 0
 |  | 
 | 
											
												
													
														|  | -      }`),
 |  | 
 | 
											
												
													
														|  | -		},
 |  | 
 | 
											
												
													
														|  | -		OutboundConfig: &point.ConnectionConfig{
 |  | 
 | 
											
												
													
														|  | -			Protocol: "freedom",
 |  | 
 | 
											
												
													
														|  | -			Settings: nil,
 |  | 
 | 
											
												
													
														|  | -		},
 |  | 
 | 
											
												
													
														|  | -	}
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -	point, err := point.NewPoint(config)
 |  | 
 | 
											
												
													
														|  | -	assert.Error(err).IsNil()
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -	err = point.Start()
 |  | 
 | 
											
												
													
														|  | 
 |  | +	port := v2nettesting.PickPort()
 | 
											
												
													
														|  | 
 |  | +	err := dokodemo.Listen(port)
 | 
											
												
													
														|  |  	assert.Error(err).IsNil()
 |  |  	assert.Error(err).IsNil()
 | 
											
												
													
														|  | 
 |  | +	netassert.Port(port).Equals(dokodemo.Port())
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  	udpClient, err := net.DialUDP("udp", nil, &net.UDPAddr{
 |  |  	udpClient, err := net.DialUDP("udp", nil, &net.UDPAddr{
 | 
											
												
													
														|  |  		IP:   []byte{127, 0, 0, 1},
 |  |  		IP:   []byte{127, 0, 0, 1},
 | 
											
												
													
														|  | -		Port: int(pointPort),
 |  | 
 | 
											
												
													
														|  | 
 |  | +		Port: int(port),
 | 
											
												
													
														|  |  		Zone: "",
 |  |  		Zone: "",
 | 
											
												
													
														|  |  	})
 |  |  	})
 | 
											
												
													
														|  |  	assert.Error(err).IsNil()
 |  |  	assert.Error(err).IsNil()
 | 
											
										
											
												
													
														|  | @@ -132,4 +87,7 @@ func TestDokodemoUDP(t *testing.T) {
 | 
											
												
													
														|  |  	udpClient.Close()
 |  |  	udpClient.Close()
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  	assert.StringLiteral("Processed: " + data2Send).Equals(string(response[:nBytes]))
 |  |  	assert.StringLiteral("Processed: " + data2Send).Equals(string(response[:nBytes]))
 | 
											
												
													
														|  | 
 |  | +	assert.Bool(testPacketDispatcher.LastPacket.Destination().IsUDP()).IsTrue()
 | 
											
												
													
														|  | 
 |  | +	netassert.Address(testPacketDispatcher.LastPacket.Destination().Address()).Equals(v2net.IPAddress([]byte{5, 6, 7, 8}))
 | 
											
												
													
														|  | 
 |  | +	netassert.Port(testPacketDispatcher.LastPacket.Destination().Port()).Equals(256)
 | 
											
												
													
														|  |  }
 |  |  }
 |