2015-11-06 07:08:20 -05:00
|
|
|
package scenarios
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/testing/assert"
|
|
|
|
"v2ray.com/core/testing/servers/tcp"
|
|
|
|
"v2ray.com/core/testing/servers/udp"
|
2015-11-06 07:08:20 -05:00
|
|
|
)
|
|
|
|
|
2015-11-06 07:45:41 -05:00
|
|
|
func TestTCPConnection(t *testing.T) {
|
2016-05-24 15:55:46 -04:00
|
|
|
assert := assert.On(t)
|
2015-11-06 07:08:20 -05:00
|
|
|
|
2015-11-06 07:45:41 -05:00
|
|
|
tcpServer := &tcp.Server{
|
|
|
|
MsgProcessor: func(data []byte) []byte {
|
|
|
|
buffer := make([]byte, 0, 2048)
|
|
|
|
buffer = append(buffer, []byte("Processed: ")...)
|
|
|
|
buffer = append(buffer, data...)
|
|
|
|
return buffer
|
2015-11-06 07:08:20 -05:00
|
|
|
},
|
|
|
|
}
|
2015-11-06 07:45:41 -05:00
|
|
|
_, err := tcpServer.Start()
|
|
|
|
assert.Error(err).IsNil()
|
2015-12-15 10:00:47 -05:00
|
|
|
defer tcpServer.Close()
|
2015-11-06 07:08:20 -05:00
|
|
|
|
2015-12-04 11:32:42 -05:00
|
|
|
assert.Error(InitializeServerSetOnce("test_1")).IsNil()
|
2015-12-04 10:49:10 -05:00
|
|
|
|
|
|
|
socksPort := v2net.Port(50000)
|
|
|
|
|
2016-01-21 11:22:56 -05:00
|
|
|
for i := 0; i < 100; i++ {
|
2015-11-06 07:45:41 -05:00
|
|
|
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
|
|
|
|
IP: []byte{127, 0, 0, 1},
|
2015-12-04 10:49:10 -05:00
|
|
|
Port: int(socksPort),
|
2015-11-06 07:45:41 -05:00
|
|
|
})
|
2016-02-22 11:13:45 -05:00
|
|
|
assert.Error(err).IsNil()
|
2015-11-06 07:45:41 -05:00
|
|
|
|
|
|
|
authRequest := socks5AuthMethodRequest(byte(0))
|
|
|
|
nBytes, err := conn.Write(authRequest)
|
|
|
|
assert.Int(nBytes).Equals(len(authRequest))
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
authResponse := make([]byte, 1024)
|
|
|
|
nBytes, err = conn.Read(authResponse)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Bytes(authResponse[:nBytes]).Equals([]byte{socks5Version, 0})
|
|
|
|
|
2016-08-15 16:12:11 -04:00
|
|
|
connectRequest := socks5Request(byte(1), v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), tcpServer.Port))
|
2015-11-06 07:45:41 -05:00
|
|
|
nBytes, err = conn.Write(connectRequest)
|
|
|
|
assert.Int(nBytes).Equals(len(connectRequest))
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
connectResponse := make([]byte, 1024)
|
|
|
|
nBytes, err = conn.Read(connectResponse)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Bytes(connectResponse[:nBytes]).Equals([]byte{socks5Version, 0, 0, 1, 0, 0, 0, 0, 6, 181})
|
|
|
|
|
|
|
|
actualRequest := []byte("Request to target server.")
|
|
|
|
nBytes, err = conn.Write(actualRequest)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Int(nBytes).Equals(len(actualRequest))
|
|
|
|
|
2015-12-16 10:42:54 -05:00
|
|
|
actualResponse := make([]byte, 1024)
|
|
|
|
nResponse, err := conn.Read(actualResponse)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
2015-11-06 07:45:41 -05:00
|
|
|
actualRequest = []byte("Request to target server again.")
|
|
|
|
nBytes, err = conn.Write(actualRequest)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Int(nBytes).Equals(len(actualRequest))
|
|
|
|
|
2015-12-16 10:42:54 -05:00
|
|
|
nBytes, err = conn.Read(actualResponse[nResponse:])
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
nResponse += nBytes
|
2015-11-06 07:45:41 -05:00
|
|
|
conn.CloseWrite()
|
|
|
|
|
2016-05-24 15:55:46 -04:00
|
|
|
assert.String(string(actualResponse[:nResponse])).Equals("Processed: Request to target server.Processed: Request to target server again.")
|
2015-11-06 07:45:41 -05:00
|
|
|
|
|
|
|
conn.Close()
|
2015-11-06 07:08:20 -05:00
|
|
|
}
|
2016-01-03 18:33:25 -05:00
|
|
|
|
|
|
|
CloseAllServers()
|
2015-11-06 07:08:20 -05:00
|
|
|
}
|
|
|
|
|
2016-08-20 03:12:44 -04:00
|
|
|
func TestPassiveTCPConnection(t *testing.T) {
|
|
|
|
assert := assert.On(t)
|
|
|
|
|
|
|
|
tcpServer := &tcp.Server{
|
|
|
|
MsgProcessor: func(data []byte) []byte {
|
|
|
|
buffer := make([]byte, 0, 2048)
|
|
|
|
buffer = append(buffer, []byte("Processed: ")...)
|
|
|
|
buffer = append(buffer, data...)
|
|
|
|
return buffer
|
|
|
|
},
|
|
|
|
SendFirst: []byte("Server sends first."),
|
|
|
|
}
|
|
|
|
_, err := tcpServer.Start()
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
defer tcpServer.Close()
|
|
|
|
|
|
|
|
assert.Error(InitializeServerSetOnce("test_1")).IsNil()
|
|
|
|
|
|
|
|
socksPort := v2net.Port(50002)
|
|
|
|
|
|
|
|
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
|
|
|
|
IP: []byte{127, 0, 0, 1},
|
|
|
|
Port: int(socksPort),
|
|
|
|
})
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
authRequest := socks5AuthMethodRequest(byte(0))
|
|
|
|
nBytes, err := conn.Write(authRequest)
|
|
|
|
assert.Int(nBytes).Equals(len(authRequest))
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
authResponse := make([]byte, 1024)
|
|
|
|
nBytes, err = conn.Read(authResponse)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Bytes(authResponse[:nBytes]).Equals([]byte{socks5Version, 0})
|
|
|
|
|
|
|
|
connectRequest := socks5Request(byte(1), v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), tcpServer.Port))
|
|
|
|
nBytes, err = conn.Write(connectRequest)
|
|
|
|
assert.Int(nBytes).Equals(len(connectRequest))
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
connectResponse := make([]byte, 1024)
|
|
|
|
nBytes, err = conn.Read(connectResponse)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Bytes(connectResponse[:nBytes]).Equals([]byte{socks5Version, 0, 0, 1, 0, 0, 0, 0, 6, 181})
|
|
|
|
|
|
|
|
actualResponse := make([]byte, 1024)
|
|
|
|
nResponse, err := conn.Read(actualResponse)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
assert.String(string(actualResponse[:nResponse])).Equals(string(tcpServer.SendFirst))
|
|
|
|
|
|
|
|
conn.Close()
|
|
|
|
|
|
|
|
CloseAllServers()
|
|
|
|
}
|
|
|
|
|
2015-11-06 07:45:41 -05:00
|
|
|
func TestTCPBind(t *testing.T) {
|
2016-05-24 15:55:46 -04:00
|
|
|
assert := assert.On(t)
|
2015-11-06 07:08:20 -05:00
|
|
|
|
|
|
|
tcpServer := &tcp.Server{
|
|
|
|
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-12-15 10:00:47 -05:00
|
|
|
defer tcpServer.Close()
|
2015-11-06 07:08:20 -05:00
|
|
|
|
2015-12-04 11:32:42 -05:00
|
|
|
assert.Error(InitializeServerSetOnce("test_1")).IsNil()
|
2015-12-04 10:49:10 -05:00
|
|
|
|
|
|
|
socksPort := v2net.Port(50000)
|
|
|
|
|
2015-11-06 07:08:20 -05:00
|
|
|
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
|
|
|
|
IP: []byte{127, 0, 0, 1},
|
2015-12-04 10:49:10 -05:00
|
|
|
Port: int(socksPort),
|
2015-11-06 07:08:20 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
authRequest := socks5AuthMethodRequest(byte(0))
|
|
|
|
nBytes, err := conn.Write(authRequest)
|
|
|
|
assert.Int(nBytes).Equals(len(authRequest))
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
authResponse := make([]byte, 1024)
|
|
|
|
nBytes, err = conn.Read(authResponse)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Bytes(authResponse[:nBytes]).Equals([]byte{socks5Version, 0})
|
|
|
|
|
2016-08-15 16:12:11 -04:00
|
|
|
connectRequest := socks5Request(byte(2), v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), tcpServer.Port))
|
2015-11-06 07:08:20 -05:00
|
|
|
nBytes, err = conn.Write(connectRequest)
|
|
|
|
assert.Int(nBytes).Equals(len(connectRequest))
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
connectResponse := make([]byte, 1024)
|
|
|
|
nBytes, err = conn.Read(connectResponse)
|
|
|
|
assert.Error(err).IsNil()
|
2015-11-06 07:45:41 -05:00
|
|
|
assert.Bytes(connectResponse[:nBytes]).Equals([]byte{socks5Version, 7, 0, 1, 0, 0, 0, 0, 0, 0})
|
2015-11-06 07:08:20 -05:00
|
|
|
|
|
|
|
conn.Close()
|
2016-01-03 18:33:25 -05:00
|
|
|
|
|
|
|
CloseAllServers()
|
2015-11-06 07:08:20 -05:00
|
|
|
}
|
|
|
|
|
2015-11-06 07:45:41 -05:00
|
|
|
func TestUDPAssociate(t *testing.T) {
|
2016-05-24 15:55:46 -04:00
|
|
|
assert := assert.On(t)
|
2015-11-06 07:08:20 -05:00
|
|
|
|
2015-11-06 07:45:41 -05:00
|
|
|
udpServer := &udp.Server{
|
2015-11-06 07:08:20 -05:00
|
|
|
MsgProcessor: func(data []byte) []byte {
|
|
|
|
buffer := make([]byte, 0, 2048)
|
|
|
|
buffer = append(buffer, []byte("Processed: ")...)
|
|
|
|
buffer = append(buffer, data...)
|
|
|
|
return buffer
|
|
|
|
},
|
|
|
|
}
|
2015-11-06 07:45:41 -05:00
|
|
|
_, err := udpServer.Start()
|
2015-11-06 07:08:20 -05:00
|
|
|
assert.Error(err).IsNil()
|
2015-12-15 10:00:47 -05:00
|
|
|
defer udpServer.Close()
|
2015-11-06 07:08:20 -05:00
|
|
|
|
2015-12-04 11:32:42 -05:00
|
|
|
assert.Error(InitializeServerSetOnce("test_1")).IsNil()
|
2015-12-04 10:49:10 -05:00
|
|
|
|
|
|
|
socksPort := v2net.Port(50000)
|
|
|
|
|
2015-11-06 07:08:20 -05:00
|
|
|
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
|
|
|
|
IP: []byte{127, 0, 0, 1},
|
2015-12-04 10:49:10 -05:00
|
|
|
Port: int(socksPort),
|
2015-11-06 07:08:20 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
authRequest := socks5AuthMethodRequest(byte(0))
|
|
|
|
nBytes, err := conn.Write(authRequest)
|
|
|
|
assert.Int(nBytes).Equals(len(authRequest))
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
authResponse := make([]byte, 1024)
|
|
|
|
nBytes, err = conn.Read(authResponse)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Bytes(authResponse[:nBytes]).Equals([]byte{socks5Version, 0})
|
|
|
|
|
2016-08-15 16:12:11 -04:00
|
|
|
connectRequest := socks5Request(byte(3), v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), udpServer.Port))
|
2015-11-06 07:08:20 -05:00
|
|
|
nBytes, err = conn.Write(connectRequest)
|
|
|
|
assert.Int(nBytes).Equals(len(connectRequest))
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
connectResponse := make([]byte, 1024)
|
|
|
|
nBytes, err = conn.Read(connectResponse)
|
|
|
|
assert.Error(err).IsNil()
|
2015-12-04 10:49:10 -05:00
|
|
|
assert.Bytes(connectResponse[:nBytes]).Equals([]byte{socks5Version, 0, 0, 1, 127, 0, 0, 1, byte(socksPort >> 8), byte(socksPort)})
|
2015-11-06 07:45:41 -05:00
|
|
|
|
|
|
|
udpConn, err := net.DialUDP("udp", nil, &net.UDPAddr{
|
|
|
|
IP: []byte{127, 0, 0, 1},
|
2015-12-04 10:49:10 -05:00
|
|
|
Port: int(socksPort),
|
2015-11-06 07:45:41 -05:00
|
|
|
})
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
2016-02-01 10:36:33 -05:00
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
udpPayload := "UDP request to udp server."
|
2016-08-15 16:12:11 -04:00
|
|
|
udpRequest := socks5UDPRequest(v2net.UDPDestination(v2net.LocalHostIP, udpServer.Port), []byte(udpPayload))
|
2015-11-06 07:45:41 -05:00
|
|
|
|
2016-02-01 10:36:33 -05:00
|
|
|
nBytes, err = udpConn.Write(udpRequest)
|
|
|
|
assert.Int(nBytes).Equals(len(udpRequest))
|
|
|
|
assert.Error(err).IsNil()
|
2015-11-06 07:45:41 -05:00
|
|
|
|
2016-02-01 10:36:33 -05:00
|
|
|
udpResponse := make([]byte, 1024)
|
|
|
|
nBytes, err = udpConn.Read(udpResponse)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Bytes(udpResponse[:nBytes]).Equals(
|
2016-08-15 16:12:11 -04:00
|
|
|
socks5UDPRequest(v2net.UDPDestination(v2net.LocalHostIP, udpServer.Port), []byte("Processed: UDP request to udp server.")))
|
2016-02-01 10:36:33 -05:00
|
|
|
}
|
2015-11-06 07:08:20 -05:00
|
|
|
|
2015-11-06 07:45:41 -05:00
|
|
|
udpConn.Close()
|
2015-11-06 07:08:20 -05:00
|
|
|
conn.Close()
|
2016-01-03 18:33:25 -05:00
|
|
|
|
|
|
|
CloseAllServers()
|
2015-11-06 07:08:20 -05:00
|
|
|
}
|