main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package main
  2. //go:generate errorgen
  3. import (
  4. "flag"
  5. "fmt"
  6. "os"
  7. "os/signal"
  8. "path/filepath"
  9. "runtime"
  10. "strings"
  11. "syscall"
  12. "v2ray.com/core"
  13. "v2ray.com/core/common/platform"
  14. "v2ray.com/core/main/confloader"
  15. _ "v2ray.com/core/main/distro/all"
  16. )
  17. var (
  18. configFile = flag.String("config", "", "Config file for V2Ray.")
  19. version = flag.Bool("version", false, "Show current version of V2Ray.")
  20. test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.")
  21. format = flag.String("format", "json", "Format of input file.")
  22. plugin = flag.Bool("plugin", false, "True to load plugins.")
  23. )
  24. func fileExists(file string) bool {
  25. info, err := os.Stat(file)
  26. return err == nil && !info.IsDir()
  27. }
  28. func getConfigFilePath() string {
  29. if len(*configFile) > 0 {
  30. return *configFile
  31. }
  32. if workingDir, err := os.Getwd(); err == nil {
  33. configFile := filepath.Join(workingDir, "config.json")
  34. if fileExists(configFile) {
  35. return configFile
  36. }
  37. }
  38. if configFile := platform.GetConfigurationPath(); fileExists(configFile) {
  39. return configFile
  40. }
  41. return ""
  42. }
  43. func GetConfigFormat() string {
  44. switch strings.ToLower(*format) {
  45. case "pb", "protobuf":
  46. return "protobuf"
  47. default:
  48. return "json"
  49. }
  50. }
  51. func startV2Ray() (core.Server, error) {
  52. configFile := getConfigFilePath()
  53. configInput, err := confloader.LoadConfig(configFile)
  54. if err != nil {
  55. return nil, newError("failed to load config: ", configFile).Base(err)
  56. }
  57. defer configInput.Close()
  58. config, err := core.LoadConfig(GetConfigFormat(), configFile, configInput)
  59. if err != nil {
  60. return nil, newError("failed to read config file: ", configFile).Base(err)
  61. }
  62. server, err := core.New(config)
  63. if err != nil {
  64. return nil, newError("failed to create server").Base(err)
  65. }
  66. return server, nil
  67. }
  68. func printVersion() {
  69. version := core.VersionStatement()
  70. for _, s := range version {
  71. fmt.Println(s)
  72. }
  73. }
  74. func main() {
  75. flag.Parse()
  76. printVersion()
  77. if *version {
  78. return
  79. }
  80. if *plugin {
  81. if err := core.LoadPlugins(); err != nil {
  82. fmt.Println("Failed to load plugins:", err.Error())
  83. os.Exit(-1)
  84. }
  85. }
  86. server, err := startV2Ray()
  87. if err != nil {
  88. fmt.Println(err.Error())
  89. // Configuration error. Exit with a special value to prevent systemd from restarting.
  90. os.Exit(23)
  91. }
  92. if *test {
  93. fmt.Println("Configuration OK.")
  94. os.Exit(0)
  95. }
  96. if err := server.Start(); err != nil {
  97. fmt.Println("Failed to start", err)
  98. os.Exit(-1)
  99. }
  100. defer server.Close()
  101. // Explicitly triggering GC to remove garbage from config loading.
  102. runtime.GC()
  103. {
  104. osSignals := make(chan os.Signal, 1)
  105. signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
  106. <-osSignals
  107. }
  108. }