2016-12-16 20:39:00 +00:00
|
|
|
package scenarios
|
|
|
|
|
|
|
|
import (
|
2017-02-08 16:57:21 +00:00
|
|
|
"crypto/rand"
|
2016-12-30 22:12:00 +00:00
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-12-16 20:39:00 +00:00
|
|
|
"v2ray.com/core"
|
2017-01-26 19:46:44 +00:00
|
|
|
"v2ray.com/core/app/proxyman"
|
2016-12-16 20:39:00 +00:00
|
|
|
v2net "v2ray.com/core/common/net"
|
2016-12-30 22:12:00 +00:00
|
|
|
"v2ray.com/core/common/protocol"
|
|
|
|
"v2ray.com/core/common/serial"
|
|
|
|
"v2ray.com/core/common/uuid"
|
|
|
|
"v2ray.com/core/proxy/dokodemo"
|
|
|
|
"v2ray.com/core/proxy/freedom"
|
|
|
|
"v2ray.com/core/proxy/vmess"
|
|
|
|
"v2ray.com/core/proxy/vmess/inbound"
|
|
|
|
"v2ray.com/core/proxy/vmess/outbound"
|
|
|
|
"v2ray.com/core/testing/assert"
|
|
|
|
"v2ray.com/core/testing/servers/tcp"
|
2017-01-12 15:10:03 +00:00
|
|
|
tlsgen "v2ray.com/core/testing/tls"
|
2016-12-30 22:12:00 +00:00
|
|
|
"v2ray.com/core/transport/internet"
|
|
|
|
"v2ray.com/core/transport/internet/tls"
|
2017-02-08 16:57:21 +00:00
|
|
|
"v2ray.com/core/transport/internet/websocket"
|
2016-12-16 20:39:00 +00:00
|
|
|
)
|
|
|
|
|
2016-12-30 22:12:00 +00:00
|
|
|
func TestSimpleTLSConnection(t *testing.T) {
|
|
|
|
assert := assert.On(t)
|
|
|
|
|
|
|
|
tcpServer := tcp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
}
|
|
|
|
dest, err := tcpServer.Start()
|
|
|
|
assert.Error(err).IsNil()
|
2017-01-05 10:20:04 +00:00
|
|
|
defer tcpServer.Close()
|
2016-12-30 22:12:00 +00:00
|
|
|
|
|
|
|
userID := protocol.NewID(uuid.New())
|
|
|
|
serverPort := pickPort()
|
|
|
|
serverConfig := &core.Config{
|
2017-01-26 19:46:44 +00:00
|
|
|
Inbound: []*proxyman.InboundHandlerConfig{
|
2016-12-30 22:12:00 +00:00
|
|
|
{
|
2017-01-26 19:46:44 +00:00
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
|
|
|
PortRange: v2net.SinglePortRange(serverPort),
|
|
|
|
Listen: v2net.NewIPOrDomain(v2net.LocalHostIP),
|
|
|
|
StreamSettings: &internet.StreamConfig{
|
|
|
|
SecurityType: serial.GetMessageType(&tls.Config{}),
|
|
|
|
SecuritySettings: []*serial.TypedMessage{
|
|
|
|
serial.ToTypedMessage(&tls.Config{
|
|
|
|
Certificate: []*tls.Certificate{tlsgen.GenerateCertificateForTest()},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&inbound.Config{
|
2016-12-30 22:12:00 +00:00
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Account: serial.ToTypedMessage(&vmess.Account{
|
|
|
|
Id: userID.String(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
2017-01-12 15:10:03 +00:00
|
|
|
},
|
|
|
|
},
|
2017-01-26 19:46:44 +00:00
|
|
|
Outbound: []*proxyman.OutboundHandlerConfig{
|
2017-01-12 15:10:03 +00:00
|
|
|
{
|
2017-01-26 19:46:44 +00:00
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
2017-01-12 15:10:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
clientPort := pickPort()
|
|
|
|
clientConfig := &core.Config{
|
2017-01-26 19:46:44 +00:00
|
|
|
Inbound: []*proxyman.InboundHandlerConfig{
|
2017-01-12 15:10:03 +00:00
|
|
|
{
|
2017-01-26 19:46:44 +00:00
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
|
|
|
PortRange: v2net.SinglePortRange(clientPort),
|
|
|
|
Listen: v2net.NewIPOrDomain(v2net.LocalHostIP),
|
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
|
2017-01-12 15:10:03 +00:00
|
|
|
Address: v2net.NewIPOrDomain(dest.Address),
|
|
|
|
Port: uint32(dest.Port),
|
|
|
|
NetworkList: &v2net.NetworkList{
|
|
|
|
Network: []v2net.Network{v2net.Network_TCP},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2017-01-26 19:46:44 +00:00
|
|
|
Outbound: []*proxyman.OutboundHandlerConfig{
|
2017-01-12 15:10:03 +00:00
|
|
|
{
|
2017-01-26 19:46:44 +00:00
|
|
|
ProxySettings: serial.ToTypedMessage(&outbound.Config{
|
2017-01-12 15:10:03 +00:00
|
|
|
Receiver: []*protocol.ServerEndpoint{
|
|
|
|
{
|
|
|
|
Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
|
|
|
|
Port: uint32(serverPort),
|
|
|
|
User: []*protocol.User{
|
2016-12-30 22:12:00 +00:00
|
|
|
{
|
2017-01-12 15:10:03 +00:00
|
|
|
Account: serial.ToTypedMessage(&vmess.Account{
|
|
|
|
Id: userID.String(),
|
|
|
|
}),
|
2016-12-30 22:12:00 +00:00
|
|
|
},
|
|
|
|
},
|
2017-01-12 15:10:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
2017-01-26 19:46:44 +00:00
|
|
|
SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
|
|
|
|
StreamSettings: &internet.StreamConfig{
|
|
|
|
SecurityType: serial.GetMessageType(&tls.Config{}),
|
|
|
|
SecuritySettings: []*serial.TypedMessage{
|
|
|
|
serial.ToTypedMessage(&tls.Config{
|
|
|
|
AllowInsecure: true,
|
|
|
|
}),
|
|
|
|
},
|
2017-01-12 15:10:03 +00:00
|
|
|
},
|
2017-01-26 19:46:44 +00:00
|
|
|
}),
|
2017-01-12 15:10:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Error(InitializeServerConfig(serverConfig)).IsNil()
|
|
|
|
assert.Error(InitializeServerConfig(clientConfig)).IsNil()
|
|
|
|
|
|
|
|
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
|
|
|
|
IP: []byte{127, 0, 0, 1},
|
|
|
|
Port: int(clientPort),
|
|
|
|
})
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
payload := "dokodemo request."
|
|
|
|
nBytes, err := conn.Write([]byte(payload))
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Int(nBytes).Equals(len(payload))
|
|
|
|
|
|
|
|
response := readFrom(conn, time.Second*2, len(payload))
|
|
|
|
assert.Bytes(response).Equals(xor([]byte(payload)))
|
|
|
|
assert.Error(conn.Close()).IsNil()
|
|
|
|
|
|
|
|
CloseAllServers()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTLSOverKCP(t *testing.T) {
|
|
|
|
assert := assert.On(t)
|
|
|
|
|
|
|
|
tcpServer := tcp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
}
|
|
|
|
dest, err := tcpServer.Start()
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
defer tcpServer.Close()
|
|
|
|
|
|
|
|
userID := protocol.NewID(uuid.New())
|
|
|
|
serverPort := pickPort()
|
|
|
|
serverConfig := &core.Config{
|
2017-01-26 19:46:44 +00:00
|
|
|
Inbound: []*proxyman.InboundHandlerConfig{
|
2017-01-12 15:10:03 +00:00
|
|
|
{
|
2017-01-26 19:46:44 +00:00
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
|
|
|
PortRange: v2net.SinglePortRange(serverPort),
|
|
|
|
Listen: v2net.NewIPOrDomain(v2net.LocalHostIP),
|
|
|
|
StreamSettings: &internet.StreamConfig{
|
|
|
|
Protocol: internet.TransportProtocol_MKCP,
|
|
|
|
SecurityType: serial.GetMessageType(&tls.Config{}),
|
|
|
|
SecuritySettings: []*serial.TypedMessage{
|
|
|
|
serial.ToTypedMessage(&tls.Config{
|
|
|
|
Certificate: []*tls.Certificate{tlsgen.GenerateCertificateForTest()},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&inbound.Config{
|
2017-01-12 15:10:03 +00:00
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Account: serial.ToTypedMessage(&vmess.Account{
|
|
|
|
Id: userID.String(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
2016-12-30 22:12:00 +00:00
|
|
|
},
|
2016-12-16 20:39:00 +00:00
|
|
|
},
|
2017-01-26 19:46:44 +00:00
|
|
|
Outbound: []*proxyman.OutboundHandlerConfig{
|
2016-12-30 22:12:00 +00:00
|
|
|
{
|
2017-01-26 19:46:44 +00:00
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
2016-12-30 22:12:00 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
clientPort := pickPort()
|
|
|
|
clientConfig := &core.Config{
|
2017-01-26 19:46:44 +00:00
|
|
|
Inbound: []*proxyman.InboundHandlerConfig{
|
2016-12-30 22:12:00 +00:00
|
|
|
{
|
2017-01-26 19:46:44 +00:00
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
|
|
|
PortRange: v2net.SinglePortRange(clientPort),
|
|
|
|
Listen: v2net.NewIPOrDomain(v2net.LocalHostIP),
|
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
|
2016-12-30 22:12:00 +00:00
|
|
|
Address: v2net.NewIPOrDomain(dest.Address),
|
|
|
|
Port: uint32(dest.Port),
|
|
|
|
NetworkList: &v2net.NetworkList{
|
|
|
|
Network: []v2net.Network{v2net.Network_TCP},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2017-01-26 19:46:44 +00:00
|
|
|
Outbound: []*proxyman.OutboundHandlerConfig{
|
2016-12-30 22:12:00 +00:00
|
|
|
{
|
2017-01-26 19:46:44 +00:00
|
|
|
ProxySettings: serial.ToTypedMessage(&outbound.Config{
|
2016-12-30 22:12:00 +00:00
|
|
|
Receiver: []*protocol.ServerEndpoint{
|
|
|
|
{
|
|
|
|
Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
|
|
|
|
Port: uint32(serverPort),
|
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Account: serial.ToTypedMessage(&vmess.Account{
|
|
|
|
Id: userID.String(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
2017-01-26 19:46:44 +00:00
|
|
|
SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
|
|
|
|
StreamSettings: &internet.StreamConfig{
|
|
|
|
Protocol: internet.TransportProtocol_MKCP,
|
|
|
|
SecurityType: serial.GetMessageType(&tls.Config{}),
|
|
|
|
SecuritySettings: []*serial.TypedMessage{
|
|
|
|
serial.ToTypedMessage(&tls.Config{
|
|
|
|
AllowInsecure: true,
|
|
|
|
}),
|
|
|
|
},
|
2016-12-30 22:12:00 +00:00
|
|
|
},
|
2017-01-26 19:46:44 +00:00
|
|
|
}),
|
2016-12-30 22:12:00 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Error(InitializeServerConfig(serverConfig)).IsNil()
|
|
|
|
assert.Error(InitializeServerConfig(clientConfig)).IsNil()
|
|
|
|
|
|
|
|
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
|
|
|
|
IP: []byte{127, 0, 0, 1},
|
|
|
|
Port: int(clientPort),
|
|
|
|
})
|
2017-01-04 14:42:06 +00:00
|
|
|
assert.Error(err).IsNil()
|
2016-12-30 22:12:00 +00:00
|
|
|
|
|
|
|
payload := "dokodemo request."
|
|
|
|
nBytes, err := conn.Write([]byte(payload))
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Int(nBytes).Equals(len(payload))
|
|
|
|
|
2017-01-04 14:42:06 +00:00
|
|
|
response := readFrom(conn, time.Second*2, len(payload))
|
|
|
|
assert.Bytes(response).Equals(xor([]byte(payload)))
|
|
|
|
assert.Error(conn.Close()).IsNil()
|
|
|
|
|
|
|
|
CloseAllServers()
|
|
|
|
}
|
|
|
|
|
2017-02-08 16:57:21 +00:00
|
|
|
func TestTLSOverWebSocket(t *testing.T) {
|
|
|
|
assert := assert.On(t)
|
|
|
|
|
|
|
|
tcpServer := tcp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
}
|
|
|
|
dest, err := tcpServer.Start()
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
defer tcpServer.Close()
|
|
|
|
|
|
|
|
userID := protocol.NewID(uuid.New())
|
|
|
|
serverPort := pickPort()
|
|
|
|
serverConfig := &core.Config{
|
|
|
|
Inbound: []*proxyman.InboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
|
|
|
PortRange: v2net.SinglePortRange(serverPort),
|
|
|
|
Listen: v2net.NewIPOrDomain(v2net.LocalHostIP),
|
|
|
|
StreamSettings: &internet.StreamConfig{
|
|
|
|
Protocol: internet.TransportProtocol_WebSocket,
|
|
|
|
SecurityType: serial.GetMessageType(&tls.Config{}),
|
|
|
|
SecuritySettings: []*serial.TypedMessage{
|
|
|
|
serial.ToTypedMessage(&tls.Config{
|
|
|
|
Certificate: []*tls.Certificate{tlsgen.GenerateCertificateForTest()},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&inbound.Config{
|
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Account: serial.ToTypedMessage(&vmess.Account{
|
|
|
|
Id: userID.String(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Outbound: []*proxyman.OutboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
clientPort := pickPort()
|
|
|
|
clientConfig := &core.Config{
|
|
|
|
Inbound: []*proxyman.InboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
|
|
|
PortRange: v2net.SinglePortRange(clientPort),
|
|
|
|
Listen: v2net.NewIPOrDomain(v2net.LocalHostIP),
|
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
|
|
|
|
Address: v2net.NewIPOrDomain(dest.Address),
|
|
|
|
Port: uint32(dest.Port),
|
|
|
|
NetworkList: &v2net.NetworkList{
|
|
|
|
Network: []v2net.Network{v2net.Network_TCP},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Outbound: []*proxyman.OutboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ProxySettings: serial.ToTypedMessage(&outbound.Config{
|
|
|
|
Receiver: []*protocol.ServerEndpoint{
|
|
|
|
{
|
|
|
|
Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
|
|
|
|
Port: uint32(serverPort),
|
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Account: serial.ToTypedMessage(&vmess.Account{
|
|
|
|
Id: userID.String(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
|
|
|
|
StreamSettings: &internet.StreamConfig{
|
|
|
|
Protocol: internet.TransportProtocol_WebSocket,
|
|
|
|
TransportSettings: []*internet.TransportConfig{
|
|
|
|
{
|
|
|
|
Protocol: internet.TransportProtocol_WebSocket,
|
2017-04-07 19:54:40 +00:00
|
|
|
Settings: serial.ToTypedMessage(&websocket.Config{}),
|
2017-02-08 16:57:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
SecurityType: serial.GetMessageType(&tls.Config{}),
|
|
|
|
SecuritySettings: []*serial.TypedMessage{
|
|
|
|
serial.ToTypedMessage(&tls.Config{
|
|
|
|
AllowInsecure: true,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Error(InitializeServerConfig(serverConfig)).IsNil()
|
|
|
|
assert.Error(InitializeServerConfig(clientConfig)).IsNil()
|
|
|
|
|
|
|
|
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
|
|
|
|
IP: []byte{127, 0, 0, 1},
|
|
|
|
Port: int(clientPort),
|
|
|
|
})
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
payload := make([]byte, 10240*1024)
|
|
|
|
rand.Read(payload)
|
|
|
|
nBytes, err := conn.Write([]byte(payload))
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Int(nBytes).Equals(len(payload))
|
|
|
|
|
|
|
|
response := readFrom(conn, time.Second*10, len(payload))
|
|
|
|
assert.Bytes(response).Equals(xor([]byte(payload)))
|
|
|
|
assert.Error(conn.Close()).IsNil()
|
|
|
|
|
|
|
|
CloseAllServers()
|
|
|
|
}
|