inbounds_remove.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package api
  2. import (
  3. "fmt"
  4. handlerService "github.com/v2fly/v2ray-core/v4/app/proxyman/command"
  5. "github.com/v2fly/v2ray-core/v4/main/commands/base"
  6. "github.com/v2fly/v2ray-core/v4/main/commands/helpers"
  7. )
  8. var cmdRemoveInbounds = &base.Command{
  9. CustomFlags: true,
  10. UsageLine: "{{.Exec}} api rmi [--server=127.0.0.1:8080] <json_file|tag> [json_file] [tag]...",
  11. Short: "remove inbounds",
  12. Long: `
  13. Remove inbounds from V2Ray.
  14. Arguments:
  15. -format <format>
  16. Specify the input format.
  17. Available values: "auto", "json", "toml", "yaml"
  18. Default: "auto"
  19. -r
  20. Load folders recursively.
  21. -s, -server <server:port>
  22. The API server address. Default 127.0.0.1:8080
  23. -t, -timeout <seconds>
  24. Timeout seconds to call API. Default 3
  25. Example:
  26. {{.Exec}} {{.LongName}} --server=127.0.0.1:8080 c1.json "tag name"
  27. `,
  28. Run: executeRemoveInbounds,
  29. }
  30. func executeRemoveInbounds(cmd *base.Command, args []string) {
  31. setSharedFlags(cmd)
  32. setSharedConfigFlags(cmd)
  33. cmd.Flag.Parse(args)
  34. c, err := helpers.LoadConfig(cmd.Flag.Args(), apiConfigFormat, apiConfigRecursively)
  35. if err != nil {
  36. base.Fatalf("%s", err)
  37. }
  38. if len(c.InboundConfigs) == 0 {
  39. base.Fatalf("no inbound to remove")
  40. }
  41. conn, ctx, close := dialAPIServer()
  42. defer close()
  43. client := handlerService.NewHandlerServiceClient(conn)
  44. for _, c := range c.InboundConfigs {
  45. fmt.Println("removing:", c.Tag)
  46. r := &handlerService.RemoveInboundRequest{
  47. Tag: c.Tag,
  48. }
  49. _, err := client.RemoveInbound(ctx, r)
  50. if err != nil {
  51. base.Fatalf("failed to remove inbound: %s", err)
  52. }
  53. }
  54. }