mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-11-07 17:36:07 +00:00
Merge pull request #124 from initsuj/Indev
Add session caching by initsuj
This commit is contained in:
commit
0fbefb5068
7 changed files with 718 additions and 383 deletions
4
MinecraftClient/Cache/CacheType.cs
Normal file
4
MinecraftClient/Cache/CacheType.cs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
namespace MinecraftClient.Cache
|
||||
{
|
||||
public enum CacheType { NONE, MEMORY, DISK };
|
||||
}
|
||||
174
MinecraftClient/Cache/SessionCache.cs
Normal file
174
MinecraftClient/Cache/SessionCache.cs
Normal file
|
|
@ -0,0 +1,174 @@
|
|||
using MinecraftClient.Protocol;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.Timers;
|
||||
|
||||
namespace MinecraftClient.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// Handle sessions caching and storage.
|
||||
/// </summary>
|
||||
|
||||
public static class SessionCache
|
||||
{
|
||||
const string filename = "cache.bin";
|
||||
private static Dictionary<string, SessionToken> sessions = new Dictionary<string, SessionToken>();
|
||||
private static FileSystemWatcher cachemonitor = new FileSystemWatcher();
|
||||
private static Timer updatetimer = new Timer(100);
|
||||
private static List<KeyValuePair<string, SessionToken>> pendingadds = new List<KeyValuePair<string, SessionToken>>();
|
||||
|
||||
private static BinaryFormatter formatter = new BinaryFormatter();
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve whether SessionCache contains a session for the given login.
|
||||
/// </summary>
|
||||
/// <param name="login">User login used with Minecraft.net</param>
|
||||
/// <returns>TRUE if session is available</returns>
|
||||
|
||||
public static bool Contains(string login)
|
||||
{
|
||||
return sessions.ContainsKey(login);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Store a session and save it to disk if required.
|
||||
/// </summary>
|
||||
/// <param name="login">User login used with Minecraft.net</param>
|
||||
/// <param name="session">User session token used with Minecraft.net</param>
|
||||
|
||||
public static void Store(string login, SessionToken session)
|
||||
{
|
||||
if (Contains(login))
|
||||
{
|
||||
sessions[login] = session;
|
||||
}
|
||||
else
|
||||
{
|
||||
sessions.Add(login, session);
|
||||
}
|
||||
|
||||
if (Settings.CacheType == CacheType.DISK && updatetimer.Enabled == true) {
|
||||
pendingadds.Add(new KeyValuePair<string, SessionToken>(login, session));
|
||||
}else if (Settings.CacheType == CacheType.DISK)
|
||||
{
|
||||
SaveToDisk();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve a session token for the given login.
|
||||
/// </summary>
|
||||
/// <param name="login">User login used with Minecraft.net</param>
|
||||
/// <returns>SessionToken for given login</returns>
|
||||
|
||||
public static SessionToken Get(string login)
|
||||
{
|
||||
return sessions[login];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize cache monitoring to keep cache updated with external changes.
|
||||
/// </summary>
|
||||
/// <returns>TRUE if session tokens are seeded from file</returns>
|
||||
|
||||
public static bool InitializeDiskCache()
|
||||
{
|
||||
cachemonitor.Path = AppDomain.CurrentDomain.BaseDirectory;
|
||||
cachemonitor.IncludeSubdirectories = false;
|
||||
cachemonitor.Filter = filename;
|
||||
cachemonitor.NotifyFilter = NotifyFilters.LastWrite;
|
||||
cachemonitor.Changed += new FileSystemEventHandler(OnChanged);
|
||||
cachemonitor.EnableRaisingEvents = true;
|
||||
|
||||
updatetimer.Elapsed += HandlePending;
|
||||
|
||||
return LoadFromDisk();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reloads cache on external cache file change.
|
||||
/// </summary>
|
||||
/// <param name="sender">Sender</param>
|
||||
/// <param name="e">Event data</param>
|
||||
|
||||
private static void OnChanged(object sender, FileSystemEventArgs e)
|
||||
{
|
||||
updatetimer.Stop();
|
||||
updatetimer.Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called after timer elapsed. Reads disk cache and adds new/modified sessions back.
|
||||
/// </summary>
|
||||
/// <param name="sender">Sender</param>
|
||||
/// <param name="e">Event data</param>
|
||||
|
||||
private static void HandlePending(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
LoadFromDisk();
|
||||
|
||||
foreach(KeyValuePair<string, SessionToken> pending in pendingadds.ToArray())
|
||||
{
|
||||
Store(pending.Key, pending.Value);
|
||||
pendingadds.Remove(pending);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads cache file and loads SessionTokens into SessionCache.
|
||||
/// </summary>
|
||||
/// <returns>True if data is successfully loaded</returns>
|
||||
|
||||
private static bool LoadFromDisk()
|
||||
{
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
try
|
||||
{
|
||||
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
|
||||
{
|
||||
sessions = (Dictionary<string, SessionToken>)formatter.Deserialize(fs);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
Console.WriteLine("Error reading cached sessions from disk: " + ex.Message);
|
||||
}
|
||||
catch (SerializationException)
|
||||
{
|
||||
Console.WriteLine("Malformed sessions from cache file ");
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves SessionToken's from SessionCache into cache file.
|
||||
/// </summary>
|
||||
|
||||
private static void SaveToDisk()
|
||||
{
|
||||
bool fileexists = File.Exists(filename);
|
||||
|
||||
using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
|
||||
{
|
||||
cachemonitor.EnableRaisingEvents = false;
|
||||
|
||||
// delete existing file contents
|
||||
if (fileexists)
|
||||
{
|
||||
fs.SetLength(0);
|
||||
fs.Flush();
|
||||
}
|
||||
|
||||
formatter.Serialize(fs, sessions);
|
||||
cachemonitor.EnableRaisingEvents = true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -63,6 +63,7 @@
|
|||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Runtime.Serialization" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
|
|
@ -72,6 +73,8 @@
|
|||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AutoTimeout.cs" />
|
||||
<Compile Include="Cache\CacheType.cs" />
|
||||
<Compile Include="Cache\SessionCache.cs" />
|
||||
<Compile Include="ChatBots\Alerts.cs" />
|
||||
<Compile Include="ChatBots\AntiAFK.cs" />
|
||||
<Compile Include="ChatBots\AutoRespond.cs" />
|
||||
|
|
@ -148,6 +151,7 @@
|
|||
<Compile Include="Protocol\IMinecraftCom.cs" />
|
||||
<Compile Include="Protocol\IMinecraftComHandler.cs" />
|
||||
<Compile Include="Protocol\ProtocolHandler.cs" />
|
||||
<Compile Include="Protocol\SessionToken.cs" />
|
||||
<Compile Include="Proxy\ProxyHandler.cs" />
|
||||
<Compile Include="Proxy\Handlers\EventArgs\CreateConnectionAsyncCompletedEventArgs.cs" />
|
||||
<Compile Include="Proxy\Handlers\Exceptions\ProxyException.cs" />
|
||||
|
|
|
|||
|
|
@ -1,374 +1,414 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using MinecraftClient.Protocol;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using MinecraftClient.Protocol.Handlers.Forge;
|
||||
|
||||
namespace MinecraftClient
|
||||
{
|
||||
/// <summary>
|
||||
/// Minecraft Console Client by ORelio (c) 2012-2014.
|
||||
/// Allows to connect to any Minecraft server, send and receive text, automated scripts.
|
||||
/// This source code is released under the CDDL 1.0 License.
|
||||
/// </summary>
|
||||
|
||||
static class Program
|
||||
{
|
||||
private static McTcpClient Client;
|
||||
public static string[] startupargs;
|
||||
|
||||
public const string Version = "1.8.2";
|
||||
public const string MCLowestVersion = "1.4.6";
|
||||
public const string MCHighestVersion = "1.8.8";
|
||||
|
||||
private static Thread offlinePrompt = null;
|
||||
private static bool useMcVersionOnce = false;
|
||||
|
||||
/// <summary>
|
||||
/// The main entry point of Minecraft Console Client
|
||||
/// </summary>
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Console Client for MC {0} to {1} - v{2} - By ORelio & Contributors", MCLowestVersion, MCHighestVersion, Version);
|
||||
|
||||
//Basic Input/Output ?
|
||||
if (args.Length >= 1 && args[args.Length - 1] == "BasicIO")
|
||||
{
|
||||
ConsoleIO.basicIO = true;
|
||||
Console.OutputEncoding = Console.InputEncoding = Encoding.GetEncoding(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage);
|
||||
args = args.Where(o => !Object.ReferenceEquals(o, args[args.Length - 1])).ToArray();
|
||||
}
|
||||
|
||||
//Process ini configuration file
|
||||
if (args.Length >= 1 && System.IO.File.Exists(args[0]) && System.IO.Path.GetExtension(args[0]).ToLower() == ".ini")
|
||||
{
|
||||
Settings.LoadSettings(args[0]);
|
||||
|
||||
//remove ini configuration file from arguments array
|
||||
List<string> args_tmp = args.ToList<string>();
|
||||
args_tmp.RemoveAt(0);
|
||||
args = args_tmp.ToArray();
|
||||
}
|
||||
else if (System.IO.File.Exists("MinecraftClient.ini"))
|
||||
{
|
||||
Settings.LoadSettings("MinecraftClient.ini");
|
||||
}
|
||||
else Settings.WriteDefaultSettings("MinecraftClient.ini");
|
||||
|
||||
//Other command-line arguments
|
||||
if (args.Length >= 1)
|
||||
{
|
||||
Settings.Login = args[0];
|
||||
if (args.Length >= 2)
|
||||
{
|
||||
Settings.Password = args[1];
|
||||
if (args.Length >= 3)
|
||||
{
|
||||
Settings.SetServerIP(args[2]);
|
||||
|
||||
//Single command?
|
||||
if (args.Length >= 4)
|
||||
{
|
||||
Settings.SingleCommand = args[3];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Settings.ConsoleTitle != "")
|
||||
{
|
||||
Settings.Username = "New Window";
|
||||
Console.Title = Settings.ExpandVars(Settings.ConsoleTitle);
|
||||
}
|
||||
|
||||
//Asking the user to type in missing data such as Username and Password
|
||||
|
||||
if (Settings.Login == "")
|
||||
{
|
||||
Console.Write(ConsoleIO.basicIO ? "Please type the username of your choice.\n" : "Username : ");
|
||||
Settings.Login = Console.ReadLine();
|
||||
}
|
||||
if (Settings.Password == "")
|
||||
{
|
||||
Console.Write(ConsoleIO.basicIO ? "Please type the password for " + Settings.Login + ".\n" : "Password : ");
|
||||
Settings.Password = ConsoleIO.basicIO ? Console.ReadLine() : ConsoleIO.ReadPassword();
|
||||
if (Settings.Password == "") { Settings.Password = "-"; }
|
||||
if (!ConsoleIO.basicIO)
|
||||
{
|
||||
//Hide password length
|
||||
Console.CursorTop--; Console.Write("Password : <******>");
|
||||
for (int i = 19; i < Console.BufferWidth; i++) { Console.Write(' '); }
|
||||
}
|
||||
}
|
||||
|
||||
startupargs = args;
|
||||
InitializeClient();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start a new Client
|
||||
/// </summary>
|
||||
|
||||
private static void InitializeClient()
|
||||
{
|
||||
ProtocolHandler.LoginResult result;
|
||||
Settings.Username = Settings.Login;
|
||||
string sessionID = "";
|
||||
string UUID = "";
|
||||
|
||||
if (Settings.Password == "-")
|
||||
{
|
||||
ConsoleIO.WriteLineFormatted("§8You chose to run in offline mode.");
|
||||
result = ProtocolHandler.LoginResult.Success;
|
||||
sessionID = "0";
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Connecting to Minecraft.net...");
|
||||
result = ProtocolHandler.GetLogin(ref Settings.Username, Settings.Password, ref sessionID, ref UUID);
|
||||
}
|
||||
|
||||
if (result == ProtocolHandler.LoginResult.Success)
|
||||
{
|
||||
if (Settings.ConsoleTitle != "")
|
||||
Console.Title = Settings.ExpandVars(Settings.ConsoleTitle);
|
||||
|
||||
if (Settings.playerHeadAsIcon)
|
||||
ConsoleIcon.setPlayerIconAsync(Settings.Username);
|
||||
|
||||
Console.WriteLine("Success. (session ID: " + sessionID + ')');
|
||||
|
||||
//ProtocolHandler.RealmsListWorlds(Settings.Username, UUID, sessionID); //TODO REMOVE
|
||||
|
||||
if (Settings.ServerIP == "")
|
||||
{
|
||||
Console.Write("Server IP : ");
|
||||
Settings.SetServerIP(Console.ReadLine());
|
||||
}
|
||||
|
||||
//Get server version
|
||||
int protocolversion = 0;
|
||||
ForgeInfo forgeInfo = null;
|
||||
|
||||
if (Settings.ServerVersion != "" && Settings.ServerVersion.ToLower() != "auto")
|
||||
{
|
||||
protocolversion = Protocol.ProtocolHandler.MCVer2ProtocolVersion(Settings.ServerVersion);
|
||||
|
||||
if (protocolversion != 0)
|
||||
{
|
||||
ConsoleIO.WriteLineFormatted("§8Using Minecraft version " + Settings.ServerVersion + " (protocol v" + protocolversion + ')');
|
||||
}
|
||||
else ConsoleIO.WriteLineFormatted("§8Unknown or not supported MC version '" + Settings.ServerVersion + "'.\nSwitching to autodetection mode.");
|
||||
|
||||
if (useMcVersionOnce)
|
||||
{
|
||||
useMcVersionOnce = false;
|
||||
Settings.ServerVersion = "";
|
||||
}
|
||||
}
|
||||
|
||||
if (protocolversion == 0)
|
||||
{
|
||||
Console.WriteLine("Retrieving Server Info...");
|
||||
if (!ProtocolHandler.GetServerInfo(Settings.ServerIP, Settings.ServerPort, ref protocolversion, ref forgeInfo))
|
||||
{
|
||||
HandleFailure("Failed to ping this IP.", true, ChatBots.AutoRelog.DisconnectReason.ConnectionLost);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (forgeInfo != null && !forgeInfo.Mods.Any())
|
||||
{
|
||||
forgeInfo = null;
|
||||
}
|
||||
|
||||
if (protocolversion != 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
//Start the main TCP client
|
||||
if (Settings.SingleCommand != "")
|
||||
{
|
||||
Client = new McTcpClient(Settings.Username, UUID, sessionID, Settings.ServerIP, Settings.ServerPort, protocolversion, forgeInfo, Settings.SingleCommand);
|
||||
}
|
||||
else Client = new McTcpClient(Settings.Username, UUID, sessionID, protocolversion, forgeInfo, Settings.ServerIP, Settings.ServerPort);
|
||||
|
||||
//Update console title
|
||||
if (Settings.ConsoleTitle != "")
|
||||
Console.Title = Settings.ExpandVars(Settings.ConsoleTitle);
|
||||
}
|
||||
catch (NotSupportedException) { HandleFailure("Cannot connect to the server : This version is not supported !", true); }
|
||||
}
|
||||
else HandleFailure("Failed to determine server version.", true);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
string failureMessage = "Minecraft Login failed : ";
|
||||
switch (result)
|
||||
{
|
||||
case ProtocolHandler.LoginResult.AccountMigrated: failureMessage += "Account migrated, use e-mail as username."; break;
|
||||
case ProtocolHandler.LoginResult.ServiceUnavailable: failureMessage += "Login servers are unavailable. Please try again later."; break;
|
||||
case ProtocolHandler.LoginResult.WrongPassword: failureMessage += "Incorrect password."; break;
|
||||
case ProtocolHandler.LoginResult.NotPremium: failureMessage += "User not premium."; break;
|
||||
case ProtocolHandler.LoginResult.OtherError: failureMessage += "Network error."; break;
|
||||
case ProtocolHandler.LoginResult.SSLError: failureMessage += "SSL Error."; break;
|
||||
default: failureMessage += "Unknown Error."; break;
|
||||
}
|
||||
if (result == ProtocolHandler.LoginResult.SSLError && isUsingMono)
|
||||
{
|
||||
ConsoleIO.WriteLineFormatted("§8It appears that you are using Mono to run this program."
|
||||
+ '\n' + "The first time, you have to import HTTPS certificates using:"
|
||||
+ '\n' + "mozroots --import --ask-remove");
|
||||
return;
|
||||
}
|
||||
HandleFailure(failureMessage, false, ChatBot.DisconnectReason.LoginRejected);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disconnect the current client from the server and restart it
|
||||
/// </summary>
|
||||
|
||||
public static void Restart()
|
||||
{
|
||||
new Thread(new ThreadStart(delegate
|
||||
{
|
||||
if (Client != null) { Client.Disconnect(); ConsoleIO.Reset(); }
|
||||
if (offlinePrompt != null) { offlinePrompt.Abort(); offlinePrompt = null; ConsoleIO.Reset(); }
|
||||
Console.WriteLine("Restarting Minecraft Console Client...");
|
||||
InitializeClient();
|
||||
})).Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disconnect the current client from the server and exit the app
|
||||
/// </summary>
|
||||
|
||||
public static void Exit()
|
||||
{
|
||||
new Thread(new ThreadStart(delegate
|
||||
{
|
||||
if (Client != null) { Client.Disconnect(); ConsoleIO.Reset(); }
|
||||
if (offlinePrompt != null) { offlinePrompt.Abort(); offlinePrompt = null; ConsoleIO.Reset(); }
|
||||
if (Settings.playerHeadAsIcon) { ConsoleIcon.revertToCMDIcon(); }
|
||||
Environment.Exit(0);
|
||||
})).Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle fatal errors such as ping failure, login failure, server disconnection, and so on.
|
||||
/// Allows AutoRelog to perform on fatal errors, prompt for server version, and offline commands.
|
||||
/// </summary>
|
||||
/// <param name="errorMessage">Error message to display and optionally pass to AutoRelog bot</param>
|
||||
/// <param name="versionError">Specify if the error is related to an incompatible or unkown server version</param>
|
||||
/// <param name="disconnectReason">If set, the error message will be processed by the AutoRelog bot</param>
|
||||
|
||||
public static void HandleFailure(string errorMessage = null, bool versionError = false, ChatBots.AutoRelog.DisconnectReason? disconnectReason = null)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(errorMessage))
|
||||
{
|
||||
ConsoleIO.Reset();
|
||||
while (Console.KeyAvailable)
|
||||
Console.ReadKey(true);
|
||||
Console.WriteLine(errorMessage);
|
||||
|
||||
if (disconnectReason.HasValue)
|
||||
{
|
||||
if (ChatBots.AutoRelog.OnDisconnectStatic(disconnectReason.Value, errorMessage))
|
||||
return; //AutoRelog is triggering a restart of the client
|
||||
}
|
||||
}
|
||||
|
||||
if (Settings.interactiveMode)
|
||||
{
|
||||
if (versionError)
|
||||
{
|
||||
Console.Write("Server version : ");
|
||||
Settings.ServerVersion = Console.ReadLine();
|
||||
if (Settings.ServerVersion != "")
|
||||
{
|
||||
useMcVersionOnce = true;
|
||||
Restart();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (offlinePrompt == null)
|
||||
{
|
||||
offlinePrompt = new Thread(new ThreadStart(delegate
|
||||
{
|
||||
string command = " ";
|
||||
ConsoleIO.WriteLineFormatted("Not connected to any server. Use '" + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + "help' for help.");
|
||||
ConsoleIO.WriteLineFormatted("Or press Enter to exit Minecraft Console Client.");
|
||||
while (command.Length > 0)
|
||||
{
|
||||
if (!ConsoleIO.basicIO) { ConsoleIO.Write('>'); }
|
||||
command = Console.ReadLine().Trim();
|
||||
if (command.Length > 0)
|
||||
{
|
||||
string message = "";
|
||||
|
||||
if (Settings.internalCmdChar != ' '
|
||||
&& command[0] == Settings.internalCmdChar)
|
||||
command = command.Substring(1);
|
||||
|
||||
if (command.StartsWith("reco"))
|
||||
{
|
||||
message = new Commands.Reco().Run(null, Settings.ExpandVars(command));
|
||||
}
|
||||
else if (command.StartsWith("connect"))
|
||||
{
|
||||
message = new Commands.Connect().Run(null, Settings.ExpandVars(command));
|
||||
}
|
||||
else if (command.StartsWith("exit") || command.StartsWith("quit"))
|
||||
{
|
||||
message = new Commands.Exit().Run(null, Settings.ExpandVars(command));
|
||||
}
|
||||
else if (command.StartsWith("help"))
|
||||
{
|
||||
ConsoleIO.WriteLineFormatted("§8MCC: " + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + new Commands.Reco().CMDDesc);
|
||||
ConsoleIO.WriteLineFormatted("§8MCC: " + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + new Commands.Connect().CMDDesc);
|
||||
}
|
||||
else ConsoleIO.WriteLineFormatted("§8Unknown command '" + command.Split(' ')[0] + "'.");
|
||||
|
||||
if (message != "")
|
||||
ConsoleIO.WriteLineFormatted("§8MCC: " + message);
|
||||
}
|
||||
}
|
||||
}));
|
||||
offlinePrompt.Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Detect if the user is running Minecraft Console Client through Mono
|
||||
/// </summary>
|
||||
|
||||
public static bool isUsingMono
|
||||
{
|
||||
get
|
||||
{
|
||||
return Type.GetType("Mono.Runtime") != null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enumerate types in namespace through reflection
|
||||
/// </summary>
|
||||
/// <param name="nameSpace">Namespace to process</param>
|
||||
/// <param name="assembly">Assembly to use. Default is Assembly.GetExecutingAssembly()</param>
|
||||
/// <returns></returns>
|
||||
|
||||
public static Type[] GetTypesInNamespace(string nameSpace, Assembly assembly = null)
|
||||
{
|
||||
if (assembly == null) { assembly = Assembly.GetExecutingAssembly(); }
|
||||
return assembly.GetTypes().Where(t => String.Equals(t.Namespace, nameSpace, StringComparison.Ordinal)).ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using MinecraftClient.Protocol;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using MinecraftClient.Protocol.Handlers.Forge;
|
||||
|
||||
namespace MinecraftClient
|
||||
{
|
||||
/// <summary>
|
||||
/// Minecraft Console Client by ORelio (c) 2012-2014.
|
||||
/// Allows to connect to any Minecraft server, send and receive text, automated scripts.
|
||||
/// This source code is released under the CDDL 1.0 License.
|
||||
/// </summary>
|
||||
|
||||
static class Program
|
||||
{
|
||||
private static McTcpClient Client;
|
||||
public static string[] startupargs;
|
||||
|
||||
public const string Version = "1.8.2";
|
||||
public const string MCLowestVersion = "1.4.6";
|
||||
public const string MCHighestVersion = "1.8.8";
|
||||
|
||||
private static Thread offlinePrompt = null;
|
||||
private static bool useMcVersionOnce = false;
|
||||
|
||||
/// <summary>
|
||||
/// The main entry point of Minecraft Console Client
|
||||
/// </summary>
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Console Client for MC {0} to {1} - v{2} - By ORelio & Contributors", MCLowestVersion, MCHighestVersion, Version);
|
||||
|
||||
//Basic Input/Output ?
|
||||
if (args.Length >= 1 && args[args.Length - 1] == "BasicIO")
|
||||
{
|
||||
ConsoleIO.basicIO = true;
|
||||
Console.OutputEncoding = Console.InputEncoding = Encoding.GetEncoding(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage);
|
||||
args = args.Where(o => !Object.ReferenceEquals(o, args[args.Length - 1])).ToArray();
|
||||
}
|
||||
|
||||
//Process ini configuration file
|
||||
if (args.Length >= 1 && System.IO.File.Exists(args[0]) && System.IO.Path.GetExtension(args[0]).ToLower() == ".ini")
|
||||
{
|
||||
Settings.LoadSettings(args[0]);
|
||||
|
||||
//remove ini configuration file from arguments array
|
||||
List<string> args_tmp = args.ToList<string>();
|
||||
args_tmp.RemoveAt(0);
|
||||
args = args_tmp.ToArray();
|
||||
}
|
||||
else if (System.IO.File.Exists("MinecraftClient.ini"))
|
||||
{
|
||||
Settings.LoadSettings("MinecraftClient.ini");
|
||||
}
|
||||
else Settings.WriteDefaultSettings("MinecraftClient.ini");
|
||||
|
||||
//Other command-line arguments
|
||||
if (args.Length >= 1)
|
||||
{
|
||||
Settings.Login = args[0];
|
||||
if (args.Length >= 2)
|
||||
{
|
||||
Settings.Password = args[1];
|
||||
if (args.Length >= 3)
|
||||
{
|
||||
Settings.SetServerIP(args[2]);
|
||||
|
||||
//Single command?
|
||||
if (args.Length >= 4)
|
||||
{
|
||||
Settings.SingleCommand = args[3];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Settings.ConsoleTitle != "")
|
||||
{
|
||||
Settings.Username = "New Window";
|
||||
Console.Title = Settings.ExpandVars(Settings.ConsoleTitle);
|
||||
}
|
||||
|
||||
//Load cached sessions from disk if necessary
|
||||
if (Settings.CacheType == Cache.CacheType.DISK)
|
||||
{
|
||||
Console.WriteLine(Cache.SessionCache.InitializeDiskCache() ? "Cached sessions loaded." : "Cached sessions could not be loaded from disk");
|
||||
}
|
||||
|
||||
//Asking the user to type in missing data such as Username and Password
|
||||
|
||||
if (Settings.Login == "")
|
||||
{
|
||||
Console.Write(ConsoleIO.basicIO ? "Please type the username of your choice.\n" : "Username : ");
|
||||
Settings.Login = Console.ReadLine();
|
||||
}
|
||||
if (Settings.Password == "" && (Settings.CacheType == Cache.CacheType.NONE || !Cache.SessionCache.Contains(Settings.Login)))
|
||||
{
|
||||
RequestPassword();
|
||||
}
|
||||
|
||||
startupargs = args;
|
||||
InitializeClient();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reduest user to submit password.
|
||||
/// </summary>
|
||||
private static void RequestPassword()
|
||||
{
|
||||
Console.Write(ConsoleIO.basicIO ? "Please type the password for " + Settings.Login + ".\n" : "Password : ");
|
||||
Settings.Password = ConsoleIO.basicIO ? Console.ReadLine() : ConsoleIO.ReadPassword();
|
||||
if (Settings.Password == "") { Settings.Password = "-"; }
|
||||
if (!ConsoleIO.basicIO)
|
||||
{
|
||||
//Hide password length
|
||||
Console.CursorTop--; Console.Write("Password : <******>");
|
||||
for (int i = 19; i < Console.BufferWidth; i++) { Console.Write(' '); }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start a new Client
|
||||
/// </summary>
|
||||
|
||||
private static void InitializeClient()
|
||||
{
|
||||
SessionToken session = new SessionToken();
|
||||
|
||||
ProtocolHandler.LoginResult result = ProtocolHandler.LoginResult.LoginRequired;
|
||||
|
||||
if (Settings.Password == "-")
|
||||
{
|
||||
ConsoleIO.WriteLineFormatted("§8You chose to run in offline mode.");
|
||||
result = ProtocolHandler.LoginResult.Success;
|
||||
session.PlayerID = "0";
|
||||
session.PlayerName = Settings.Login;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Validate cached session or login new session.
|
||||
if (Settings.CacheType != Cache.CacheType.NONE && Cache.SessionCache.Contains(Settings.Login))
|
||||
{
|
||||
session = Cache.SessionCache.Get(Settings.Login);
|
||||
result = ProtocolHandler.GetTokenValidation(session);
|
||||
|
||||
if (result != ProtocolHandler.LoginResult.Success && Settings.Password == "")
|
||||
{
|
||||
RequestPassword();
|
||||
}
|
||||
|
||||
Console.WriteLine("Cached session is " + (result == ProtocolHandler.LoginResult.Success ? "valid." : "invalid."));
|
||||
|
||||
}
|
||||
|
||||
if (result != ProtocolHandler.LoginResult.Success)
|
||||
{
|
||||
Console.WriteLine("Connecting to Minecraft.net...");
|
||||
result = ProtocolHandler.GetLogin(Settings.Login, Settings.Password, out session);
|
||||
|
||||
if (result == ProtocolHandler.LoginResult.Success && Settings.CacheType != Cache.CacheType.NONE)
|
||||
{
|
||||
Cache.SessionCache.Store(Settings.Login, session);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (result == ProtocolHandler.LoginResult.Success)
|
||||
{
|
||||
Settings.Username = session.PlayerName;
|
||||
|
||||
if (Settings.ConsoleTitle != "")
|
||||
Console.Title = Settings.ExpandVars(Settings.ConsoleTitle);
|
||||
|
||||
if (Settings.playerHeadAsIcon)
|
||||
ConsoleIcon.setPlayerIconAsync(Settings.Username);
|
||||
|
||||
Console.WriteLine("Success. (session ID: " + session.ID + ')');
|
||||
|
||||
//ProtocolHandler.RealmsListWorlds(Settings.Username, PlayerID, sessionID); //TODO REMOVE
|
||||
|
||||
if (Settings.ServerIP == "")
|
||||
{
|
||||
Console.Write("Server IP : ");
|
||||
Settings.SetServerIP(Console.ReadLine());
|
||||
}
|
||||
|
||||
//Get server version
|
||||
int protocolversion = 0;
|
||||
ForgeInfo forgeInfo = null;
|
||||
|
||||
if (Settings.ServerVersion != "" && Settings.ServerVersion.ToLower() != "auto")
|
||||
{
|
||||
protocolversion = Protocol.ProtocolHandler.MCVer2ProtocolVersion(Settings.ServerVersion);
|
||||
|
||||
if (protocolversion != 0)
|
||||
{
|
||||
ConsoleIO.WriteLineFormatted("§8Using Minecraft version " + Settings.ServerVersion + " (protocol v" + protocolversion + ')');
|
||||
}
|
||||
else ConsoleIO.WriteLineFormatted("§8Unknown or not supported MC version '" + Settings.ServerVersion + "'.\nSwitching to autodetection mode.");
|
||||
|
||||
if (useMcVersionOnce)
|
||||
{
|
||||
useMcVersionOnce = false;
|
||||
Settings.ServerVersion = "";
|
||||
}
|
||||
}
|
||||
|
||||
if (protocolversion == 0)
|
||||
{
|
||||
Console.WriteLine("Retrieving Server Info...");
|
||||
if (!ProtocolHandler.GetServerInfo(Settings.ServerIP, Settings.ServerPort, ref protocolversion, ref forgeInfo))
|
||||
{
|
||||
HandleFailure("Failed to ping this IP.", true, ChatBots.AutoRelog.DisconnectReason.ConnectionLost);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (forgeInfo != null && !forgeInfo.Mods.Any())
|
||||
{
|
||||
forgeInfo = null;
|
||||
}
|
||||
|
||||
if (protocolversion != 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
//Start the main TCP client
|
||||
if (Settings.SingleCommand != "")
|
||||
{
|
||||
Client = new McTcpClient(session.PlayerName, session.PlayerID, session.ID, Settings.ServerIP, Settings.ServerPort, protocolversion, forgeInfo, Settings.SingleCommand);
|
||||
}
|
||||
else Client = new McTcpClient(session.PlayerName, session.PlayerID, session.ID, protocolversion, forgeInfo, Settings.ServerIP, Settings.ServerPort);
|
||||
|
||||
//Update console title
|
||||
if (Settings.ConsoleTitle != "")
|
||||
Console.Title = Settings.ExpandVars(Settings.ConsoleTitle);
|
||||
}
|
||||
catch (NotSupportedException) { HandleFailure("Cannot connect to the server : This version is not supported !", true); }
|
||||
}
|
||||
else HandleFailure("Failed to determine server version.", true);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
string failureMessage = "Minecraft Login failed : ";
|
||||
switch (result)
|
||||
{
|
||||
case ProtocolHandler.LoginResult.AccountMigrated: failureMessage += "Account migrated, use e-mail as username."; break;
|
||||
case ProtocolHandler.LoginResult.ServiceUnavailable: failureMessage += "Login servers are unavailable. Please try again later."; break;
|
||||
case ProtocolHandler.LoginResult.WrongPassword: failureMessage += "Incorrect password."; break;
|
||||
case ProtocolHandler.LoginResult.NotPremium: failureMessage += "User not premium."; break;
|
||||
case ProtocolHandler.LoginResult.OtherError: failureMessage += "Network error."; break;
|
||||
case ProtocolHandler.LoginResult.SSLError: failureMessage += "SSL Error."; break;
|
||||
default: failureMessage += "Unknown Error."; break;
|
||||
}
|
||||
if (result == ProtocolHandler.LoginResult.SSLError && isUsingMono)
|
||||
{
|
||||
ConsoleIO.WriteLineFormatted("§8It appears that you are using Mono to run this program."
|
||||
+ '\n' + "The first time, you have to import HTTPS certificates using:"
|
||||
+ '\n' + "mozroots --import --ask-remove");
|
||||
return;
|
||||
}
|
||||
HandleFailure(failureMessage, false, ChatBot.DisconnectReason.LoginRejected);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disconnect the current client from the server and restart it
|
||||
/// </summary>
|
||||
|
||||
public static void Restart()
|
||||
{
|
||||
new Thread(new ThreadStart(delegate
|
||||
{
|
||||
if (Client != null) { Client.Disconnect(); ConsoleIO.Reset(); }
|
||||
if (offlinePrompt != null) { offlinePrompt.Abort(); offlinePrompt = null; ConsoleIO.Reset(); }
|
||||
Console.WriteLine("Restarting Minecraft Console Client...");
|
||||
InitializeClient();
|
||||
})).Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disconnect the current client from the server and exit the app
|
||||
/// </summary>
|
||||
|
||||
public static void Exit()
|
||||
{
|
||||
new Thread(new ThreadStart(delegate
|
||||
{
|
||||
if (Client != null) { Client.Disconnect(); ConsoleIO.Reset(); }
|
||||
if (offlinePrompt != null) { offlinePrompt.Abort(); offlinePrompt = null; ConsoleIO.Reset(); }
|
||||
if (Settings.playerHeadAsIcon) { ConsoleIcon.revertToCMDIcon(); }
|
||||
Environment.Exit(0);
|
||||
})).Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle fatal errors such as ping failure, login failure, server disconnection, and so on.
|
||||
/// Allows AutoRelog to perform on fatal errors, prompt for server version, and offline commands.
|
||||
/// </summary>
|
||||
/// <param name="errorMessage">Error message to display and optionally pass to AutoRelog bot</param>
|
||||
/// <param name="versionError">Specify if the error is related to an incompatible or unkown server version</param>
|
||||
/// <param name="disconnectReason">If set, the error message will be processed by the AutoRelog bot</param>
|
||||
|
||||
public static void HandleFailure(string errorMessage = null, bool versionError = false, ChatBots.AutoRelog.DisconnectReason? disconnectReason = null)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(errorMessage))
|
||||
{
|
||||
ConsoleIO.Reset();
|
||||
while (Console.KeyAvailable)
|
||||
Console.ReadKey(true);
|
||||
Console.WriteLine(errorMessage);
|
||||
|
||||
if (disconnectReason.HasValue)
|
||||
{
|
||||
if (ChatBots.AutoRelog.OnDisconnectStatic(disconnectReason.Value, errorMessage))
|
||||
return; //AutoRelog is triggering a restart of the client
|
||||
}
|
||||
}
|
||||
|
||||
if (Settings.interactiveMode)
|
||||
{
|
||||
if (versionError)
|
||||
{
|
||||
Console.Write("Server version : ");
|
||||
Settings.ServerVersion = Console.ReadLine();
|
||||
if (Settings.ServerVersion != "")
|
||||
{
|
||||
useMcVersionOnce = true;
|
||||
Restart();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (offlinePrompt == null)
|
||||
{
|
||||
offlinePrompt = new Thread(new ThreadStart(delegate
|
||||
{
|
||||
string command = " ";
|
||||
ConsoleIO.WriteLineFormatted("Not connected to any server. Use '" + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + "help' for help.");
|
||||
ConsoleIO.WriteLineFormatted("Or press Enter to exit Minecraft Console Client.");
|
||||
while (command.Length > 0)
|
||||
{
|
||||
if (!ConsoleIO.basicIO) { ConsoleIO.Write('>'); }
|
||||
command = Console.ReadLine().Trim();
|
||||
if (command.Length > 0)
|
||||
{
|
||||
string message = "";
|
||||
|
||||
if (Settings.internalCmdChar != ' '
|
||||
&& command[0] == Settings.internalCmdChar)
|
||||
command = command.Substring(1);
|
||||
|
||||
if (command.StartsWith("reco"))
|
||||
{
|
||||
message = new Commands.Reco().Run(null, Settings.ExpandVars(command));
|
||||
}
|
||||
else if (command.StartsWith("connect"))
|
||||
{
|
||||
message = new Commands.Connect().Run(null, Settings.ExpandVars(command));
|
||||
}
|
||||
else if (command.StartsWith("exit") || command.StartsWith("quit"))
|
||||
{
|
||||
message = new Commands.Exit().Run(null, Settings.ExpandVars(command));
|
||||
}
|
||||
else if (command.StartsWith("help"))
|
||||
{
|
||||
ConsoleIO.WriteLineFormatted("§8MCC: " + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + new Commands.Reco().CMDDesc);
|
||||
ConsoleIO.WriteLineFormatted("§8MCC: " + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + new Commands.Connect().CMDDesc);
|
||||
}
|
||||
else ConsoleIO.WriteLineFormatted("§8Unknown command '" + command.Split(' ')[0] + "'.");
|
||||
|
||||
if (message != "")
|
||||
ConsoleIO.WriteLineFormatted("§8MCC: " + message);
|
||||
}
|
||||
}
|
||||
}));
|
||||
offlinePrompt.Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Detect if the user is running Minecraft Console Client through Mono
|
||||
/// </summary>
|
||||
|
||||
public static bool isUsingMono
|
||||
{
|
||||
get
|
||||
{
|
||||
return Type.GetType("Mono.Runtime") != null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enumerate types in namespace through reflection
|
||||
/// </summary>
|
||||
/// <param name="nameSpace">Namespace to process</param>
|
||||
/// <param name="assembly">Assembly to use. Default is Assembly.GetExecutingAssembly()</param>
|
||||
/// <returns></returns>
|
||||
|
||||
public static Type[] GetTypesInNamespace(string nameSpace, Assembly assembly = null)
|
||||
{
|
||||
if (assembly == null) { assembly = Assembly.GetExecutingAssembly(); }
|
||||
return assembly.GetTypes().Where(t => String.Equals(t.Namespace, nameSpace, StringComparison.Ordinal)).ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@ using MinecraftClient.Protocol.Handlers;
|
|||
using MinecraftClient.Proxy;
|
||||
using System.Net.Sockets;
|
||||
using System.Net.Security;
|
||||
using MinecraftClient.Protocol.Handlers.Forge;
|
||||
using MinecraftClient.Protocol.Handlers.Forge;
|
||||
|
||||
|
||||
namespace MinecraftClient.Protocol
|
||||
{
|
||||
|
|
@ -140,7 +141,7 @@ namespace MinecraftClient.Protocol
|
|||
}
|
||||
}
|
||||
|
||||
public enum LoginResult { OtherError, ServiceUnavailable, SSLError, Success, WrongPassword, AccountMigrated, NotPremium };
|
||||
public enum LoginResult { OtherError, ServiceUnavailable, SSLError, Success, WrongPassword, AccountMigrated, NotPremium, LoginRequired, InvalidToken, NullError };
|
||||
|
||||
/// <summary>
|
||||
/// Allows to login to a premium Minecraft account using the Yggdrasil authentication scheme.
|
||||
|
|
@ -148,15 +149,19 @@ namespace MinecraftClient.Protocol
|
|||
/// <param name="user">Login</param>
|
||||
/// <param name="pass">Password</param>
|
||||
/// <param name="accesstoken">Will contain the access token returned by Minecraft.net, if the login is successful</param>
|
||||
/// <param name="uuid">Will contain the player's UUID, needed for multiplayer</param>
|
||||
/// <param name="clienttoken">Will contain the client token generated before sending to Minecraft.net</param>
|
||||
/// <param name="uuid">Will contain the player's PlayerID, needed for multiplayer</param>
|
||||
/// <returns>Returns the status of the login (Success, Failure, etc.)</returns>
|
||||
|
||||
public static LoginResult GetLogin(ref string user, string pass, ref string accesstoken, ref string uuid)
|
||||
public static LoginResult GetLogin(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) + "\" }";
|
||||
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);
|
||||
if (code == 200)
|
||||
{
|
||||
|
|
@ -167,11 +172,11 @@ namespace MinecraftClient.Protocol
|
|||
else
|
||||
{
|
||||
string[] temp = result.Split(new string[] { "accessToken\":\"" }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (temp.Length >= 2) { accesstoken = temp[1].Split('"')[0]; }
|
||||
if (temp.Length >= 2) { session.ID = temp[1].Split('"')[0]; }
|
||||
temp = result.Split(new string[] { "name\":\"" }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (temp.Length >= 2) { user = temp[1].Split('"')[0]; }
|
||||
if (temp.Length >= 2) { session.PlayerName = temp[1].Split('"')[0]; }
|
||||
temp = result.Split(new string[] { "availableProfiles\":[{\"id\":\"" }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (temp.Length >= 2) { uuid = temp[1].Split('"')[0]; }
|
||||
if (temp.Length >= 2) { session.PlayerID = temp[1].Split('"')[0]; }
|
||||
return LoginResult.Success;
|
||||
}
|
||||
}
|
||||
|
|
@ -211,6 +216,90 @@ namespace MinecraftClient.Protocol
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates whether accessToken must be refreshed
|
||||
/// </summary>
|
||||
/// <param name="accesstoken">Will contain the cached access token previously returned by Minecraft.net</param>
|
||||
/// <param name="clienttoken">Will contain the cached client token created on login</param>
|
||||
/// <returns>Returns the status of the token (Valid, Invalid, etc.)</returns>
|
||||
///
|
||||
public static LoginResult GetTokenValidation(SessionToken session)
|
||||
{
|
||||
try
|
||||
{
|
||||
string result = "";
|
||||
string json_request = "{\"accessToken\": \"" + jsonEncode(session.ID) + "\", \"clientToken\": \"" + jsonEncode(session.ClientID) + "\" }";
|
||||
int code = doHTTPSPost("authserver.mojang.com", "/validate", json_request, ref result);
|
||||
if (code == 204)
|
||||
{
|
||||
return LoginResult.Success;
|
||||
}
|
||||
else if (code == 403)
|
||||
{
|
||||
return LoginResult.LoginRequired;
|
||||
}
|
||||
else
|
||||
{
|
||||
return LoginResult.OtherError;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return LoginResult.OtherError;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Refreshes invalid token
|
||||
/// </summary>
|
||||
/// <param name="user">Login</param>
|
||||
/// <param name="accesstoken">Will contain the new access token returned by Minecraft.net, if the refresh is successful</param>
|
||||
/// <param name="clienttoken">Will contain the client token generated before sending to Minecraft.net</param>
|
||||
/// <param name="uuid">Will contain the player's PlayerID, needed for multiplayer</param>
|
||||
/// <returns>Returns the status of the new token request (Success, Failure, etc.)</returns>
|
||||
///
|
||||
public static LoginResult GetNewToken(SessionToken currentsession, out SessionToken newsession)
|
||||
{
|
||||
newsession = 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("authserver.mojang.com", "/refresh", json_request, ref result);
|
||||
if (code == 200)
|
||||
{
|
||||
if (result == null)
|
||||
{
|
||||
return LoginResult.NullError;
|
||||
}
|
||||
else {
|
||||
string[] temp = result.Split(new string[] { "accessToken\":\"" }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (temp.Length >= 2) { newsession.ID = temp[1].Split('"')[0]; }
|
||||
temp = result.Split(new string[] { "clientToken\":\"" }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (temp.Length >= 2) { newsession.ClientID = temp[1].Split('"')[0]; }
|
||||
temp = result.Split(new string[] { "name\":\"" }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (temp.Length >= 2) { newsession.PlayerName = temp[1].Split('"')[0]; }
|
||||
temp = result.Split(new string[] { "selectedProfile\":[{\"id\":\"" }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (temp.Length >= 2) { newsession.PlayerID = temp[1].Split('"')[0]; }
|
||||
return LoginResult.Success;
|
||||
}
|
||||
}
|
||||
else if (code == 403 && result.Contains("InvalidToken"))
|
||||
{
|
||||
return LoginResult.InvalidToken;
|
||||
}
|
||||
else
|
||||
{
|
||||
ConsoleIO.WriteLineFormatted("§8Got error code from server while refreshing authentication: " + 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>
|
||||
|
|
|
|||
14
MinecraftClient/Protocol/SessionToken.cs
Normal file
14
MinecraftClient/Protocol/SessionToken.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
using System;
|
||||
|
||||
|
||||
namespace MinecraftClient.Protocol
|
||||
{
|
||||
[Serializable]
|
||||
public class SessionToken
|
||||
{
|
||||
public string ID { get; set; } = string.Empty;
|
||||
public string PlayerName { get; set; } = string.Empty;
|
||||
public string PlayerID { get; set; } = string.Empty;
|
||||
public string ClientID { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
|
|
@ -29,6 +29,9 @@ namespace MinecraftClient
|
|||
public static string SingleCommand = "";
|
||||
public static string ConsoleTitle = "";
|
||||
|
||||
//Cache Settings
|
||||
public static Cache.CacheType CacheType = Cache.CacheType.NONE;
|
||||
|
||||
//Proxy Settings
|
||||
public static bool ProxyEnabledLogin = false;
|
||||
public static bool ProxyEnabledIngame = false;
|
||||
|
|
@ -200,6 +203,12 @@ namespace MinecraftClient
|
|||
}
|
||||
break;
|
||||
|
||||
case "accountcache":
|
||||
if (argValue == "none") { CacheType = Cache.CacheType.NONE; }
|
||||
else if (argValue == "memory") { CacheType = Cache.CacheType.MEMORY; }
|
||||
else if (argValue == "disk") { CacheType = Cache.CacheType.DISK; }
|
||||
break;
|
||||
|
||||
case "accountlist":
|
||||
if (File.Exists(argValue))
|
||||
{
|
||||
|
|
@ -416,6 +425,7 @@ namespace MinecraftClient
|
|||
+ "showsystemmessages=true #system messages for server ops\r\n"
|
||||
+ "showxpbarmessages=true #messages displayed above xp bar\r\n"
|
||||
+ "terrainandmovements=false #uses more ram, cpu, bandwidth\r\n"
|
||||
+ "accountcache=none #use 'none', 'memory' or 'disk'\r\n"
|
||||
+ "accountlist=accounts.txt\r\n"
|
||||
+ "serverlist=servers.txt\r\n"
|
||||
+ "playerheadicon=true\r\n"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue