github.com/imannamdari/v2ray-core/v5@v5.0.5/main/commands/all/api/balancer_override.go (about)

     1  package api
     2  
     3  import (
     4  	routerService "github.com/imannamdari/v2ray-core/v5/app/router/command"
     5  	"github.com/imannamdari/v2ray-core/v5/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 selection is overridden:
    19  
    20  - The balancer's selection result will always be outboundTag
    21  
    22  Arguments:
    23  
    24  	-b, -balancer <tag>
    25  		Tag of the target balancer. Required.
    26  
    27  	-r, -remove
    28  		Remove the override
    29  
    30  	-s, -server <server:port>
    31  		The API server address. Default 127.0.0.1:8080
    32  
    33  	-t, -timeout <seconds>
    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 override balancer: %s", err)
    76  	}
    77  }