Player session

This commit is contained in:
ReinforceZwei 2023-01-11 17:25:25 +08:00
parent 08da49756f
commit 4be7a05006
6 changed files with 87 additions and 8 deletions

View file

@ -9,6 +9,7 @@ using MinecraftClient.Inventory;
using MinecraftClient.Logger; using MinecraftClient.Logger;
using MinecraftClient.Mapping; using MinecraftClient.Mapping;
using MinecraftClient.Protocol; using MinecraftClient.Protocol;
using MinecraftClient.Protocol.Handlers;
using MinecraftClient.Protocol.Handlers.Forge; using MinecraftClient.Protocol.Handlers.Forge;
using MinecraftClient.Protocol.Keys; using MinecraftClient.Protocol.Keys;
using MinecraftClient.Protocol.Message; using MinecraftClient.Protocol.Message;
@ -1127,6 +1128,11 @@ namespace MinecraftClient
} }
} }
public PlayerKeyPair? GetPlayerKeyPair()
{
return playerKeyPair;
}
#endregion #endregion
#region Action methods: Perform an action on the Server #region Action methods: Perform an action on the Server
@ -2402,6 +2408,10 @@ namespace MinecraftClient
Config.MCSettings.Skin.GetByte(), Config.MCSettings.Skin.GetByte(),
(byte)Config.MCSettings.MainHand); (byte)Config.MCSettings.MainHand);
if (protocolversion >= Protocol18Handler.MC_1_19_3_Version
&& playerKeyPair != null)
handler.SendPlayerSession(playerKeyPair);
if (inventoryHandlingRequested) if (inventoryHandlingRequested)
{ {

View file

@ -909,5 +909,10 @@ namespace MinecraftClient.Protocol.Handlers
{ {
return false; //Currently not implemented return false; //Currently not implemented
} }
public bool SendPlayerSession(PlayerKeyPair? playerKeyPair)
{
return false;
}
} }
} }

View file

@ -2490,6 +2490,8 @@ namespace MinecraftClient.Protocol.Handlers
fields.AddRange(dataTypes.GetLong(0)); // Salt: Long fields.AddRange(dataTypes.GetLong(0)); // Salt: Long
if (protocolVersion < MC_1_19_3_Version) if (protocolVersion < MC_1_19_3_Version)
fields.AddRange(dataTypes.GetVarInt(0)); // Signature Length: VarInt (1.19 - 1.19.2) 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 else
{ {
@ -2507,23 +2509,34 @@ namespace MinecraftClient.Protocol.Handlers
else // 1.19.3 else // 1.19.3
sign = playerKeyPair.PrivateKey.SignMessage(message, uuid, timeNow, ref salt); // TODO? sign = playerKeyPair.PrivateKey.SignMessage(message, uuid, timeNow, ref salt); // TODO?
if (protocolVersion < MC_1_19_3_Version) if (protocolVersion >= MC_1_19_3_Version)
fields.AddRange(dataTypes.GetVarInt(sign.Length));
else
fields.AddRange(dataTypes.GetBool(true)); fields.AddRange(dataTypes.GetBool(true));
else
fields.AddRange(dataTypes.GetVarInt(sign.Length));
fields.AddRange(sign); fields.AddRange(sign);
} }
// Signed Preview: Boolean // Signed Preview: Boolean
if (protocolVersion < MC_1_19_3_Version) if (protocolVersion >= MC_1_19_3_Version)
fields.AddRange(dataTypes.GetBool(false));
else
fields.AddRange(dataTypes.GetVarInt(1)); // message count fields.AddRange(dataTypes.GetVarInt(1)); // message count
else
fields.AddRange(dataTypes.GetBool(false));
if (protocolVersion >= MC_1_19_2_Version) if (protocolVersion >= MC_1_19_2_Version)
{ {
// Message Acknowledgment if (protocolVersion >= MC_1_19_3_Version)
fields.AddRange(dataTypes.GetAcknowledgment(acknowledgment!, isOnlineMode && Config.Signature.LoginWithSecureProfile)); {
// 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; } else { return false; }
} }
public bool SendPlayerSession(PlayerKeyPair? playerKeyPair)
{
if (protocolVersion >= MC_1_19_3_Version)
{
try
{
List<byte> 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() private byte[] GenerateSalt()
{ {
byte[] salt = new byte[8]; byte[] salt = new byte[8];

View file

@ -255,6 +255,12 @@ namespace MinecraftClient.Protocol
/// <param name="uuid">The uuid of the player/entity to spectate/teleport to.</param> /// <param name="uuid">The uuid of the player/entity to spectate/teleport to.</param>
bool SendSpectate(Guid uuid); bool SendSpectate(Guid uuid);
/// <summary>
/// Send player session
/// </summary>
/// <returns></returns>
bool SendPlayerSession(PlayerKeyPair? playerKeyPair);
/// <summary> /// <summary>
/// Get net read thread (main thread) ID /// Get net read thread (main thread) ID
/// </summary> /// </summary>

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using MinecraftClient.Inventory; using MinecraftClient.Inventory;
using MinecraftClient.Logger; using MinecraftClient.Logger;
using MinecraftClient.Mapping; using MinecraftClient.Mapping;
using MinecraftClient.Protocol.Keys;
using MinecraftClient.Protocol.Message; using MinecraftClient.Protocol.Message;
namespace MinecraftClient.Protocol namespace MinecraftClient.Protocol
@ -27,6 +28,7 @@ namespace MinecraftClient.Protocol
string[] GetOnlinePlayers(); string[] GetOnlinePlayers();
Dictionary<string, string> GetOnlinePlayersWithUUID(); Dictionary<string, string> GetOnlinePlayersWithUUID();
PlayerInfo? GetPlayerInfo(Guid uuid); PlayerInfo? GetPlayerInfo(Guid uuid);
PlayerKeyPair? GetPlayerKeyPair();
Location GetCurrentLocation(); Location GetCurrentLocation();
World GetWorld(); World GetWorld();
bool GetIsSupportPreviewsChat(); bool GetIsSupportPreviewsChat();

View file

@ -124,6 +124,15 @@ namespace MinecraftClient.Protocol.Keys
return data.ToArray(); return data.ToArray();
} }
public static byte[] GetSignatureData(string message, DateTimeOffset timestamp, ref byte[] salt, Guid sender, Guid sessionUuid)
{
List<byte> data = new();
// TODO!
return data.ToArray();
}
// https://github.com/mono/mono/blob/master/mcs/class/System.Json/System.Json/JsonValue.cs // https://github.com/mono/mono/blob/master/mcs/class/System.Json/System.Json/JsonValue.cs
public static string EscapeString(string src) public static string EscapeString(string src)
{ {