refactored session checks for better readability

This commit is contained in:
mcflurrybaby 2023-12-02 13:15:46 +02:00
parent ceff78a821
commit e19de8eb0b
4 changed files with 9 additions and 21 deletions

View file

@ -538,8 +538,7 @@ namespace MinecraftClient.Protocol.Handlers
if (needCheckSession) if (needCheckSession)
{ {
bool notYggdrasil = type == LoginType.mojang || type == LoginType.microsoft; if (ProtocolHandler.SessionCheck(uuid, sessionID, serverHash, type))
if ((notYggdrasil && ProtocolHandler.SessionCheck(uuid, sessionID, serverHash)) || (type == LoginType.yggdrasil && ProtocolHandler.YggdrasilSessionCheck(uuid, sessionID, serverHash)))
{ {
session.ServerIDhash = serverIDhash; session.ServerIDhash = serverIDhash;
session.ServerPublicKey = serverPublicKey; session.ServerPublicKey = serverPublicKey;

View file

@ -2619,8 +2619,7 @@ namespace MinecraftClient.Protocol.Handlers
if (needCheckSession) if (needCheckSession)
{ {
string serverHash = CryptoHandler.GetServerHash(serverIDhash, serverPublicKey, secretKey); string serverHash = CryptoHandler.GetServerHash(serverIDhash, serverPublicKey, secretKey);
bool notYggdrasil = type == LoginType.mojang || type == LoginType.microsoft; if (ProtocolHandler.SessionCheck(uuid, sessionID, serverHash, type))
if ((notYggdrasil && ProtocolHandler.SessionCheck(uuid, sessionID, serverHash) )|| (type == LoginType.yggdrasil && ProtocolHandler.YggdrasilSessionCheck(uuid, sessionID, serverHash)))
{ {
session.ServerIDhash = serverIDhash; session.ServerIDhash = serverIDhash;
session.ServerPublicKey = serverPublicKey; session.ServerPublicKey = serverPublicKey;

View file

@ -881,26 +881,19 @@ namespace MinecraftClient.Protocol
/// <param name="user">Username</param> /// <param name="user">Username</param>
/// <param name="accesstoken">Session ID</param> /// <param name="accesstoken">Session ID</param>
/// <param name="serverhash">Server ID</param> /// <param name="serverhash">Server ID</param>
/// <param name="type">LoginType</param>
/// <returns>TRUE if session was successfully checked</returns> /// <returns>TRUE if session was successfully checked</returns>
public static bool SessionCheck(string uuid, string accesstoken, string serverhash) public static bool SessionCheck(string uuid, string accesstoken, string serverhash, LoginType type)
{ {
try try
{ {
string result = ""; string result = "";
string json_request = "{\"accessToken\":\"" + accesstoken + "\",\"selectedProfile\":\"" + uuid + "\",\"serverId\":\"" + serverhash + "\"}"; string json_request = "{\"accessToken\":\"" + accesstoken + "\",\"selectedProfile\":\"" + uuid + "\",\"serverId\":\"" + serverhash + "\"}";
int code = DoHTTPSPost("sessionserver.mojang.com",443, "/session/minecraft/join", json_request, ref result); string host = type == LoginType.yggdrasil ? Config.Main.General.AuthServer.Host : "sessionserver.mojang.com";
return (code >= 200 && code < 300); int port = type == LoginType.yggdrasil ? Config.Main.General.AuthServer.Port : 443;
} string endpoint = type == LoginType.yggdrasil ? "/api/yggdrasil/sessionserver/session/minecraft/join" : "/session/minecraft/join";
catch { return false; }
}
public static bool YggdrasilSessionCheck(string uuid, string accesstoken, string serverhash) int code = DoHTTPSPost(host, port, endpoint, json_request, ref result);
{
try
{
string result = "";
string json_request = "{\"accessToken\":\"" + accesstoken + "\",\"selectedProfile\":\"" + uuid + "\",\"serverId\":\"" + serverhash + "\"}";
int code = DoHTTPSPost(Config.Main.General.AuthServer.Host, Config.Main.General.AuthServer.Port, "/api/yggdrasil/sessionserver/session/minecraft/join", json_request, ref result);
return (code >= 200 && code < 300); return (code >= 200 && code < 300);
} }
catch { return false; } catch { return false; }

View file

@ -39,10 +39,7 @@ namespace MinecraftClient.Protocol.Session
return false; return false;
Crypto.CryptoHandler.ClientAESPrivateKey ??= Crypto.CryptoHandler.GenerateAESPrivateKey(); Crypto.CryptoHandler.ClientAESPrivateKey ??= Crypto.CryptoHandler.GenerateAESPrivateKey();
string serverHash = Crypto.CryptoHandler.GetServerHash(ServerIDhash, ServerPublicKey, Crypto.CryptoHandler.ClientAESPrivateKey); string serverHash = Crypto.CryptoHandler.GetServerHash(ServerIDhash, ServerPublicKey, Crypto.CryptoHandler.ClientAESPrivateKey);
bool notYggdrasil = type == LoginType.mojang || type == LoginType.microsoft; if (ProtocolHandler.SessionCheck(PlayerID, ID, serverHash, type))
if (notYggdrasil && ProtocolHandler.SessionCheck(PlayerID, ID, serverHash))
return true;
if (type == LoginType.yggdrasil && ProtocolHandler.YggdrasilSessionCheck(PlayerID, ID, serverHash))
return true; return true;
return false; return false;
} }