main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 printVersion() {
  75. version := core.VersionStatement()
  76. for _, s := range version {
  77. fmt.Println(s)
  78. }
  79. }
  80. func main() {
  81. flag.Parse()
  82. printVersion()
  83. if *version {
  84. return
  85. }
  86. if *plugin {
  87. if err := core.LoadPlugins(); err != nil {
  88. fmt.Println("Failed to load plugins:", err.Error())
  89. os.Exit(-1)
  90. }
  91. }
  92. server, err := startV2Ray()
  93. if err != nil {
  94. fmt.Println(err.Error())
  95. os.Exit(-1)
  96. }
  97. if *test {
  98. fmt.Println("Configuration OK.")
  99. os.Exit(0)
  100. }
  101. if err := server.Start(); err != nil {
  102. fmt.Println("Failed to start", err)
  103. os.Exit(-1)
  104. }
  105. osSignals := make(chan os.Signal, 1)
  106. signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
  107. <-osSignals
  108. server.Close()
  109. }