ImplPlayerSection

This commit is contained in:
Polaris_Light 2023-11-16 00:04:56 +08:00
parent 3c97193b70
commit a97096cddf
10 changed files with 146 additions and 17 deletions

View file

@ -416,7 +416,7 @@ namespace MinecraftClient
else
{
// Validate cached session or login new session.
if (Config.Main.Advanced.SessionCache != CacheType.none && SessionCache.Contains(loginLower) && Config.Main.General.AccountType != LoginType.Yggdrasil)
if (Config.Main.Advanced.SessionCache != CacheType.none && SessionCache.Contains(loginLower) && Config.Main.General.AccountType != LoginType.yggdrasil)
{
session = SessionCache.Get(loginLower);
result = ProtocolHandler.GetTokenValidation(session);
@ -447,7 +447,7 @@ namespace MinecraftClient
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);
}
@ -649,6 +649,7 @@ namespace MinecraftClient
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
};

View file

@ -538,7 +538,7 @@ namespace MinecraftClient.Protocol.Handlers
if (needCheckSession)
{
if ((type == LoginType.mojang && ProtocolHandler.SessionCheck(uuid, sessionID, serverHash)) || (type == LoginType.Yggdrasil && ProtocolHandler.YggdrasilSessionCheck(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;

View file

@ -2614,7 +2614,7 @@ namespace MinecraftClient.Protocol.Handlers
{
string serverHash = CryptoHandler.GetServerHash(serverIDhash, serverPublicKey, secretKey);
if ((type == LoginType.mojang && ProtocolHandler.SessionCheck(uuid, sessionID, serverHash) )|| (type == LoginType.Yggdrasil && ProtocolHandler.YggdrasilSessionCheck(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;

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data.Odbc;
using System.Globalization;
using System.Linq;
using System.Net.Security;
@ -11,6 +12,7 @@ using MinecraftClient.Protocol.Handlers;
using MinecraftClient.Protocol.Handlers.Forge;
using MinecraftClient.Protocol.Session;
using MinecraftClient.Proxy;
using static MinecraftClient.Protocol.Microsoft;
using static MinecraftClient.Settings;
using static MinecraftClient.Settings.MainConfigHealper.MainConfig.GeneralConfig;
@ -423,7 +425,7 @@ namespace MinecraftClient.Protocol
return Protocol18Forge.ServerForceForge(protocol);
}
public enum LoginResult { OtherError, ServiceUnavailable, SSLError, Success, WrongPassword, AccountMigrated, NotPremium, LoginRequired, InvalidToken, InvalidResponse, NullError, UserCancel };
public enum LoginResult { OtherError, ServiceUnavailable, SSLError, Success, WrongPassword, AccountMigrated, NotPremium, LoginRequired, InvalidToken, InvalidResponse, NullError, UserCancel, WrongSelection };
public enum AccountType { Mojang, Microsoft };
/// <summary>
@ -446,7 +448,7 @@ namespace MinecraftClient.Protocol
{
return MojangLogin(user, pass, out session);
}
else if (type == LoginType.Yggdrasil)
else if (type == LoginType.yggdrasil)
{
return YggdrasiLogin(user, pass, out session);
}
@ -556,16 +558,48 @@ namespace MinecraftClient.Protocol
else
{
Json.JSONData loginResponse = Json.ParseJson(result);
if (loginResponse.Properties.ContainsKey("accessToken")
&& loginResponse.Properties.ContainsKey("selectedProfile")
if (loginResponse.Properties.ContainsKey("accessToken"))
{
session.ID = loginResponse.Properties["accessToken"].StringValue;
if (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
{
string availablePlayers = "";
foreach (Json.JSONData profile in loginResponse.Properties["availableProfiles"].DataArray)
{
availablePlayers += " " + profile.Properties["name"].StringValue;
}
ConsoleIO.WriteLine(Translations.mcc_avaliable_players + availablePlayers);
ConsoleIO.WriteLine(Translations.mcc_select_player);
string selectedPlayer = ConsoleIO.ReadLine();
ConsoleIO.WriteLine(Translations.mcc_selected_player + selectedPlayer);
Json.JSONData selectedProfile = null;
foreach (Json.JSONData profile in loginResponse.Properties["availableProfiles"].DataArray)
{
selectedProfile = profile.Properties["name"].StringValue == selectedPlayer ? profile : selectedProfile;
}
if (selectedProfile != null)
{
session.PlayerID = selectedProfile.Properties["id"].StringValue;
session.PlayerName = selectedProfile.Properties["name"].StringValue;
SessionToken currentsession = session;
return GetNewYggdrasilToken(currentsession, out session);
}
else
{
return LoginResult.WrongSelection;
}
}
}
else return LoginResult.InvalidResponse;
}
}
@ -795,6 +829,52 @@ namespace MinecraftClient.Protocol
}
}
public static LoginResult GetNewYggdrasilToken(SessionToken currentsession, out SessionToken session)
{
session = new SessionToken();
try
{
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(Config.Main.General.AuthServer.Host, Config.Main.General.AuthServer.Port, "/api/yggdrasil/authserver/refresh", json_request, ref result);
if (code == 200)
{
if (result == null)
{
return LoginResult.NullError;
}
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 && result.Contains("InvalidToken"))
{
return LoginResult.InvalidToken;
}
else
{
ConsoleIO.WriteLineFormatted("§8" + string.Format(Translations.error_auth, code));
return LoginResult.OtherError;
}
}
catch
{
return LoginResult.OtherError;
}
}
/// <summary>
/// Check session using Mojang's Yggdrasil authentication scheme. Allows to join an online-mode server
/// </summary>

View file

@ -41,7 +41,7 @@ namespace MinecraftClient.Protocol.Session
string serverHash = Crypto.CryptoHandler.GetServerHash(ServerIDhash, ServerPublicKey, Crypto.CryptoHandler.ClientAESPrivateKey);
if (type == LoginType.mojang && ProtocolHandler.SessionCheck(PlayerID, ID, serverHash))
return true;
if (type == LoginType.Yggdrasil && ProtocolHandler.YggdrasilSessionCheck(PlayerID, ID, serverHash))
if (type == LoginType.yggdrasil && ProtocolHandler.YggdrasilSessionCheck(PlayerID, ID, serverHash))
return true;
return false;
}

View file

@ -1722,6 +1722,15 @@ namespace MinecraftClient {
}
}
/// <summary>
/// Looks up a localized string similar to Yggdrasil authlib server domain name and port..
/// </summary>
internal static string Main_General_AuthlibServer {
get {
return ResourceManager.GetString("Main.General.AuthlibServer", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The address of the game server, &quot;Host&quot; can be filled in with domain name or IP address. (The &quot;Port&quot; field can be deleted, it will be resolved automatically).
/// </summary>
@ -1741,7 +1750,7 @@ namespace MinecraftClient {
}
/// <summary>
/// Looks up a localized string similar to Account type: &quot;mojang&quot; OR &quot;microsoft&quot;. Also affects interactive login in console..
/// Looks up a localized string similar to Account type: &quot;mojang&quot; OR &quot;microsoft&quot; OR &quot;yggdrasil&quot;. Also affects interactive login in console..
/// </summary>
internal static string Main_General_server_info {
get {

View file

@ -724,7 +724,7 @@ Usage examples: "/tell &lt;mybot&gt; connect Server1", "/connect Server2"</value
<value>Microsoft Account sign-in method: "mcc" OR "browser". If the login always fails, please try to use the "browser" once.</value>
</data>
<data name="Main.General.server_info" xml:space="preserve">
<value>Account type: "mojang" OR "microsoft". Also affects interactive login in console.</value>
<value>Account type: "mojang" OR "microsoft" OR "yggdrasil". Also affects interactive login in console.</value>
</data>
<data name="MCSettings" xml:space="preserve">
<value>Settings below are sent to the server and only affect server-side things like your skin.</value>
@ -846,4 +846,7 @@ If the connection to the Minecraft game server is blocked by the firewall, set E
<data name="Main.Advanced.ignore_invalid_playername" xml:space="preserve">
<value>Ignore invalid player name</value>
</data>
<data name="Main.General.AuthlibServer" xml:space="preserve">
<value>Yggdrasil authlib server domain name and port.</value>
</data>
</root>

View file

@ -5373,6 +5373,15 @@ namespace MinecraftClient {
}
}
/// <summary>
/// Looks up a localized string similar to Avaliable players:.
/// </summary>
internal static string mcc_avaliable_players {
get {
return ResourceManager.GetString("mcc.avaliable_players", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The old MinecraftClient.ini has been backed up as {0}.
/// </summary>
@ -5746,6 +5755,24 @@ namespace MinecraftClient {
}
}
/// <summary>
/// Looks up a localized string similar to Select a player from available players:.
/// </summary>
internal static string mcc_select_player {
get {
return ResourceManager.GetString("mcc.select_player", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Selected player:.
/// </summary>
internal static string mcc_selected_player {
get {
return ResourceManager.GetString("mcc.selected_player", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Server is in offline mode..
/// </summary>

View file

@ -2121,4 +2121,13 @@ Logging in...</value>
<data name="cmd.inventory.shiftrightclick" xml:space="preserve">
<value>Shift right-clicking slot {0} in window #{1}</value>
</data>
<data name="mcc.avaliable_players" xml:space="preserve">
<value>Avaliable players:</value>
</data>
<data name="mcc.selected_player" xml:space="preserve">
<value>Selected player:</value>
</data>
<data name="mcc.select_player" xml:space="preserve">
<value>Select a player from available players:</value>
</data>
</root>

View file

@ -498,7 +498,7 @@ namespace MinecraftClient
public AuthlibServer AuthServer = new(string.Empty);
public enum LoginType { mojang, microsoft,Yggdrasil };
public enum LoginType { mojang, microsoft,yggdrasil };
public enum LoginMethod { mcc, browser };
}