main.go 2.7 KB

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