main.go 2.6 KB

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