main.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. )
  25. func init() {
  26. defaultConfigFile := ""
  27. workingDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
  28. if err == nil {
  29. defaultConfigFile = filepath.Join(workingDir, "config.json")
  30. }
  31. flag.StringVar(&configFile, "config", defaultConfigFile, "Config file for this Point server.")
  32. }
  33. func main() {
  34. flag.Parse()
  35. core.PrintVersion()
  36. if *version {
  37. return
  38. }
  39. switch *logLevel {
  40. case "debug":
  41. log.SetLogLevel(log.DebugLevel)
  42. case "info":
  43. log.SetLogLevel(log.InfoLevel)
  44. case "warning":
  45. log.SetLogLevel(log.WarningLevel)
  46. case "error":
  47. log.SetLogLevel(log.ErrorLevel)
  48. default:
  49. fmt.Println("Unknown log level: " + *logLevel)
  50. return
  51. }
  52. if len(configFile) == 0 {
  53. log.Error("Config file is not set.")
  54. return
  55. }
  56. config, err := point.LoadConfig(configFile)
  57. if err != nil {
  58. log.Error("Failed to read config file (%s): %v", configFile, err)
  59. return
  60. }
  61. if config.LogConfig != nil && len(config.LogConfig.AccessLog) > 0 {
  62. log.InitAccessLogger(config.LogConfig.AccessLog)
  63. }
  64. vPoint, err := point.NewPoint(config)
  65. if err != nil {
  66. log.Error("Failed to create Point server: %v", err)
  67. return
  68. }
  69. err = vPoint.Start()
  70. if err != nil {
  71. log.Error("Error starting Point server: %v", err)
  72. return
  73. }
  74. finish := make(chan bool)
  75. <-finish
  76. }