Write a test covering both methods and ignore commander when listing outbounds.

This commit is contained in:
Sergey Gorbunov 2025-05-14 16:08:40 +03:00
parent 1f014eb655
commit 3af7a19c06
No known key found for this signature in database
GPG key ID: A8C3D74066B8E768
2 changed files with 104 additions and 0 deletions

View file

@ -3,6 +3,7 @@ package command
import ( import (
"context" "context"
"github.com/xtls/xray-core/app/commander"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/protocol" "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) handlers := s.ohm.ListHandlers(ctx)
response := &ListOutboundsResponse{} response := &ListOutboundsResponse{}
for _, handler := range handlers { for _, handler := range handlers {
// Ignore gRPC outbound
if _, ok := handler.(*commander.Outbound); ok {
continue
}
response.Outbounds = append(response.Outbounds, &core.OutboundHandlerConfig{ response.Outbounds = append(response.Outbounds, &core.OutboundHandlerConfig{
Tag: handler.Tag(), Tag: handler.Tag(),
SenderSettings: handler.SenderSettings(), SenderSettings: handler.SenderSettings(),

View file

@ -31,6 +31,7 @@ import (
"github.com/xtls/xray-core/testing/servers/tcp" "github.com/xtls/xray-core/testing/servers/tcp"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/testing/protocmp"
) )
func TestCommanderListenConfigurationItem(t *testing.T) { 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) { func TestCommanderAddRemoveUser(t *testing.T) {
tcpServer := tcp.Server{ tcpServer := tcp.Server{
MsgProcessor: xor, MsgProcessor: xor,