Online Mode done, 1.7 compatibility should be OK

+ Fixed chat colors because of 1.7 changes
+ Some code cleanup here and there
This commit is contained in:
ORelio 2014-01-09 23:28:41 +01:00
parent 0d0a7e9c4a
commit 30cbc85850
5 changed files with 42 additions and 105 deletions

View file

@ -59,17 +59,17 @@ namespace MinecraftClient
case "black": return "§0"; case "black": return "§0";
case "dark_blue": return "§1"; case "dark_blue": return "§1";
case "dark_green": return "§2"; case "dark_green": return "§2";
case "dark_cyan": return "§3"; case "dark_aqua": return "§3";
case "dark_cyanred": return "§4"; case "dark_red": return "§4";
case "dark_magenta": return "§5"; case "dark_purple": return "§5";
case "dark_yellow": return "§6"; case "gold": return "§6";
case "gray": return "§7"; case "gray": return "§7";
case "dark_gray": return "§8"; case "dark_gray": return "§8";
case "blue": return "§9"; case "blue": return "§9";
case "green": return "§a"; case "green": return "§a";
case "cyan": return "§b"; case "aqua": return "§b";
case "red": return "§c"; case "red": return "§c";
case "magenta": return "§d"; case "light_purple": return "§d";
case "yellow": return "§e"; case "yellow": return "§e";
case "white": return "§f"; case "white": return "§f";
default: return ""; default: return "";
@ -294,7 +294,7 @@ namespace MinecraftClient
{ {
JSONData[] extras = data.Properties["extra"].DataArray.ToArray(); JSONData[] extras = data.Properties["extra"].DataArray.ToArray();
foreach (JSONData item in extras) foreach (JSONData item in extras)
extra_result += JSONData2String(item); extra_result = extra_result + JSONData2String(item) + "§r";
} }
if (data.Properties.ContainsKey("color")) if (data.Properties.ContainsKey("color"))
{ {

View file

@ -34,9 +34,9 @@ namespace MinecraftClient
/// <param name="sessionID">A valid sessionID obtained with MinecraftCom.GetLogin()</param> /// <param name="sessionID">A valid sessionID obtained with MinecraftCom.GetLogin()</param>
/// <param name="server_port">The server IP (serveradress or serveradress:port)</param> /// <param name="server_port">The server IP (serveradress or serveradress:port)</param>
public McTcpClient(string username, string sessionID, string server_port, MinecraftCom handler) public McTcpClient(string username, string uuid, string sessionID, string server_port, MinecraftCom handler)
{ {
StartClient(username, sessionID, server_port, false, handler, ""); StartClient(username, uuid, sessionID, server_port, false, handler, "");
} }
/// <summary> /// <summary>
@ -47,9 +47,9 @@ namespace MinecraftClient
/// <param name="server_port">The server IP (serveradress or serveradress:port)</param> /// <param name="server_port">The server IP (serveradress or serveradress:port)</param>
/// <param name="command">The text or command to send.</param> /// <param name="command">The text or command to send.</param>
public McTcpClient(string username, string sessionID, string server_port, MinecraftCom handler, string command) public McTcpClient(string username, string uuid, string sessionID, string server_port, MinecraftCom handler, string command)
{ {
StartClient(username, sessionID, server_port, true, handler, command); StartClient(username, uuid, sessionID, server_port, true, handler, command);
} }
/// <summary> /// <summary>
@ -61,7 +61,7 @@ namespace MinecraftClient
/// <param name="singlecommand">If set to true, the client will send a single command and then disconnect from the server</param> /// <param name="singlecommand">If set to true, the client will send a single command and then disconnect from the server</param>
/// <param name="command">The text or command to send. Will only be sent if singlecommand is set to true.</param> /// <param name="command">The text or command to send. Will only be sent if singlecommand is set to true.</param>
private void StartClient(string user, string sessionID, string server_port, bool singlecommand, MinecraftCom handler, string command) private void StartClient(string user, string uuid, string sessionID, string server_port, bool singlecommand, MinecraftCom handler, string command)
{ {
this.handler = handler; this.handler = handler;
username = user; username = user;
@ -86,7 +86,7 @@ namespace MinecraftClient
client = new TcpClient(host, port); client = new TcpClient(host, port);
client.ReceiveBufferSize = 1024 * 1024; client.ReceiveBufferSize = 1024 * 1024;
handler.setClient(client); handler.setClient(client);
if (handler.Login(user, sessionID, host, port)) if (handler.Login(user, uuid, sessionID, host, port))
{ {
//Single command sending //Single command sending
if (singlecommand) if (singlecommand)

View file

@ -13,7 +13,7 @@ namespace MinecraftClient
public class MinecraftCom : IAutoComplete public class MinecraftCom : IAutoComplete
{ {
#region Login to Minecraft.net, Obtaining a session ID #region Login to Minecraft.net and get a new session ID
public enum LoginResult { Error, Success, WrongPassword, Blocked, AccountMigrated, NotPremium }; public enum LoginResult { Error, Success, WrongPassword, Blocked, AccountMigrated, NotPremium };
@ -77,35 +77,23 @@ namespace MinecraftClient
#region Session checking when joining a server in online mode #region Session checking when joining a server in online mode
/// <summary> /// <summary>
/// This method allows to join an online-mode server. /// Check session using the Yggdrasil authentication scheme. Allow to join an online-mode server
/// It Should be called between the handshake and the login attempt.
/// </summary> /// </summary>
/// <param name="user">Username</param> /// <param name="user">Username</param>
/// <param name="sessionID">A valid session ID for this username</param> /// <param name="accesstoken">Session ID</param>
/// <param name="hash">Hash returned by the server during the handshake</param> /// <param name="serverhash">Server ID</param>
/// <returns>Returns true if the check was successful</returns> /// <returns>TRUE if session was successfully checked</returns>
public static bool SessionCheck(string user, string sessionID, string hash) public static bool SessionCheck(string uuid, string accesstoken, string serverhash)
{ {
Console.ForegroundColor = ConsoleColor.DarkGray;
WebClient client = new WebClient();
Console.Write("http://session.minecraft.net/game/joinserver.jsp?user=" + user + "&sessionId=" + sessionID + "&serverId=" + hash + " ... ");
string result;
try try
{ {
result = client.DownloadString("http://session.minecraft.net/game/joinserver.jsp?user=" + user + "&sessionId=" + sessionID + "&serverId=" + hash); WebClient wClient = new WebClient();
wClient.Headers.Add("Content-Type: application/json");
string json_request = "{\"accessToken\":\"" + accesstoken + "\",\"selectedProfile\":\"" + uuid + "\",\"serverId\":\"" + serverhash + "\"}";
return (wClient.UploadString("https://sessionserver.mojang.com/session/minecraft/join", json_request) == "");
} }
catch (WebException e) catch (WebException) { return false; }
{
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine();
Console.WriteLine("Error while connecting to session server: " + e.Message);
Console.ForegroundColor = ConsoleColor.DarkGray;
return false;
}
Console.WriteLine(result);
Console.ForegroundColor = ConsoleColor.Gray;
return (result == "OK");
} }
#endregion #endregion
@ -166,7 +154,7 @@ namespace MinecraftClient
autocomplete_received = true; autocomplete_received = true;
tab_list = tab_list.Trim(); tab_list = tab_list.Trim();
if (tab_list.Length > 0) if (tab_list.Length > 0)
ConsoleIO.WriteLine(tab_list); printstring("§8" + tab_list, false);
break; break;
case 0x40: string reason = readNextString(); case 0x40: string reason = readNextString();
ConsoleIO.WriteLine("Disconnected by Server :"); ConsoleIO.WriteLine("Disconnected by Server :");
@ -447,7 +435,7 @@ namespace MinecraftClient
return false; return false;
} }
} }
public bool Login(string username, string sessionID, string host, int port) public bool Login(string username, string uuid, string sessionID, string host, int port)
{ {
byte[] packet_id = getVarInt(0); byte[] packet_id = getVarInt(0);
byte[] protocol_version = getVarInt(4); byte[] protocol_version = getVarInt(4);
@ -470,13 +458,14 @@ namespace MinecraftClient
int size = readNextVarInt(); //Packet size int size = readNextVarInt(); //Packet size
int pid = readNextVarInt(); //Packet ID int pid = readNextVarInt(); //Packet ID
size -= getVarInt(pid).Length; size -= getVarInt(pid).Length;
/*
while (pid == 0x3F) //Skip some early plugin messages while (pid == 0x3F) //Skip some early plugin messages
{ {
readData(size); readData(size);
size = readNextVarInt(); size = readNextVarInt();
pid = readNextVarInt(); pid = readNextVarInt();
size -= getVarInt(pid).Length; size -= getVarInt(pid).Length;
} }*/
if (pid == 0x00) //Login rejected if (pid == 0x00) //Login rejected
{ {
Console.WriteLine("Login rejected by Server :"); Console.WriteLine("Login rejected by Server :");
@ -490,10 +479,7 @@ namespace MinecraftClient
byte[] token = readNextByteArray(); byte[] token = readNextByteArray();
var PublicServerkey = Crypto.GenerateRSAPublicKey(Serverkey_RAW); var PublicServerkey = Crypto.GenerateRSAPublicKey(Serverkey_RAW);
var SecretKey = Crypto.GenerateAESPrivateKey(); var SecretKey = Crypto.GenerateAESPrivateKey();
Console.ForegroundColor = ConsoleColor.DarkGray; return StartEncryption(uuid, sessionID, token, serverID, PublicServerkey, SecretKey);
Console.WriteLine("Handshake successful. (Server ID: " + serverID + ')');
Console.ForegroundColor = ConsoleColor.Gray;
return StartEncryption(username, sessionID, token, serverID, PublicServerkey, SecretKey);
} }
else if (pid == 0x02) //Login successfull else if (pid == 0x02) //Login successfull
{ {
@ -504,7 +490,7 @@ namespace MinecraftClient
} }
else return false; else return false;
} }
public bool StartEncryption(string username, string sessionID, byte[] token, string serverIDhash, java.security.PublicKey serverKey, javax.crypto.SecretKey secretKey) public bool StartEncryption(string uuid, string sessionID, byte[] token, string serverIDhash, java.security.PublicKey serverKey, javax.crypto.SecretKey secretKey)
{ {
Console.ForegroundColor = ConsoleColor.DarkGray; Console.ForegroundColor = ConsoleColor.DarkGray;
ConsoleIO.WriteLine("Crypto keys & hash generated."); ConsoleIO.WriteLine("Crypto keys & hash generated.");
@ -513,7 +499,7 @@ namespace MinecraftClient
if (serverIDhash != "-") if (serverIDhash != "-")
{ {
Console.WriteLine("Checking Session..."); Console.WriteLine("Checking Session...");
if (!SessionCheck(username, sessionID, new java.math.BigInteger(Crypto.getServerHash(serverIDhash, serverKey, secretKey)).toString(16))) if (!SessionCheck(uuid, sessionID, new java.math.BigInteger(Crypto.getServerHash(serverIDhash, serverKey, secretKey)).toString(16)))
{ {
return false; return false;
} }
@ -531,61 +517,13 @@ namespace MinecraftClient
byte[] encryption_response_tosend = concatBytes(getVarInt(encryption_response.Length), encryption_response); byte[] encryption_response_tosend = concatBytes(getVarInt(encryption_response.Length), encryption_response);
Send(encryption_response_tosend); Send(encryption_response_tosend);
//Get the next packet //Start client-side encryption
int size = readNextVarInt(); //Packet size
int pid = readNextVarInt(); //Packet ID
if (pid == 0x02)
{
setEncryptedClient(Crypto.SwitchToAesMode(c.GetStream(), secretKey)); setEncryptedClient(Crypto.SwitchToAesMode(c.GetStream(), secretKey));
return true;
//Get the next packet
readNextVarInt(); //Skip Packet size (not needed)
return (readNextVarInt() == 0x02); //Packet ID. 0x02 = Login Success
} }
else return false;
}
/*
public bool FinalizeLogin()
{
Send(new byte[] { 0xCD, 0 });
try
{
byte[] pid = new byte[1];
try
{
if (c.Connected)
{
Receive(pid, 0, 1, SocketFlags.None);
while (pid[0] >= 0xC0 && pid[0] != 0xFF) //Skip some early packets or plugin messages
{
processPacket(pid[0]);
Receive(pid, 0, 1, SocketFlags.None);
}
if (pid[0] == (byte)1)
{
readData(4); readNextString(); readData(5);
return true; //The Server accepted the request
}
else if (pid[0] == (byte)0xFF)
{
string reason = readNextString();
Console.WriteLine("Login rejected by Server :"); printstring(reason, true);
for (int i = 0; i < bots.Count; i++) { bots[i].OnDisconnect(ChatBot.DisconnectReason.LoginRejected, reason); }
return false;
}
}
}
catch
{
//Connection failed
return false;
}
}
catch
{
//Network error
Console.WriteLine("Connection Lost.");
return false;
}
return false; //Login was unsuccessful (received a kick...)
}*/
public bool SendChatMessage(string message) public bool SendChatMessage(string message)
{ {

View file

@ -15,7 +15,7 @@ namespace MinecraftClient
{ {
private static McTcpClient Client; private static McTcpClient Client;
public static string[] startupargs; public static string[] startupargs;
public const string Version = "1.6.0"; public const string Version = "1.7.0";
/// <summary> /// <summary>
/// The main entry point of Minecraft Console Client /// The main entry point of Minecraft Console Client
@ -179,8 +179,9 @@ namespace MinecraftClient
{ {
MinecraftCom.LoginResult result; MinecraftCom.LoginResult result;
string username = Settings.Login; Settings.Username = Settings.Login;
string sessionID = ""; string sessionID = "";
string UUID = "";
if (Settings.Password == "-") if (Settings.Password == "-")
{ {
@ -193,9 +194,8 @@ namespace MinecraftClient
else else
{ {
Console.WriteLine("Connecting to Minecraft.net..."); Console.WriteLine("Connecting to Minecraft.net...");
result = MinecraftCom.GetLogin(ref username, Settings.Password, ref sessionID, ref Settings.UUID); result = MinecraftCom.GetLogin(ref Settings.Username, Settings.Password, ref sessionID, ref UUID);
} }
Settings.Username = username;
if (result == MinecraftCom.LoginResult.Success) if (result == MinecraftCom.LoginResult.Success)
{ {
Console.WriteLine("Success. (session ID: " + sessionID + ')'); Console.WriteLine("Success. (session ID: " + sessionID + ')');
@ -236,9 +236,9 @@ namespace MinecraftClient
//Start the main TCP client //Start the main TCP client
if (Settings.SingleCommand != "") if (Settings.SingleCommand != "")
{ {
Client = new McTcpClient(Settings.Username, sessionID, Settings.ServerIP, handler, Settings.SingleCommand); Client = new McTcpClient(Settings.Username, UUID, sessionID, Settings.ServerIP, handler, Settings.SingleCommand);
} }
else Client = new McTcpClient(Settings.Username, sessionID, Settings.ServerIP, handler); else Client = new McTcpClient(Settings.Username, UUID, sessionID, Settings.ServerIP, handler);
} }
else else
{ {

View file

@ -19,7 +19,6 @@ namespace MinecraftClient
public static string Login = ""; public static string Login = "";
public static string Username = ""; public static string Username = "";
public static string Password = ""; public static string Password = "";
public static string UUID = "";
public static string ServerIP = ""; public static string ServerIP = "";
public static string SingleCommand = ""; public static string SingleCommand = "";