2016-02-04 05:11:11 -05:00
|
|
|
package freedom_test
|
2015-09-24 12:01:02 -04:00
|
|
|
|
|
|
|
import (
|
2016-12-28 18:56:17 -05:00
|
|
|
"fmt"
|
2015-09-24 12:01:02 -04:00
|
|
|
"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"
|
2016-12-16 17:02:11 -05:00
|
|
|
dnsserver "v2ray.com/core/app/dns/server"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/app/proxyman"
|
2016-12-16 17:02:11 -05:00
|
|
|
"v2ray.com/core/app/proxyman/outbound"
|
2016-08-20 14:55:45 -04:00
|
|
|
"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"
|
2016-12-28 18:56:17 -05:00
|
|
|
_ "v2ray.com/core/transport/internet/tcp"
|
2016-08-20 14:55:45 -04:00
|
|
|
"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
|
|
|
|
},
|
|
|
|
}
|
2016-12-28 18:56:17 -05:00
|
|
|
tcpServerAddr, err := tcpServer.Start()
|
2015-10-04 14:22:52 -04:00
|
|
|
assert.Error(err).IsNil()
|
2015-09-24 12:01:02 -04:00
|
|
|
|
2016-05-22 16:30:21 -04:00
|
|
|
space := app.NewSpace()
|
2016-12-22 07:33:28 -05:00
|
|
|
freedom := New(
|
2016-06-14 16:54:08 -04:00
|
|
|
&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-12-22 07:33:28 -05:00
|
|
|
assert.Error(space.Initialize()).IsNil()
|
2016-05-22 16:30:21 -04:00
|
|
|
|
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-12-28 18:56:17 -05:00
|
|
|
fmt.Println(tcpServerAddr.Network, tcpServerAddr.Address, tcpServerAddr.Port)
|
|
|
|
go freedom.Dispatch(tcpServerAddr, 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()
|
2016-12-16 17:02:11 -05:00
|
|
|
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outbound.New())
|
2016-05-22 16:30:21 -04:00
|
|
|
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)
|
2016-12-16 17:02:11 -05:00
|
|
|
dnsServer := dnsserver.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-12-22 07:33:28 -05:00
|
|
|
freedom := New(
|
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
|
|
|
|
2016-12-22 07:33:28 -05:00
|
|
|
assert.Error(space.Initialize()).IsNil()
|
2016-05-22 16:30:21 -04:00
|
|
|
|
|
|
|
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
|
|
|
}
|