2016-01-15 06:43:06 -05:00
|
|
|
package dokodemo_test
|
2015-11-01 16:17:49 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2017-01-12 18:56:21 -05:00
|
|
|
"context"
|
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/app"
|
|
|
|
"v2ray.com/core/app/dispatcher"
|
2017-01-06 09:32:36 -05:00
|
|
|
_ "v2ray.com/core/app/dispatcher/impl"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/app/proxyman"
|
2017-01-06 09:32:36 -05:00
|
|
|
_ "v2ray.com/core/app/proxyman/outbound"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/dice"
|
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/proxy"
|
|
|
|
. "v2ray.com/core/proxy/dokodemo"
|
|
|
|
"v2ray.com/core/proxy/freedom"
|
|
|
|
"v2ray.com/core/testing/assert"
|
|
|
|
"v2ray.com/core/testing/servers/tcp"
|
|
|
|
"v2ray.com/core/testing/servers/udp"
|
|
|
|
"v2ray.com/core/transport/internet"
|
2016-12-28 18:56:17 -05:00
|
|
|
_ "v2ray.com/core/transport/internet/tcp"
|
2015-11-01 16:17:49 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDokodemoTCP(t *testing.T) {
|
2016-05-24 15:55:46 -04:00
|
|
|
assert := assert.On(t)
|
2015-11-01 16:17:49 -05:00
|
|
|
|
2016-05-22 13:32:37 -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()
|
|
|
|
|
|
|
|
defer tcpServer.Close()
|
|
|
|
|
|
|
|
space := app.NewSpace()
|
2017-01-13 07:41:40 -05:00
|
|
|
ctx := app.ContextWithSpace(context.Background(), space)
|
|
|
|
app.AddApplicationToSpace(ctx, new(dispatcher.Config))
|
|
|
|
app.AddApplicationToSpace(ctx, new(proxyman.OutboundConfig))
|
2017-01-06 09:32:36 -05:00
|
|
|
|
|
|
|
ohm := proxyman.OutboundHandlerManagerFromSpace(space)
|
2017-01-12 18:56:21 -05:00
|
|
|
freedom, err := freedom.New(proxy.ContextWithOutboundMeta(ctx, &proxy.OutboundHandlerMeta{
|
|
|
|
Address: v2net.LocalHostIP,
|
|
|
|
StreamSettings: &internet.StreamConfig{
|
|
|
|
Protocol: internet.TransportProtocol_TCP,
|
|
|
|
},
|
|
|
|
}), &freedom.Config{})
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
ohm.SetDefaultHandler(freedom)
|
2015-11-01 16:17:49 -05:00
|
|
|
|
|
|
|
data2Send := "Data to be sent to remote."
|
|
|
|
|
2016-08-15 16:12:11 -04:00
|
|
|
port := v2net.Port(dice.Roll(20000) + 10000)
|
2017-01-12 18:56:21 -05:00
|
|
|
|
|
|
|
ctx = proxy.ContextWithInboundMeta(ctx, &proxy.InboundHandlerMeta{
|
2016-06-14 16:54:08 -04:00
|
|
|
Address: v2net.LocalHostIP,
|
|
|
|
Port: port,
|
2016-10-02 17:43:58 -04:00
|
|
|
StreamSettings: &internet.StreamConfig{
|
2017-01-12 06:54:34 -05:00
|
|
|
Protocol: internet.TransportProtocol_TCP,
|
2016-06-14 16:54:08 -04:00
|
|
|
}})
|
2017-01-12 18:56:21 -05:00
|
|
|
|
2017-01-13 17:38:04 -05:00
|
|
|
dokodemo, err := New(ctx, &Config{
|
2017-01-12 18:56:21 -05:00
|
|
|
Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
|
|
|
|
Port: uint32(tcpServer.Port),
|
|
|
|
NetworkList: v2net.Network_TCP.AsList(),
|
|
|
|
Timeout: 600,
|
|
|
|
})
|
|
|
|
assert.Error(err).IsNil()
|
2016-02-04 12:03:15 -05:00
|
|
|
defer dokodemo.Close()
|
2015-11-01 16:17:49 -05:00
|
|
|
|
2016-05-22 13:32:37 -04:00
|
|
|
assert.Error(space.Initialize()).IsNil()
|
|
|
|
|
2016-06-03 18:38:22 -04:00
|
|
|
err = dokodemo.Start()
|
2015-11-01 16:17:49 -05:00
|
|
|
assert.Error(err).IsNil()
|
2016-05-24 09:29:08 -04:00
|
|
|
assert.Port(port).Equals(dokodemo.Port())
|
2015-11-01 16:17:49 -05:00
|
|
|
|
|
|
|
tcpClient, err := net.DialTCP("tcp", nil, &net.TCPAddr{
|
|
|
|
IP: []byte{127, 0, 0, 1},
|
2016-02-04 07:13:15 -05:00
|
|
|
Port: int(port),
|
2015-11-01 16:17:49 -05:00
|
|
|
Zone: "",
|
|
|
|
})
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
tcpClient.Write([]byte(data2Send))
|
|
|
|
tcpClient.CloseWrite()
|
|
|
|
|
|
|
|
response := make([]byte, 1024)
|
|
|
|
nBytes, err := tcpClient.Read(response)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
tcpClient.Close()
|
|
|
|
|
2016-05-24 15:55:46 -04:00
|
|
|
assert.String("Processed: " + data2Send).Equals(string(response[:nBytes]))
|
2015-11-01 16:17:49 -05:00
|
|
|
}
|
2015-11-10 18:08:43 -05:00
|
|
|
|
|
|
|
func TestDokodemoUDP(t *testing.T) {
|
2016-05-24 15:55:46 -04:00
|
|
|
assert := assert.On(t)
|
2015-11-10 18:08:43 -05:00
|
|
|
|
2016-05-22 13:32:37 -04:00
|
|
|
udpServer := &udp.Server{
|
|
|
|
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()
|
|
|
|
|
|
|
|
defer udpServer.Close()
|
|
|
|
|
|
|
|
space := app.NewSpace()
|
2017-01-13 07:41:40 -05:00
|
|
|
ctx := app.ContextWithSpace(context.Background(), space)
|
|
|
|
app.AddApplicationToSpace(ctx, new(dispatcher.Config))
|
|
|
|
app.AddApplicationToSpace(ctx, new(proxyman.OutboundConfig))
|
2017-01-06 09:32:36 -05:00
|
|
|
|
|
|
|
ohm := proxyman.OutboundHandlerManagerFromSpace(space)
|
2017-01-12 18:56:21 -05:00
|
|
|
freedom, err := freedom.New(proxy.ContextWithOutboundMeta(ctx, &proxy.OutboundHandlerMeta{
|
|
|
|
Address: v2net.AnyIP,
|
|
|
|
StreamSettings: &internet.StreamConfig{
|
|
|
|
Protocol: internet.TransportProtocol_TCP,
|
|
|
|
},
|
|
|
|
}), &freedom.Config{})
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
ohm.SetDefaultHandler(freedom)
|
2015-11-10 18:08:43 -05:00
|
|
|
|
|
|
|
data2Send := "Data to be sent to remote."
|
|
|
|
|
2016-08-15 16:12:11 -04:00
|
|
|
port := v2net.Port(dice.Roll(20000) + 10000)
|
2017-01-12 18:56:21 -05:00
|
|
|
|
|
|
|
ctx = proxy.ContextWithInboundMeta(ctx, &proxy.InboundHandlerMeta{
|
2016-06-14 16:54:08 -04:00
|
|
|
Address: v2net.LocalHostIP,
|
|
|
|
Port: port,
|
2016-10-02 17:43:58 -04:00
|
|
|
StreamSettings: &internet.StreamConfig{
|
2017-01-12 06:54:34 -05:00
|
|
|
Protocol: internet.TransportProtocol_TCP,
|
2016-06-14 16:54:08 -04:00
|
|
|
}})
|
2017-01-12 18:56:21 -05:00
|
|
|
|
2017-01-13 17:38:04 -05:00
|
|
|
dokodemo, err := New(ctx, &Config{
|
2017-01-12 18:56:21 -05:00
|
|
|
Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
|
|
|
|
Port: uint32(udpServer.Port),
|
|
|
|
NetworkList: v2net.Network_UDP.AsList(),
|
|
|
|
Timeout: 600,
|
|
|
|
})
|
|
|
|
assert.Error(err).IsNil()
|
2016-02-04 12:03:15 -05:00
|
|
|
defer dokodemo.Close()
|
2015-11-10 18:08:43 -05:00
|
|
|
|
2016-05-22 13:32:37 -04:00
|
|
|
assert.Error(space.Initialize()).IsNil()
|
|
|
|
|
2016-06-03 18:38:22 -04:00
|
|
|
err = dokodemo.Start()
|
2015-11-10 18:08:43 -05:00
|
|
|
assert.Error(err).IsNil()
|
2016-05-24 09:29:08 -04:00
|
|
|
assert.Port(port).Equals(dokodemo.Port())
|
2015-11-10 18:08:43 -05:00
|
|
|
|
|
|
|
udpClient, err := net.DialUDP("udp", nil, &net.UDPAddr{
|
|
|
|
IP: []byte{127, 0, 0, 1},
|
2016-02-04 07:13:15 -05:00
|
|
|
Port: int(port),
|
2015-11-10 18:08:43 -05:00
|
|
|
Zone: "",
|
|
|
|
})
|
|
|
|
assert.Error(err).IsNil()
|
2016-05-22 13:32:37 -04:00
|
|
|
defer udpClient.Close()
|
2015-11-10 18:08:43 -05:00
|
|
|
|
|
|
|
udpClient.Write([]byte(data2Send))
|
|
|
|
|
2016-05-22 13:32:37 -04:00
|
|
|
response := make([]byte, 1024)
|
|
|
|
nBytes, addr, err := udpClient.ReadFromUDP(response)
|
|
|
|
assert.Error(err).IsNil()
|
2016-05-24 09:29:08 -04:00
|
|
|
assert.IP(addr.IP).Equals(v2net.LocalHostIP.IP())
|
2016-05-22 13:32:37 -04:00
|
|
|
assert.Bytes(response[:nBytes]).Equals([]byte("Processed: " + data2Send))
|
2015-11-10 18:08:43 -05:00
|
|
|
}
|