From 4be7a05006cfafbe9449468a204cd923d8242670 Mon Sep 17 00:00:00 2001 From: ReinforceZwei <39955851+ReinforceZwei@users.noreply.github.com> Date: Wed, 11 Jan 2023 17:25:25 +0800 Subject: [PATCH] Player session --- MinecraftClient/McClient.cs | 10 +++ .../Protocol/Handlers/Protocol16.cs | 5 ++ .../Protocol/Handlers/Protocol18.cs | 63 ++++++++++++++++--- MinecraftClient/Protocol/IMinecraftCom.cs | 6 ++ .../Protocol/IMinecraftComHandler.cs | 2 + .../Protocol/ProfileKey/KeyUtils.cs | 9 +++ 6 files changed, 87 insertions(+), 8 deletions(-) diff --git a/MinecraftClient/McClient.cs b/MinecraftClient/McClient.cs index de92a500..9d49ae59 100644 --- a/MinecraftClient/McClient.cs +++ b/MinecraftClient/McClient.cs @@ -9,6 +9,7 @@ using MinecraftClient.Inventory; using MinecraftClient.Logger; using MinecraftClient.Mapping; using MinecraftClient.Protocol; +using MinecraftClient.Protocol.Handlers; using MinecraftClient.Protocol.Handlers.Forge; using MinecraftClient.Protocol.Keys; using MinecraftClient.Protocol.Message; @@ -1127,6 +1128,11 @@ namespace MinecraftClient } } + public PlayerKeyPair? GetPlayerKeyPair() + { + return playerKeyPair; + } + #endregion #region Action methods: Perform an action on the Server @@ -2402,6 +2408,10 @@ namespace MinecraftClient Config.MCSettings.Skin.GetByte(), (byte)Config.MCSettings.MainHand); + if (protocolversion >= Protocol18Handler.MC_1_19_3_Version + && playerKeyPair != null) + handler.SendPlayerSession(playerKeyPair); + if (inventoryHandlingRequested) { diff --git a/MinecraftClient/Protocol/Handlers/Protocol16.cs b/MinecraftClient/Protocol/Handlers/Protocol16.cs index 6ca5a609..aba5c555 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol16.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol16.cs @@ -909,5 +909,10 @@ namespace MinecraftClient.Protocol.Handlers { return false; //Currently not implemented } + + public bool SendPlayerSession(PlayerKeyPair? playerKeyPair) + { + return false; + } } } diff --git a/MinecraftClient/Protocol/Handlers/Protocol18.cs b/MinecraftClient/Protocol/Handlers/Protocol18.cs index dffb9bbc..937e6c22 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol18.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol18.cs @@ -2490,6 +2490,8 @@ namespace MinecraftClient.Protocol.Handlers fields.AddRange(dataTypes.GetLong(0)); // Salt: Long if (protocolVersion < MC_1_19_3_Version) fields.AddRange(dataTypes.GetVarInt(0)); // Signature Length: VarInt (1.19 - 1.19.2) + else + fields.AddRange(dataTypes.GetBool(false)); // Has signature: bool (1.19.3) } else { @@ -2507,23 +2509,34 @@ namespace MinecraftClient.Protocol.Handlers else // 1.19.3 sign = playerKeyPair.PrivateKey.SignMessage(message, uuid, timeNow, ref salt); // TODO? - if (protocolVersion < MC_1_19_3_Version) - fields.AddRange(dataTypes.GetVarInt(sign.Length)); - else + if (protocolVersion >= MC_1_19_3_Version) fields.AddRange(dataTypes.GetBool(true)); + else + fields.AddRange(dataTypes.GetVarInt(sign.Length)); fields.AddRange(sign); } // Signed Preview: Boolean - if (protocolVersion < MC_1_19_3_Version) - fields.AddRange(dataTypes.GetBool(false)); - else + if (protocolVersion >= MC_1_19_3_Version) fields.AddRange(dataTypes.GetVarInt(1)); // message count + else + fields.AddRange(dataTypes.GetBool(false)); if (protocolVersion >= MC_1_19_2_Version) { - // Message Acknowledgment - fields.AddRange(dataTypes.GetAcknowledgment(acknowledgment!, isOnlineMode && Config.Signature.LoginWithSecureProfile)); + if (protocolVersion >= MC_1_19_3_Version) + { + // 1.19.3 + // Acknowledged: BitSet (no idea what is this) + //fields.AddRange(dataTypes.GetVarInt(0)); + fields.AddRange(new byte[3] {0,0,0 }); + } + else + { + // 1.19.2 + // Message Acknowledgment + fields.AddRange(dataTypes.GetAcknowledgment(acknowledgment!, isOnlineMode && Config.Signature.LoginWithSecureProfile)); + } } } @@ -3175,6 +3188,40 @@ namespace MinecraftClient.Protocol.Handlers else { return false; } } + public bool SendPlayerSession(PlayerKeyPair? playerKeyPair) + { + if (protocolVersion >= MC_1_19_3_Version) + { + try + { + List packet = new(); + + var uuid = Guid.NewGuid(); + + byte[] timestampByte = BitConverter.GetBytes(playerKeyPair.GetExpirationMilliseconds()); + Array.Reverse(timestampByte); + var signature = KeyUtils.ComputeHash(timestampByte.Concat(playerKeyPair.PublicKey.Key).ToArray()); + + packet.AddRange(dataTypes.GetUUID(uuid)); + packet.AddRange(dataTypes.GetLong(playerKeyPair.GetExpirationMilliseconds())); + packet.AddRange(dataTypes.GetVarInt(playerKeyPair.PublicKey.Key.Length)); + packet.AddRange(playerKeyPair.PublicKey.Key); + //packet.AddRange(dataTypes.GetVarInt(signature.Length)); + //packet.AddRange(signature); + packet.AddRange(dataTypes.GetVarInt(playerKeyPair.PublicKey.SignatureV2.Length)); + packet.AddRange(playerKeyPair.PublicKey.SignatureV2); + + SendPacket(PacketTypesOut.PlayerSession, packet); + + return true; + } + catch (SocketException) { return false; } + catch (System.IO.IOException) { return false; } + catch (ObjectDisposedException) { return false; } + } + else { return false; } + } + private byte[] GenerateSalt() { byte[] salt = new byte[8]; diff --git a/MinecraftClient/Protocol/IMinecraftCom.cs b/MinecraftClient/Protocol/IMinecraftCom.cs index babd004e..44468e9b 100644 --- a/MinecraftClient/Protocol/IMinecraftCom.cs +++ b/MinecraftClient/Protocol/IMinecraftCom.cs @@ -255,6 +255,12 @@ namespace MinecraftClient.Protocol /// The uuid of the player/entity to spectate/teleport to. bool SendSpectate(Guid uuid); + /// + /// Send player session + /// + /// + bool SendPlayerSession(PlayerKeyPair? playerKeyPair); + /// /// Get net read thread (main thread) ID /// diff --git a/MinecraftClient/Protocol/IMinecraftComHandler.cs b/MinecraftClient/Protocol/IMinecraftComHandler.cs index eab385c3..59823062 100644 --- a/MinecraftClient/Protocol/IMinecraftComHandler.cs +++ b/MinecraftClient/Protocol/IMinecraftComHandler.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using MinecraftClient.Inventory; using MinecraftClient.Logger; using MinecraftClient.Mapping; +using MinecraftClient.Protocol.Keys; using MinecraftClient.Protocol.Message; namespace MinecraftClient.Protocol @@ -27,6 +28,7 @@ namespace MinecraftClient.Protocol string[] GetOnlinePlayers(); Dictionary GetOnlinePlayersWithUUID(); PlayerInfo? GetPlayerInfo(Guid uuid); + PlayerKeyPair? GetPlayerKeyPair(); Location GetCurrentLocation(); World GetWorld(); bool GetIsSupportPreviewsChat(); diff --git a/MinecraftClient/Protocol/ProfileKey/KeyUtils.cs b/MinecraftClient/Protocol/ProfileKey/KeyUtils.cs index 94cce827..12889fac 100644 --- a/MinecraftClient/Protocol/ProfileKey/KeyUtils.cs +++ b/MinecraftClient/Protocol/ProfileKey/KeyUtils.cs @@ -124,6 +124,15 @@ namespace MinecraftClient.Protocol.Keys return data.ToArray(); } + public static byte[] GetSignatureData(string message, DateTimeOffset timestamp, ref byte[] salt, Guid sender, Guid sessionUuid) + { + List data = new(); + + // TODO! + + return data.ToArray(); + } + // https://github.com/mono/mono/blob/master/mcs/class/System.Json/System.Json/JsonValue.cs public static string EscapeString(string src) {