main.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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() 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. var configInput io.Reader
  53. if configFile == "stdin:" {
  54. configInput = os.Stdin
  55. } else {
  56. fixedFile := os.ExpandEnv(configFile)
  57. file, err := os.Open(fixedFile)
  58. if err != nil {
  59. return nil, newError("config file not readable").Base(err)
  60. }
  61. defer file.Close()
  62. configInput = file
  63. }
  64. config, err := core.LoadConfig(GetConfigFormat(), configFile, configInput)
  65. if err != nil {
  66. return nil, newError("failed to read config file: ", configFile).Base(err)
  67. }
  68. server, err := core.New(config)
  69. if err != nil {
  70. return nil, newError("failed to create server").Base(err)
  71. }
  72. return server, nil
  73. }
  74. func main() {
  75. flag.Parse()
  76. core.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. os.Exit(-1)
  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. }