Skip to content

Commit

Permalink
[skip ci] Add Yggdrasil Login (authlib-injector support)
Browse files Browse the repository at this point in the history
Add Yggdrasil Login (authlib-injector support)
  • Loading branch information
milutinke authored Nov 25, 2023
2 parents f50bfbb + a08bfca commit c6da4e2
Show file tree
Hide file tree
Showing 10 changed files with 2,287 additions and 2,027 deletions.
7 changes: 4 additions & 3 deletions MinecraftClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ private static void InitializeClient()
else
{
// Validate cached session or login new session.
if (Config.Main.Advanced.SessionCache != CacheType.none && SessionCache.Contains(loginLower))
if (Config.Main.Advanced.SessionCache != CacheType.none && SessionCache.Contains(loginLower) && Config.Main.General.AccountType != LoginType.yggdrasil)
{
session = SessionCache.Get(loginLower);
result = ProtocolHandler.GetTokenValidation(session);
Expand Down Expand Up @@ -447,15 +447,15 @@ private static void InitializeClient()

if (result != ProtocolHandler.LoginResult.Success)
{
ConsoleIO.WriteLine(string.Format(Translations.mcc_connecting, Config.Main.General.AccountType == LoginType.mojang ? "Minecraft.net" : "Microsoft"));
ConsoleIO.WriteLine(string.Format(Translations.mcc_connecting, Config.Main.General.AccountType == LoginType.mojang ? "Minecraft.net" : (Config.Main.General.AccountType == LoginType.microsoft ? "Microsoft" : Config.Main.General.AuthServer.Host)));
result = ProtocolHandler.GetLogin(InternalConfig.Account.Login, InternalConfig.Account.Password, Config.Main.General.AccountType, out session);
}

if (result == ProtocolHandler.LoginResult.Success && Config.Main.Advanced.SessionCache != CacheType.none)
SessionCache.Store(loginLower, session);

if (result == ProtocolHandler.LoginResult.Success)
session.SessionPreCheckTask = Task.Factory.StartNew(() => session.SessionPreCheck());
session.SessionPreCheckTask = Task.Factory.StartNew(() => session.SessionPreCheck(Config.Main.General.AccountType));
}

if (result == ProtocolHandler.LoginResult.Success)
Expand Down Expand Up @@ -649,6 +649,7 @@ private static void InitializeClient()
ProtocolHandler.LoginResult.OtherError => Translations.error_login_network,
ProtocolHandler.LoginResult.SSLError => Translations.error_login_ssl,
ProtocolHandler.LoginResult.UserCancel => Translations.error_login_cancel,
ProtocolHandler.LoginResult.WrongSelection => Translations.error_login_blocked,
_ => Translations.error_login_unknown,
#pragma warning restore format // @formatter:on
};
Expand Down
7 changes: 4 additions & 3 deletions MinecraftClient/Protocol/Handlers/Protocol16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using MinecraftClient.Proxy;
using MinecraftClient.Scripting;
using static MinecraftClient.Settings;
using static MinecraftClient.Settings.MainConfigHealper.MainConfig.GeneralConfig;

namespace MinecraftClient.Protocol.Handlers
{
Expand Down Expand Up @@ -504,7 +505,7 @@ private bool Handshake(string uuid, string username, string sessionID, string ho
else if (Settings.Config.Logging.DebugMessages)
ConsoleIO.WriteLineFormatted("§8" + string.Format(Translations.mcc_handshake, serverID));

return StartEncryption(uuid, username, sessionID, token, serverID, PublicServerkey, session);
return StartEncryption(uuid, username, sessionID, Config.Main.General.AccountType, token, serverID, PublicServerkey, session);
}
else
{
Expand All @@ -513,7 +514,7 @@ private bool Handshake(string uuid, string username, string sessionID, string ho
}
}

private bool StartEncryption(string uuid, string username, string sessionID, byte[] token, string serverIDhash, byte[] serverPublicKey, SessionToken session)
private bool StartEncryption(string uuid, string username, string sessionID, LoginType type, byte[] token, string serverIDhash, byte[] serverPublicKey, SessionToken session)
{
RSACryptoServiceProvider RSAService = CryptoHandler.DecodeRSAPublicKey(serverPublicKey)!;
byte[] secretKey = CryptoHandler.ClientAESPrivateKey ?? CryptoHandler.GenerateAESPrivateKey();
Expand All @@ -537,7 +538,7 @@ private bool StartEncryption(string uuid, string username, string sessionID, byt

if (needCheckSession)
{
if (ProtocolHandler.SessionCheck(uuid, sessionID, serverHash))
if ((type == LoginType.mojang && ProtocolHandler.SessionCheck(uuid, sessionID, serverHash)) || (type == LoginType.yggdrasil && ProtocolHandler.YggdrasilSessionCheck(uuid, sessionID, serverHash)))
{
session.ServerIDhash = serverIDhash;
session.ServerPublicKey = serverPublicKey;
Expand Down
7 changes: 4 additions & 3 deletions MinecraftClient/Protocol/Handlers/Protocol18.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using MinecraftClient.Scripting;
using Newtonsoft.Json;
using static MinecraftClient.Settings;
using static MinecraftClient.Settings.MainConfigHealper.MainConfig.GeneralConfig;

namespace MinecraftClient.Protocol.Handlers
{
Expand Down Expand Up @@ -2568,7 +2569,7 @@ public bool Login(PlayerKeyPair? playerKeyPair, SessionToken session)
string serverID = dataTypes.ReadNextString(packetData);
byte[] serverPublicKey = dataTypes.ReadNextByteArray(packetData);
byte[] token = dataTypes.ReadNextByteArray(packetData);
return StartEncryption(handler.GetUserUuidStr(), handler.GetSessionID(), token, serverID,
return StartEncryption(handler.GetUserUuidStr(), handler.GetSessionID(), Config.Main.General.AccountType, token, serverID,
serverPublicKey, playerKeyPair, session);
}
else if (packetID == 0x02) //Login successful
Expand All @@ -2593,7 +2594,7 @@ public bool Login(PlayerKeyPair? playerKeyPair, SessionToken session)
/// Start network encryption. Automatically called by Login() if the server requests encryption.
/// </summary>
/// <returns>True if encryption was successful</returns>
private bool StartEncryption(string uuid, string sessionID, byte[] token, string serverIDhash,
private bool StartEncryption(string uuid, string sessionID, LoginType type, byte[] token, string serverIDhash,
byte[] serverPublicKey, PlayerKeyPair? playerKeyPair, SessionToken session)
{
RSACryptoServiceProvider RSAService = CryptoHandler.DecodeRSAPublicKey(serverPublicKey)!;
Expand All @@ -2619,7 +2620,7 @@ private bool StartEncryption(string uuid, string sessionID, byte[] token, string
{
string serverHash = CryptoHandler.GetServerHash(serverIDhash, serverPublicKey, secretKey);

if (ProtocolHandler.SessionCheck(uuid, sessionID, serverHash))
if ((type == LoginType.mojang && ProtocolHandler.SessionCheck(uuid, sessionID, serverHash) )|| (type == LoginType.yggdrasil && ProtocolHandler.YggdrasilSessionCheck(uuid, sessionID, serverHash)))
{
session.ServerIDhash = serverIDhash;
session.ServerPublicKey = serverPublicKey;
Expand Down
Loading

0 comments on commit c6da4e2

Please sign in to comment.