func (handler *VMessInboundHandler) HandleConnection(connection net.Conn) error { defer connection.Close() reader := vmessio.NewVMessRequestReader(handler.clients) request, err := reader.Read(connection) if err != nil { log.Debug("Failed to parse VMess request: %v", err) return err } log.Debug("Received request for %s", request.Address.String()) ray := handler.vPoint.NewInboundConnectionAccepted(request.Address) input := ray.InboundInput() output := ray.InboundOutput() readFinish := make(chan bool) writeFinish := make(chan bool) go handleInput(request, connection, input, readFinish) responseKey := md5.Sum(request.RequestKey[:]) responseIV := md5.Sum(request.RequestIV[:]) response := vmessio.NewVMessResponse(request) responseWriter, err := v2io.NewAesEncryptWriter(responseKey[:], responseIV[:], connection) if err != nil { return log.Error("Failed to create encrypt writer: %v", err) } //responseWriter.Write(response[:]) // Optimize for small response packet buffer := make([]byte, 0, 1024) buffer = append(buffer, response[:]...) data, open := <-output if open { buffer = append(buffer, data...) } responseWriter.Write(buffer) if open { go handleOutput(request, responseWriter, output, writeFinish) } else { close(writeFinish) } <-writeFinish if tcpConn, ok := connection.(*net.TCPConn); ok { log.Debug("VMessIn closing write") tcpConn.CloseWrite() } <-readFinish return nil }
func (handler *VMessInboundHandler) HandleConnection(connection net.Conn) error { defer connection.Close() reader := vmessio.NewVMessRequestReader(handler.clients) request, err := reader.Read(connection) if err != nil { log.Debug("Failed to parse VMess request: %v", err) return err } log.Debug("Received request for %s", request.Address.String()) response := vmessio.NewVMessResponse(request) nBytes, err := connection.Write(response[:]) if err != nil { return log.Error("Failed to write VMess response (%d bytes): %v", nBytes, err) } requestKey := request.RequestKey[:] requestIV := request.RequestIV[:] responseKey := md5.Sum(requestKey) responseIV := md5.Sum(requestIV) requestReader, err := v2io.NewAesDecryptReader(requestKey, requestIV, connection) if err != nil { return log.Error("Failed to create decrypt reader: %v", err) } responseWriter, err := v2io.NewAesEncryptWriter(responseKey[:], responseIV[:], connection) if err != nil { return log.Error("Failed to create encrypt writer: %v", err) } ray := handler.vPoint.NewInboundConnectionAccepted(request.Address) input := ray.InboundInput() output := ray.InboundOutput() readFinish := make(chan bool) writeFinish := make(chan bool) go handler.dumpInput(requestReader, input, readFinish) go handler.dumpOutput(responseWriter, output, writeFinish) <-writeFinish if tcpConn, ok := connection.(*net.TCPConn); ok { log.Debug("VMessIn closing write") tcpConn.CloseWrite() } <-readFinish return nil }
func (handler *VMessInboundHandler) HandleConnection(connection net.Conn) error { defer connection.Close() reader := vmessio.NewVMessRequestReader(handler.clients) request, err := reader.Read(connection) if err != nil { return err } log.Debug("Received request for %s", request.Address.String()) response := vmessio.NewVMessResponse(request) nBytes, err := connection.Write(response[:]) log.Debug("Writing VMess response %v", response) if err != nil { return log.Error("Failed to write VMess response (%d bytes): %v", nBytes, err) } requestKey := request.RequestKey[:] requestIV := request.RequestIV[:] responseKey := md5.Sum(requestKey) responseIV := md5.Sum(requestIV) requestReader, err := v2io.NewAesDecryptReader(requestKey, requestIV, connection) if err != nil { return log.Error("Failed to create decrypt reader: %v", err) } responseWriter, err := v2io.NewAesEncryptWriter(responseKey[:], responseIV[:], connection) if err != nil { return log.Error("Failed to create encrypt writer: %v", err) } ray := handler.vPoint.NewInboundConnectionAccepted(request.Address) input := ray.InboundInput() output := ray.InboundOutput() finish := make(chan bool, 2) go handler.dumpInput(requestReader, input, finish) go handler.dumpOutput(responseWriter, output, finish) handler.waitForFinish(finish) return nil }
func (handler *VMessInboundHandler) HandleConnection(connection net.Conn) error { defer connection.Close() reader := vmessio.NewVMessRequestReader(handler.clients) request, err := reader.Read(connection) if err != nil { return err } response := vmessio.NewVMessResponse(request) connection.Write(response[:]) requestKey := request.RequestKey[:] requestIV := request.RequestIV[:] responseKey := md5.Sum(requestKey) responseIV := md5.Sum(requestIV) requestReader, err := v2io.NewAesDecryptReader(requestKey, requestIV, connection) if err != nil { return err } responseWriter, err := v2io.NewAesEncryptWriter(responseKey[:], responseIV[:], connection) if err != nil { return err } ray := handler.vPoint.NewInboundConnectionAccepted(request.Address) input := ray.InboundInput() output := ray.InboundOutput() finish := make(chan bool, 2) go handler.dumpInput(requestReader, input, finish) go handler.dumpOutput(responseWriter, output, finish) handler.waitForFinish(finish) return nil }