main.go 3.0 KB

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