main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package main
  2. //go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg main -path Main
  3. import (
  4. "flag"
  5. "fmt"
  6. "io"
  7. "os"
  8. "os/signal"
  9. "path/filepath"
  10. "strings"
  11. "syscall"
  12. "v2ray.com/core"
  13. "v2ray.com/core/common/platform"
  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() core.ConfigFormat {
  43. switch strings.ToLower(*format) {
  44. case "json":
  45. return core.ConfigFormat_JSON
  46. case "pb", "protobuf":
  47. return core.ConfigFormat_Protobuf
  48. default:
  49. return core.ConfigFormat_JSON
  50. }
  51. }
  52. func startV2Ray() (core.Server, error) {
  53. configFile := getConfigFilePath()
  54. var configInput io.Reader
  55. if configFile == "stdin:" {
  56. configInput = os.Stdin
  57. } else {
  58. fixedFile := os.ExpandEnv(configFile)
  59. file, err := os.Open(fixedFile)
  60. if err != nil {
  61. return nil, newError("config file not readable").Base(err)
  62. }
  63. defer file.Close()
  64. configInput = file
  65. }
  66. config, err := core.LoadConfig(GetConfigFormat(), configInput)
  67. if err != nil {
  68. return nil, newError("failed to read config file: ", configFile).Base(err)
  69. }
  70. server, err := core.New(config)
  71. if err != nil {
  72. return nil, newError("failed to create server").Base(err)
  73. }
  74. return server, nil
  75. }
  76. func main() {
  77. flag.Parse()
  78. core.PrintVersion()
  79. if *version {
  80. return
  81. }
  82. if *plugin {
  83. if err := core.LoadPlugins(); err != nil {
  84. fmt.Println("Failed to load plugins:", err.Error())
  85. os.Exit(-1)
  86. }
  87. }
  88. server, err := startV2Ray()
  89. if err != nil {
  90. fmt.Println(err.Error())
  91. os.Exit(-1)
  92. }
  93. if *test {
  94. fmt.Println("Configuration OK.")
  95. os.Exit(0)
  96. }
  97. if err := server.Start(); err != nil {
  98. fmt.Println("Failed to start", err)
  99. os.Exit(-1)
  100. }
  101. osSignals := make(chan os.Signal, 1)
  102. signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
  103. <-osSignals
  104. server.Close()
  105. }