2016-01-15 06:43:06 -05:00
|
|
|
package dokodemo_test
|
2015-11-01 16:17:49 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2016-05-22 13:32:37 -04:00
|
|
|
"github.com/v2ray/v2ray-core/app"
|
|
|
|
"github.com/v2ray/v2ray-core/app/dispatcher"
|
|
|
|
dispatchers "github.com/v2ray/v2ray-core/app/dispatcher/impl"
|
|
|
|
"github.com/v2ray/v2ray-core/app/proxyman"
|
2016-02-04 07:13:15 -05:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2015-11-01 16:17:49 -05:00
|
|
|
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
|
2016-06-04 08:25:13 -04:00
|
|
|
"github.com/v2ray/v2ray-core/proxy"
|
2016-02-04 07:13:15 -05:00
|
|
|
. "github.com/v2ray/v2ray-core/proxy/dokodemo"
|
2016-05-22 13:32:37 -04:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/freedom"
|
2015-12-02 09:27:18 -05:00
|
|
|
"github.com/v2ray/v2ray-core/testing/assert"
|
2016-05-22 13:32:37 -04:00
|
|
|
"github.com/v2ray/v2ray-core/testing/servers/tcp"
|
|
|
|
"github.com/v2ray/v2ray-core/testing/servers/udp"
|
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{
|
|
|
|
Port: v2nettesting.PickPort(),
|
|
|
|
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(
|
|
|
|
&freedom.Config{}, space, &proxy.OutboundHandlerMeta{Address: v2net.LocalHostIP}))
|
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-06-03 18:38:22 -04:00
|
|
|
port := v2nettesting.PickPort()
|
2016-02-04 07:13:15 -05:00
|
|
|
dokodemo := NewDokodemoDoor(&Config{
|
2016-05-22 13:32:37 -04:00
|
|
|
Address: v2net.LocalHostIP,
|
|
|
|
Port: tcpServer.Port,
|
2016-02-04 07:13:15 -05:00
|
|
|
Network: v2net.TCPNetwork.AsList(),
|
|
|
|
Timeout: 600,
|
2016-06-04 08:25:13 -04:00
|
|
|
}, space, &proxy.InboundHandlerMeta{Address: v2net.LocalHostIP, Port: port})
|
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{
|
|
|
|
Port: v2nettesting.PickPort(),
|
|
|
|
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(
|
|
|
|
&freedom.Config{}, space, &proxy.OutboundHandlerMeta{Address: v2net.AnyIP}))
|
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-06-03 18:38:22 -04:00
|
|
|
port := v2nettesting.PickPort()
|
2016-02-04 07:13:15 -05:00
|
|
|
dokodemo := NewDokodemoDoor(&Config{
|
2016-05-22 13:32:37 -04:00
|
|
|
Address: v2net.LocalHostIP,
|
|
|
|
Port: udpServer.Port,
|
2016-02-04 07:13:15 -05:00
|
|
|
Network: v2net.UDPNetwork.AsList(),
|
|
|
|
Timeout: 600,
|
2016-06-04 08:25:13 -04:00
|
|
|
}, space, &proxy.InboundHandlerMeta{Address: v2net.LocalHostIP, Port: port})
|
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
|
|
|
}
|