main.go 3.5 KB

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