2015-09-24 12:01:02 -04:00
|
|
|
package freedom
|
|
|
|
|
|
|
|
import (
|
2015-10-04 14:22:52 -04:00
|
|
|
"bytes"
|
2015-09-24 12:01:02 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"golang.org/x/net/proxy"
|
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core"
|
2015-10-08 08:46:18 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
2015-10-04 14:22:52 -04:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2015-09-24 12:01:02 -04:00
|
|
|
_ "github.com/v2ray/v2ray-core/proxy/socks"
|
2015-10-06 17:11:08 -04:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/socks/config/json"
|
2015-09-24 12:01:02 -04:00
|
|
|
"github.com/v2ray/v2ray-core/testing/mocks"
|
2015-10-04 14:22:52 -04:00
|
|
|
"github.com/v2ray/v2ray-core/testing/servers/tcp"
|
|
|
|
"github.com/v2ray/v2ray-core/testing/servers/udp"
|
2015-09-24 12:01:02 -04:00
|
|
|
"github.com/v2ray/v2ray-core/testing/unit"
|
|
|
|
)
|
|
|
|
|
2015-10-04 14:22:52 -04:00
|
|
|
func TestUDPSend(t *testing.T) {
|
2015-09-24 12:01:02 -04:00
|
|
|
assert := unit.Assert(t)
|
|
|
|
|
|
|
|
data2Send := "Data to be sent to remote"
|
|
|
|
|
2015-10-04 14:22:52 -04:00
|
|
|
udpServer := &udp.Server{
|
|
|
|
Port: 0,
|
|
|
|
MsgProcessor: func(data []byte) []byte {
|
|
|
|
buffer := make([]byte, 0, 2048)
|
|
|
|
buffer = append(buffer, []byte("Processed: ")...)
|
|
|
|
buffer = append(buffer, data...)
|
|
|
|
return buffer
|
|
|
|
},
|
|
|
|
}
|
2015-09-24 12:08:55 -04:00
|
|
|
|
2015-10-04 14:22:52 -04:00
|
|
|
udpServerAddr, err := udpServer.Start()
|
|
|
|
assert.Error(err).IsNil()
|
2015-09-24 12:08:55 -04:00
|
|
|
|
2015-10-04 14:22:52 -04:00
|
|
|
ich := &mocks.InboundConnectionHandler{
|
|
|
|
Data2Send: []byte("Not Used"),
|
|
|
|
DataReturned: bytes.NewBuffer(make([]byte, 0, 1024)),
|
|
|
|
}
|
2015-09-24 12:01:02 -04:00
|
|
|
|
2015-10-04 14:22:52 -04:00
|
|
|
core.RegisterInboundConnectionHandlerFactory("mock_ich", ich)
|
2015-09-24 12:01:02 -04:00
|
|
|
|
2015-10-04 14:22:52 -04:00
|
|
|
pointPort := uint16(38724)
|
|
|
|
config := mocks.Config{
|
|
|
|
PortValue: pointPort,
|
|
|
|
InboundConfigValue: &mocks.ConnectionConfig{
|
|
|
|
ProtocolValue: "mock_ich",
|
2015-10-06 17:11:08 -04:00
|
|
|
SettingsValue: nil,
|
2015-10-04 14:22:52 -04:00
|
|
|
},
|
|
|
|
OutboundConfigValue: &mocks.ConnectionConfig{
|
|
|
|
ProtocolValue: "freedom",
|
2015-10-06 17:11:08 -04:00
|
|
|
SettingsValue: nil,
|
2015-10-04 14:22:52 -04:00
|
|
|
},
|
|
|
|
}
|
2015-09-24 12:01:02 -04:00
|
|
|
|
2015-10-04 14:22:52 -04:00
|
|
|
point, err := core.NewPoint(&config)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
err = point.Start()
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
2015-10-08 08:46:18 -04:00
|
|
|
data2SendBuffer := alloc.NewBuffer()
|
|
|
|
data2SendBuffer.Clear()
|
|
|
|
data2SendBuffer.Append([]byte(data2Send))
|
2015-10-04 14:22:52 -04:00
|
|
|
dest := v2net.NewUDPDestination(udpServerAddr)
|
2015-10-08 08:46:18 -04:00
|
|
|
ich.Communicate(v2net.NewPacket(dest, data2SendBuffer, false))
|
2015-10-04 14:22:52 -04:00
|
|
|
assert.Bytes(ich.DataReturned.Bytes()).Equals([]byte("Processed: Data to be sent to remote"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSocksTcpConnect(t *testing.T) {
|
|
|
|
assert := unit.Assert(t)
|
|
|
|
port := uint16(38293)
|
|
|
|
|
|
|
|
data2Send := "Data to be sent to remote"
|
|
|
|
|
|
|
|
tcpServer := &tcp.Server{
|
|
|
|
Port: port,
|
|
|
|
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
|
|
|
|
|
|
|
pointPort := uint16(38724)
|
|
|
|
config := mocks.Config{
|
|
|
|
PortValue: pointPort,
|
|
|
|
InboundConfigValue: &mocks.ConnectionConfig{
|
|
|
|
ProtocolValue: "socks",
|
2015-10-06 17:11:08 -04:00
|
|
|
SettingsValue: &json.SocksConfig{
|
|
|
|
AuthMethod: "auth",
|
|
|
|
},
|
2015-09-24 12:01:02 -04:00
|
|
|
},
|
|
|
|
OutboundConfigValue: &mocks.ConnectionConfig{
|
|
|
|
ProtocolValue: "freedom",
|
2015-10-06 17:11:08 -04:00
|
|
|
SettingsValue: nil,
|
2015-09-24 12:01:02 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
point, err := core.NewPoint(&config)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
err = point.Start()
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
socks5Client, err := proxy.SOCKS5("tcp", "127.0.0.1:38724", nil, proxy.Direct)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
2015-10-04 14:22:52 -04:00
|
|
|
targetServer := "127.0.0.1:38293"
|
2015-09-24 12:01:02 -04:00
|
|
|
conn, err := socks5Client.Dial("tcp", targetServer)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
conn.Write([]byte(data2Send))
|
|
|
|
if tcpConn, ok := conn.(*net.TCPConn); ok {
|
|
|
|
tcpConn.CloseWrite()
|
|
|
|
}
|
|
|
|
|
|
|
|
dataReturned, err := ioutil.ReadAll(conn)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
conn.Close()
|
|
|
|
|
2015-10-04 14:22:52 -04:00
|
|
|
assert.Bytes(dataReturned).Equals([]byte("Processed: Data to be sent to remote"))
|
2015-09-24 12:01:02 -04:00
|
|
|
}
|