diff --git a/proxy/blackhole/blackhole.go b/proxy/blackhole/blackhole.go index 59766dbdc..58359c9de 100644 --- a/proxy/blackhole/blackhole.go +++ b/proxy/blackhole/blackhole.go @@ -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. diff --git a/testing/scenarios/data/test_3_client.json b/testing/scenarios/data/test_3_client.json deleted file mode 100644 index 80d052c47..000000000 --- a/testing/scenarios/data/test_3_client.json +++ /dev/null @@ -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" - } - ] - } - } -} diff --git a/testing/scenarios/data/test_3_server.json b/testing/scenarios/data/test_3_server.json deleted file mode 100644 index 0e38ad10e..000000000 --- a/testing/scenarios/data/test_3_server.json +++ /dev/null @@ -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": {} - } -} diff --git a/testing/scenarios/feature_test.go b/testing/scenarios/feature_test.go index a0b7e2b6f..18a5b8170 100644 --- a/testing/scenarios/feature_test.go +++ b/testing/scenarios/feature_test.go @@ -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() diff --git a/testing/scenarios/router_test.go b/testing/scenarios/router_test.go deleted file mode 100644 index af85f3383..000000000 --- a/testing/scenarios/router_test.go +++ /dev/null @@ -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() -}