vmessin.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package vmess
  2. import (
  3. "crypto/md5"
  4. "io"
  5. "net"
  6. "github.com/v2ray/v2ray-core"
  7. v2io "github.com/v2ray/v2ray-core/io"
  8. vmessio "github.com/v2ray/v2ray-core/io/vmess"
  9. "github.com/v2ray/v2ray-core/log"
  10. )
  11. type VMessInboundHandler struct {
  12. vPoint *core.VPoint
  13. clients *core.VUserSet
  14. accepting bool
  15. }
  16. func NewVMessInboundHandler(vp *core.VPoint, clients *core.VUserSet) *VMessInboundHandler {
  17. handler := new(VMessInboundHandler)
  18. handler.vPoint = vp
  19. handler.clients = clients
  20. return handler
  21. }
  22. func (handler *VMessInboundHandler) Listen(port uint8) error {
  23. listener, err := net.Listen("tcp", ":"+string(port))
  24. if err != nil {
  25. return err
  26. }
  27. handler.accepting = true
  28. go handler.AcceptConnections(listener)
  29. return nil
  30. }
  31. func (handler *VMessInboundHandler) AcceptConnections(listener net.Listener) error {
  32. for handler.accepting {
  33. connection, err := listener.Accept()
  34. if err != nil {
  35. return err
  36. }
  37. go handler.HandleConnection(connection)
  38. }
  39. return nil
  40. }
  41. func (handler *VMessInboundHandler) HandleConnection(connection net.Conn) error {
  42. defer connection.Close()
  43. reader := vmessio.NewVMessRequestReader(handler.clients)
  44. request, err := reader.Read(connection)
  45. if err != nil {
  46. return err
  47. }
  48. response := vmessio.NewVMessResponse(request)
  49. connection.Write(response[:])
  50. requestKey := request.RequestKey[:]
  51. requestIV := request.RequestIV[:]
  52. responseKey := md5.Sum(requestKey)
  53. responseIV := md5.Sum(requestIV)
  54. requestReader, err := v2io.NewAesDecryptReader(requestKey, requestIV, connection)
  55. if err != nil {
  56. return err
  57. }
  58. responseWriter, err := v2io.NewAesEncryptWriter(responseKey[:], responseIV[:], connection)
  59. if err != nil {
  60. return err
  61. }
  62. ray := handler.vPoint.NewInboundConnectionAccepted(request.Address)
  63. input := ray.InboundInput()
  64. output := ray.InboundOutput()
  65. finish := make(chan bool, 2)
  66. go handler.dumpInput(requestReader, input, finish)
  67. go handler.dumpOutput(responseWriter, output, finish)
  68. handler.waitForFinish(finish)
  69. return nil
  70. }
  71. func (handler *VMessInboundHandler) dumpInput(reader io.Reader, input chan<- []byte, finish chan<- bool) {
  72. for {
  73. buffer := make([]byte, BufferSize)
  74. nBytes, err := reader.Read(buffer)
  75. if err == io.EOF {
  76. close(input)
  77. finish <- true
  78. break
  79. }
  80. input <- buffer[:nBytes]
  81. }
  82. }
  83. func (handler *VMessInboundHandler) dumpOutput(writer io.Writer, output <-chan []byte, finish chan<- bool) {
  84. for {
  85. buffer, open := <-output
  86. if !open {
  87. finish <- true
  88. break
  89. }
  90. writer.Write(buffer)
  91. }
  92. }
  93. func (handler *VMessInboundHandler) waitForFinish(finish <-chan bool) {
  94. for i := 0; i < 2; i++ {
  95. <-finish
  96. }
  97. }
  98. type VMessInboundHandlerFactory struct {
  99. }
  100. func (factory *VMessInboundHandlerFactory) Create(vp *core.VPoint, rawConfig []byte) *VMessInboundHandler {
  101. config, err := loadInboundConfig(rawConfig)
  102. if err != nil {
  103. panic(log.Error("Failed to load VMess inbound config: %v", err))
  104. }
  105. allowedClients := core.NewVUserSet()
  106. for _, user := range config.AllowedClients {
  107. allowedClients.AddUser(user)
  108. }
  109. return NewVMessInboundHandler(vp, allowedClients)
  110. }