mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
AddYggdrasilLogin
This commit is contained in:
parent
eb8ccc43d7
commit
3c97193b70
6 changed files with 156 additions and 25 deletions
|
|
@ -15,6 +15,7 @@ using MinecraftClient.Protocol.Session;
|
|||
using MinecraftClient.Proxy;
|
||||
using MinecraftClient.Scripting;
|
||||
using static MinecraftClient.Settings;
|
||||
using static MinecraftClient.Settings.MainConfigHealper.MainConfig.GeneralConfig;
|
||||
|
||||
namespace MinecraftClient.Protocol.Handlers
|
||||
{
|
||||
|
|
@ -504,7 +505,7 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
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
|
||||
{
|
||||
|
|
@ -513,7 +514,7 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
|
@ -537,7 +538,7 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ using MinecraftClient.Proxy;
|
|||
using MinecraftClient.Scripting;
|
||||
using Newtonsoft.Json;
|
||||
using static MinecraftClient.Settings;
|
||||
using static MinecraftClient.Settings.MainConfigHealper.MainConfig.GeneralConfig;
|
||||
|
||||
namespace MinecraftClient.Protocol.Handlers
|
||||
{
|
||||
|
|
@ -2562,7 +2563,7 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
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
|
||||
|
|
@ -2587,7 +2588,7 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
/// 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)!;
|
||||
|
|
@ -2613,7 +2614,7 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
{
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -134,7 +134,7 @@ namespace MinecraftClient.Protocol
|
|||
if (Array.IndexOf(supportedVersions_Protocol16, ProtocolVersion) > -1)
|
||||
return new Protocol16Handler(Client, ProtocolVersion, Handler);
|
||||
|
||||
int[] supportedVersions_Protocol18 = { 4, 5, 47, 107, 108, 109, 110, 210, 315, 316, 335, 338, 340, 393, 401, 404, 477, 480, 485, 490, 498, 573, 575, 578, 735, 736, 751, 753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763 };
|
||||
int[] supportedVersions_Protocol18 = { 4, 5, 47, 107, 108, 109, 110, 210, 315, 316, 335, 338, 340, 393, 401, 404, 477, 480, 485, 490, 498, 573, 575, 578, 735, 736, 751, 753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763};
|
||||
|
||||
if (Array.IndexOf(supportedVersions_Protocol18, ProtocolVersion) > -1)
|
||||
return new Protocol18Handler(Client, ProtocolVersion, Handler, forgeInfo);
|
||||
|
|
@ -446,7 +446,11 @@ namespace MinecraftClient.Protocol
|
|||
{
|
||||
return MojangLogin(user, pass, out session);
|
||||
}
|
||||
else throw new InvalidOperationException("Account type must be Mojang or Microsoft");
|
||||
else if (type == LoginType.Yggdrasil)
|
||||
{
|
||||
return YggdrasiLogin(user, pass, out session);
|
||||
}
|
||||
else throw new InvalidOperationException("Account type must be Mojang or Microsoft or valid authlib 3rd Servers!");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -464,7 +468,7 @@ namespace MinecraftClient.Protocol
|
|||
{
|
||||
string result = "";
|
||||
string json_request = "{\"agent\": { \"name\": \"Minecraft\", \"version\": 1 }, \"username\": \"" + JsonEncode(user) + "\", \"password\": \"" + JsonEncode(pass) + "\", \"clientToken\": \"" + JsonEncode(session.ClientID) + "\" }";
|
||||
int code = DoHTTPSPost("authserver.mojang.com", "/authenticate", json_request, ref result);
|
||||
int code = DoHTTPSPost("authserver.mojang.com",443, "/authenticate", json_request, ref result);
|
||||
if (code == 200)
|
||||
{
|
||||
if (result.Contains("availableProfiles\":[]}"))
|
||||
|
|
@ -534,7 +538,84 @@ namespace MinecraftClient.Protocol
|
|||
return LoginResult.OtherError;
|
||||
}
|
||||
}
|
||||
private static LoginResult YggdrasiLogin(string user, string pass, out SessionToken session)
|
||||
{
|
||||
session = new SessionToken() { ClientID = Guid.NewGuid().ToString().Replace("-", "") };
|
||||
|
||||
try
|
||||
{
|
||||
string result = "";
|
||||
string json_request = "{\"agent\": { \"name\": \"Minecraft\", \"version\": 1 }, \"username\": \"" + JsonEncode(user) + "\", \"password\": \"" + JsonEncode(pass) + "\", \"clientToken\": \"" + JsonEncode(session.ClientID) + "\" }";
|
||||
int code = DoHTTPSPost(Config.Main.General.AuthServer.Host,Config.Main.General.AuthServer.Port, "/api/yggdrasil/authserver/authenticate", json_request, ref result);
|
||||
if (code == 200)
|
||||
{
|
||||
if (result.Contains("availableProfiles\":[]}"))
|
||||
{
|
||||
return LoginResult.NotPremium;
|
||||
}
|
||||
else
|
||||
{
|
||||
Json.JSONData loginResponse = Json.ParseJson(result);
|
||||
if (loginResponse.Properties.ContainsKey("accessToken")
|
||||
&& loginResponse.Properties.ContainsKey("selectedProfile")
|
||||
&& loginResponse.Properties["selectedProfile"].Properties.ContainsKey("id")
|
||||
&& loginResponse.Properties["selectedProfile"].Properties.ContainsKey("name"))
|
||||
{
|
||||
session.ID = loginResponse.Properties["accessToken"].StringValue;
|
||||
session.PlayerID = loginResponse.Properties["selectedProfile"].Properties["id"].StringValue;
|
||||
session.PlayerName = loginResponse.Properties["selectedProfile"].Properties["name"].StringValue;
|
||||
return LoginResult.Success;
|
||||
}
|
||||
else return LoginResult.InvalidResponse;
|
||||
}
|
||||
}
|
||||
else if (code == 403)
|
||||
{
|
||||
if (result.Contains("UserMigratedException"))
|
||||
{
|
||||
return LoginResult.AccountMigrated;
|
||||
}
|
||||
else return LoginResult.WrongPassword;
|
||||
}
|
||||
else if (code == 503)
|
||||
{
|
||||
return LoginResult.ServiceUnavailable;
|
||||
}
|
||||
else
|
||||
{
|
||||
ConsoleIO.WriteLineFormatted("§8" + string.Format(Translations.error_http_code, code));
|
||||
return LoginResult.OtherError;
|
||||
}
|
||||
}
|
||||
catch (System.Security.Authentication.AuthenticationException e)
|
||||
{
|
||||
if (Settings.Config.Logging.DebugMessages)
|
||||
{
|
||||
ConsoleIO.WriteLineFormatted("§8" + e.ToString());
|
||||
}
|
||||
return LoginResult.SSLError;
|
||||
}
|
||||
catch (System.IO.IOException e)
|
||||
{
|
||||
if (Settings.Config.Logging.DebugMessages)
|
||||
{
|
||||
ConsoleIO.WriteLineFormatted("§8" + e.ToString());
|
||||
}
|
||||
if (e.Message.Contains("authentication"))
|
||||
{
|
||||
return LoginResult.SSLError;
|
||||
}
|
||||
else return LoginResult.OtherError;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (Settings.Config.Logging.DebugMessages)
|
||||
{
|
||||
ConsoleIO.WriteLineFormatted("§8" + e.ToString());
|
||||
}
|
||||
return LoginResult.OtherError;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Sign-in to Microsoft Account without using browser. Only works if 2FA is disabled.
|
||||
/// Might not work well in some rare cases.
|
||||
|
|
@ -675,7 +756,7 @@ namespace MinecraftClient.Protocol
|
|||
{
|
||||
string result = "";
|
||||
string json_request = "{ \"accessToken\": \"" + JsonEncode(currentsession.ID) + "\", \"clientToken\": \"" + JsonEncode(currentsession.ClientID) + "\", \"selectedProfile\": { \"id\": \"" + JsonEncode(currentsession.PlayerID) + "\", \"name\": \"" + JsonEncode(currentsession.PlayerName) + "\" } }";
|
||||
int code = DoHTTPSPost("authserver.mojang.com", "/refresh", json_request, ref result);
|
||||
int code = DoHTTPSPost("authserver.mojang.com",443, "/refresh", json_request, ref result);
|
||||
if (code == 200)
|
||||
{
|
||||
if (result == null)
|
||||
|
|
@ -727,7 +808,19 @@ namespace MinecraftClient.Protocol
|
|||
{
|
||||
string result = "";
|
||||
string json_request = "{\"accessToken\":\"" + accesstoken + "\",\"selectedProfile\":\"" + uuid + "\",\"serverId\":\"" + serverhash + "\"}";
|
||||
int code = DoHTTPSPost("sessionserver.mojang.com", "/session/minecraft/join", json_request, ref result);
|
||||
int code = DoHTTPSPost("sessionserver.mojang.com",443, "/session/minecraft/join", json_request, ref result);
|
||||
return (code >= 200 && code < 300);
|
||||
}
|
||||
catch { return false; }
|
||||
}
|
||||
|
||||
public static bool YggdrasilSessionCheck(string uuid, string accesstoken, string serverhash)
|
||||
{
|
||||
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);
|
||||
}
|
||||
catch { return false; }
|
||||
|
|
@ -747,7 +840,7 @@ namespace MinecraftClient.Protocol
|
|||
{
|
||||
string result = "";
|
||||
string cookies = String.Format("sid=token:{0}:{1};user={2};version={3}", accesstoken, uuid, username, Program.MCHighestVersion);
|
||||
DoHTTPSGet("pc.realms.minecraft.net", "/worlds", cookies, ref result);
|
||||
DoHTTPSGet("pc.realms.minecraft.net", 443,"/worlds", cookies, ref result);
|
||||
Json.JSONData realmsWorlds = Json.ParseJson(result);
|
||||
if (realmsWorlds.Properties.ContainsKey("servers")
|
||||
&& realmsWorlds.Properties["servers"].Type == Json.JSONData.DataType.Array
|
||||
|
|
@ -808,7 +901,7 @@ namespace MinecraftClient.Protocol
|
|||
{
|
||||
string result = "";
|
||||
string cookies = String.Format("sid=token:{0}:{1};user={2};version={3}", accesstoken, uuid, username, Program.MCHighestVersion);
|
||||
int statusCode = DoHTTPSGet("pc.realms.minecraft.net", "/worlds/v1/" + worldId + "/join/pc", cookies, ref result);
|
||||
int statusCode = DoHTTPSGet("pc.realms.minecraft.net",443, "/worlds/v1/" + worldId + "/join/pc", cookies, ref result);
|
||||
if (statusCode == 200)
|
||||
{
|
||||
Json.JSONData serverAddress = Json.ParseJson(result);
|
||||
|
|
@ -845,7 +938,7 @@ namespace MinecraftClient.Protocol
|
|||
/// <param name="cookies">Cookies for making the request</param>
|
||||
/// <param name="result">Request result</param>
|
||||
/// <returns>HTTP Status code</returns>
|
||||
private static int DoHTTPSGet(string host, string endpoint, string cookies, ref string result)
|
||||
private static int DoHTTPSGet(string host,int port, string endpoint, string cookies, ref string result)
|
||||
{
|
||||
List<String> http_request = new()
|
||||
{
|
||||
|
|
@ -860,7 +953,7 @@ namespace MinecraftClient.Protocol
|
|||
"",
|
||||
""
|
||||
};
|
||||
return DoHTTPSRequest(http_request, host, ref result);
|
||||
return DoHTTPSRequest(http_request, host,port, ref result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -871,7 +964,7 @@ namespace MinecraftClient.Protocol
|
|||
/// <param name="request">Request payload</param>
|
||||
/// <param name="result">Request result</param>
|
||||
/// <returns>HTTP Status code</returns>
|
||||
private static int DoHTTPSPost(string host, string endpoint, string request, ref string result)
|
||||
private static int DoHTTPSPost(string host, int port, string endpoint, string request, ref string result)
|
||||
{
|
||||
List<String> http_request = new()
|
||||
{
|
||||
|
|
@ -884,7 +977,7 @@ namespace MinecraftClient.Protocol
|
|||
"",
|
||||
request
|
||||
};
|
||||
return DoHTTPSRequest(http_request, host, ref result);
|
||||
return DoHTTPSRequest(http_request, host,port, ref result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -895,7 +988,7 @@ namespace MinecraftClient.Protocol
|
|||
/// <param name="host">Host to connect to</param>
|
||||
/// <param name="result">Request result</param>
|
||||
/// <returns>HTTP Status code</returns>
|
||||
private static int DoHTTPSRequest(List<string> headers, string host, ref string result)
|
||||
private static int DoHTTPSRequest(List<string> headers, string host,int port, ref string result)
|
||||
{
|
||||
string? postResult = null;
|
||||
int statusCode = 520;
|
||||
|
|
@ -907,7 +1000,7 @@ namespace MinecraftClient.Protocol
|
|||
if (Settings.Config.Logging.DebugMessages)
|
||||
ConsoleIO.WriteLineFormatted("§8" + string.Format(Translations.debug_request, host));
|
||||
|
||||
TcpClient client = ProxyHandler.NewTcpClient(host, 443, true);
|
||||
TcpClient client = ProxyHandler.NewTcpClient(host, port, true);
|
||||
SslStream stream = new(client.GetStream());
|
||||
stream.AuthenticateAsClient(host, null, SslProtocols.Tls12, true); // Enable TLS 1.2. Hotfix for #1780
|
||||
|
||||
|
|
@ -928,8 +1021,15 @@ namespace MinecraftClient.Protocol
|
|||
|
||||
if (raw_result.StartsWith("HTTP/1.1"))
|
||||
{
|
||||
postResult = raw_result[(raw_result.IndexOf("\r\n\r\n") + 4)..];
|
||||
statusCode = int.Parse(raw_result.Split(' ')[1], NumberStyles.Any, CultureInfo.CurrentCulture);
|
||||
if (statusCode != 204)
|
||||
{
|
||||
postResult = raw_result[(raw_result.IndexOf("\r\n\r\n") + 4)..].Split("\r\n")[1];
|
||||
}
|
||||
else
|
||||
{
|
||||
postResult = "No Content";
|
||||
}
|
||||
}
|
||||
else statusCode = 520; //Web server is returning an unknown error
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ using System.IO;
|
|||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using MinecraftClient.Scripting;
|
||||
using static MinecraftClient.Settings.MainConfigHealper.MainConfig.GeneralConfig;
|
||||
|
||||
namespace MinecraftClient.Protocol.Session
|
||||
{
|
||||
|
|
@ -32,13 +33,15 @@ namespace MinecraftClient.Protocol.Session
|
|||
ServerPublicKey = null;
|
||||
}
|
||||
|
||||
public bool SessionPreCheck()
|
||||
public bool SessionPreCheck(LoginType type)
|
||||
{
|
||||
if (ID == string.Empty || PlayerID == String.Empty || ServerPublicKey == null)
|
||||
return false;
|
||||
Crypto.CryptoHandler.ClientAESPrivateKey ??= Crypto.CryptoHandler.GenerateAESPrivateKey();
|
||||
string serverHash = Crypto.CryptoHandler.GetServerHash(ServerIDhash, ServerPublicKey, Crypto.CryptoHandler.ClientAESPrivateKey);
|
||||
if (ProtocolHandler.SessionCheck(PlayerID, ID, serverHash))
|
||||
if (type == LoginType.mojang && ProtocolHandler.SessionCheck(PlayerID, ID, serverHash))
|
||||
return true;
|
||||
if (type == LoginType.Yggdrasil && ProtocolHandler.YggdrasilSessionCheck(PlayerID, ID, serverHash))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue