caching works. needs documentation and testing

This commit is contained in:
initsuj 2016-03-02 17:11:15 -07:00
parent 75f2f738a2
commit 57c53be09f
8 changed files with 262 additions and 73 deletions

View file

@ -1,12 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.Cache
{
public static class AuthCacheHandler
{
public enum Type { NONE, MEMORY, DISK };
}
}

View file

@ -0,0 +1,4 @@
namespace MinecraftClient.Cache
{
public enum CacheType { NONE, MEMORY, DISK };
}

View file

@ -0,0 +1,139 @@
using MinecraftClient.Protocol;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using System.Text;
namespace MinecraftClient.Cache
{
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 BinaryFormatter formatter = new BinaryFormatter();
public static bool Contains(string login)
{
return sessions.ContainsKey(login);
}
public static void Store(string login, SessionToken session)
{
if (Contains(login))
{
sessions[login] = session;
}
else
{
sessions.Add(login, session);
}
if (Settings.CacheType == CacheType.DISK)
{
SaveToDisk();
}
}
public static SessionToken Get(string login)
{
return sessions[login];
}
public static bool LoadFromDisk()
{
cachemonitor.Path = AppDomain.CurrentDomain.BaseDirectory;
cachemonitor.IncludeSubdirectories = false;
cachemonitor.Filter = filename;
cachemonitor.NotifyFilter = NotifyFilters.LastWrite;
cachemonitor.Changed += new FileSystemEventHandler(OnChanged);
cachemonitor.EnableRaisingEvents = true;
return ReadCacheFile();
}
public static void OnChanged(object source, FileSystemEventArgs e)
{
ReadCacheFile();
}
private static bool ReadCacheFile()
{
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("Error getting sessions from cache file ");
}
}
return false;
}
public static void SaveToDisk()
{
bool fileexists = File.Exists(filename);
using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
{
cachemonitor.EnableRaisingEvents = false;
if (fileexists)
{
fs.SetLength(0);
fs.Flush();
}
formatter.Serialize(fs, sessions);
cachemonitor.EnableRaisingEvents = true;
}
}
private static byte[] GetHash(FileStream fs, bool resetposition = true)
{
using (var md5 = MD5.Create())
{
long pos = fs.Position;
byte[] hash = md5.ComputeHash(fs);
fs.Position = resetposition ? pos : fs.Position;
return hash;
}
}
private static bool HashesEqual(byte[] hash1, byte[] hash2)
{
if (hash1.Length != hash2.Length)
{
return false;
}
for (int i = 0; i < hash1.Length; i++)
{
if (hash1[i] != hash2[i])
{
return false;
}
}
return true;
}
}
}

View file

@ -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,7 +73,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AutoTimeout.cs" />
<Compile Include="Cache\AuthCacheHandler.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" />
@ -149,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" />

View file

@ -85,6 +85,12 @@ namespace MinecraftClient
Console.Title = Settings.ExpandVars(Settings.ConsoleTitle);
}
//Load cached sessions from disk if necessary
if (Settings.CacheType == Cache.CacheType.DISK)
{
ConsoleIO.WriteLineFormatted(Cache.SessionCache.LoadFromDisk() ? "Cached sessions loaded." : "§8Cached sessions could not be loaded from disk");
}
//Asking the user to type in missing data such as Username and Password
if (Settings.Login == "")
@ -92,7 +98,19 @@ namespace MinecraftClient
Console.Write(ConsoleIO.basicIO ? "Please type the username of your choice.\n" : "Username : ");
Settings.Login = Console.ReadLine();
}
if (Settings.Password == "")
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();
@ -105,45 +123,66 @@ namespace MinecraftClient
}
}
startupargs = args;
InitializeClient();
}
/// <summary>
/// Start a new Client
/// </summary>
private static void InitializeClient()
{
ProtocolHandler.LoginResult result;
Settings.Username = Settings.Login;
string sessionID = "";
string clientID = "";
string UUID = "";
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;
sessionID = "0";
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(ref Settings.Username, Settings.Password, ref sessionID, ref clientID, ref UUID);
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: " + sessionID + ')');
Console.WriteLine("Success. (session ID: " + session.ID + ')');
//ProtocolHandler.RealmsListWorlds(Settings.Username, UUID, sessionID); //TODO REMOVE
//ProtocolHandler.RealmsListWorlds(Settings.Username, PlayerID, sessionID); //TODO REMOVE
if (Settings.ServerIP == "")
{
@ -194,9 +233,9 @@ namespace MinecraftClient
//Start the main TCP client
if (Settings.SingleCommand != "")
{
Client = new McTcpClient(Settings.Username, UUID, sessionID, Settings.ServerIP, Settings.ServerPort, protocolversion, forgeInfo, Settings.SingleCommand);
Client = new McTcpClient(session.PlayerName, session.PlayerID, session.ID, Settings.ServerIP, Settings.ServerPort, protocolversion, forgeInfo, Settings.SingleCommand);
}
else Client = new McTcpClient(Settings.Username, UUID, sessionID, protocolversion, forgeInfo, Settings.ServerIP, Settings.ServerPort);
else Client = new McTcpClient(session.PlayerName, session.PlayerID, session.ID, protocolversion, forgeInfo, Settings.ServerIP, Settings.ServerPort);
//Update console title
if (Settings.ConsoleTitle != "")

View file

@ -141,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.
@ -150,19 +150,18 @@ namespace MinecraftClient.Protocol
/// <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="clienttoken">Will contain the client token generated before sending to Minecraft.net</param>
/// <param name="uuid">Will contain the player's UUID, needed for multiplayer</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 clienttoken, 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 = "";
if (clienttoken == string.Empty)
{
clienttoken = Guid.NewGuid().ToString().Replace("-","");
}
string json_request = "{\"agent\": { \"name\": \"Minecraft\", \"version\": 1 }, \"username\": \"" + jsonEncode(user) + "\", \"password\": \"" + jsonEncode(pass) + "\", \"clientToken\": \"" + jsonEncode(clienttoken) + "\" }";
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)
{
@ -173,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;
}
}
@ -226,29 +225,29 @@ namespace MinecraftClient.Protocol
/// <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 ValidationResult GetTokenValidation(string accesstoken, string clienttoken)
public static LoginResult GetTokenValidation(SessionToken session)
{
try
{
string result = "";
string json_request = "{\"accessToken\": \"" + jsonEncode(accesstoken) + "\", \"clientToken\": \"" + jsonEncode(clienttoken) + "\" }";
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 ValidationResult.Validated;
return LoginResult.Success;
}
else if (code == 403)
{
return ValidationResult.NewTokenRequired;
return LoginResult.LoginRequired;
}
else
{
return ValidationResult.Error;
return LoginResult.OtherError;
}
}
catch
{
return ValidationResult.Error;
return LoginResult.OtherError;
}
}
@ -260,45 +259,48 @@ namespace MinecraftClient.Protocol
/// <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 UUID, needed for multiplayer</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 NewTokenResult GetNewToken(ref string user, ref string accesstoken, ref string clienttoken, ref string uuid)
public static LoginResult GetNewToken(SessionToken currentsession, out SessionToken newsession)
{
newsession = new SessionToken();
try
{
string result = "";
string json_request = "{ \"accessToken\": \"" + jsonEncode(accesstoken) + "\", \"clientToken\": \"" + jsonEncode(clienttoken) + "\", \"selectedProfile\": { \"id\": \"" + jsonEncode(uuid) + "\", \"name\": \"" + jsonEncode(user) + "\" } }";
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 NewTokenResult.NullError;
}else{
if (result == null)
{
return LoginResult.NullError;
}
else {
string[] temp = result.Split(new string[] { "accessToken\":\"" }, StringSplitOptions.RemoveEmptyEntries);
if (temp.Length >= 2) { accesstoken = temp[1].Split('"')[0]; }
if (temp.Length >= 2) { newsession.ID = temp[1].Split('"')[0]; }
temp = result.Split(new string[] { "clientToken\":\"" }, StringSplitOptions.RemoveEmptyEntries);
if (temp.Length >= 2) { clienttoken = temp[1].Split('"')[0]; }
if (temp.Length >= 2) { newsession.ClientID = 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) { newsession.PlayerName = temp[1].Split('"')[0]; }
temp = result.Split(new string[] { "selectedProfile\":[{\"id\":\"" }, StringSplitOptions.RemoveEmptyEntries);
if (temp.Length >= 2) { uuid = temp[1].Split('"')[0]; }
return NewTokenResult.Success;
if (temp.Length >= 2) { newsession.PlayerID = temp[1].Split('"')[0]; }
return LoginResult.Success;
}
}
else if (code == 403 && result.Contains("InvalidToken"))
{
return NewTokenResult.InvalidToken;
return LoginResult.InvalidToken;
}
else
{
ConsoleIO.WriteLineFormatted("§8Got error code from server while refreshing authentication: " + code);
return NewTokenResult.OtherError;
return LoginResult.OtherError;
}
}
catch
{
return NewTokenResult.OtherError;
return LoginResult.OtherError;
}
}

View 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;
}
}

View file

@ -30,7 +30,7 @@ namespace MinecraftClient
public static string ConsoleTitle = "";
//Cache Settings
public static Cache.AuthCacheHandler.Type CacheType = Cache.AuthCacheHandler.Type.NONE;
public static Cache.CacheType CacheType = Cache.CacheType.NONE;
//Proxy Settings
public static bool ProxyEnabledLogin = false;
@ -204,9 +204,9 @@ namespace MinecraftClient
break;
case "accountcache":
if(argValue == "none") { CacheType = Cache.AuthCacheHandler.Type.NONE; }
else if(argValue == "memory") { CacheType = Cache.AuthCacheHandler.Type.MEMORY; }
else if(argValue == "disk") { CacheType = Cache.AuthCacheHandler.Type.DISK; }
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":