main.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. configFiles.Set(path.Join(dirPath, f.Name()))
  43. }
  44. }
  45. func getConfigFilePath() (cmdarg.Arg, error) {
  46. if dirExists(configDir) {
  47. readConfDir(configDir)
  48. }
  49. if len(configFiles) > 0 {
  50. return configFiles, nil
  51. }
  52. if workingDir, err := os.Getwd(); err == nil {
  53. configFile := filepath.Join(workingDir, "config.json")
  54. if fileExists(configFile) {
  55. log.Println("Using default config: ", configFile)
  56. return cmdarg.Arg{configFile}, nil
  57. }
  58. }
  59. if configFile := platform.GetConfigurationPath(); fileExists(configFile) {
  60. log.Println("Using config from env: ", configFile)
  61. return cmdarg.Arg{configFile}, nil
  62. }
  63. if envConfDir := platform.GetConfDirPath(); dirExists(envConfDir) {
  64. log.Println("Using confdir from env: ", envConfDir)
  65. readConfDir(envConfDir)
  66. if len(configFiles) > 0 {
  67. return configFiles, nil
  68. }
  69. }
  70. log.Println("Using config from STDIN")
  71. return cmdarg.Arg{"stdin:"}, nil
  72. }
  73. func GetConfigFormat() string {
  74. switch strings.ToLower(*format) {
  75. case "pb", "protobuf":
  76. return "protobuf"
  77. default:
  78. return "json"
  79. }
  80. }
  81. func startV2Ray() (core.Server, error) {
  82. configFiles, err := getConfigFilePath()
  83. if err != nil {
  84. return nil, err
  85. }
  86. config, err := core.LoadConfig(GetConfigFormat(), configFiles[0], configFiles)
  87. if err != nil {
  88. return nil, newError("failed to read config files: [", configFiles.String(), "]").Base(err)
  89. }
  90. server, err := core.New(config)
  91. if err != nil {
  92. return nil, newError("failed to create server").Base(err)
  93. }
  94. return server, nil
  95. }
  96. func printVersion() {
  97. version := core.VersionStatement()
  98. for _, s := range version {
  99. fmt.Println(s)
  100. }
  101. }
  102. func main() {
  103. flag.Var(&configFiles, "config", "Config file for V2Ray. Multiple assign is accepted (only json). Latter ones overrides the former ones.")
  104. flag.Var(&configFiles, "c", "Short alias of -config")
  105. flag.StringVar(&configDir, "confdir", "", "A dir with multiple json config")
  106. flag.Parse()
  107. printVersion()
  108. if *version {
  109. return
  110. }
  111. server, err := startV2Ray()
  112. if err != nil {
  113. fmt.Println(err.Error())
  114. // Configuration error. Exit with a special value to prevent systemd from restarting.
  115. if err == errNoConfig {
  116. flag.PrintDefaults()
  117. }
  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, os.Kill, syscall.SIGTERM)
  134. <-osSignals
  135. }
  136. }