1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 01:27:03 -05:00

test case for passive connection

This commit is contained in:
v2ray 2016-08-20 09:12:44 +02:00
parent 2268f3d1bc
commit 7a1bdfcf00
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
4 changed files with 80 additions and 1 deletions

View File

@ -1,4 +1,7 @@
{
"log": {
"loglevel": "debug"
},
"inbound": {
"port": 50000,
"listen": "127.0.0.1",
@ -9,6 +12,19 @@
"ip": "127.0.0.1"
}
},
"inboundDetour": [
{
"port": 50002,
"listen": "127.0.0.1",
"protocol": "socks",
"allowPassive": true,
"settings": {
"auth": "noauth",
"udp": true,
"ip": "127.0.0.1"
}
}
],
"outbound": {
"protocol": "vmess",
"settings": {

View File

@ -1,11 +1,12 @@
{
"port": 50001,
"log": {
"loglevel": "none"
"loglevel": "debug"
},
"inbound": {
"listen": "127.0.0.1",
"protocol": "vmess",
"allowPassive": true,
"settings": {
"clients": [
{
@ -31,6 +32,7 @@
"listen": "127.0.0.1",
"port": "50005-50009",
"tag": "detour",
"allowPassive": true,
"settings": {
"clients": [
{

View File

@ -83,6 +83,63 @@ func TestTCPConnection(t *testing.T) {
CloseAllServers()
}
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()
}
func TestTCPBind(t *testing.T) {
assert := assert.On(t)

View File

@ -10,6 +10,7 @@ import (
type Server struct {
Port v2net.Port
MsgProcessor func(msg []byte) []byte
SendFirst []byte
accepting bool
listener *net.TCPListener
}
@ -44,6 +45,9 @@ func (server *Server) acceptConnections(listener *net.TCPListener) {
}
func (server *Server) handleConnection(conn net.Conn) {
if len(server.SendFirst) > 0 {
conn.Write(server.SendFirst)
}
request := make([]byte, 4096)
for true {
nBytes, err := conn.Read(request)