From 3af7a19c0696c140da4a3e2e934ac905846475b7 Mon Sep 17 00:00:00 2001 From: Sergey Gorbunov Date: Wed, 14 May 2025 16:08:40 +0300 Subject: [PATCH] Write a test covering both methods and ignore commander when listing outbounds. --- app/proxyman/command/command.go | 5 ++ testing/scenarios/command_test.go | 99 +++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/app/proxyman/command/command.go b/app/proxyman/command/command.go index 842580ee..227a95c3 100644 --- a/app/proxyman/command/command.go +++ b/app/proxyman/command/command.go @@ -3,6 +3,7 @@ package command import ( "context" + "github.com/xtls/xray-core/app/commander" "github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/protocol" @@ -181,6 +182,10 @@ func (s *handlerServer) ListOutbounds(ctx context.Context, request *ListOutbound handlers := s.ohm.ListHandlers(ctx) response := &ListOutboundsResponse{} for _, handler := range handlers { + // Ignore gRPC outbound + if _, ok := handler.(*commander.Outbound); ok { + continue + } response.Outbounds = append(response.Outbounds, &core.OutboundHandlerConfig{ Tag: handler.Tag(), SenderSettings: handler.SenderSettings(), diff --git a/testing/scenarios/command_test.go b/testing/scenarios/command_test.go index 037255a9..e562952b 100644 --- a/testing/scenarios/command_test.go +++ b/testing/scenarios/command_test.go @@ -31,6 +31,7 @@ import ( "github.com/xtls/xray-core/testing/servers/tcp" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" + "google.golang.org/protobuf/testing/protocmp" ) func TestCommanderListenConfigurationItem(t *testing.T) { @@ -202,6 +203,104 @@ func TestCommanderRemoveHandler(t *testing.T) { } } +func TestCommanderListHandlers(t *testing.T) { + tcpServer := tcp.Server{ + MsgProcessor: xor, + } + dest, err := tcpServer.Start() + common.Must(err) + defer tcpServer.Close() + + clientPort := tcp.PickPort() + cmdPort := tcp.PickPort() + clientConfig := &core.Config{ + App: []*serial.TypedMessage{ + serial.ToTypedMessage(&commander.Config{ + Tag: "api", + Service: []*serial.TypedMessage{ + serial.ToTypedMessage(&command.Config{}), + }, + }), + serial.ToTypedMessage(&router.Config{ + Rule: []*router.RoutingRule{ + { + InboundTag: []string{"api"}, + TargetTag: &router.RoutingRule_Tag{ + Tag: "api", + }, + }, + }, + }), + }, + Inbound: []*core.InboundHandlerConfig{ + { + Tag: "d", + ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ + PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}}, + Listen: net.NewIPOrDomain(net.LocalHostIP), + }), + ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ + Address: net.NewIPOrDomain(dest.Address), + Port: uint32(dest.Port), + Networks: []net.Network{net.Network_TCP}, + }), + }, + { + Tag: "api", + ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ + PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(cmdPort)}}, + Listen: net.NewIPOrDomain(net.LocalHostIP), + }), + ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ + Address: net.NewIPOrDomain(dest.Address), + Port: uint32(dest.Port), + Networks: []net.Network{net.Network_TCP}, + }), + }, + }, + Outbound: []*core.OutboundHandlerConfig{ + { + Tag: "default-outbound", + SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{}), + ProxySettings: serial.ToTypedMessage(&freedom.Config{}), + }, + }, + } + + servers, err := InitializeServerConfigs(clientConfig) + common.Must(err) + defer CloseAllServers(servers) + + if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != nil { + t.Fatal(err) + } + + cmdConn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", cmdPort), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock()) + common.Must(err) + defer cmdConn.Close() + + hsClient := command.NewHandlerServiceClient(cmdConn) + inboundResp, err := hsClient.ListInbounds(context.Background(), &command.ListInboundsRequest{}) + common.Must(err) + if inboundResp == nil { + t.Error("unexpected nil response") + } + + if !cmp.Equal(inboundResp.Inbounds, clientConfig.Inbound, protocmp.Transform()) { + t.Fatal("inbound response doesn't match config") + } + + outboundResp, err := hsClient.ListOutbounds(context.Background(), &command.ListOutboundsRequest{}) + common.Must(err) + if outboundResp == nil { + t.Error("unexpected nil response") + } + + if !cmp.Equal(outboundResp.Outbounds, clientConfig.Outbound, protocmp.Transform()) { + t.Fatal("outbound response doesn't match config") + } +} + func TestCommanderAddRemoveUser(t *testing.T) { tcpServer := tcp.Server{ MsgProcessor: xor,