main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "io"
  6. "os"
  7. "os/signal"
  8. "path/filepath"
  9. "syscall"
  10. "v2ray.com/core"
  11. _ "v2ray.com/core/app/router/rules"
  12. "v2ray.com/core/common/log"
  13. "v2ray.com/core/shell/point"
  14. // The following are necessary as they register handlers in their init functions.
  15. _ "v2ray.com/core/proxy/blackhole"
  16. _ "v2ray.com/core/proxy/dokodemo"
  17. _ "v2ray.com/core/proxy/freedom"
  18. _ "v2ray.com/core/proxy/http"
  19. _ "v2ray.com/core/proxy/shadowsocks"
  20. _ "v2ray.com/core/proxy/socks"
  21. _ "v2ray.com/core/proxy/vmess/inbound"
  22. _ "v2ray.com/core/proxy/vmess/outbound"
  23. _ "v2ray.com/core/transport/internet/kcp"
  24. _ "v2ray.com/core/transport/internet/tcp"
  25. _ "v2ray.com/core/transport/internet/udp"
  26. _ "v2ray.com/core/transport/internet/ws"
  27. _ "v2ray.com/core/transport/internet/authenticators/noop"
  28. _ "v2ray.com/core/transport/internet/authenticators/srtp"
  29. _ "v2ray.com/core/transport/internet/authenticators/utp"
  30. )
  31. var (
  32. configFile string
  33. logLevel = flag.String("loglevel", "warning", "Level of log info to be printed to console, available value: debug, info, warning, error")
  34. version = flag.Bool("version", false, "Show current version of V2Ray.")
  35. test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.")
  36. format = flag.String("format", "json", "Format of input file.")
  37. )
  38. func init() {
  39. defaultConfigFile := ""
  40. workingDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
  41. if err == nil {
  42. defaultConfigFile = filepath.Join(workingDir, "config.json")
  43. }
  44. flag.StringVar(&configFile, "config", defaultConfigFile, "Config file for this Point server.")
  45. }
  46. func startV2Ray() *point.Point {
  47. switch *logLevel {
  48. case "debug":
  49. log.SetLogLevel(log.DebugLevel)
  50. case "info":
  51. log.SetLogLevel(log.InfoLevel)
  52. case "warning":
  53. log.SetLogLevel(log.WarningLevel)
  54. case "error":
  55. log.SetLogLevel(log.ErrorLevel)
  56. default:
  57. fmt.Println("Unknown log level: " + *logLevel)
  58. return nil
  59. }
  60. if len(configFile) == 0 {
  61. log.Error("Config file is not set.")
  62. return nil
  63. }
  64. var configInput io.Reader
  65. if configFile == "stdin:" {
  66. configInput = os.Stdin
  67. } else {
  68. fixedFile := os.ExpandEnv(configFile)
  69. file, err := os.Open(fixedFile)
  70. if err != nil {
  71. log.Error("Config file not readable: ", err)
  72. return nil
  73. }
  74. defer file.Close()
  75. configInput = file
  76. }
  77. config, err := point.LoadConfig(configInput)
  78. if err != nil {
  79. log.Error("Failed to read config file (", configFile, "): ", configFile, err)
  80. return nil
  81. }
  82. if config.LogConfig != nil && len(config.LogConfig.AccessLog) > 0 {
  83. log.InitAccessLogger(config.LogConfig.AccessLog)
  84. }
  85. vPoint, err := point.NewPoint(config)
  86. if err != nil {
  87. log.Error("Failed to create Point server: ", err)
  88. return nil
  89. }
  90. if *test {
  91. fmt.Println("Configuration OK.")
  92. return nil
  93. }
  94. err = vPoint.Start()
  95. if err != nil {
  96. log.Error("Error starting Point server: ", err)
  97. return nil
  98. }
  99. return vPoint
  100. }
  101. func main() {
  102. flag.Parse()
  103. core.PrintVersion()
  104. if *version {
  105. return
  106. }
  107. if point := startV2Ray(); point != nil {
  108. osSignals := make(chan os.Signal, 1)
  109. signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
  110. <-osSignals
  111. point.Close()
  112. }
  113. log.Close()
  114. }