mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-30 05:56:54 -05:00
migrate router test to proto
This commit is contained in:
parent
2eb313d0c2
commit
5013d387a5
@ -31,12 +31,11 @@ func New(space app.Space, config *Config, meta *proxy.OutboundHandlerMeta) (prox
|
||||
// Dispatch implements OutboundHandler.Dispatch().
|
||||
func (v *Handler) Dispatch(destination v2net.Destination, ray ray.OutboundRay) {
|
||||
v.response.WriteTo(ray.OutboundOutput())
|
||||
ray.OutboundOutput().Close()
|
||||
|
||||
// CloseError() will immediately close the connection.
|
||||
// Sleep a little here to make sure the response is sent to client.
|
||||
time.Sleep(time.Millisecond * 500)
|
||||
ray.OutboundInput().CloseError()
|
||||
ray.OutboundOutput().CloseError()
|
||||
}
|
||||
|
||||
// Factory is an utility for creating blackhole handlers.
|
||||
|
@ -1,62 +0,0 @@
|
||||
{
|
||||
"port": 50020,
|
||||
"log": {
|
||||
"loglevel": "none"
|
||||
},
|
||||
"inbound": {
|
||||
"listen": "127.0.0.1",
|
||||
"protocol": "dokodemo-door",
|
||||
"settings": {
|
||||
"address": "127.0.0.1",
|
||||
"port": 50024,
|
||||
"network": "tcp",
|
||||
"timeout": 0
|
||||
}
|
||||
},
|
||||
"outbound": {
|
||||
"protocol": "vmess",
|
||||
"settings": {
|
||||
"vnext": [
|
||||
{
|
||||
"address": "127.0.0.1",
|
||||
"port": 50021,
|
||||
"users": [
|
||||
{"id": "d17a1af7-efa5-42ca-b7e9-6a35282d737f"}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"inboundDetour": [
|
||||
{
|
||||
"protocol": "dokodemo-door",
|
||||
"port": 50022,
|
||||
"listen": "127.0.0.1",
|
||||
"settings": {
|
||||
"address": "127.0.0.1",
|
||||
"port": 50025,
|
||||
"network": "tcp",
|
||||
"timeout": 0
|
||||
}
|
||||
}
|
||||
],
|
||||
"outboundDetour": [
|
||||
{
|
||||
"protocol": "blackhole",
|
||||
"tag": "blocked",
|
||||
"settings": {}
|
||||
}
|
||||
],
|
||||
"routing": {
|
||||
"strategy": "rules",
|
||||
"settings": {
|
||||
"rules": [
|
||||
{
|
||||
"type": "field",
|
||||
"port": "50025-50029",
|
||||
"outboundTag": "blocked"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
{
|
||||
"port": 50021,
|
||||
"inbound": {
|
||||
"listen": "127.0.0.1",
|
||||
"protocol": "vmess",
|
||||
"settings": {
|
||||
"clients": [
|
||||
{
|
||||
"id": "d17a1af7-efa5-42ca-b7e9-6a35282d737f",
|
||||
"level": 1
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"outbound": {
|
||||
"protocol": "freedom",
|
||||
"settings": {}
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"v2ray.com/core"
|
||||
"v2ray.com/core/app/router"
|
||||
v2net "v2ray.com/core/common/net"
|
||||
"v2ray.com/core/common/protocol"
|
||||
"v2ray.com/core/common/serial"
|
||||
@ -240,13 +241,20 @@ func TestBlackhole(t *testing.T) {
|
||||
assert.Error(err).IsNil()
|
||||
defer tcpServer.Close()
|
||||
|
||||
tcpServer2 := tcp.Server{
|
||||
MsgProcessor: xor,
|
||||
}
|
||||
dest2, err := tcpServer2.Start()
|
||||
assert.Error(err).IsNil()
|
||||
defer tcpServer2.Close()
|
||||
|
||||
serverPort := pickPort()
|
||||
serverPort2 := pickPort()
|
||||
serverConfig := &core.Config{
|
||||
Inbound: []*core.InboundConnectionConfig{
|
||||
{
|
||||
PortRange: v2net.SinglePortRange(serverPort),
|
||||
ListenOn: v2net.NewIPOrDomain(v2net.LocalHostIP),
|
||||
AllowPassiveConnection: true,
|
||||
PortRange: v2net.SinglePortRange(serverPort),
|
||||
ListenOn: v2net.NewIPOrDomain(v2net.LocalHostIP),
|
||||
Settings: serial.ToTypedMessage(&dokodemo.Config{
|
||||
Address: v2net.NewIPOrDomain(dest.Address),
|
||||
Port: uint32(dest.Port),
|
||||
@ -255,19 +263,45 @@ func TestBlackhole(t *testing.T) {
|
||||
},
|
||||
}),
|
||||
},
|
||||
{
|
||||
PortRange: v2net.SinglePortRange(serverPort2),
|
||||
ListenOn: v2net.NewIPOrDomain(v2net.LocalHostIP),
|
||||
Settings: serial.ToTypedMessage(&dokodemo.Config{
|
||||
Address: v2net.NewIPOrDomain(dest2.Address),
|
||||
Port: uint32(dest2.Port),
|
||||
NetworkList: &v2net.NetworkList{
|
||||
Network: []v2net.Network{v2net.Network_TCP},
|
||||
},
|
||||
}),
|
||||
},
|
||||
},
|
||||
Outbound: []*core.OutboundConnectionConfig{
|
||||
{
|
||||
Tag: "direct",
|
||||
Settings: serial.ToTypedMessage(&freedom.Config{}),
|
||||
},
|
||||
{
|
||||
Tag: "blocked",
|
||||
Settings: serial.ToTypedMessage(&blackhole.Config{}),
|
||||
},
|
||||
},
|
||||
App: []*serial.TypedMessage{
|
||||
serial.ToTypedMessage(&router.Config{
|
||||
Rule: []*router.RoutingRule{
|
||||
{
|
||||
Tag: "blocked",
|
||||
PortRange: v2net.SinglePortRange(dest2.Port),
|
||||
},
|
||||
},
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
assert.Error(InitializeServerConfig(serverConfig)).IsNil()
|
||||
|
||||
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
|
||||
IP: []byte{127, 0, 0, 1},
|
||||
Port: int(serverPort),
|
||||
Port: int(serverPort2),
|
||||
})
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
|
@ -1,82 +0,0 @@
|
||||
package scenarios
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
v2net "v2ray.com/core/common/net"
|
||||
"v2ray.com/core/testing/assert"
|
||||
"v2ray.com/core/testing/servers/tcp"
|
||||
)
|
||||
|
||||
func TestRouter(t *testing.T) {
|
||||
assert := assert.On(t)
|
||||
|
||||
tcpServer := &tcp.Server{
|
||||
Port: v2net.Port(50024),
|
||||
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()
|
||||
defer tcpServer.Close()
|
||||
|
||||
tcpServer2Accessed := false
|
||||
tcpServer2 := &tcp.Server{
|
||||
Port: v2net.Port(50025),
|
||||
MsgProcessor: func(data []byte) []byte {
|
||||
tcpServer2Accessed = true
|
||||
return data
|
||||
},
|
||||
}
|
||||
_, err = tcpServer2.Start()
|
||||
assert.Error(err).IsNil()
|
||||
defer tcpServer2.Close()
|
||||
|
||||
assert.Error(InitializeServerSetOnce("test_3")).IsNil()
|
||||
|
||||
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
|
||||
IP: []byte{127, 0, 0, 1},
|
||||
Port: int(50020),
|
||||
})
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
payload := "direct dokodemo request."
|
||||
nBytes, err := conn.Write([]byte(payload))
|
||||
assert.Error(err).IsNil()
|
||||
assert.Int(nBytes).Equals(len(payload))
|
||||
|
||||
conn.CloseWrite()
|
||||
|
||||
response := make([]byte, 1024)
|
||||
nBytes, err = conn.Read(response)
|
||||
assert.Error(err).IsNil()
|
||||
assert.String("Processed: " + payload).Equals(string(response[:nBytes]))
|
||||
conn.Close()
|
||||
|
||||
conn, err = net.DialTCP("tcp", nil, &net.TCPAddr{
|
||||
IP: []byte{127, 0, 0, 1},
|
||||
Port: int(50022),
|
||||
})
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
payload = "blocked dokodemo request."
|
||||
nBytes, err = conn.Write([]byte(payload))
|
||||
assert.Error(err).IsNil()
|
||||
assert.Int(nBytes).Equals(len(payload))
|
||||
|
||||
conn.CloseWrite()
|
||||
|
||||
response = make([]byte, 1024)
|
||||
nBytes, err = conn.Read(response)
|
||||
assert.Error(err).IsNotNil()
|
||||
assert.Int(nBytes).Equals(0)
|
||||
assert.Bool(tcpServer2Accessed).IsFalse()
|
||||
conn.Close()
|
||||
|
||||
CloseAllServers()
|
||||
}
|
Loading…
Reference in New Issue
Block a user