main.go 2.6 KB

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