run_default.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //go:build !pprof
  2. // +build !pprof
  3. package commands
  4. import (
  5. "os"
  6. "os/signal"
  7. "runtime"
  8. "syscall"
  9. "github.com/v2fly/v2ray-core/v5/main/commands/base"
  10. )
  11. // CmdRun runs V2Ray with config
  12. var CmdRun = &base.Command{
  13. CustomFlags: true,
  14. UsageLine: "{{.Exec}} run [-c config.json] [-d dir]",
  15. Short: "run V2Ray with config",
  16. Long: `
  17. Run V2Ray with config.
  18. {{.Exec}} will also use the config directory specified by environment
  19. variable "v2ray.location.confdir". If no config found, it tries
  20. to load config from one of below:
  21. 1. The default "config.json" in the current directory
  22. 2. The config file from ENV "v2ray.location.config"
  23. 3. The stdin if all failed above
  24. Arguments:
  25. -c, -config <file>
  26. Config file for V2Ray. Multiple assign is accepted.
  27. -d, -confdir <dir>
  28. A directory with config files. Multiple assign is accepted.
  29. -r
  30. Load confdir recursively.
  31. -format <format>
  32. Format of config input. (default "auto")
  33. Examples:
  34. {{.Exec}} {{.LongName}} -c config.json
  35. {{.Exec}} {{.LongName}} -d path/to/dir
  36. Use "{{.Exec}} help format-loader" for more information about format.
  37. `,
  38. Run: executeRun,
  39. }
  40. func executeRun(cmd *base.Command, args []string) {
  41. setConfigFlags(cmd)
  42. cmd.Flag.Parse(args)
  43. printVersion()
  44. configFiles = getConfigFilePath()
  45. server, err := startV2Ray()
  46. if err != nil {
  47. base.Fatalf("Failed to start: %s", err)
  48. }
  49. if err := server.Start(); err != nil {
  50. base.Fatalf("Failed to start: %s", err)
  51. }
  52. defer server.Close()
  53. // Explicitly triggering GC to remove garbage from config loading.
  54. runtime.GC()
  55. {
  56. osSignals := make(chan os.Signal, 1)
  57. signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM)
  58. <-osSignals
  59. }
  60. }