main.go 2.0 KB

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