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

     1  package jsonv4
     2  
     3  import (
     4  	"fmt"
     5  
     6  	handlerService "github.com/imannamdari/v2ray-core/v5/app/proxyman/command"
     7  	"github.com/imannamdari/v2ray-core/v5/main/commands/all/api"
     8  	"github.com/imannamdari/v2ray-core/v5/main/commands/base"
     9  	"github.com/imannamdari/v2ray-core/v5/main/commands/helpers"
    10  )
    11  
    12  var cmdRemoveInbounds = &base.Command{
    13  	CustomFlags: true,
    14  	UsageLine:   "{{.Exec}} api rmi [--server=127.0.0.1:8080] [c1.json] [dir1]...",
    15  	Short:       "remove inbounds",
    16  	Long: `
    17  Remove inbounds from V2Ray.
    18  
    19  > Make sure you have "HandlerService" set in "config.api.services" 
    20  of server config.
    21  
    22  Arguments:
    23  
    24  	-format <format>
    25  		The input format.
    26  		Available values: "auto", "json", "toml", "yaml"
    27  		Default: "auto"
    28  
    29  	-r
    30  		Load folders recursively.
    31  
    32  	-tags
    33  		The input are tags instead of config files
    34  
    35  	-s, -server <server:port>
    36  		The API server address. Default 127.0.0.1:8080
    37  
    38  	-t, -timeout <seconds>
    39  		Timeout seconds to call API. Default 3
    40  
    41  Example:
    42  
    43      {{.Exec}} {{.LongName}} dir
    44      {{.Exec}} {{.LongName}} c1.json c2.yaml
    45      {{.Exec}} {{.LongName}} -tags tag1 tag2
    46  `,
    47  	Run: executeRemoveInbounds,
    48  }
    49  
    50  func executeRemoveInbounds(cmd *base.Command, args []string) {
    51  	api.SetSharedFlags(cmd)
    52  	api.SetSharedConfigFlags(cmd)
    53  	isTags := cmd.Flag.Bool("tags", false, "")
    54  	cmd.Flag.Parse(args)
    55  
    56  	var tags []string
    57  	if *isTags {
    58  		tags = cmd.Flag.Args()
    59  	} else {
    60  		c, err := helpers.LoadConfig(cmd.Flag.Args(), api.APIConfigFormat, api.APIConfigRecursively)
    61  		if err != nil {
    62  			base.Fatalf("failed to load: %s", err)
    63  		}
    64  		tags = make([]string, 0)
    65  		for _, c := range c.InboundConfigs {
    66  			tags = append(tags, c.Tag)
    67  		}
    68  	}
    69  	if len(tags) == 0 {
    70  		base.Fatalf("no inbound to remove")
    71  	}
    72  
    73  	conn, ctx, close := api.DialAPIServer()
    74  	defer close()
    75  
    76  	client := handlerService.NewHandlerServiceClient(conn)
    77  	for _, tag := range tags {
    78  		fmt.Println("removing:", tag)
    79  		r := &handlerService.RemoveInboundRequest{
    80  			Tag: tag,
    81  		}
    82  		_, err := client.RemoveInbound(ctx, r)
    83  		if err != nil {
    84  			base.Fatalf("failed to remove inbound: %s", err)
    85  		}
    86  	}
    87  }