2021-05-02 16:40:11 -04:00
|
|
|
package jsonv4
|
2020-11-30 07:08:08 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-12-02 17:11:24 -05:00
|
|
|
handlerService "github.com/v2fly/v2ray-core/v4/app/proxyman/command"
|
2021-06-04 19:42:53 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/main/commands/all/api"
|
2020-12-04 09:32:55 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/main/commands/base"
|
2021-02-21 10:02:42 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/main/commands/helpers"
|
2020-11-30 07:08:08 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var cmdRemoveInbounds = &base.Command{
|
|
|
|
CustomFlags: true,
|
2021-03-10 16:03:26 -05:00
|
|
|
UsageLine: "{{.Exec}} api rmi [--server=127.0.0.1:8080] [c1.json] [dir1]...",
|
2021-02-21 10:02:42 -05:00
|
|
|
Short: "remove inbounds",
|
2020-11-30 07:08:08 -05:00
|
|
|
Long: `
|
|
|
|
Remove inbounds from V2Ray.
|
|
|
|
|
2021-03-10 16:03:26 -05:00
|
|
|
> Make sure you have "HandlerService" set in "config.api.services"
|
|
|
|
of server config.
|
|
|
|
|
2020-11-30 07:08:08 -05:00
|
|
|
Arguments:
|
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
-format <format>
|
2021-03-10 16:03:26 -05:00
|
|
|
The input format.
|
2021-02-21 10:02:42 -05:00
|
|
|
Available values: "auto", "json", "toml", "yaml"
|
|
|
|
Default: "auto"
|
|
|
|
|
|
|
|
-r
|
|
|
|
Load folders recursively.
|
|
|
|
|
2021-03-10 16:03:26 -05:00
|
|
|
-tags
|
|
|
|
The input are tags instead of config files
|
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
-s, -server <server:port>
|
2020-11-30 07:08:08 -05:00
|
|
|
The API server address. Default 127.0.0.1:8080
|
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
-t, -timeout <seconds>
|
2020-11-30 07:08:08 -05:00
|
|
|
Timeout seconds to call API. Default 3
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2021-03-10 16:03:26 -05:00
|
|
|
{{.Exec}} {{.LongName}} dir
|
|
|
|
{{.Exec}} {{.LongName}} c1.json c2.yaml
|
|
|
|
{{.Exec}} {{.LongName}} -tags tag1 tag2
|
2020-11-30 07:08:08 -05:00
|
|
|
`,
|
|
|
|
Run: executeRemoveInbounds,
|
|
|
|
}
|
|
|
|
|
|
|
|
func executeRemoveInbounds(cmd *base.Command, args []string) {
|
2021-05-02 16:40:11 -04:00
|
|
|
api.SetSharedFlags(cmd)
|
|
|
|
api.SetSharedConfigFlags(cmd)
|
2021-03-10 16:03:26 -05:00
|
|
|
isTags := cmd.Flag.Bool("tags", false, "")
|
2020-11-30 07:08:08 -05:00
|
|
|
cmd.Flag.Parse(args)
|
2021-03-10 16:03:26 -05:00
|
|
|
|
|
|
|
var tags []string
|
|
|
|
if *isTags {
|
|
|
|
tags = cmd.Flag.Args()
|
|
|
|
} else {
|
2021-06-04 19:42:53 -04:00
|
|
|
c, err := helpers.LoadConfig(cmd.Flag.Args(), api.APIConfigFormat, api.APIConfigRecursively)
|
2021-03-10 16:03:26 -05:00
|
|
|
if err != nil {
|
|
|
|
base.Fatalf("failed to load: %s", err)
|
|
|
|
}
|
|
|
|
tags = make([]string, 0)
|
|
|
|
for _, c := range c.InboundConfigs {
|
|
|
|
tags = append(tags, c.Tag)
|
|
|
|
}
|
2020-11-30 07:08:08 -05:00
|
|
|
}
|
2021-03-10 16:03:26 -05:00
|
|
|
if len(tags) == 0 {
|
2020-11-30 07:08:08 -05:00
|
|
|
base.Fatalf("no inbound to remove")
|
|
|
|
}
|
|
|
|
|
2021-05-02 16:40:11 -04:00
|
|
|
conn, ctx, close := api.DialAPIServer()
|
2020-11-30 07:08:08 -05:00
|
|
|
defer close()
|
|
|
|
|
|
|
|
client := handlerService.NewHandlerServiceClient(conn)
|
2021-03-10 16:03:26 -05:00
|
|
|
for _, tag := range tags {
|
|
|
|
fmt.Println("removing:", tag)
|
2020-11-30 07:08:08 -05:00
|
|
|
r := &handlerService.RemoveInboundRequest{
|
2021-03-10 16:03:26 -05:00
|
|
|
Tag: tag,
|
2020-11-30 07:08:08 -05:00
|
|
|
}
|
2021-02-21 10:02:42 -05:00
|
|
|
_, err := client.RemoveInbound(ctx, r)
|
2020-11-30 07:08:08 -05:00
|
|
|
if err != nil {
|
|
|
|
base.Fatalf("failed to remove inbound: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|