main.go 2.9 KB

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