2015-09-24 12:01:02 -04:00
|
|
|
package freedom
|
|
|
|
|
|
|
|
import (
|
2015-10-04 14:22:52 -04:00
|
|
|
"bytes"
|
2015-11-01 15:32:08 -05:00
|
|
|
"fmt"
|
2015-09-24 12:01:02 -04:00
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"golang.org/x/net/proxy"
|
|
|
|
|
2016-01-01 17:44:11 -05:00
|
|
|
"github.com/v2ray/v2ray-core/app"
|
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-11-01 15:32:08 -05:00
|
|
|
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
|
2016-01-02 17:32:18 -05:00
|
|
|
v2proxy "github.com/v2ray/v2ray-core/proxy"
|
2015-09-24 12:01:02 -04:00
|
|
|
_ "github.com/v2ray/v2ray-core/proxy/socks"
|
2016-01-02 11:40:51 -05:00
|
|
|
_ "github.com/v2ray/v2ray-core/proxy/socks/json"
|
2016-01-01 17:44:11 -05:00
|
|
|
proxytesting "github.com/v2ray/v2ray-core/proxy/testing"
|
2015-11-01 15:15:08 -05:00
|
|
|
proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks"
|
2015-11-29 08:45:32 -05:00
|
|
|
"github.com/v2ray/v2ray-core/shell/point"
|
2015-12-06 10:41:41 -05:00
|
|
|
"github.com/v2ray/v2ray-core/shell/point/testing/mocks"
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing "github.com/v2ray/v2ray-core/testing"
|
|
|
|
"github.com/v2ray/v2ray-core/testing/assert"
|
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
|
|
|
)
|
|
|
|
|
2015-10-04 14:22:52 -04:00
|
|
|
func TestUDPSend(t *testing.T) {
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing.Current(t)
|
2015-09-24 12:01:02 -04:00
|
|
|
|
|
|
|
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-11-01 15:15:08 -05:00
|
|
|
connOutput := bytes.NewBuffer(make([]byte, 0, 1024))
|
|
|
|
ich := &proxymocks.InboundConnectionHandler{
|
|
|
|
ConnInput: bytes.NewReader([]byte("Not Used")),
|
|
|
|
ConnOutput: connOutput,
|
2015-10-04 14:22:52 -04:00
|
|
|
}
|
2015-09-24 12:01:02 -04:00
|
|
|
|
2016-01-06 10:23:54 -05:00
|
|
|
protocol, err := proxytesting.RegisterInboundConnectionHandlerCreator("mock_ich",
|
|
|
|
func(space app.Space, config interface{}) (v2proxy.InboundConnectionHandler, error) {
|
|
|
|
ich.Space = space
|
|
|
|
return ich, nil
|
|
|
|
})
|
2016-01-01 17:44:11 -05:00
|
|
|
assert.Error(err).IsNil()
|
2015-09-24 12:01:02 -04:00
|
|
|
|
2015-11-01 15:32:08 -05:00
|
|
|
pointPort := v2nettesting.PickPort()
|
2015-10-04 14:22:52 -04:00
|
|
|
config := mocks.Config{
|
|
|
|
PortValue: pointPort,
|
|
|
|
InboundConfigValue: &mocks.ConnectionConfig{
|
2016-01-01 17:44:11 -05:00
|
|
|
ProtocolValue: protocol,
|
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-14 08:56:11 -04:00
|
|
|
point, err := point.NewPoint(&config)
|
2015-10-04 14:22:52 -04:00
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
err = point.Start()
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
2015-11-01 15:15:08 -05:00
|
|
|
data2SendBuffer := alloc.NewBuffer().Clear()
|
2015-10-08 08:46:18 -04:00
|
|
|
data2SendBuffer.Append([]byte(data2Send))
|
2015-12-16 17:53:38 -05:00
|
|
|
ich.Communicate(v2net.NewPacket(udpServerAddr, data2SendBuffer, false))
|
2015-11-01 15:15:08 -05:00
|
|
|
assert.Bytes(connOutput.Bytes()).Equals([]byte("Processed: Data to be sent to remote"))
|
2015-10-04 14:22:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSocksTcpConnect(t *testing.T) {
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing.Current(t)
|
2015-11-01 15:32:08 -05:00
|
|
|
port := v2nettesting.PickPort()
|
2015-10-04 14:22:52 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
2015-11-01 15:32:08 -05:00
|
|
|
pointPort := v2nettesting.PickPort()
|
2015-09-24 12:01:02 -04:00
|
|
|
config := mocks.Config{
|
|
|
|
PortValue: pointPort,
|
|
|
|
InboundConfigValue: &mocks.ConnectionConfig{
|
|
|
|
ProtocolValue: "socks",
|
2016-01-02 11:40:51 -05:00
|
|
|
SettingsValue: []byte(`{"auth": "noauth"}`),
|
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
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-10-14 08:56:11 -04:00
|
|
|
point, err := point.NewPoint(&config)
|
2015-09-24 12:01:02 -04:00
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
err = point.Start()
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
2015-11-01 15:32:08 -05:00
|
|
|
socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", pointPort), nil, proxy.Direct)
|
2015-09-24 12:01:02 -04:00
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
2015-11-01 15:32:08 -05:00
|
|
|
targetServer := fmt.Sprintf("127.0.0.1:%d", port)
|
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()
|
|
|
|
}
|
|
|
|
|
2015-12-15 10:00:47 -05:00
|
|
|
dataReturned, err := v2net.ReadFrom(conn, nil)
|
2015-09-24 12:01:02 -04:00
|
|
|
assert.Error(err).IsNil()
|
|
|
|
conn.Close()
|
|
|
|
|
2015-12-15 10:00:47 -05:00
|
|
|
assert.Bytes(dataReturned.Value).Equals([]byte("Processed: Data to be sent to remote"))
|
2015-09-24 12:01:02 -04:00
|
|
|
}
|