main.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. pointjson "github.com/v2ray/v2ray-core/shell/point/json"
  12. // The following are neccesary as they register handlers in their init functions.
  13. _ "github.com/v2ray/v2ray-core/proxy/blackhole"
  14. _ "github.com/v2ray/v2ray-core/proxy/dokodemo"
  15. _ "github.com/v2ray/v2ray-core/proxy/freedom"
  16. _ "github.com/v2ray/v2ray-core/proxy/http"
  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. )
  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 := pointjson.LoadConfig(configFile)
  58. if err != nil {
  59. log.Error("Failed to read config file (%s): %v", 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: %v", err)
  68. return
  69. }
  70. err = vPoint.Start()
  71. if err != nil {
  72. log.Error("Error starting Point server: %v", err)
  73. return
  74. }
  75. finish := make(chan bool)
  76. <-finish
  77. }