main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package main
  2. //go:generate errorgen
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "flag"
  7. "fmt"
  8. "os"
  9. "os/signal"
  10. "path/filepath"
  11. "runtime"
  12. "strings"
  13. "syscall"
  14. "v2ray.com/core"
  15. "v2ray.com/core/common/platform"
  16. _ "v2ray.com/core/main/distro/all"
  17. )
  18. type CmdConfig []string
  19. func (c *CmdConfig) String() string {
  20. return strings.Join([]string(*c), ",")
  21. }
  22. func (c *CmdConfig) Set(value string) error {
  23. *c = append(*c, value)
  24. return nil
  25. }
  26. var (
  27. configFiles CmdConfig // "Config file for V2Ray.", the option is customed type, parse in main
  28. version = flag.Bool("version", false, "Show current version of V2Ray.")
  29. test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.")
  30. format = flag.String("format", "json", "Format of input file.")
  31. )
  32. func fileExists(file string) bool {
  33. info, err := os.Stat(file)
  34. return err == nil && !info.IsDir()
  35. }
  36. func getConfigFilePath() CmdConfig {
  37. if len(configFiles) > 0 {
  38. return configFiles
  39. }
  40. if workingDir, err := os.Getwd(); err == nil {
  41. configFile := filepath.Join(workingDir, "config.json")
  42. if fileExists(configFile) {
  43. return []string{configFile}
  44. }
  45. }
  46. if configFile := platform.GetConfigurationPath(); fileExists(configFile) {
  47. return []string{configFile}
  48. }
  49. return []string{}
  50. }
  51. func GetConfigFormat() string {
  52. switch strings.ToLower(*format) {
  53. case "pb", "protobuf":
  54. return "protobuf"
  55. default:
  56. return "json"
  57. }
  58. }
  59. func startV2Ray() (core.Server, error) {
  60. configFiles := getConfigFilePath()
  61. fs, _ := json.Marshal(configFiles)
  62. config, err := core.LoadConfig(GetConfigFormat(), configFiles[0], bytes.NewBuffer(fs))
  63. if err != nil {
  64. return nil, newError("failed to read config files: [", configFiles.String(), "]").Base(err)
  65. }
  66. server, err := core.New(config)
  67. if err != nil {
  68. return nil, newError("failed to create server").Base(err)
  69. }
  70. return server, nil
  71. }
  72. func printVersion() {
  73. version := core.VersionStatement()
  74. for _, s := range version {
  75. fmt.Println(s)
  76. }
  77. }
  78. func main() {
  79. flag.Var(&configFiles, "config", "Config file for V2Ray. Multiple assign is accepted (only json). Latter ones overrides the former ones.")
  80. flag.Parse()
  81. printVersion()
  82. if *version {
  83. return
  84. }
  85. server, err := startV2Ray()
  86. if err != nil {
  87. fmt.Println(err.Error())
  88. // Configuration error. Exit with a special value to prevent systemd from restarting.
  89. os.Exit(23)
  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. defer server.Close()
  100. // Explicitly triggering GC to remove garbage from config loading.
  101. runtime.GC()
  102. {
  103. osSignals := make(chan os.Signal, 1)
  104. signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
  105. <-osSignals
  106. }
  107. }