main.go 2.5 KB

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