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