main.go 2.4 KB

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