| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561 |
- package scenarios
- import (
- "context"
- "fmt"
- "io"
- "strings"
- "testing"
- "time"
- "github.com/google/go-cmp/cmp"
- "github.com/google/go-cmp/cmp/cmpopts"
- "github.com/xtls/xray-core/app/commander"
- "github.com/xtls/xray-core/app/policy"
- "github.com/xtls/xray-core/app/proxyman"
- "github.com/xtls/xray-core/app/proxyman/command"
- "github.com/xtls/xray-core/app/router"
- "github.com/xtls/xray-core/app/stats"
- statscmd "github.com/xtls/xray-core/app/stats/command"
- "github.com/xtls/xray-core/common"
- "github.com/xtls/xray-core/common/net"
- "github.com/xtls/xray-core/common/protocol"
- "github.com/xtls/xray-core/common/serial"
- "github.com/xtls/xray-core/common/uuid"
- core "github.com/xtls/xray-core/core"
- "github.com/xtls/xray-core/proxy/dokodemo"
- "github.com/xtls/xray-core/proxy/freedom"
- "github.com/xtls/xray-core/proxy/vmess"
- "github.com/xtls/xray-core/proxy/vmess/inbound"
- "github.com/xtls/xray-core/proxy/vmess/outbound"
- "github.com/xtls/xray-core/testing/servers/tcp"
- "google.golang.org/grpc"
- "google.golang.org/grpc/credentials/insecure"
- )
- func TestCommanderListenConfigurationItem(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",
- Listen: fmt.Sprintf("127.0.0.1:%d", cmdPort),
- Service: []*serial.TypedMessage{
- serial.ToTypedMessage(&command.Config{}),
- },
- }),
- },
- 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},
- }),
- },
- },
- Outbound: []*core.OutboundHandlerConfig{
- {
- Tag: "default-outbound",
- 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)
- resp, err := hsClient.RemoveInbound(context.Background(), &command.RemoveInboundRequest{
- Tag: "d",
- })
- common.Must(err)
- if resp == nil {
- t.Error("unexpected nil response")
- }
- {
- _, err := net.DialTCP("tcp", nil, &net.TCPAddr{
- IP: []byte{127, 0, 0, 1},
- Port: int(clientPort),
- })
- if err == nil {
- t.Error("unexpected nil error")
- }
- }
- }
- func TestCommanderRemoveHandler(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",
- 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)
- resp, err := hsClient.RemoveInbound(context.Background(), &command.RemoveInboundRequest{
- Tag: "d",
- })
- common.Must(err)
- if resp == nil {
- t.Error("unexpected nil response")
- }
- {
- _, err := net.DialTCP("tcp", nil, &net.TCPAddr{
- IP: []byte{127, 0, 0, 1},
- Port: int(clientPort),
- })
- if err == nil {
- t.Error("unexpected nil error")
- }
- }
- }
- func TestCommanderAddRemoveUser(t *testing.T) {
- tcpServer := tcp.Server{
- MsgProcessor: xor,
- }
- dest, err := tcpServer.Start()
- common.Must(err)
- defer tcpServer.Close()
- u1 := protocol.NewID(uuid.New())
- u2 := protocol.NewID(uuid.New())
- cmdPort := tcp.PickPort()
- serverPort := tcp.PickPort()
- serverConfig := &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",
- },
- },
- },
- }),
- serial.ToTypedMessage(&policy.Config{
- Level: map[uint32]*policy.Policy{
- 0: {
- Timeout: &policy.Policy_Timeout{
- UplinkOnly: &policy.Second{Value: 0},
- DownlinkOnly: &policy.Second{Value: 0},
- },
- },
- },
- }),
- },
- Inbound: []*core.InboundHandlerConfig{
- {
- Tag: "v",
- ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
- PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}},
- Listen: net.NewIPOrDomain(net.LocalHostIP),
- }),
- ProxySettings: serial.ToTypedMessage(&inbound.Config{
- User: []*protocol.User{
- {
- Account: serial.ToTypedMessage(&vmess.Account{
- Id: u1.String(),
- }),
- },
- },
- }),
- },
- {
- 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{
- {
- ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
- },
- },
- }
- clientPort := tcp.PickPort()
- clientConfig := &core.Config{
- App: []*serial.TypedMessage{
- serial.ToTypedMessage(&policy.Config{
- Level: map[uint32]*policy.Policy{
- 0: {
- Timeout: &policy.Policy_Timeout{
- UplinkOnly: &policy.Second{Value: 0},
- DownlinkOnly: &policy.Second{Value: 0},
- },
- },
- },
- }),
- },
- 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},
- }),
- },
- },
- Outbound: []*core.OutboundHandlerConfig{
- {
- ProxySettings: serial.ToTypedMessage(&outbound.Config{
- Receiver: []*protocol.ServerEndpoint{
- {
- Address: net.NewIPOrDomain(net.LocalHostIP),
- Port: uint32(serverPort),
- User: []*protocol.User{
- {
- Account: serial.ToTypedMessage(&vmess.Account{
- Id: u2.String(),
- SecuritySettings: &protocol.SecurityConfig{
- Type: protocol.SecurityType_AES128_GCM,
- },
- }),
- },
- },
- },
- },
- }),
- },
- },
- }
- servers, err := InitializeServerConfigs(serverConfig, clientConfig)
- common.Must(err)
- defer CloseAllServers(servers)
- if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != io.EOF &&
- /*We might wish to drain the connection*/
- (err != nil && !strings.HasSuffix(err.Error(), "i/o timeout")) {
- t.Fatal("expected error: ", 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)
- resp, err := hsClient.AlterInbound(context.Background(), &command.AlterInboundRequest{
- Tag: "v",
- Operation: serial.ToTypedMessage(
- &command.AddUserOperation{
- User: &protocol.User{
- Email: "[email protected]",
- Account: serial.ToTypedMessage(&vmess.Account{
- Id: u2.String(),
- }),
- },
- }),
- })
- common.Must(err)
- if resp == nil {
- t.Fatal("nil response")
- }
- if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != nil {
- t.Fatal(err)
- }
- resp, err = hsClient.AlterInbound(context.Background(), &command.AlterInboundRequest{
- Tag: "v",
- Operation: serial.ToTypedMessage(&command.RemoveUserOperation{Email: "[email protected]"}),
- })
- common.Must(err)
- if resp == nil {
- t.Fatal("nil response")
- }
- }
- func TestCommanderStats(t *testing.T) {
- tcpServer := tcp.Server{
- MsgProcessor: xor,
- }
- dest, err := tcpServer.Start()
- common.Must(err)
- defer tcpServer.Close()
- userID := protocol.NewID(uuid.New())
- serverPort := tcp.PickPort()
- cmdPort := tcp.PickPort()
- serverConfig := &core.Config{
- App: []*serial.TypedMessage{
- serial.ToTypedMessage(&stats.Config{}),
- serial.ToTypedMessage(&commander.Config{
- Tag: "api",
- Service: []*serial.TypedMessage{
- serial.ToTypedMessage(&statscmd.Config{}),
- },
- }),
- serial.ToTypedMessage(&router.Config{
- Rule: []*router.RoutingRule{
- {
- InboundTag: []string{"api"},
- TargetTag: &router.RoutingRule_Tag{
- Tag: "api",
- },
- },
- },
- }),
- serial.ToTypedMessage(&policy.Config{
- Level: map[uint32]*policy.Policy{
- 0: {
- Timeout: &policy.Policy_Timeout{
- UplinkOnly: &policy.Second{Value: 0},
- DownlinkOnly: &policy.Second{Value: 0},
- },
- },
- 1: {
- Stats: &policy.Policy_Stats{
- UserUplink: true,
- UserDownlink: true,
- },
- },
- },
- System: &policy.SystemPolicy{
- Stats: &policy.SystemPolicy_Stats{
- InboundUplink: true,
- },
- },
- }),
- },
- Inbound: []*core.InboundHandlerConfig{
- {
- Tag: "vmess",
- ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
- PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}},
- Listen: net.NewIPOrDomain(net.LocalHostIP),
- }),
- ProxySettings: serial.ToTypedMessage(&inbound.Config{
- User: []*protocol.User{
- {
- Level: 1,
- Email: "test",
- Account: serial.ToTypedMessage(&vmess.Account{
- Id: userID.String(),
- }),
- },
- },
- }),
- },
- {
- 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{
- {
- ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
- },
- },
- }
- clientPort := tcp.PickPort()
- clientConfig := &core.Config{
- Inbound: []*core.InboundHandlerConfig{
- {
- 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},
- }),
- },
- },
- Outbound: []*core.OutboundHandlerConfig{
- {
- ProxySettings: serial.ToTypedMessage(&outbound.Config{
- Receiver: []*protocol.ServerEndpoint{
- {
- Address: net.NewIPOrDomain(net.LocalHostIP),
- Port: uint32(serverPort),
- User: []*protocol.User{
- {
- Account: serial.ToTypedMessage(&vmess.Account{
- Id: userID.String(),
- SecuritySettings: &protocol.SecurityConfig{
- Type: protocol.SecurityType_AES128_GCM,
- },
- }),
- },
- },
- },
- },
- }),
- },
- },
- }
- servers, err := InitializeServerConfigs(serverConfig, clientConfig)
- if err != nil {
- t.Fatal("Failed to create all servers", err)
- }
- defer CloseAllServers(servers)
- if err := testTCPConn(clientPort, 10240*1024, time.Second*20)(); 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()
- const name = "user>>>test>>>traffic>>>uplink"
- sClient := statscmd.NewStatsServiceClient(cmdConn)
- sresp, err := sClient.GetStats(context.Background(), &statscmd.GetStatsRequest{
- Name: name,
- Reset_: true,
- })
- common.Must(err)
- if r := cmp.Diff(sresp.Stat, &statscmd.Stat{
- Name: name,
- Value: 10240 * 1024,
- }, cmpopts.IgnoreUnexported(statscmd.Stat{})); r != "" {
- t.Error(r)
- }
- sresp, err = sClient.GetStats(context.Background(), &statscmd.GetStatsRequest{
- Name: name,
- })
- common.Must(err)
- if r := cmp.Diff(sresp.Stat, &statscmd.Stat{
- Name: name,
- Value: 0,
- }, cmpopts.IgnoreUnexported(statscmd.Stat{})); r != "" {
- t.Error(r)
- }
- sresp, err = sClient.GetStats(context.Background(), &statscmd.GetStatsRequest{
- Name: "inbound>>>vmess>>>traffic>>>uplink",
- Reset_: true,
- })
- common.Must(err)
- if sresp.Stat.Value <= 10240*1024 {
- t.Error("value < 10240*1024: ", sresp.Stat.Value)
- }
- }
|