main.go 2.3 KB

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