github.com/xmplusdev/xray-core@v1.8.10/main/commands/all/api/balancer_override.go (about)

     1  package api
     2  
     3  import (
     4  	routerService "github.com/xmplusdev/xray-core/app/router/command"
     5  	"github.com/xmplusdev/xray-core/main/commands/base"
     6  )
     7  
     8  var cmdBalancerOverride = &base.Command{
     9  	CustomFlags: true,
    10  	UsageLine:   "{{.Exec}} api bo [--server=127.0.0.1:8080] <-b balancer> outboundTag",
    11  	Short:       "balancer override",
    12  	Long: `
    13  Override a balancer's selection.
    14  
    15  > Make sure you have "RoutingService" set in "config.api.services" 
    16  of server config.
    17  
    18  Once a balancer's selecting is overridden:
    19  
    20  - The balancer's selection result will always be outboundTag
    21  
    22  Arguments:
    23  
    24  	-r, -remove
    25  		Remove the overridden
    26  
    27  	-r, -remove
    28  		Remove the override
    29  
    30  	-s, -server 
    31  		The API server address. Default 127.0.0.1:8080
    32  
    33  	-t, -timeout
    34  		Timeout seconds to call API. Default 3
    35  
    36  Example:
    37  
    38      {{.Exec}} {{.LongName}} --server=127.0.0.1:8080 -b balancer tag
    39      {{.Exec}} {{.LongName}} --server=127.0.0.1:8080 -b balancer -r
    40  `,
    41  	Run: executeBalancerOverride,
    42  }
    43  
    44  func executeBalancerOverride(cmd *base.Command, args []string) {
    45  	var (
    46  		balancer string
    47  		remove   bool
    48  	)
    49  	cmd.Flag.StringVar(&balancer, "b", "", "")
    50  	cmd.Flag.StringVar(&balancer, "balancer", "", "")
    51  	cmd.Flag.BoolVar(&remove, "r", false, "")
    52  	cmd.Flag.BoolVar(&remove, "remove", false, "")
    53  	setSharedFlags(cmd)
    54  	cmd.Flag.Parse(args)
    55  
    56  	if balancer == "" {
    57  		base.Fatalf("balancer tag not specified")
    58  	}
    59  
    60  	conn, ctx, close := dialAPIServer()
    61  	defer close()
    62  
    63  	client := routerService.NewRoutingServiceClient(conn)
    64  	target := ""
    65  	if !remove {
    66  		target = cmd.Flag.Args()[0]
    67  	}
    68  	r := &routerService.OverrideBalancerTargetRequest{
    69  		BalancerTag: balancer,
    70  		Target:      target,
    71  	}
    72  
    73  	_, err := client.OverrideBalancerTarget(ctx, r)
    74  	if err != nil {
    75  		base.Fatalf("failed to perform balancer health checks: %s", err)
    76  	}
    77  }