main.go 2.5 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/config/json"
  9. _ "github.com/v2ray/v2ray-core/app/router/rules"
  10. _ "github.com/v2ray/v2ray-core/app/router/rules/config/json"
  11. "github.com/v2ray/v2ray-core/common/log"
  12. "github.com/v2ray/v2ray-core/shell/point"
  13. jsonconf "github.com/v2ray/v2ray-core/shell/point/config/json"
  14. // The following are neccesary as they register handlers in their init functions.
  15. _ "github.com/v2ray/v2ray-core/proxy/blackhole"
  16. _ "github.com/v2ray/v2ray-core/proxy/blackhole/config/json"
  17. _ "github.com/v2ray/v2ray-core/proxy/dokodemo"
  18. _ "github.com/v2ray/v2ray-core/proxy/dokodemo/config/json"
  19. _ "github.com/v2ray/v2ray-core/proxy/freedom"
  20. _ "github.com/v2ray/v2ray-core/proxy/freedom/config/json"
  21. _ "github.com/v2ray/v2ray-core/proxy/socks"
  22. _ "github.com/v2ray/v2ray-core/proxy/socks/config/json"
  23. _ "github.com/v2ray/v2ray-core/proxy/vmess"
  24. _ "github.com/v2ray/v2ray-core/proxy/vmess/config/json"
  25. )
  26. var (
  27. configFile string
  28. logLevel = flag.String("loglevel", "warning", "Level of log info to be printed to console, available value: debug, info, warning, error")
  29. version = flag.Bool("version", false, "Show current version of V2Ray.")
  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 main() {
  40. flag.Parse()
  41. core.PrintVersion()
  42. if *version {
  43. return
  44. }
  45. switch *logLevel {
  46. case "debug":
  47. log.SetLogLevel(log.DebugLevel)
  48. case "info":
  49. log.SetLogLevel(log.InfoLevel)
  50. case "warning":
  51. log.SetLogLevel(log.WarningLevel)
  52. case "error":
  53. log.SetLogLevel(log.ErrorLevel)
  54. default:
  55. fmt.Println("Unknown log level: " + *logLevel)
  56. return
  57. }
  58. if len(configFile) == 0 {
  59. log.Error("Config file is not set.")
  60. return
  61. }
  62. config, err := jsonconf.LoadConfig(configFile)
  63. if err != nil {
  64. log.Error("Failed to read config file (%s): %v", configFile, err)
  65. return
  66. }
  67. if config.LogConfig() != nil && len(config.LogConfig().AccessLog()) > 0 {
  68. log.InitAccessLogger(config.LogConfig().AccessLog())
  69. }
  70. vPoint, err := point.NewPoint(config)
  71. if err != nil {
  72. log.Error("Failed to create Point server: %v", err)
  73. return
  74. }
  75. err = vPoint.Start()
  76. if err != nil {
  77. log.Error("Error starting Point server: %v", err)
  78. return
  79. }
  80. finish := make(chan bool)
  81. <-finish
  82. }