main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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", "json", "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, error) {
  29. if len(configFiles) > 0 {
  30. return configFiles, nil
  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}, nil
  36. }
  37. }
  38. if configFile := platform.GetConfigurationPath(); fileExists(configFile) {
  39. return cmdarg.Arg{configFile}, nil
  40. }
  41. return cmdarg.Arg{"stdin:"}, nil
  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, err := getConfigFilePath()
  53. if err != nil {
  54. return nil, err
  55. }
  56. config, err := core.LoadConfig(GetConfigFormat(), configFiles[0], configFiles)
  57. if err != nil {
  58. return nil, newError("failed to read config files: [", configFiles.String(), "]").Base(err)
  59. }
  60. server, err := core.New(config)
  61. if err != nil {
  62. return nil, newError("failed to create server").Base(err)
  63. }
  64. return server, nil
  65. }
  66. func printVersion() {
  67. version := core.VersionStatement()
  68. for _, s := range version {
  69. fmt.Println(s)
  70. }
  71. }
  72. func main() {
  73. flag.Var(&configFiles, "config", "Config file for V2Ray. Multiple assign is accepted (only json). Latter ones overrides the former ones.")
  74. flag.Var(&configFiles, "c", "short alias of -config")
  75. flag.Parse()
  76. printVersion()
  77. if *version {
  78. return
  79. }
  80. server, err := startV2Ray()
  81. if err != nil {
  82. fmt.Println(err.Error())
  83. // Configuration error. Exit with a special value to prevent systemd from restarting.
  84. if err == errNoConfig {
  85. flag.PrintDefaults()
  86. }
  87. os.Exit(23)
  88. }
  89. if *test {
  90. fmt.Println("Configuration OK.")
  91. os.Exit(0)
  92. }
  93. if err := server.Start(); err != nil {
  94. fmt.Println("Failed to start", err)
  95. os.Exit(-1)
  96. }
  97. defer server.Close()
  98. // Explicitly triggering GC to remove garbage from config loading.
  99. runtime.GC()
  100. {
  101. osSignals := make(chan os.Signal, 1)
  102. signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
  103. <-osSignals
  104. }
  105. }