Faster AES, Packet Stream, more async changes

This commit is contained in:
Anon 2026-04-04 12:26:27 +02:00
parent 3b7c9c8510
commit 3a20f2e235
19 changed files with 1009 additions and 272 deletions

View file

@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using MinecraftClient.Crypto;
using MinecraftClient.Protocol.PacketPipeline;
namespace MinecraftClient.Protocol.Handlers
{
@ -12,9 +15,14 @@ namespace MinecraftClient.Protocol.Handlers
/// </summary>
public class SocketWrapper
{
readonly TcpClient c;
AesCfb8Stream? s;
bool encrypted = false;
private readonly TcpClient client;
private readonly Stream networkStream;
private readonly SemaphoreSlim sendSemaphore = new(1, 1);
private readonly byte[] singleByteBuffer = new byte[1];
private AesCfb8Stream? encryptedStream;
private Stream readStream;
private Stream writeStream;
private bool encrypted = false;
/// <summary>
/// Initialize a new SocketWrapper
@ -22,7 +30,9 @@ namespace MinecraftClient.Protocol.Handlers
/// <param name="client">TcpClient connected to the server</param>
public SocketWrapper(TcpClient client)
{
c = client;
this.client = client;
networkStream = client.GetStream();
readStream = writeStream = networkStream;
}
/// <summary>
@ -32,7 +42,7 @@ namespace MinecraftClient.Protocol.Handlers
/// <remarks>Silently dropped connection can only be detected by attempting to read/write data</remarks>
public bool IsConnected()
{
return c.Client is not null && c.Connected;
return client.Client is not null && client.Connected;
}
/// <summary>
@ -41,7 +51,7 @@ namespace MinecraftClient.Protocol.Handlers
/// <returns>TRUE if data is available to read</returns>
public bool HasDataAvailable()
{
return c.Client.Available > 0;
return client.Client.Available > 0;
}
/// <summary>
@ -52,39 +62,21 @@ namespace MinecraftClient.Protocol.Handlers
{
if (encrypted)
throw new InvalidOperationException("Stream is already encrypted!?");
s = new AesCfb8Stream(c.GetStream(), secretKey);
encryptedStream = new AesCfb8Stream(networkStream, secretKey);
readStream = writeStream = encryptedStream;
encrypted = true;
}
/// <summary>
/// Network reading method. Read bytes from the socket or encrypted socket.
/// </summary>
private void Receive(byte[] buffer, int start, int offset, SocketFlags f)
public byte ReadByteRAW()
{
int read = 0;
while (read < offset)
{
if (encrypted)
read += s!.Read(buffer, start + read, offset - read);
else
read += c.Client.Receive(buffer, start + read, offset - read, f);
}
readStream.ReadExactly(singleByteBuffer);
return singleByteBuffer[0];
}
private async Task ReceiveAsync(Memory<byte> buffer, CancellationToken cancellationToken)
public async ValueTask<byte> ReadByteRAWAsync(CancellationToken cancellationToken)
{
int read = 0;
while (read < buffer.Length)
{
int currentRead = encrypted
? await s!.ReadAsync(buffer[read..], cancellationToken)
: await c.GetStream().ReadAsync(buffer[read..], cancellationToken);
if (currentRead == 0)
throw new IOException("Connection closed.");
read += currentRead;
}
await readStream.ReadExactlyAsync(singleByteBuffer.AsMemory(0, 1), cancellationToken);
return singleByteBuffer[0];
}
/// <summary>
@ -96,8 +88,8 @@ namespace MinecraftClient.Protocol.Handlers
{
if (length > 0)
{
byte[] cache = new byte[length];
Receive(cache, 0, length, SocketFlags.None);
byte[] cache = GC.AllocateUninitializedArray<byte>(length);
readStream.ReadExactly(cache);
return cache;
}
return Array.Empty<byte>();
@ -107,14 +99,34 @@ namespace MinecraftClient.Protocol.Handlers
{
if (length > 0)
{
byte[] cache = new byte[length];
await ReceiveAsync(cache, cancellationToken);
byte[] cache = GC.AllocateUninitializedArray<byte>(length);
await readStream.ReadExactlyAsync(cache.AsMemory(0, length), cancellationToken);
return cache;
}
return Array.Empty<byte>();
}
internal Tuple<int, Queue<byte>> GetNextPacket(int compressionThreshold, DataTypes dataTypes)
{
int packetLength = ReadNextVarIntRaw();
using PacketReadStream packetStream = new(readStream, packetLength);
byte[] payload = ReadPacketPayload(packetStream, compressionThreshold);
Queue<byte> packetData = new(payload);
int packetId = dataTypes.ReadNextVarInt(packetData);
return new(packetId, packetData);
}
internal async Task<Tuple<int, Queue<byte>>> GetNextPacketAsync(int compressionThreshold, DataTypes dataTypes, CancellationToken cancellationToken)
{
int packetLength = await ReadNextVarIntRawAsync(cancellationToken);
await using PacketReadStream packetStream = new(readStream, packetLength);
byte[] payload = await ReadPacketPayloadAsync(packetStream, compressionThreshold, cancellationToken);
Queue<byte> packetData = new(payload);
int packetId = dataTypes.ReadNextVarInt(packetData);
return new(packetId, packetData);
}
/// <summary>
/// Send raw data to the server.
/// </summary>
@ -124,10 +136,16 @@ namespace MinecraftClient.Protocol.Handlers
if (!IsConnected())
throw new SocketException((int)SocketError.NotConnected);
if (encrypted)
s!.Write(buffer, 0, buffer.Length);
else
c.Client.Send(buffer);
sendSemaphore.Wait();
try
{
writeStream.Write(buffer, 0, buffer.Length);
writeStream.Flush();
}
finally
{
sendSemaphore.Release();
}
}
public async Task SendDataRAWAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken)
@ -135,10 +153,16 @@ namespace MinecraftClient.Protocol.Handlers
if (!IsConnected())
throw new SocketException((int)SocketError.NotConnected);
if (encrypted)
await s!.WriteAsync(buffer, cancellationToken);
else
await c.GetStream().WriteAsync(buffer, cancellationToken);
await sendSemaphore.WaitAsync(cancellationToken);
try
{
await writeStream.WriteAsync(buffer, cancellationToken);
await writeStream.FlushAsync(cancellationToken);
}
finally
{
sendSemaphore.Release();
}
}
/// <summary>
@ -148,12 +172,117 @@ namespace MinecraftClient.Protocol.Handlers
{
try
{
c.Close();
encryptedStream?.Dispose();
client.Close();
}
catch (SocketException) { }
catch (System.IO.IOException) { }
catch (NullReferenceException) { }
catch (ObjectDisposedException) { }
}
private int ReadNextVarIntRaw()
{
int value = 0;
int position = 0;
while (true)
{
byte current = ReadByteRAW();
value |= (current & 0x7F) << position++ * 7;
if (position > 5)
throw new OverflowException("VarInt too big");
if ((current & 0x80) != 0x80)
return value;
}
}
private async Task<int> ReadNextVarIntRawAsync(CancellationToken cancellationToken)
{
int value = 0;
int position = 0;
while (true)
{
byte current = await ReadByteRAWAsync(cancellationToken);
value |= (current & 0x7F) << position++ * 7;
if (position > 5)
throw new OverflowException("VarInt too big");
if ((current & 0x80) != 0x80)
return value;
}
}
private static byte[] ReadPacketPayload(PacketReadStream packetStream, int compressionThreshold)
{
if (compressionThreshold >= 0)
{
int uncompressedLength = ReadNextVarIntRaw(packetStream);
if (uncompressedLength > 0)
{
using ZLibStream zlibStream = new(packetStream, CompressionMode.Decompress, leaveOpen: true);
byte[] payload = GC.AllocateUninitializedArray<byte>(uncompressedLength);
zlibStream.ReadExactly(payload);
return payload;
}
}
return packetStream.ReadRemaining();
}
private static async Task<byte[]> ReadPacketPayloadAsync(PacketReadStream packetStream, int compressionThreshold, CancellationToken cancellationToken)
{
if (compressionThreshold >= 0)
{
int uncompressedLength = await ReadNextVarIntRawAsync(packetStream, cancellationToken);
if (uncompressedLength > 0)
{
await using ZLibStream zlibStream = new(packetStream, CompressionMode.Decompress, leaveOpen: true);
byte[] payload = GC.AllocateUninitializedArray<byte>(uncompressedLength);
await zlibStream.ReadExactlyAsync(payload.AsMemory(0, uncompressedLength), cancellationToken);
return payload;
}
}
return await packetStream.ReadRemainingAsync(cancellationToken);
}
private static int ReadNextVarIntRaw(Stream stream)
{
int value = 0;
int position = 0;
while (true)
{
int current = stream.ReadByte();
if (current < 0)
throw new IOException("Connection closed.");
value |= (current & 0x7F) << position++ * 7;
if (position > 5)
throw new OverflowException("VarInt too big");
if ((current & 0x80) != 0x80)
return value;
}
}
private static async Task<int> ReadNextVarIntRawAsync(Stream stream, CancellationToken cancellationToken)
{
byte[] buffer = new byte[1];
int value = 0;
int position = 0;
while (true)
{
await stream.ReadExactlyAsync(buffer.AsMemory(0, 1), cancellationToken);
byte current = buffer[0];
value |= (current & 0x7F) << position++ * 7;
if (position > 5)
throw new OverflowException("VarInt too big");
if ((current & 0x80) != 0x80)
return value;
}
}
}
}