1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00
v2fly/proxy/freedom/freedom_test.go

94 lines
2.5 KiB
Go
Raw Normal View History

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