outbounds_add.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package jsonv4
  2. import (
  3. "fmt"
  4. handlerService "github.com/v2fly/v2ray-core/v5/app/proxyman/command"
  5. "github.com/v2fly/v2ray-core/v5/main/commands/all/api"
  6. "github.com/v2fly/v2ray-core/v5/main/commands/base"
  7. "github.com/v2fly/v2ray-core/v5/main/commands/helpers"
  8. )
  9. var cmdAddOutbounds = &base.Command{
  10. CustomFlags: true,
  11. UsageLine: "{{.Exec}} api ado [--server=127.0.0.1:8080] [c1.json] [dir1]...",
  12. Short: "add outbounds",
  13. Long: `
  14. Add outbounds to V2Ray.
  15. > Make sure you have "HandlerService" set in "config.api.services"
  16. of server config.
  17. Arguments:
  18. -format <format>
  19. The input format.
  20. Available values: "auto", "json", "toml", "yaml"
  21. Default: "auto"
  22. -r
  23. Load folders recursively.
  24. -s, -server <server:port>
  25. The API server address. Default 127.0.0.1:8080
  26. -t, -timeout <seconds>
  27. Timeout seconds to call API. Default 3
  28. Example:
  29. {{.Exec}} {{.LongName}} dir
  30. {{.Exec}} {{.LongName}} c1.json c2.yaml
  31. `,
  32. Run: executeAddOutbounds,
  33. }
  34. func executeAddOutbounds(cmd *base.Command, args []string) {
  35. api.SetSharedFlags(cmd)
  36. api.SetSharedConfigFlags(cmd)
  37. cmd.Flag.Parse(args)
  38. c, err := helpers.LoadConfig(cmd.Flag.Args(), api.APIConfigFormat, api.APIConfigRecursively)
  39. if err != nil {
  40. base.Fatalf("failed to load: %s", err)
  41. }
  42. if len(c.OutboundConfigs) == 0 {
  43. base.Fatalf("no valid outbound found")
  44. }
  45. conn, ctx, close := api.DialAPIServer()
  46. defer close()
  47. client := handlerService.NewHandlerServiceClient(conn)
  48. for _, out := range c.OutboundConfigs {
  49. fmt.Println("adding:", out.Tag)
  50. o, err := out.Build()
  51. if err != nil {
  52. base.Fatalf("failed to build conf: %s", err)
  53. }
  54. r := &handlerService.AddOutboundRequest{
  55. Outbound: o,
  56. }
  57. _, err = client.AddOutbound(ctx, r)
  58. if err != nil {
  59. base.Fatalf("failed to add outbound: %s", err)
  60. }
  61. }
  62. }