inbounds_add.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 cmdAddInbounds = &base.Command{
  9. CustomFlags: true,
  10. UsageLine: "{{.Exec}} api adi [--server=127.0.0.1:8080] [c1.json] [dir1]...",
  11. Short: "add inbounds",
  12. Long: `
  13. Add inbounds to V2Ray.
  14. > Make sure you have "HandlerService" set in "config.api.services"
  15. of server config.
  16. Arguments:
  17. -format <format>
  18. The input format.
  19. Available values: "auto", "json", "toml", "yaml"
  20. Default: "auto"
  21. -r
  22. Load folders recursively.
  23. -s, -server <server:port>
  24. The API server address. Default 127.0.0.1:8080
  25. -t, -timeout <seconds>
  26. Timeout seconds to call API. Default 3
  27. Example:
  28. {{.Exec}} {{.LongName}} dir
  29. {{.Exec}} {{.LongName}} c1.json c2.yaml
  30. `,
  31. Run: executeAddInbounds,
  32. }
  33. func executeAddInbounds(cmd *base.Command, args []string) {
  34. setSharedFlags(cmd)
  35. setSharedConfigFlags(cmd)
  36. cmd.Flag.Parse(args)
  37. c, err := helpers.LoadConfig(cmd.Flag.Args(), apiConfigFormat, apiConfigRecursively)
  38. if err != nil {
  39. base.Fatalf("failed to load: %s", err)
  40. }
  41. if len(c.InboundConfigs) == 0 {
  42. base.Fatalf("no valid inbound found")
  43. }
  44. conn, ctx, close := dialAPIServer()
  45. defer close()
  46. client := handlerService.NewHandlerServiceClient(conn)
  47. for _, in := range c.InboundConfigs {
  48. fmt.Println("adding:", in.Tag)
  49. i, err := in.Build()
  50. if err != nil {
  51. base.Fatalf("failed to build conf: %s", err)
  52. }
  53. r := &handlerService.AddInboundRequest{
  54. Inbound: i,
  55. }
  56. _, err = client.AddInbound(ctx, r)
  57. if err != nil {
  58. base.Fatalf("failed to add inbound: %s", err)
  59. }
  60. }
  61. }