main.go 2.9 KB

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