2021-01-29 19:31:11 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-01-02 10:16:23 -05:00
|
|
|
routerService "github.com/v2fly/v2ray-core/v5/app/router/command"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/main/commands/base"
|
2021-01-29 19:31:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var cmdBalancerOverride = &base.Command{
|
|
|
|
CustomFlags: true,
|
2021-06-19 03:57:40 -04:00
|
|
|
UsageLine: "{{.Exec}} api bo [--server=127.0.0.1:8080] <-b balancer> outboundTag",
|
2021-02-21 10:02:42 -05:00
|
|
|
Short: "balancer override",
|
2021-01-29 19:31:11 -05:00
|
|
|
Long: `
|
2021-06-19 03:57:40 -04:00
|
|
|
Override a balancer's selection.
|
2021-01-29 19:31:11 -05:00
|
|
|
|
|
|
|
> Make sure you have "RoutingService" set in "config.api.services"
|
|
|
|
of server config.
|
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
Once a balancer's selection is overridden:
|
2021-01-29 19:31:11 -05:00
|
|
|
|
2021-06-19 03:57:40 -04:00
|
|
|
- The balancer's selection result will always be outboundTag
|
2021-01-29 19:31:11 -05:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
-b, -balancer <tag>
|
|
|
|
Tag of the target balancer. Required.
|
2021-01-29 19:31:11 -05:00
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
-r, -remove
|
|
|
|
Remove the override
|
2021-01-29 19:31:11 -05:00
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
-s, -server <server:port>
|
2021-01-29 19:31:11 -05:00
|
|
|
The API server address. Default 127.0.0.1:8080
|
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
-t, -timeout <seconds>
|
2021-01-29 19:31:11 -05:00
|
|
|
Timeout seconds to call API. Default 3
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2021-06-19 03:57:40 -04:00
|
|
|
{{.Exec}} {{.LongName}} --server=127.0.0.1:8080 -b balancer tag
|
2021-01-29 19:31:11 -05:00
|
|
|
{{.Exec}} {{.LongName}} --server=127.0.0.1:8080 -b balancer -r
|
|
|
|
`,
|
|
|
|
Run: executeBalancerOverride,
|
|
|
|
}
|
|
|
|
|
|
|
|
func executeBalancerOverride(cmd *base.Command, args []string) {
|
|
|
|
var (
|
|
|
|
balancer string
|
|
|
|
remove bool
|
|
|
|
)
|
|
|
|
cmd.Flag.StringVar(&balancer, "b", "", "")
|
|
|
|
cmd.Flag.StringVar(&balancer, "balancer", "", "")
|
|
|
|
cmd.Flag.BoolVar(&remove, "r", false, "")
|
|
|
|
cmd.Flag.BoolVar(&remove, "remove", false, "")
|
|
|
|
setSharedFlags(cmd)
|
|
|
|
cmd.Flag.Parse(args)
|
|
|
|
|
|
|
|
if balancer == "" {
|
|
|
|
base.Fatalf("balancer tag not specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, ctx, close := dialAPIServer()
|
|
|
|
defer close()
|
|
|
|
|
2021-06-19 03:57:40 -04:00
|
|
|
client := routerService.NewRoutingServiceClient(conn)
|
|
|
|
target := ""
|
2021-01-29 19:31:11 -05:00
|
|
|
if !remove {
|
2021-06-19 03:57:40 -04:00
|
|
|
target = cmd.Flag.Args()[0]
|
2021-01-29 19:31:11 -05:00
|
|
|
}
|
2021-06-19 03:57:40 -04:00
|
|
|
r := &routerService.OverrideBalancerTargetRequest{
|
2021-01-29 19:31:11 -05:00
|
|
|
BalancerTag: balancer,
|
2021-06-19 03:57:40 -04:00
|
|
|
Target: target,
|
2021-01-29 19:31:11 -05:00
|
|
|
}
|
2021-06-19 03:57:40 -04:00
|
|
|
|
|
|
|
_, err := client.OverrideBalancerTarget(ctx, r)
|
2021-01-29 19:31:11 -05:00
|
|
|
if err != nil {
|
2021-02-21 10:02:42 -05:00
|
|
|
base.Fatalf("failed to override balancer: %s", err)
|
2021-01-29 19:31:11 -05:00
|
|
|
}
|
|
|
|
}
|