add decrypted buffer
This commit is contained in:
@@ -14,6 +14,7 @@ namespace CSNetwork
|
||||
private TcpClient? _client;
|
||||
private NetworkStream? _stream;
|
||||
private readonly Octets _receiveOctets; // Underlying buffer for receiving
|
||||
private readonly Octets _decryptedOctets;
|
||||
private readonly OctetsStream _receiveBufferStream; // Stream wrapper (less used now)
|
||||
private BaseSecurity? _inputSecurity = null; // Use abstract Security class
|
||||
private BaseSecurity? _outputSecurity = null; // Use abstract Security class
|
||||
@@ -42,6 +43,7 @@ namespace CSNetwork
|
||||
{
|
||||
// Removed TcpClient initialization here, do it in ConnectAsync
|
||||
_receiveOctets = new Octets(8192); // Initial buffer size
|
||||
_decryptedOctets = new Octets(8192); // initial the decrypted buffer
|
||||
_receiveBufferStream = new OctetsStream(_receiveOctets); // Keep for reference maybe?
|
||||
// Initialize security using the factory
|
||||
_inputSecurity = BaseSecurity.Create(SecurityType.NULLSECURITY);
|
||||
@@ -301,8 +303,8 @@ namespace CSNetwork
|
||||
// Read data from client-server stream into the receive buffer
|
||||
bytesRead = await stream.ReadAsync(
|
||||
_receiveOctets.RawBuffer,
|
||||
currentBufferLength,
|
||||
_receiveOctets.Capacity - currentBufferLength,
|
||||
0,
|
||||
_receiveOctets.Capacity,
|
||||
token
|
||||
);
|
||||
|
||||
@@ -343,12 +345,12 @@ namespace CSNetwork
|
||||
}
|
||||
|
||||
currentBufferLength += bytesRead;
|
||||
_receiveOctets.SetSize(currentBufferLength);
|
||||
_receiveOctets.SetSize(bytesRead);
|
||||
|
||||
// Process the data currently in the buffer
|
||||
ProcessBuffer();
|
||||
// After processing, the buffer might have been compacted, update length
|
||||
currentBufferLength = _receiveOctets.Length;
|
||||
// currentBufferLength = _receiveOctets.Length;
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
@@ -381,7 +383,7 @@ namespace CSNetwork
|
||||
bool securityApplied =
|
||||
currentIsec != null && currentIsec.GetType() != typeof(NullSecurity);
|
||||
Octets dataToProcess; // This will hold the data block we decode from
|
||||
int originalBlockLength = _receiveOctets.Length; // Length before security
|
||||
int originalBlockLength = _decryptedOctets.Length; // Length before security
|
||||
int bytesConsumedFromOriginal = 0; // How many bytes of _receiveOctets are processed
|
||||
|
||||
// 1. Apply Input Security (if active)
|
||||
@@ -389,16 +391,16 @@ namespace CSNetwork
|
||||
{
|
||||
try
|
||||
{
|
||||
// Create a temporary Octets with the current buffer content
|
||||
// Create a temporary Octets with the current raw buffer content
|
||||
Octets currentData = new Octets(
|
||||
_receiveOctets.RawBuffer,
|
||||
0,
|
||||
originalBlockLength
|
||||
);
|
||||
_logger.Log(LogType.Info, $"ProcessBuffer:: raw first byte {currentData.RawBuffer[0]} - Length: {currentData.Length}");
|
||||
// Update returns a NEW Octets object with processed data
|
||||
dataToProcess = currentIsec!.Update(currentData);
|
||||
_logger.Log(LogType.Info, $"ProcessBuffer:: decompressed first byte {dataToProcess.RawBuffer[0]} - Length: {dataToProcess.Length}");
|
||||
_decryptedOctets.Insert(_decryptedOctets.Size, dataToProcess.ByteArray);
|
||||
dataToProcess = _decryptedOctets;
|
||||
// _logger.Log(LogType.Info, $"Input security applied. Original size: {originalBlockLength}, Processed size: {dataToProcess.Length}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -481,12 +483,13 @@ namespace CSNetwork
|
||||
}
|
||||
|
||||
EndProcessing:
|
||||
_receiveOctets.SetSize(0);
|
||||
// 4. Compact the *original* _receiveOctets buffer
|
||||
CompactOriginalBuffer(bytesConsumedFromOriginal, originalBlockLength);
|
||||
CompactDecryptedBuffer(bytesConsumedFromOriginal, originalBlockLength);
|
||||
}
|
||||
|
||||
// *** Helper to compact the original receive buffer ***
|
||||
private void CompactOriginalBuffer(int bytesToConsume, int originalLength)
|
||||
private void CompactDecryptedBuffer(int bytesToConsume, int originalLength)
|
||||
{
|
||||
if (bytesToConsume <= 0 || _receiveOctets == null) // Add null check
|
||||
{
|
||||
@@ -501,9 +504,9 @@ namespace CSNetwork
|
||||
int remaining = originalLength - bytesToConsume;
|
||||
// _logger.Log(LogType.Info, $"Compacting original buffer: Consumed {bytesToConsume}, Moving {remaining} bytes from pos {bytesToConsume}");
|
||||
Buffer.BlockCopy(
|
||||
_receiveOctets.RawBuffer,
|
||||
_decryptedOctets.RawBuffer,
|
||||
bytesToConsume,
|
||||
_receiveOctets.RawBuffer,
|
||||
_decryptedOctets.RawBuffer,
|
||||
0,
|
||||
remaining
|
||||
);
|
||||
|
||||
@@ -80,8 +80,7 @@ namespace CSNetwork.Security
|
||||
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
_index1++; // Note: byte overflows wrap around from 255 to 0 automatically
|
||||
_index2 = (byte)(_index2 + _perm[_index1]);
|
||||
_index2 += _perm[++_index1];
|
||||
|
||||
// Swap perm[index1] and perm[index2]
|
||||
byte temp = _perm[_index1];
|
||||
@@ -89,9 +88,8 @@ namespace CSNetwork.Security
|
||||
_perm[_index2] = temp;
|
||||
|
||||
byte j = (byte)(_perm[_index1] + _perm[_index2]);
|
||||
byte keystreamByte = _perm[j];
|
||||
|
||||
buffer[offset + i] ^= keystreamByte;
|
||||
buffer[offset + i] ^= _perm[j];
|
||||
}
|
||||
|
||||
return data; // Return the modified Octets
|
||||
|
||||
@@ -45,8 +45,6 @@ namespace CSNetwork.Security
|
||||
{
|
||||
if (data == null || data.Length == 0)
|
||||
{
|
||||
_logger.Log(LogType.Debug,$"HoangDev: AF _arcFour data{data.RawBuffer[0]} - Length: {data.Length}");
|
||||
|
||||
return new Octets(); // Return empty if input is empty
|
||||
}
|
||||
// 1. Decrypt using ARCFour
|
||||
@@ -57,8 +55,8 @@ namespace CSNetwork.Security
|
||||
// or just to be safe. Ensure _arcFour.Update returns a *new* Octets.
|
||||
// *** If ARCFourSecurity.Update modified the input Octets in-place, this would be wrong. ***
|
||||
// *** Assuming ARCFourSecurity.Update follows the abstract Security pattern and returns new Octets ***
|
||||
UnityEngine.Debug.Log($"ENCRYPTED: {data.RawBuffer[0]}");
|
||||
decryptedData = _arcFour.Update(data);
|
||||
_logger.Log(LogType.Debug,$"HoangDev: AF _arcFour data{decryptedData.RawBuffer[0]} - Length: {decryptedData.Length}");
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -78,8 +76,8 @@ namespace CSNetwork.Security
|
||||
Octets decompressedData;
|
||||
try
|
||||
{
|
||||
UnityEngine.Debug.Log($"DECRYPTED: {decryptedData.RawBuffer[0]}");
|
||||
decompressedData = decompressor.Update(decryptedData);
|
||||
_logger.Log(LogType.Debug, $"Decompressed {decryptedData.Length} bytes to {decompressedData.Length} bytes. Decompressed Data: {decompressedData.ToString()}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -66,7 +66,8 @@ namespace CSNetwork.Security
|
||||
byte[] fourBytes = new byte[4];
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
fourBytes[i] = legacyInput[rptr + i];
|
||||
if (rptr + i < legacyInput.Count) fourBytes[i] = legacyInput[rptr + i];
|
||||
else fourBytes[i] = 0;
|
||||
}
|
||||
|
||||
uint value = BitConverter.ToUInt32(fourBytes, 0);
|
||||
|
||||
Reference in New Issue
Block a user