main.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package main
  2. //go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
  3. import (
  4. "flag"
  5. "fmt"
  6. "log"
  7. "os"
  8. "os/signal"
  9. "path"
  10. "path/filepath"
  11. "runtime"
  12. "strings"
  13. "syscall"
  14. core "github.com/v2fly/v2ray-core/v4"
  15. "github.com/v2fly/v2ray-core/v4/common/cmdarg"
  16. "github.com/v2fly/v2ray-core/v4/common/platform"
  17. _ "github.com/v2fly/v2ray-core/v4/main/distro/all"
  18. )
  19. var (
  20. configFiles cmdarg.Arg // "Config file for V2Ray.", the option is customed type, parse in main
  21. configDir string
  22. version = flag.Bool("version", false, "Show current version of V2Ray.")
  23. test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.")
  24. format = flag.String("format", "json", "Format of input file.")
  25. /* We have to do this here because Golang's Test will also need to parse flag, before
  26. * main func in this file is run.
  27. */
  28. _ = func() error { // nolint: unparam
  29. flag.Var(&configFiles, "config", "Config file for V2Ray. Multiple assign is accepted (only json). Latter ones overrides the former ones.")
  30. flag.Var(&configFiles, "c", "Short alias of -config")
  31. flag.StringVar(&configDir, "confdir", "", "A dir with multiple json config")
  32. return nil
  33. }()
  34. )
  35. func fileExists(file string) bool {
  36. info, err := os.Stat(file)
  37. return err == nil && !info.IsDir()
  38. }
  39. func dirExists(file string) bool {
  40. if file == "" {
  41. return false
  42. }
  43. info, err := os.Stat(file)
  44. return err == nil && info.IsDir()
  45. }
  46. func readConfDir(dirPath string) {
  47. confs, err := os.ReadDir(dirPath)
  48. if err != nil {
  49. log.Fatalln(err)
  50. }
  51. for _, f := range confs {
  52. if strings.HasSuffix(f.Name(), ".json") {
  53. configFiles.Set(path.Join(dirPath, f.Name()))
  54. }
  55. }
  56. }
  57. func getConfigFilePath() cmdarg.Arg {
  58. if dirExists(configDir) {
  59. log.Println("Using confdir from arg:", configDir)
  60. readConfDir(configDir)
  61. } else if envConfDir := platform.GetConfDirPath(); dirExists(envConfDir) {
  62. log.Println("Using confdir from env:", envConfDir)
  63. readConfDir(envConfDir)
  64. }
  65. if len(configFiles) > 0 {
  66. return configFiles
  67. }
  68. if workingDir, err := os.Getwd(); err == nil {
  69. configFile := filepath.Join(workingDir, "config.json")
  70. if fileExists(configFile) {
  71. log.Println("Using default config: ", configFile)
  72. return cmdarg.Arg{configFile}
  73. }
  74. }
  75. if configFile := platform.GetConfigurationPath(); fileExists(configFile) {
  76. log.Println("Using config from env: ", configFile)
  77. return cmdarg.Arg{configFile}
  78. }
  79. log.Println("Using config from STDIN")
  80. return cmdarg.Arg{"stdin:"}
  81. }
  82. func GetConfigFormat() string {
  83. switch strings.ToLower(*format) {
  84. case "pb", "protobuf":
  85. return "protobuf"
  86. default:
  87. return "json"
  88. }
  89. }
  90. func startV2Ray() (core.Server, error) {
  91. configFiles := getConfigFilePath()
  92. config, err := core.LoadConfig(GetConfigFormat(), configFiles[0], configFiles)
  93. if err != nil {
  94. return nil, newError("failed to read config files: [", configFiles.String(), "]").Base(err)
  95. }
  96. server, err := core.New(config)
  97. if err != nil {
  98. return nil, newError("failed to create server").Base(err)
  99. }
  100. return server, nil
  101. }
  102. func printVersion() {
  103. version := core.VersionStatement()
  104. for _, s := range version {
  105. fmt.Println(s)
  106. }
  107. }
  108. func main() {
  109. flag.Parse()
  110. printVersion()
  111. if *version {
  112. return
  113. }
  114. server, err := startV2Ray()
  115. if err != nil {
  116. fmt.Println(err)
  117. // Configuration error. Exit with a special value to prevent systemd from restarting.
  118. os.Exit(23)
  119. }
  120. if *test {
  121. fmt.Println("Configuration OK.")
  122. os.Exit(0)
  123. }
  124. if err := server.Start(); err != nil {
  125. fmt.Println("Failed to start", err)
  126. os.Exit(-1)
  127. }
  128. defer server.Close()
  129. // Explicitly triggering GC to remove garbage from config loading.
  130. runtime.GC()
  131. {
  132. osSignals := make(chan os.Signal, 1)
  133. signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM)
  134. <-osSignals
  135. }
  136. }