|  | @@ -1,11 +1,20 @@
 | 
											
												
													
														|  |  package freedom_test
 |  |  package freedom_test
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  import (
 |  |  import (
 | 
											
												
													
														|  | 
 |  | +	"net"
 | 
											
												
													
														|  |  	"testing"
 |  |  	"testing"
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +	"github.com/v2ray/v2ray-core/app"
 | 
											
												
													
														|  | 
 |  | +	"github.com/v2ray/v2ray-core/app/dispatcher"
 | 
											
												
													
														|  | 
 |  | +	dispatchers "github.com/v2ray/v2ray-core/app/dispatcher/impl"
 | 
											
												
													
														|  | 
 |  | +	"github.com/v2ray/v2ray-core/app/dns"
 | 
											
												
													
														|  | 
 |  | +	"github.com/v2ray/v2ray-core/app/proxyman"
 | 
											
												
													
														|  | 
 |  | +	"github.com/v2ray/v2ray-core/app/router"
 | 
											
												
													
														|  | 
 |  | +	"github.com/v2ray/v2ray-core/app/router/rules"
 | 
											
												
													
														|  |  	"github.com/v2ray/v2ray-core/common/alloc"
 |  |  	"github.com/v2ray/v2ray-core/common/alloc"
 | 
											
												
													
														|  |  	v2net "github.com/v2ray/v2ray-core/common/net"
 |  |  	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"
 | 
											
												
													
														|  | 
 |  | +	netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
 | 
											
												
													
														|  |  	. "github.com/v2ray/v2ray-core/proxy/freedom"
 |  |  	. "github.com/v2ray/v2ray-core/proxy/freedom"
 | 
											
												
													
														|  |  	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"
 | 
											
										
											
												
													
														|  | @@ -29,7 +38,10 @@ func TestSinglePacket(t *testing.T) {
 | 
											
												
													
														|  |  	_, err := tcpServer.Start()
 |  |  	_, err := tcpServer.Start()
 | 
											
												
													
														|  |  	assert.Error(err).IsNil()
 |  |  	assert.Error(err).IsNil()
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -	freedom := &FreedomConnection{}
 |  | 
 | 
											
												
													
														|  | 
 |  | +	space := app.NewSpace()
 | 
											
												
													
														|  | 
 |  | +	freedom := NewFreedomConnection(&Config{}, space)
 | 
											
												
													
														|  | 
 |  | +	space.Initialize()
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  |  	traffic := ray.NewRay()
 |  |  	traffic := ray.NewRay()
 | 
											
												
													
														|  |  	data2Send := "Data to be sent to remote"
 |  |  	data2Send := "Data to be sent to remote"
 | 
											
												
													
														|  |  	payload := alloc.NewSmallBuffer().Clear().Append([]byte(data2Send))
 |  |  	payload := alloc.NewSmallBuffer().Clear().Append([]byte(data2Send))
 | 
											
										
											
												
													
														|  | @@ -47,7 +59,7 @@ func TestSinglePacket(t *testing.T) {
 | 
											
												
													
														|  |  func TestUnreachableDestination(t *testing.T) {
 |  |  func TestUnreachableDestination(t *testing.T) {
 | 
											
												
													
														|  |  	v2testing.Current(t)
 |  |  	v2testing.Current(t)
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -	freedom := &FreedomConnection{}
 |  | 
 | 
											
												
													
														|  | 
 |  | +	freedom := NewFreedomConnection(&Config{}, app.NewSpace())
 | 
											
												
													
														|  |  	traffic := ray.NewRay()
 |  |  	traffic := ray.NewRay()
 | 
											
												
													
														|  |  	data2Send := "Data to be sent to remote"
 |  |  	data2Send := "Data to be sent to remote"
 | 
											
												
													
														|  |  	payload := alloc.NewSmallBuffer().Clear().Append([]byte(data2Send))
 |  |  	payload := alloc.NewSmallBuffer().Clear().Append([]byte(data2Send))
 | 
											
										
											
												
													
														|  | @@ -55,3 +67,27 @@ func TestUnreachableDestination(t *testing.T) {
 | 
											
												
													
														|  |  	err := freedom.Dispatch(v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 128), payload, traffic)
 |  |  	err := freedom.Dispatch(v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 128), payload, traffic)
 | 
											
												
													
														|  |  	assert.Error(err).IsNotNil()
 |  |  	assert.Error(err).IsNotNil()
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +func TestIPResolution(t *testing.T) {
 | 
											
												
													
														|  | 
 |  | +	v2testing.Current(t)
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +	space := app.NewSpace()
 | 
											
												
													
														|  | 
 |  | +	space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, proxyman.NewDefaultOutboundHandlerManager())
 | 
											
												
													
														|  | 
 |  | +	space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))
 | 
											
												
													
														|  | 
 |  | +	r, _ := router.CreateRouter("rules", &rules.RouterRuleConfig{}, space)
 | 
											
												
													
														|  | 
 |  | +	space.BindApp(router.APP_ID, r)
 | 
											
												
													
														|  | 
 |  | +	dnsServer := dns.NewCacheServer(space, &dns.Config{
 | 
											
												
													
														|  | 
 |  | +		Hosts: map[string]net.IP{
 | 
											
												
													
														|  | 
 |  | +			"v2ray.com": net.IP([]byte{127, 0, 0, 1}),
 | 
											
												
													
														|  | 
 |  | +		},
 | 
											
												
													
														|  | 
 |  | +	})
 | 
											
												
													
														|  | 
 |  | +	space.BindApp(dns.APP_ID, dnsServer)
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +	freedom := NewFreedomConnection(&Config{DomainStrategy: DomainStrategyUseIP}, space)
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +	space.Initialize()
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +	ipDest := freedom.ResolveIP(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), v2net.Port(80)))
 | 
											
												
													
														|  | 
 |  | +	netassert.Destination(ipDest).IsTCP()
 | 
											
												
													
														|  | 
 |  | +	netassert.Address(ipDest.Address()).Equals(v2net.LocalHostIP)
 | 
											
												
													
														|  | 
 |  | +}
 |