main.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package main
  2. //go:generate errorgen
  3. import (
  4. "flag"
  5. "fmt"
  6. "os"
  7. "os/signal"
  8. "path/filepath"
  9. "runtime"
  10. "strings"
  11. "syscall"
  12. "v2ray.com/core"
  13. "v2ray.com/core/common/cmdarg"
  14. "v2ray.com/core/common/platform"
  15. _ "v2ray.com/core/main/distro/all"
  16. )
  17. var (
  18. configFiles cmdarg.Arg // "Config file for V2Ray.", the option is customed type, parse in main
  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", "", "Format of input file.")
  22. errNoConfig = newError("no valid config")
  23. )
  24. func fileExists(file string) bool {
  25. info, err := os.Stat(file)
  26. return err == nil && !info.IsDir()
  27. }
  28. func getConfigFilePath() cmdarg.Arg {
  29. if len(configFiles) > 0 {
  30. return configFiles
  31. }
  32. if workingDir, err := os.Getwd(); err == nil {
  33. configFile := filepath.Join(workingDir, "config.json")
  34. if fileExists(configFile) {
  35. return cmdarg.Arg{configFile}
  36. }
  37. }
  38. if configFile := platform.GetConfigurationPath(); fileExists(configFile) {
  39. return cmdarg.Arg{configFile}
  40. }
  41. return configFiles
  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. configFiles := getConfigFilePath()
  53. if len(configFiles) == 0 {
  54. if *format == "" {
  55. return nil, errNoConfig
  56. }
  57. configFiles = []string{"stdin:"}
  58. }
  59. config, err := core.LoadConfig(GetConfigFormat(), configFiles[0], configFiles)
  60. if err != nil {
  61. return nil, newError("failed to read config files: [", configFiles.String(), "]").Base(err)
  62. }
  63. server, err := core.New(config)
  64. if err != nil {
  65. return nil, newError("failed to create server").Base(err)
  66. }
  67. return server, nil
  68. }
  69. func printVersion() {
  70. version := core.VersionStatement()
  71. for _, s := range version {
  72. fmt.Println(s)
  73. }
  74. }
  75. func main() {
  76. flag.Var(&configFiles, "config", "Config file for V2Ray. Multiple assign is accepted (only json). Latter ones overrides the former ones.")
  77. flag.Var(&configFiles, "c", "short alias of -config")
  78. flag.Parse()
  79. printVersion()
  80. if *version {
  81. return
  82. }
  83. server, err := startV2Ray()
  84. if err != nil {
  85. fmt.Println(err.Error())
  86. // Configuration error. Exit with a special value to prevent systemd from restarting.
  87. if err == errNoConfig {
  88. flag.PrintDefaults()
  89. }
  90. os.Exit(23)
  91. }
  92. if *test {
  93. fmt.Println("Configuration OK.")
  94. os.Exit(0)
  95. }
  96. if err := server.Start(); err != nil {
  97. fmt.Println("Failed to start", err)
  98. os.Exit(-1)
  99. }
  100. defer server.Close()
  101. // Explicitly triggering GC to remove garbage from config loading.
  102. runtime.GC()
  103. {
  104. osSignals := make(chan os.Signal, 1)
  105. signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
  106. <-osSignals
  107. }
  108. }