From db17babe58987bc306ee49404d2b486b5fdeb17a Mon Sep 17 00:00:00 2001 From: BruceChen Date: Sun, 4 Sep 2022 17:34:12 +0800 Subject: [PATCH] Bug fix --- MinecraftClient/Mapping/Chunk.cs | 23 ++++++------------- MinecraftClient/Mapping/Movement.cs | 6 ++--- MinecraftClient/McClient.cs | 2 +- .../Protocol/Handlers/Protocol18.cs | 9 +++++--- MinecraftClient/Protocol/PlayerInfo.cs | 2 +- .../Protocol/ProfileKey/PublicKey.cs | 7 ++++-- 6 files changed, 23 insertions(+), 26 deletions(-) diff --git a/MinecraftClient/Mapping/Chunk.cs b/MinecraftClient/Mapping/Chunk.cs index 89991bfd..90ef604e 100644 --- a/MinecraftClient/Mapping/Chunk.cs +++ b/MinecraftClient/Mapping/Chunk.cs @@ -19,17 +19,8 @@ namespace MinecraftClient.Mapping /// /// Blocks contained into the chunk /// - private readonly Block[] blocks; + private readonly Block[] blocks = new Block[SizeY * SizeZ * SizeX]; - public Chunk() - { - this.blocks = new Block[SizeY * SizeZ * SizeX]; - } - - public Chunk(Block[] blocks) - { - this.blocks = blocks; - } /// /// Read, or set the specified block @@ -43,22 +34,22 @@ namespace MinecraftClient.Mapping get { if (blockX < 0 || blockX >= SizeX) - throw new ArgumentOutOfRangeException("blockX", "Must be between 0 and " + (SizeX - 1) + " (inclusive)"); + throw new ArgumentOutOfRangeException(nameof(blockX), "Must be between 0 and " + (SizeX - 1) + " (inclusive)"); if (blockY < 0 || blockY >= SizeY) - throw new ArgumentOutOfRangeException("blockY", "Must be between 0 and " + (SizeY - 1) + " (inclusive)"); + throw new ArgumentOutOfRangeException(nameof(blockY), "Must be between 0 and " + (SizeY - 1) + " (inclusive)"); if (blockZ < 0 || blockZ >= SizeZ) - throw new ArgumentOutOfRangeException("blockZ", "Must be between 0 and " + (SizeZ - 1) + " (inclusive)"); + throw new ArgumentOutOfRangeException(nameof(blockZ), "Must be between 0 and " + (SizeZ - 1) + " (inclusive)"); return blocks[(blockY << 8) | (blockZ << 4) | blockX]; } set { if (blockX < 0 || blockX >= SizeX) - throw new ArgumentOutOfRangeException("blockX", "Must be between 0 and " + (SizeX - 1) + " (inclusive)"); + throw new ArgumentOutOfRangeException(nameof(blockX), "Must be between 0 and " + (SizeX - 1) + " (inclusive)"); if (blockY < 0 || blockY >= SizeY) - throw new ArgumentOutOfRangeException("blockY", "Must be between 0 and " + (SizeY - 1) + " (inclusive)"); + throw new ArgumentOutOfRangeException(nameof(blockY), "Must be between 0 and " + (SizeY - 1) + " (inclusive)"); if (blockZ < 0 || blockZ >= SizeZ) - throw new ArgumentOutOfRangeException("blockZ", "Must be between 0 and " + (SizeZ - 1) + " (inclusive)"); + throw new ArgumentOutOfRangeException(nameof(blockZ), "Must be between 0 and " + (SizeZ - 1) + " (inclusive)"); blocks[(blockY << 8) | (blockZ << 4) | blockX] = value; } diff --git a/MinecraftClient/Mapping/Movement.cs b/MinecraftClient/Mapping/Movement.cs index 0853bbb2..be573c9e 100644 --- a/MinecraftClient/Mapping/Movement.cs +++ b/MinecraftClient/Mapping/Movement.cs @@ -134,10 +134,10 @@ namespace MinecraftClient.Mapping /// How long to wait before stopping computation /// When location is unreachable, computation will reach timeout, then optionally fallback to a close location within maxOffset /// A list of locations, or null if calculation failed - public static Queue CalculatePath(World world, Location start, Location goal, bool allowUnsafe, int maxOffset, int minOffset, TimeSpan timeout) + public static Queue? CalculatePath(World world, Location start, Location goal, bool allowUnsafe, int maxOffset, int minOffset, TimeSpan timeout) { CancellationTokenSource cts = new CancellationTokenSource(); - Task> pathfindingTask = Task.Factory.StartNew(() => Movement.CalculatePath(world, start, goal, allowUnsafe, maxOffset, minOffset, cts.Token)); + Task?> pathfindingTask = Task.Factory.StartNew(() => Movement.CalculatePath(world, start, goal, allowUnsafe, maxOffset, minOffset, cts.Token)); pathfindingTask.Wait(timeout); if (!pathfindingTask.IsCompleted) { @@ -161,7 +161,7 @@ namespace MinecraftClient.Mapping /// Do not get closer of destination than specified distance /// Token for stopping computation after a certain time /// A list of locations, or null if calculation failed - public static Queue CalculatePath(World world, Location start, Location goal, bool allowUnsafe, int maxOffset, int minOffset, CancellationToken ct) + public static Queue? CalculatePath(World world, Location start, Location goal, bool allowUnsafe, int maxOffset, int minOffset, CancellationToken ct) { // This is a bad configuration if (minOffset > maxOffset) diff --git a/MinecraftClient/McClient.cs b/MinecraftClient/McClient.cs index 3d7cb684..a0bb0069 100644 --- a/MinecraftClient/McClient.cs +++ b/MinecraftClient/McClient.cs @@ -55,7 +55,7 @@ namespace MinecraftClient private bool locationReceived = false; private World world = new(); private Queue steps; - private Queue path; + private Queue? path; private Location location; private float? _yaw; // Used for calculation ONLY!!! Doesn't reflect the client yaw private float? _pitch; // Used for calculation ONLY!!! Doesn't reflect the client pitch diff --git a/MinecraftClient/Protocol/Handlers/Protocol18.cs b/MinecraftClient/Protocol/Handlers/Protocol18.cs index 6b0b39af..8751ca5c 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol18.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol18.cs @@ -572,6 +572,7 @@ namespace MinecraftClient.Protocol.Handlers byte[] bodyDigest = dataTypes.ReadNextByteArray(packetData); bool verifyResult; + if (!isOnlineMode) verifyResult = false; else if (senderUUID == handler.GetUserUuid()) @@ -579,6 +580,7 @@ namespace MinecraftClient.Protocol.Handlers else { PlayerInfo? player = handler.GetPlayerInfo(senderUUID); + if (player == null || !player.IsMessageChainLegal()) verifyResult = false; else @@ -586,7 +588,7 @@ namespace MinecraftClient.Protocol.Handlers bool lastVerifyResult = player.IsMessageChainLegal(); verifyResult = player.VerifyMessageHead(ref precedingSignature, ref headerSignature, ref bodyDigest); if (lastVerifyResult && !verifyResult) - log.Warn("Player " + player.DisplayName + "'s message chain is broken!"); + log.Warn("Player " + player.Name + "'s message chain is broken!"); } } } @@ -1705,7 +1707,7 @@ namespace MinecraftClient.Protocol.Handlers if (protocolVersion >= MC_1_19_2_Version) fullLoginPacket.AddRange(dataTypes.GetArray(playerKeyPair.PublicKey.SignatureV2!)); // Public key signature received from Microsoft API else - fullLoginPacket.AddRange(dataTypes.GetArray(playerKeyPair.PublicKey.Signature!)); // Public key signature received from Microsoft API + fullLoginPacket.AddRange(dataTypes.GetArray(playerKeyPair.PublicKey.Signature!)); // Public key signature received from Microsoft API } } if (protocolVersion >= MC_1_19_2_Version) @@ -1769,7 +1771,6 @@ namespace MinecraftClient.Protocol.Handlers if (serverIDhash != "-") { log.Info(Translations.Get("mcc.session")); - string serverHash = CryptoHandler.getServerHash(serverIDhash, serverPublicKey, secretKey); bool needCheckSession = true; if (session.ServerPublicKey != null && session.SessionPreCheckTask != null @@ -1782,6 +1783,8 @@ namespace MinecraftClient.Protocol.Handlers if (needCheckSession) { + string serverHash = CryptoHandler.getServerHash(serverIDhash, serverPublicKey, secretKey); + if (ProtocolHandler.SessionCheck(uuid, sessionID, serverHash)) { session.ServerIDhash = serverIDhash; diff --git a/MinecraftClient/Protocol/PlayerInfo.cs b/MinecraftClient/Protocol/PlayerInfo.cs index aa482b89..73d96018 100644 --- a/MinecraftClient/Protocol/PlayerInfo.cs +++ b/MinecraftClient/Protocol/PlayerInfo.cs @@ -148,7 +148,7 @@ namespace MinecraftClient.Protocol if (this.precedingSignature != null && !this.precedingSignature.SequenceEqual(precedingSignature!)) return false; - bool res = PublicKey.VerifyHeader(ref bodyDigest, ref headerSignature); + bool res = PublicKey.VerifyHeader(Uuid, ref bodyDigest, ref headerSignature, ref precedingSignature); this.lastMessageVerified = res; this.precedingSignature = headerSignature; diff --git a/MinecraftClient/Protocol/ProfileKey/PublicKey.cs b/MinecraftClient/Protocol/ProfileKey/PublicKey.cs index 6bec8ae9..8ceb55e1 100644 --- a/MinecraftClient/Protocol/ProfileKey/PublicKey.cs +++ b/MinecraftClient/Protocol/ProfileKey/PublicKey.cs @@ -88,9 +88,12 @@ namespace MinecraftClient.Protocol.Keys /// Message body hash /// Message signature /// Is this message header vaild - public bool VerifyHeader(ref byte[] bodyDigest, ref byte[] signature) + public bool VerifyHeader(Guid uuid, ref byte[] bodyDigest, ref byte[] signature, ref byte[]? precedingSignature) { - return VerifyData(bodyDigest, signature); + + byte[] msgSignData = KeyUtils.GetSignatureData(precedingSignature, uuid, bodyDigest); + + return VerifyData(msgSignData, signature); } }