main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/common/platform/ctlcmd"
  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. var configInput io.Reader
  54. if configFile == "stdin:" {
  55. configInput = os.Stdin
  56. } else if strings.HasPrefix(configFile, "http://") || strings.HasPrefix(configFile, "https://") {
  57. content, err := ctlcmd.Run([]string{"fetch", configFile}, nil)
  58. if err != nil {
  59. return nil, err
  60. }
  61. configInput = &content
  62. } else {
  63. fixedFile := os.ExpandEnv(configFile)
  64. file, err := os.Open(fixedFile)
  65. if err != nil {
  66. return nil, newError("config file not readable").Base(err)
  67. }
  68. defer file.Close()
  69. configInput = file
  70. }
  71. config, err := core.LoadConfig(GetConfigFormat(), configFile, configInput)
  72. if err != nil {
  73. return nil, newError("failed to read config file: ", configFile).Base(err)
  74. }
  75. server, err := core.New(config)
  76. if err != nil {
  77. return nil, newError("failed to create server").Base(err)
  78. }
  79. return server, nil
  80. }
  81. func printVersion() {
  82. version := core.VersionStatement()
  83. for _, s := range version {
  84. fmt.Println(s)
  85. }
  86. }
  87. func main() {
  88. flag.Parse()
  89. printVersion()
  90. if *version {
  91. return
  92. }
  93. if *plugin {
  94. if err := core.LoadPlugins(); err != nil {
  95. fmt.Println("Failed to load plugins:", err.Error())
  96. os.Exit(-1)
  97. }
  98. }
  99. server, err := startV2Ray()
  100. if err != nil {
  101. fmt.Println(err.Error())
  102. os.Exit(-1)
  103. }
  104. if *test {
  105. fmt.Println("Configuration OK.")
  106. os.Exit(0)
  107. }
  108. if err := server.Start(); err != nil {
  109. fmt.Println("Failed to start", err)
  110. os.Exit(-1)
  111. }
  112. osSignals := make(chan os.Signal, 1)
  113. signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
  114. <-osSignals
  115. server.Close()
  116. }