main.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 necessary 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/shadowsocks"
  17. _ "github.com/v2ray/v2ray-core/proxy/socks"
  18. _ "github.com/v2ray/v2ray-core/proxy/vmess/inbound"
  19. _ "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
  20. )
  21. var (
  22. configFile string
  23. logLevel = flag.String("loglevel", "warning", "Level of log info to be printed to console, available value: debug, info, warning, error")
  24. version = flag.Bool("version", false, "Show current version of V2Ray.")
  25. test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.")
  26. )
  27. func init() {
  28. defaultConfigFile := ""
  29. workingDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
  30. if err == nil {
  31. defaultConfigFile = filepath.Join(workingDir, "config.json")
  32. }
  33. flag.StringVar(&configFile, "config", defaultConfigFile, "Config file for this Point server.")
  34. }
  35. func main() {
  36. flag.Parse()
  37. core.PrintVersion()
  38. if *version {
  39. return
  40. }
  41. switch *logLevel {
  42. case "debug":
  43. log.SetLogLevel(log.DebugLevel)
  44. case "info":
  45. log.SetLogLevel(log.InfoLevel)
  46. case "warning":
  47. log.SetLogLevel(log.WarningLevel)
  48. case "error":
  49. log.SetLogLevel(log.ErrorLevel)
  50. default:
  51. fmt.Println("Unknown log level: " + *logLevel)
  52. return
  53. }
  54. if len(configFile) == 0 {
  55. log.Error("Config file is not set.")
  56. return
  57. }
  58. config, err := point.LoadConfig(configFile)
  59. if err != nil {
  60. log.Error("Failed to read config file (", configFile, "): ", configFile, err)
  61. return
  62. }
  63. if config.LogConfig != nil && len(config.LogConfig.AccessLog) > 0 {
  64. log.InitAccessLogger(config.LogConfig.AccessLog)
  65. }
  66. vPoint, err := point.NewPoint(config)
  67. if err != nil {
  68. log.Error("Failed to create Point server: ", err)
  69. return
  70. }
  71. if *test {
  72. fmt.Println("Configuration OK.")
  73. return
  74. }
  75. err = vPoint.Start()
  76. if err != nil {
  77. log.Error("Error starting Point server: ", err)
  78. return
  79. }
  80. finish := make(chan bool)
  81. <-finish
  82. }