public int BlindlyRelayData( MyBinaryReader inputStreamReader, BinaryWriter outputStreamWriter, SniffedDataChunk sniffedDataChunk = null) { int noBytesTransferred = 0; int bytesRead = 0; int chunkCounter = 0; byte[] buffer = new byte[MaxBufferSize]; while ((bytesRead = inputStreamReader.Read(buffer, 0, buffer.Length)) > 0) { // Forward received data packets to peer outputStreamWriter.Write(buffer, 0, bytesRead); noBytesTransferred += bytesRead; // If a sniffer data chunk object is defined write application data to it if (sniffedDataChunk != null) { sniffedDataChunk.AppendData(buffer, bytesRead); } chunkCounter++; Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "BlindlyRelayData(): FragmentNo:{0} bytesTransferred:{1}", chunkCounter, bytesRead); } return(noBytesTransferred); }
public int ForwardSingleLineToPeer( MyBinaryReader clientStreamReader, BinaryWriter webServerStreamWriter, Encoding contentCharsetEncoding, byte[] serverNewlineBytes, SniffedDataChunk sniffedDataChunk, bool mustBeProcessed) { int noBytesTransferred = 0; byte[] clientData = clientStreamReader.ReadBinaryLine(); if (clientData?.Length > 0) { // Forward received data packets to peer webServerStreamWriter.Write(clientData, 0, clientData.Length); noBytesTransferred += clientData.Length; // If a sniffer data chunk object is defined write application data to it if (sniffedDataChunk != null) { sniffedDataChunk.AppendData(clientData, clientData.Length); } Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardSingleLineNonprocessedDataToPeer(): clientData.Length:{0}", clientData.Length); } else { Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardSingleLineNonprocessedDataToPeer(): clientData:NULL"); } return(noBytesTransferred); }
public int ForwardNonchunkedDataToPeerChunked( MyBinaryReader inputStreamReader, BinaryWriter outputStreamWriter, Encoding contentCharsetEncoding, int transferredContentLength, byte[] serverNewlineBytes, SniffedDataChunk sniffedDataChunk, bool mustBeProcessed) { int noBytesTransferred = 0; byte[] buffer = new byte[MaxBufferSize]; int totalTransferredBytes = 0; int bytesRead = 0; while (totalTransferredBytes < transferredContentLength) { // Read data from peer 1 int maxDataToTransfer = transferredContentLength - totalTransferredBytes; bytesRead = inputStreamReader.Read(buffer, 0, buffer.Length); if (bytesRead <= 0) { Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardNonchunkedDataToPeer3(2:DATA), No data to transfer"); break; } // if (sniffedDataChunk != null) { sniffedDataChunk.AppendData(buffer, bytesRead); } // Encode received bytes to the announced format DataChunk serverDataChunk = new DataChunk(buffer, bytesRead, this.requestObj.ServerResponseObj.ContentTypeEncoding.ContentCharsetEncoding); if (mustBeProcessed == true) { Lib.PluginCalls.ServerDataTransfer(this.requestObj, serverDataChunk); } // Send chunk size to recepient string chunkSizeHexStringTmp = serverDataChunk.ContentDataLength.ToString("x"); byte[] chunkSizeDeclaration = contentCharsetEncoding.GetBytes(chunkSizeHexStringTmp); outputStreamWriter.Write(chunkSizeDeclaration, 0, chunkSizeDeclaration.Length); outputStreamWriter.Write(serverNewlineBytes, 0, serverNewlineBytes.Length); // Send data packet to recipient outputStreamWriter.Write(serverDataChunk.ContentData, 0, serverDataChunk.ContentDataLength); outputStreamWriter.Write(serverNewlineBytes, 0, serverNewlineBytes.Length); outputStreamWriter.Flush(); noBytesTransferred += serverDataChunk.ContentDataLength; Logging.Instance.LogMessage( this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardNonchunkedDataToPeer3(): Data successfully relayed to client. ContentDataLength:{0} chunkSizeHexStringTmp={1} noBytesTransferred={2}", serverDataChunk.ContentDataLength, chunkSizeHexStringTmp, noBytesTransferred); totalTransferredBytes += bytesRead; } // Send trailing "0 length" chunk string chunkSizeZeroHexString = 0.ToString("x"); byte[] chunkSizeZeroDeclaration = contentCharsetEncoding.GetBytes(chunkSizeZeroHexString); outputStreamWriter.Write(chunkSizeZeroDeclaration, 0, chunkSizeZeroDeclaration.Length); outputStreamWriter.Write(serverNewlineBytes, 0, serverNewlineBytes.Length); outputStreamWriter.Write(serverNewlineBytes, 0, serverNewlineBytes.Length); Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardNonchunkedDataToPeer3(2:DATA): Total amount of transferred data={0}", totalTransferredBytes); return(noBytesTransferred); }
public int ForwardNonchunkedDataToPeerNonChunked( MyBinaryReader inputStreamReader, BinaryWriter outputStreamWriter, Encoding contentCharsetEncoding, int transferredContentLength, SniffedDataChunk sniffedDataChunk, bool mustBeProcessed) { int noBytesTransferred = 0; MemoryStream memStream = new MemoryStream(); byte[] buffer = new byte[MaxBufferSize]; int totalTransferredBytes = 0; int bytesRead = 0; while (totalTransferredBytes < transferredContentLength) { // Read data from peer 1 int maxDataToTransfer = transferredContentLength - totalTransferredBytes; bytesRead = inputStreamReader.Read(buffer, 0, buffer.Length); if (bytesRead <= 0) { Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardNonchunkedDataToPeer2(2:DATA), No data to transfer"); break; } // Write data to memory stream memStream.Write(buffer, 0, bytesRead); if (sniffedDataChunk != null) { sniffedDataChunk.AppendData(buffer, bytesRead); } totalTransferredBytes += bytesRead; Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardNonchunkedDataToPeer2(2:DATA, TotalDataToTransfer:{0}): Relaying data from: Client -> Server (bytesRead:{1} totalTransferredBytes:{2})", buffer.Length, bytesRead, totalTransferredBytes); } byte[] dataPacket = memStream.ToArray(); //if (dataPacket.Length != MaxBufferSize) //{ // throw new Exception("The announced content length and the amount of received data are not the same"); //} // Encode received bytes to the announced format DataChunk serverDataChunk = new DataChunk(dataPacket, dataPacket.Length, this.requestObj.ServerResponseObj.ContentTypeEncoding.ContentCharsetEncoding); if (mustBeProcessed == true) { Lib.PluginCalls.ServerDataTransfer(this.requestObj, serverDataChunk); } // Send data packet to recipient outputStreamWriter.Write(serverDataChunk.ContentData, 0, serverDataChunk.ContentDataLength); outputStreamWriter.Flush(); noBytesTransferred += serverDataChunk.ContentDataLength; Logging.Instance.LogMessage( this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardNonchunkedDataToPeer2(): Data successfully relayed to client. ContentDataSize:{0})", serverDataChunk.ContentDataLength); Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardNonchunkedDataToPeer2(2:DATA): Total amount of transferred data={0}", totalTransferredBytes); return(noBytesTransferred); }