main.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "runtime"
  8. "time"
  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. )
  23. var (
  24. configFile string
  25. logLevel = flag.String("loglevel", "warning", "Level of log info to be printed to console, available value: debug, info, warning, error")
  26. version = flag.Bool("version", false, "Show current version of V2Ray.")
  27. test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.")
  28. )
  29. func init() {
  30. defaultConfigFile := ""
  31. workingDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
  32. if err == nil {
  33. defaultConfigFile = filepath.Join(workingDir, "config.json")
  34. }
  35. flag.StringVar(&configFile, "config", defaultConfigFile, "Config file for this Point server.")
  36. }
  37. func main() {
  38. flag.Parse()
  39. core.PrintVersion()
  40. if *version {
  41. return
  42. }
  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
  55. }
  56. if len(configFile) == 0 {
  57. log.Error("Config file is not set.")
  58. return
  59. }
  60. config, err := point.LoadConfig(configFile)
  61. if err != nil {
  62. log.Error("Failed to read config file (", configFile, "): ", configFile, err)
  63. return
  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
  72. }
  73. if *test {
  74. fmt.Println("Configuration OK.")
  75. return
  76. }
  77. err = vPoint.Start()
  78. if err != nil {
  79. log.Error("Error starting Point server: ", err)
  80. return
  81. }
  82. for range time.Tick(time.Minute) {
  83. runtime.GC()
  84. }
  85. }