1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-23 02:26:49 -05:00
v2fly/proxy/dokodemo/dokodemo_test.go

169 lines
4.2 KiB
Go
Raw Normal View History

package dokodemo_test
2015-11-01 16:17:49 -05:00
import (
"net"
"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/proxyman"
"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"
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()
space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))
ohm := proxyman.NewDefaultOutboundHandlerManager()
2016-06-04 08:25:13 -04:00
ohm.SetDefaultHandler(
freedom.NewFreedomConnection(
2016-06-14 16:54:08 -04:00
&freedom.Config{},
space,
&proxy.OutboundHandlerMeta{
Address: v2net.LocalHostIP,
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 13:32:37 -04:00
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, ohm)
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)
2016-02-04 07:13:15 -05:00
dokodemo := NewDokodemoDoor(&Config{
2016-10-12 12:43:55 -04:00
Address: &v2net.IPOrDomain{
Address: &v2net.IPOrDomain_Ip{
2016-09-22 10:49:20 -04:00
Ip: v2net.LocalHostIP.IP(),
},
},
Port: uint32(tcpServer.Port),
NetworkList: v2net.Network_TCP.AsList(),
Timeout: 600,
2016-06-14 16:54:08 -04:00
}, space, &proxy.InboundHandlerMeta{
Address: v2net.LocalHostIP,
Port: port,
2016-10-02 17:43:58 -04:00
StreamSettings: &internet.StreamConfig{
Network: v2net.Network_RawTCP,
2016-06-14 16:54:08 -04:00
}})
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()
space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))
ohm := proxyman.NewDefaultOutboundHandlerManager()
2016-06-04 08:25:13 -04:00
ohm.SetDefaultHandler(
freedom.NewFreedomConnection(
2016-06-14 16:54:08 -04:00
&freedom.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 13:32:37 -04:00
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, ohm)
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)
2016-02-04 07:13:15 -05:00
dokodemo := NewDokodemoDoor(&Config{
2016-10-12 12:43:55 -04:00
Address: &v2net.IPOrDomain{
Address: &v2net.IPOrDomain_Ip{
2016-09-22 10:49:20 -04:00
Ip: v2net.LocalHostIP.IP(),
},
},
Port: uint32(udpServer.Port),
NetworkList: v2net.Network_UDP.AsList(),
Timeout: 600,
2016-06-14 16:54:08 -04:00
}, space, &proxy.InboundHandlerMeta{
Address: v2net.LocalHostIP,
Port: port,
2016-10-02 17:43:58 -04:00
StreamSettings: &internet.StreamConfig{
Network: v2net.Network_RawTCP,
2016-06-14 16:54:08 -04:00
}})
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
}