main.go 2.6 KB

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