main.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "io"
  6. "os"
  7. "os/signal"
  8. "path/filepath"
  9. "syscall"
  10. "v2ray.com/core"
  11. "v2ray.com/core/common/log"
  12. // The following are necessary as they register handlers in their init functions.
  13. _ "v2ray.com/core/proxy/blackhole"
  14. _ "v2ray.com/core/proxy/dokodemo"
  15. _ "v2ray.com/core/proxy/freedom"
  16. _ "v2ray.com/core/proxy/http"
  17. _ "v2ray.com/core/proxy/shadowsocks"
  18. _ "v2ray.com/core/proxy/socks"
  19. _ "v2ray.com/core/proxy/vmess/inbound"
  20. _ "v2ray.com/core/proxy/vmess/outbound"
  21. _ "v2ray.com/core/transport/internet/kcp"
  22. _ "v2ray.com/core/transport/internet/tcp"
  23. _ "v2ray.com/core/transport/internet/udp"
  24. _ "v2ray.com/core/transport/internet/ws"
  25. _ "v2ray.com/core/transport/internet/authenticators/noop"
  26. _ "v2ray.com/core/transport/internet/authenticators/srtp"
  27. _ "v2ray.com/core/transport/internet/authenticators/utp"
  28. )
  29. var (
  30. configFile string
  31. version = flag.Bool("version", false, "Show current version of V2Ray.")
  32. test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.")
  33. format = flag.String("format", "json", "Format of input file.")
  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 startV2Ray() *core.Point {
  44. if len(configFile) == 0 {
  45. log.Error("Config file is not set.")
  46. return nil
  47. }
  48. var configInput io.Reader
  49. if configFile == "stdin:" {
  50. configInput = os.Stdin
  51. } else {
  52. fixedFile := os.ExpandEnv(configFile)
  53. file, err := os.Open(fixedFile)
  54. if err != nil {
  55. log.Error("Config file not readable: ", err)
  56. return nil
  57. }
  58. defer file.Close()
  59. configInput = file
  60. }
  61. config, err := core.LoadConfig(configInput)
  62. if err != nil {
  63. log.Error("Failed to read config file (", configFile, "): ", configFile, err)
  64. return nil
  65. }
  66. vPoint, err := core.NewPoint(config)
  67. if err != nil {
  68. log.Error("Failed to create Point server: ", err)
  69. return nil
  70. }
  71. if *test {
  72. fmt.Println("Configuration OK.")
  73. return nil
  74. }
  75. err = vPoint.Start()
  76. if err != nil {
  77. log.Error("Error starting Point server: ", err)
  78. return nil
  79. }
  80. return vPoint
  81. }
  82. func main() {
  83. flag.Parse()
  84. core.PrintVersion()
  85. if *version {
  86. return
  87. }
  88. if point := startV2Ray(); point != nil {
  89. osSignals := make(chan os.Signal, 1)
  90. signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
  91. <-osSignals
  92. point.Close()
  93. }
  94. log.Close()
  95. }