github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/main/commands/all/api/outbounds_remove.go (about)

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  
     6  	handlerService "github.com/xmplusdev/xmcore/app/proxyman/command"
     7  	"github.com/xmplusdev/xmcore/infra/conf/serial"
     8  	"github.com/xmplusdev/xmcore/main/commands/base"
     9  )
    10  
    11  var cmdRemoveOutbounds = &base.Command{
    12  	CustomFlags: true,
    13  	UsageLine:   "{{.Exec}} api rmo [--server=127.0.0.1:8080] <json_file|tag> [json_file] [tag]...",
    14  	Short:       "Remove outbounds",
    15  	Long: `
    16  Remove outbounds from Xray.
    17  Arguments:
    18  	-s, -server 
    19  		The API server address. Default 127.0.0.1:8080
    20  	-t, -timeout
    21  		Timeout seconds to call API. Default 3
    22  Example:
    23      {{.Exec}} {{.LongName}} --server=127.0.0.1:8080 c1.json "tag name"
    24  `,
    25  	Run: executeRemoveOutbounds,
    26  }
    27  
    28  func executeRemoveOutbounds(cmd *base.Command, args []string) {
    29  	setSharedFlags(cmd)
    30  	cmd.Flag.Parse(args)
    31  	unnamedArgs := cmd.Flag.Args()
    32  	if len(unnamedArgs) == 0 {
    33  		fmt.Println("reading from stdin:")
    34  		unnamedArgs = []string{"stdin:"}
    35  	}
    36  
    37  	tags := make([]string, 0)
    38  	for _, arg := range unnamedArgs {
    39  		if r, err := loadArg(arg); err == nil {
    40  			conf, err := serial.DecodeJSONConfig(r)
    41  			if err != nil {
    42  				base.Fatalf("failed to decode %s: %s", arg, err)
    43  			}
    44  			outs := conf.OutboundConfigs
    45  			for _, o := range outs {
    46  				tags = append(tags, o.Tag)
    47  			}
    48  		} else {
    49  			// take request as tag
    50  			tags = append(tags, arg)
    51  		}
    52  	}
    53  
    54  	if len(tags) == 0 {
    55  		base.Fatalf("no outbound to remove")
    56  	}
    57  
    58  	conn, ctx, close := dialAPIServer()
    59  	defer close()
    60  
    61  	client := handlerService.NewHandlerServiceClient(conn)
    62  	for _, tag := range tags {
    63  		fmt.Println("removing:", tag)
    64  		r := &handlerService.RemoveOutboundRequest{
    65  			Tag: tag,
    66  		}
    67  		resp, err := client.RemoveOutbound(ctx, r)
    68  		if err != nil {
    69  			base.Fatalf("failed to remove outbound: %s", err)
    70  		}
    71  		showJSONResponse(resp)
    72  	}
    73  }