main.go 2.5 KB

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