main.go 3.7 KB

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