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