main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/ws"
  25. _ "github.com/v2ray/v2ray-core/transport/internet/authenticators/noop"
  26. _ "github.com/v2ray/v2ray-core/transport/internet/authenticators/srtp"
  27. )
  28. var (
  29. configFile string
  30. logLevel = flag.String("loglevel", "warning", "Level of log info to be printed to console, available value: debug, info, warning, error")
  31. version = flag.Bool("version", false, "Show current version of V2Ray.")
  32. test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.")
  33. )
  34. func init() {
  35. defaultConfigFile := ""
  36. workingDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
  37. if err == nil {
  38. defaultConfigFile = filepath.Join(workingDir, "config.json")
  39. }
  40. flag.StringVar(&configFile, "config", defaultConfigFile, "Config file for this Point server.")
  41. }
  42. func startV2Ray() *point.Point {
  43. switch *logLevel {
  44. case "debug":
  45. log.SetLogLevel(log.DebugLevel)
  46. case "info":
  47. log.SetLogLevel(log.InfoLevel)
  48. case "warning":
  49. log.SetLogLevel(log.WarningLevel)
  50. case "error":
  51. log.SetLogLevel(log.ErrorLevel)
  52. default:
  53. fmt.Println("Unknown log level: " + *logLevel)
  54. return nil
  55. }
  56. if len(configFile) == 0 {
  57. log.Error("Config file is not set.")
  58. return nil
  59. }
  60. config, err := point.LoadConfig(configFile)
  61. if err != nil {
  62. log.Error("Failed to read config file (", configFile, "): ", configFile, err)
  63. return nil
  64. }
  65. if config.LogConfig != nil && len(config.LogConfig.AccessLog) > 0 {
  66. log.InitAccessLogger(config.LogConfig.AccessLog)
  67. }
  68. vPoint, err := point.NewPoint(config)
  69. if err != nil {
  70. log.Error("Failed to create Point server: ", err)
  71. return nil
  72. }
  73. if *test {
  74. fmt.Println("Configuration OK.")
  75. return nil
  76. }
  77. err = vPoint.Start()
  78. if err != nil {
  79. log.Error("Error starting Point server: ", err)
  80. return nil
  81. }
  82. return vPoint
  83. }
  84. func main() {
  85. flag.Parse()
  86. core.PrintVersion()
  87. if *version {
  88. return
  89. }
  90. if point := startV2Ray(); point != nil {
  91. osSignals := make(chan os.Signal, 1)
  92. signal.Notify(osSignals, os.Interrupt, os.Kill)
  93. <-osSignals
  94. point.Close()
  95. }
  96. log.Close()
  97. }