| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package freedom
- import (
- "bytes"
- "io/ioutil"
- "net"
- "testing"
- "golang.org/x/net/proxy"
- "github.com/v2ray/v2ray-core"
- v2net "github.com/v2ray/v2ray-core/common/net"
- _ "github.com/v2ray/v2ray-core/proxy/socks"
- "github.com/v2ray/v2ray-core/proxy/socks/config/json"
- "github.com/v2ray/v2ray-core/testing/mocks"
- "github.com/v2ray/v2ray-core/testing/servers/tcp"
- "github.com/v2ray/v2ray-core/testing/servers/udp"
- "github.com/v2ray/v2ray-core/testing/unit"
- )
- func TestUDPSend(t *testing.T) {
- assert := unit.Assert(t)
- data2Send := "Data to be sent to remote"
- 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
- },
- }
- udpServerAddr, err := udpServer.Start()
- assert.Error(err).IsNil()
- ich := &mocks.InboundConnectionHandler{
- Data2Send: []byte("Not Used"),
- DataReturned: bytes.NewBuffer(make([]byte, 0, 1024)),
- }
- core.RegisterInboundConnectionHandlerFactory("mock_ich", ich)
- pointPort := uint16(38724)
- config := mocks.Config{
- PortValue: pointPort,
- InboundConfigValue: &mocks.ConnectionConfig{
- ProtocolValue: "mock_ich",
- SettingsValue: nil,
- },
- OutboundConfigValue: &mocks.ConnectionConfig{
- ProtocolValue: "freedom",
- SettingsValue: nil,
- },
- }
- point, err := core.NewPoint(&config)
- assert.Error(err).IsNil()
- err = point.Start()
- assert.Error(err).IsNil()
- dest := v2net.NewUDPDestination(udpServerAddr)
- ich.Communicate(v2net.NewPacket(dest, []byte(data2Send), false))
- 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()
- pointPort := uint16(38724)
- config := mocks.Config{
- PortValue: pointPort,
- InboundConfigValue: &mocks.ConnectionConfig{
- ProtocolValue: "socks",
- SettingsValue: &json.SocksConfig{
- AuthMethod: "auth",
- },
- },
- OutboundConfigValue: &mocks.ConnectionConfig{
- ProtocolValue: "freedom",
- SettingsValue: nil,
- },
- }
- 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()
- targetServer := "127.0.0.1:38293"
- 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()
- assert.Bytes(dataReturned).Equals([]byte("Processed: Data to be sent to remote"))
- }
|