vmessin.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package vmess
  2. import (
  3. "crypto/md5"
  4. "io"
  5. "net"
  6. "strconv"
  7. "github.com/v2ray/v2ray-core"
  8. v2io "github.com/v2ray/v2ray-core/common/io"
  9. v2net "github.com/v2ray/v2ray-core/common/net"
  10. "github.com/v2ray/v2ray-core/log"
  11. protocol "github.com/v2ray/v2ray-core/proxy/vmess/protocol"
  12. )
  13. type VMessInboundHandler struct {
  14. vPoint *core.Point
  15. clients protocol.UserSet
  16. accepting bool
  17. }
  18. func NewVMessInboundHandler(vp *core.Point, clients protocol.UserSet) *VMessInboundHandler {
  19. return &VMessInboundHandler{
  20. vPoint: vp,
  21. clients: clients,
  22. }
  23. }
  24. func (handler *VMessInboundHandler) Listen(port uint16) error {
  25. listener, err := net.Listen("tcp", ":"+strconv.Itoa(int(port)))
  26. if err != nil {
  27. return log.Error("Unable to listen tcp:%d", port)
  28. }
  29. handler.accepting = true
  30. go handler.AcceptConnections(listener)
  31. return nil
  32. }
  33. func (handler *VMessInboundHandler) AcceptConnections(listener net.Listener) error {
  34. for handler.accepting {
  35. connection, err := listener.Accept()
  36. if err != nil {
  37. return log.Error("Failed to accpet connection: %s", err.Error())
  38. }
  39. go handler.HandleConnection(connection)
  40. }
  41. return nil
  42. }
  43. func (handler *VMessInboundHandler) HandleConnection(connection net.Conn) error {
  44. defer connection.Close()
  45. reader := protocol.NewVMessRequestReader(handler.clients)
  46. request, err := reader.Read(connection)
  47. if err != nil {
  48. log.Warning("VMessIn: Invalid request from (%s): %v", connection.RemoteAddr().String(), err)
  49. return err
  50. }
  51. log.Debug("VMessIn: Received request for %s", request.Address.String())
  52. ray := handler.vPoint.NewInboundConnectionAccepted(request.Address)
  53. input := ray.InboundInput()
  54. output := ray.InboundOutput()
  55. readFinish := make(chan bool)
  56. writeFinish := make(chan bool)
  57. go handleInput(request, connection, input, readFinish)
  58. responseKey := md5.Sum(request.RequestKey[:])
  59. responseIV := md5.Sum(request.RequestIV[:])
  60. response := protocol.NewVMessResponse(request)
  61. responseWriter, err := v2io.NewAesEncryptWriter(responseKey[:], responseIV[:], connection)
  62. if err != nil {
  63. return log.Error("VMessIn: Failed to create encrypt writer: %v", err)
  64. }
  65. // Optimize for small response packet
  66. buffer := make([]byte, 0, 1024)
  67. buffer = append(buffer, response[:]...)
  68. if data, open := <-output; open {
  69. buffer = append(buffer, data...)
  70. responseWriter.Write(buffer)
  71. go handleOutput(request, responseWriter, output, writeFinish)
  72. <-writeFinish
  73. }
  74. if tcpConn, ok := connection.(*net.TCPConn); ok {
  75. tcpConn.CloseWrite()
  76. }
  77. <-readFinish
  78. return nil
  79. }
  80. func handleInput(request *protocol.VMessRequest, reader io.Reader, input chan<- []byte, finish chan<- bool) {
  81. defer close(input)
  82. defer close(finish)
  83. requestReader, err := v2io.NewAesDecryptReader(request.RequestKey[:], request.RequestIV[:], reader)
  84. if err != nil {
  85. log.Error("VMessIn: Failed to create decrypt reader: %v", err)
  86. return
  87. }
  88. v2net.ReaderToChan(input, requestReader)
  89. }
  90. func handleOutput(request *protocol.VMessRequest, writer io.Writer, output <-chan []byte, finish chan<- bool) {
  91. v2net.ChanToWriter(writer, output)
  92. close(finish)
  93. }
  94. type VMessInboundHandlerFactory struct {
  95. }
  96. func (factory *VMessInboundHandlerFactory) Create(vp *core.Point, rawConfig []byte) (core.InboundConnectionHandler, error) {
  97. config, err := loadInboundConfig(rawConfig)
  98. if err != nil {
  99. panic(log.Error("VMessIn: Failed to load VMess inbound config: %v", err))
  100. }
  101. allowedClients := protocol.NewTimedUserSet()
  102. for _, client := range config.AllowedClients {
  103. user, err := client.ToUser()
  104. if err != nil {
  105. panic(log.Error("VMessIn: Failed to parse user id %s: %v", client.Id, err))
  106. }
  107. allowedClients.AddUser(user)
  108. }
  109. return NewVMessInboundHandler(vp, allowedClients), nil
  110. }
  111. func init() {
  112. core.RegisterInboundConnectionHandlerFactory("vmess", &VMessInboundHandlerFactory{})
  113. }