main.go 2.8 KB

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