main.go 3.6 KB

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