main.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "github.com/v2ray/v2ray-core"
  8. _ "github.com/v2ray/v2ray-core/app/router/rules"
  9. "github.com/v2ray/v2ray-core/common/log"
  10. "github.com/v2ray/v2ray-core/shell/point"
  11. // The following are neccesary as they register handlers in their init functions.
  12. _ "github.com/v2ray/v2ray-core/proxy/blackhole"
  13. _ "github.com/v2ray/v2ray-core/proxy/dokodemo"
  14. _ "github.com/v2ray/v2ray-core/proxy/freedom"
  15. _ "github.com/v2ray/v2ray-core/proxy/http"
  16. _ "github.com/v2ray/v2ray-core/proxy/socks"
  17. _ "github.com/v2ray/v2ray-core/proxy/vmess/inbound"
  18. _ "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
  19. )
  20. var (
  21. configFile string
  22. logLevel = flag.String("loglevel", "warning", "Level of log info to be printed to console, available value: debug, info, warning, error")
  23. version = flag.Bool("version", false, "Show current version of V2Ray.")
  24. test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.")
  25. )
  26. func init() {
  27. defaultConfigFile := ""
  28. workingDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
  29. if err == nil {
  30. defaultConfigFile = filepath.Join(workingDir, "config.json")
  31. }
  32. flag.StringVar(&configFile, "config", defaultConfigFile, "Config file for this Point server.")
  33. }
  34. func main() {
  35. flag.Parse()
  36. core.PrintVersion()
  37. if *version {
  38. return
  39. }
  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
  52. }
  53. if len(configFile) == 0 {
  54. log.Error("Config file is not set.")
  55. return
  56. }
  57. config, err := point.LoadConfig(configFile)
  58. if err != nil {
  59. log.Error("Failed to read config file (", configFile, "): ", configFile, err)
  60. return
  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
  69. }
  70. if *test {
  71. fmt.Println("Configuration OK.")
  72. return
  73. }
  74. err = vPoint.Start()
  75. if err != nil {
  76. log.Error("Error starting Point server: ", err)
  77. return
  78. }
  79. finish := make(chan bool)
  80. <-finish
  81. }