v2ray-core/proxy/freedom/freedom_test.go

96 lines
2.5 KiB
Go
Raw Normal View History

2016-02-04 10:11:11 +00:00
package freedom_test
2015-09-24 16:01:02 +00:00
import (
"testing"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
dispatchers "v2ray.com/core/app/dispatcher/impl"
"v2ray.com/core/app/dns"
2016-12-16 22:02:11 +00:00
dnsserver "v2ray.com/core/app/dns/server"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/app/proxyman"
2016-12-16 22:02:11 +00:00
"v2ray.com/core/app/proxyman/outbound"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/app/router"
2016-12-09 10:35:27 +00:00
"v2ray.com/core/common/buf"
2016-08-20 18:55:45 +00:00
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
. "v2ray.com/core/proxy/freedom"
"v2ray.com/core/testing/assert"
"v2ray.com/core/testing/servers/tcp"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/ray"
2015-09-24 16:01:02 +00:00
)
2016-02-04 10:11:11 +00:00
func TestSinglePacket(t *testing.T) {
2016-05-24 19:55:46 +00:00
assert := assert.On(t)
2015-10-04 18:22:52 +00:00
tcpServer := &tcp.Server{
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()
2015-09-24 16:01:02 +00:00
2016-05-22 20:30:21 +00:00
space := app.NewSpace()
2016-06-14 20:54:08 +00:00
freedom := NewFreedomConnection(
&Config{},
space,
&proxy.OutboundHandlerMeta{
Address: v2net.AnyIP,
2016-10-02 21:43:58 +00:00
StreamSettings: &internet.StreamConfig{
Network: v2net.Network_RawTCP,
2016-06-14 20:54:08 +00:00
},
})
2016-05-22 20:30:21 +00:00
space.Initialize()
2016-02-04 10:11:11 +00:00
traffic := ray.NewRay()
data2Send := "Data to be sent to remote"
2016-12-09 11:08:25 +00:00
payload := buf.NewLocal(2048)
2016-12-06 10:03:42 +00:00
payload.Append([]byte(data2Send))
2015-09-24 16:01:02 +00:00
2016-08-15 21:02:03 +00:00
go freedom.Dispatch(v2net.TCPDestination(v2net.LocalHostIP, tcpServer.Port), payload, traffic)
2016-04-18 16:44:10 +00:00
traffic.InboundInput().Close()
2015-09-24 16:01:02 +00:00
2016-04-18 16:44:10 +00:00
respPayload, err := traffic.InboundOutput().Read()
assert.Error(err).IsNil()
2016-12-05 14:19:14 +00:00
assert.String(respPayload.String()).Equals("Processed: Data to be sent to remote")
2015-09-24 16:01:02 +00:00
2016-02-04 10:11:11 +00:00
tcpServer.Close()
}
2015-09-24 16:01:02 +00:00
2016-05-22 20:30:21 +00:00
func TestIPResolution(t *testing.T) {
2016-05-24 19:55:46 +00:00
assert := assert.On(t)
2016-05-22 20:30:21 +00:00
space := app.NewSpace()
2016-12-16 22:02:11 +00:00
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outbound.New())
2016-05-22 20:30:21 +00:00
space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))
2016-10-12 14:11:13 +00:00
r := router.NewRouter(&router.Config{}, space)
2016-05-22 20:30:21 +00:00
space.BindApp(router.APP_ID, r)
2016-12-16 22:02:11 +00:00
dnsServer := dnsserver.NewCacheServer(space, &dns.Config{
2016-10-12 16:43:55 +00:00
Hosts: map[string]*v2net.IPOrDomain{
2016-12-14 22:04:02 +00:00
"v2ray.com": v2net.NewIPOrDomain(v2net.LocalHostIP),
2016-05-22 20:30:21 +00:00
},
})
space.BindApp(dns.APP_ID, dnsServer)
2016-06-04 12:25:13 +00:00
freedom := NewFreedomConnection(
2016-08-25 19:55:49 +00:00
&Config{DomainStrategy: Config_USE_IP},
2016-06-04 12:25:13 +00:00
space,
2016-06-14 20:54:08 +00:00
&proxy.OutboundHandlerMeta{
Address: v2net.AnyIP,
2016-10-02 21:43:58 +00:00
StreamSettings: &internet.StreamConfig{
Network: v2net.Network_RawTCP,
2016-06-14 20:54:08 +00:00
},
})
2016-05-22 20:30:21 +00:00
space.Initialize()
ipDest := freedom.ResolveIP(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), v2net.Port(80)))
2016-05-24 13:29:08 +00:00
assert.Destination(ipDest).IsTCP()
2016-09-20 09:53:05 +00:00
assert.Address(ipDest.Address).Equals(v2net.LocalHostIP)
2016-05-22 20:30:21 +00:00
}