mirror of https://github.com/v2ray/v2ray-core
test case for socks.net
parent
729312f3b5
commit
8be5d695c8
|
@ -1,39 +1,63 @@
|
||||||
package socks
|
package socks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// "bytes"
|
"bytes"
|
||||||
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
// "github.com/v2ray/v2ray-core"
|
"golang.org/x/net/proxy"
|
||||||
// "github.com/v2ray/v2ray-core/testing/mocks"
|
|
||||||
// "github.com/v2ray/v2ray-core/testing/unit"
|
"github.com/v2ray/v2ray-core"
|
||||||
|
"github.com/v2ray/v2ray-core/testing/mocks"
|
||||||
|
"github.com/v2ray/v2ray-core/testing/unit"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSocksTcpConnect(t *testing.T) {
|
func TestSocksTcpConnect(t *testing.T) {
|
||||||
t.Skip("Not ready yet.")
|
|
||||||
/*
|
|
||||||
assert := unit.Assert(t)
|
assert := unit.Assert(t)
|
||||||
|
port := uint16(12385)
|
||||||
|
|
||||||
port := uint16(12384)
|
och := &mocks.OutboundConnectionHandler{
|
||||||
|
Data2Send: bytes.NewBuffer(make([]byte, 0, 1024)),
|
||||||
|
Data2Return: []byte("The data to be returned to socks server."),
|
||||||
|
}
|
||||||
|
|
||||||
uuid := "2418d087-648d-4990-86e8-19dca1d006d3"
|
core.RegisterOutboundConnectionHandlerFactory("mock_och", och)
|
||||||
id, err := core.UUIDToID(uuid)
|
|
||||||
|
config := mocks.Config{
|
||||||
|
PortValue: port,
|
||||||
|
InboundConfigValue: &mocks.ConnectionConfig{
|
||||||
|
ProtocolValue: "socks",
|
||||||
|
ContentValue: []byte("{\"auth\": \"noauth\"}"),
|
||||||
|
},
|
||||||
|
OutboundConfigValue: &mocks.ConnectionConfig{
|
||||||
|
ProtocolValue: "mock_och",
|
||||||
|
ContentValue: nil,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
point, err := core.NewPoint(&config)
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
|
|
||||||
config := core.VConfig{
|
err = point.Start()
|
||||||
port,
|
|
||||||
[]core.User{core.User{id}},
|
|
||||||
"",
|
|
||||||
[]core.VNext{}}
|
|
||||||
|
|
||||||
och := new(mocks.FakeOutboundConnectionHandler)
|
|
||||||
och.Data2Send = bytes.NewBuffer(make([]byte, 1024))
|
|
||||||
och.Data2Return = []byte("The data to be returned to socks server.")
|
|
||||||
|
|
||||||
vpoint, err := core.NewPoint(&config, SocksServerFactory{}, och)
|
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
|
|
||||||
err = vpoint.Start()
|
socks5Client, err := proxy.SOCKS5("tcp", "127.0.0.1:12385", nil, proxy.Direct)
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
*/
|
|
||||||
|
conn, err := socks5Client.Dial("tcp", "google.com:80")
|
||||||
|
assert.Error(err).IsNil()
|
||||||
|
|
||||||
|
data2Send := "The data to be sent to remote server."
|
||||||
|
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([]byte(data2Send)).Equals(och.Data2Send.Bytes())
|
||||||
|
assert.Bytes(dataReturned).Equals(och.Data2Return)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
package mocks
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/v2ray/v2ray-core"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ConnectionConfig struct {
|
||||||
|
ProtocolValue string
|
||||||
|
ContentValue []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config *ConnectionConfig) Protocol() string {
|
||||||
|
return config.ProtocolValue
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config *ConnectionConfig) Content() []byte {
|
||||||
|
return config.ContentValue
|
||||||
|
}
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
PortValue uint16
|
||||||
|
InboundConfigValue *ConnectionConfig
|
||||||
|
OutboundConfigValue *ConnectionConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config *Config) Port() uint16 {
|
||||||
|
return config.PortValue
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config *Config) InboundConfig() core.ConnectionConfig {
|
||||||
|
return config.InboundConfigValue
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config *Config) OutboundConfig() core.ConnectionConfig {
|
||||||
|
return config.OutboundConfigValue
|
||||||
|
}
|
|
@ -1,33 +0,0 @@
|
||||||
package mocks
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core"
|
|
||||||
v2net "github.com/v2ray/v2ray-core/net"
|
|
||||||
)
|
|
||||||
|
|
||||||
type FakeOutboundConnectionHandler struct {
|
|
||||||
Data2Send *bytes.Buffer
|
|
||||||
Data2Return []byte
|
|
||||||
Destination v2net.Address
|
|
||||||
}
|
|
||||||
|
|
||||||
func (handler *FakeOutboundConnectionHandler) Start(ray core.OutboundRay) error {
|
|
||||||
input := ray.OutboundInput()
|
|
||||||
output := ray.OutboundOutput()
|
|
||||||
|
|
||||||
output <- handler.Data2Return
|
|
||||||
for {
|
|
||||||
data, open := <-input
|
|
||||||
if !open {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
handler.Data2Send.Write(data)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (handler *FakeOutboundConnectionHandler) Create(point *core.Point, dest v2net.Address) (core.OutboundConnectionHandler, error) {
|
|
||||||
return handler, nil
|
|
||||||
}
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
package mocks
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
|
||||||
|
"github.com/v2ray/v2ray-core"
|
||||||
|
v2net "github.com/v2ray/v2ray-core/net"
|
||||||
|
)
|
||||||
|
|
||||||
|
type OutboundConnectionHandler struct {
|
||||||
|
Data2Send *bytes.Buffer
|
||||||
|
Data2Return []byte
|
||||||
|
Destination v2net.Address
|
||||||
|
}
|
||||||
|
|
||||||
|
func (handler *OutboundConnectionHandler) Start(ray core.OutboundRay) error {
|
||||||
|
input := ray.OutboundInput()
|
||||||
|
output := ray.OutboundOutput()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
data, open := <-input
|
||||||
|
if !open {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
handler.Data2Send.Write(data)
|
||||||
|
}
|
||||||
|
output <- handler.Data2Return
|
||||||
|
close(output)
|
||||||
|
}()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (handler *OutboundConnectionHandler) Create(point *core.Point, config []byte, dest v2net.Address) (core.OutboundConnectionHandler, error) {
|
||||||
|
handler.Destination = dest
|
||||||
|
return handler, nil
|
||||||
|
}
|
Loading…
Reference in New Issue