/// <summary> /// Starts the main chat client, wich will login to the server using the MinecraftCom class. /// </summary> /// <param name="user">The chosen username of a premium Minecraft Account</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="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> private void StartClient(string user, string sessionID, string server_port, bool singlecommand, MinecraftCom handler, string command) { this.handler = handler; username = user; string[] sip = server_port.Split(':'); host = sip[0]; if (sip.Length == 1) { port = 25565; } else { try { port = Convert.ToInt32(sip[1]); } catch (FormatException) { port = 25565; } } try { Console.WriteLine("Connecting..."); client = new TcpClient(host, port); client.ReceiveBufferSize = 1024 * 1024; handler.setClient(client); byte[] token = new byte[1]; string serverID = ""; if (handler.Handshake(user, sessionID, ref serverID, ref token, host, port)) { Console.WriteLine("Logging in..."); if (handler.FinalizeLogin()) { //Single command sending if (singlecommand) { handler.SendChatMessage(command); Console.Write("Command "); Console.ForegroundColor = ConsoleColor.DarkGray; Console.Write(command); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(" sent."); Thread.Sleep(5000); handler.Disconnect("disconnect.quitting"); Thread.Sleep(1000); } else { Console.WriteLine("Server was successfuly joined.\nType '/quit' to leave the server."); //Command sending thread, allowing user input t_sender = new Thread(new ThreadStart(StartTalk)); t_sender.Name = "CommandSender"; t_sender.Start(); //Data receiving thread, allowing text receiving t_updater = new Thread(new ThreadStart(Updater)); t_updater.Name = "PacketHandler"; t_updater.Start(); } } else { Console.WriteLine("Login failed."); if (!singlecommand) { Program.ReadLineReconnect(); } } } else { Console.WriteLine("Invalid session ID."); if (!singlecommand) { Program.ReadLineReconnect(); } } } catch (SocketException) { Console.WriteLine("Failed to connect to this IP."); if (AttemptsLeft > 0) { ChatBot.LogToConsole("Waiting 5 seconds (" + AttemptsLeft + " attempts left)..."); Thread.Sleep(5000); AttemptsLeft--; Program.Restart(); } else if (!singlecommand) { Console.ReadLine(); } } }
/// <summary> /// Starts the main chat client, wich will login to the server using the MinecraftCom class. /// </summary> /// <param name="username">The chosen username of a premium Minecraft Account</param> /// <param name="sessionID">A valid sessionID obtained with MinecraftCom.GetLogin()</param> /// <param name="server_port">The server IP (serveradress or serveradress:port)</param> public McTcpClient(string username, string sessionID, string server_port, MinecraftCom handler) { StartClient(username, sessionID, server_port, false, handler, ""); }
/// <summary> /// Starts the main chat client in single command sending mode, wich will login to the server using the MinecraftCom class, send the command and close. /// </summary> /// <param name="username">The chosen username of a premium Minecraft Account</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="command">The text or command to send.</param> public McTcpClient(string username, string sessionID, string server_port, MinecraftCom handler, string command) { StartClient(username, sessionID, server_port, true, handler, command); }
public static bool GetServerInfo(string serverIP, ref byte protocolversion, ref string version) { try { string host; int port; string[] sip = serverIP.Split(':'); host = sip[0]; if (sip.Length == 1) { port = 25565; } else { try { port = Convert.ToInt32(sip[1]); } catch (FormatException) { port = 25565; } } TcpClient tcp = new TcpClient(host, port); byte[] ping = new byte[2] { 0xfe, 0x01 }; tcp.Client.Send(ping, SocketFlags.None); tcp.Client.Receive(ping, 0, 1, SocketFlags.None); if (ping[0] == 0xff) { MinecraftCom ComTmp = new MinecraftCom(); ComTmp.setClient(tcp); string result = ComTmp.readNextString(); Console.ForegroundColor = ConsoleColor.DarkGray; //Console.WriteLine(result.Replace((char)0x00, ' ')); if (result.Length > 2 && result[0] == '§' && result[1] == '1') { string[] tmp = result.Split((char)0x00); protocolversion = (byte)Int16.Parse(tmp[1]); version = tmp[2]; } else { protocolversion = (byte)39; version = "B1.8.1 - 1.3.2"; } Console.WriteLine("Server version : MC " + version + " (protocol v" + protocolversion + ")."); Console.ForegroundColor = ConsoleColor.Gray; return true; } else { Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine("Unexpected answer from the server (is that a Minecraft server ?)"); Console.ForegroundColor = ConsoleColor.Gray; return false; } } catch { Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine("An error occured while attempting to connect to this IP."); Console.ForegroundColor = ConsoleColor.Gray; return false; } }
/// <summary> /// Start a new Client /// </summary> private static void InitializeClient() { MinecraftCom.LoginResult result; string logindata = ""; if (Settings.Password == "-") { Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine("You chose to run in offline mode."); Console.ForegroundColor = ConsoleColor.Gray; result = MinecraftCom.LoginResult.Success; logindata = "0:deprecated:" + Settings.Login + ":0"; } else { Console.WriteLine("Connecting to Minecraft.net..."); result = MinecraftCom.GetLogin(Settings.Login, Settings.Password, ref logindata); } if (result == MinecraftCom.LoginResult.Success) { Settings.Username = logindata.Split(':')[2]; string sessionID = logindata.Split(':')[3]; Console.WriteLine("Success. (session ID: " + sessionID + ')'); if (Settings.ServerIP == "") { Console.Write("Server IP : "); Settings.ServerIP = Console.ReadLine(); } //Get server version Console.WriteLine("Retrieving Server Info..."); byte protocolversion = 0; string version = ""; if (MinecraftCom.GetServerInfo(Settings.ServerIP, ref protocolversion, ref version)) { //Supported protocol version ? int[] supportedVersions = { 51, 60, 61, 72, 73, 74 }; if (Array.IndexOf(supportedVersions, protocolversion) > -1) { //Minecraft 1.6+ ? Load translations if (protocolversion >= 72) { ChatParser.InitTranslations(); } //Will handle the connection for this client Console.WriteLine("Version is supported."); MinecraftCom handler = new MinecraftCom(); ConsoleIO.SetAutoCompleteEngine(handler); handler.setVersion(protocolversion); //Load & initialize bots if needed if (Settings.AntiAFK_Enabled) { handler.BotLoad(new Bots.AntiAFK(Settings.AntiAFK_Delay)); } if (Settings.Hangman_Enabled) { handler.BotLoad(new Bots.Pendu(Settings.Hangman_English)); } if (Settings.Alerts_Enabled) { handler.BotLoad(new Bots.Alerts()); } if (Settings.ChatLog_Enabled) { handler.BotLoad(new Bots.ChatLog(Settings.ChatLog_File, Settings.ChatLog_Filter, Settings.ChatLog_DateTime)); } if (Settings.PlayerLog_Enabled) { handler.BotLoad(new Bots.PlayerListLogger(Settings.PlayerLog_Delay, Settings.PlayerLog_File)); } if (Settings.AutoRelog_Enabled) { handler.BotLoad(new Bots.AutoRelog(Settings.AutoRelog_Delay, Settings.AutoRelog_Retries)); } if (Settings.xAuth_Enabled) { handler.BotLoad(new Bots.xAuth(Settings.xAuth_Password)); } if (Settings.Scripting_Enabled) { handler.BotLoad(new Bots.Scripting(Settings.Scripting_ScriptFile)); } //Start the main TCP client if (Settings.SingleCommand != "") { Client = new McTcpClient(Settings.Username, sessionID, Settings.ServerIP, handler, Settings.SingleCommand); } else Client = new McTcpClient(Settings.Username, sessionID, Settings.ServerIP, handler); } else { Console.WriteLine("Cannot connect to the server : This version is not supported !"); ReadLineReconnect(); } } else { Console.WriteLine("Failed to ping this IP."); ReadLineReconnect(); } } else { Console.ForegroundColor = ConsoleColor.Gray; Console.Write("Connection failed : "); switch (result) { case MinecraftCom.LoginResult.AccountMigrated: Console.WriteLine("Account migrated, use e-mail as username."); break; case MinecraftCom.LoginResult.Blocked: Console.WriteLine("Too many failed logins. Please try again later."); break; case MinecraftCom.LoginResult.BadRequest: Console.WriteLine("Login attempt rejected: Bad request."); break; case MinecraftCom.LoginResult.WrongPassword: Console.WriteLine("Incorrect password."); break; case MinecraftCom.LoginResult.NotPremium: Console.WriteLine("User not premium."); break; case MinecraftCom.LoginResult.Error: Console.WriteLine("Network error."); break; } while (Console.KeyAvailable) { Console.ReadKey(false); } if (Settings.SingleCommand == "") { ReadLineReconnect(); } } }
//Will be automatically set on bot loading, don't worry about this public void SetHandler(MinecraftCom handler) { this.handler = handler; }
/// <summary> /// Start a new Client /// </summary> private static void InitializeClient() { MinecraftLoginResult result = MinecraftLoginResult.OtherError; Settings.Username = Settings.Login; string sessionID = ""; string UUID = ""; if (Settings.Password == "-") { Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine("You chose to run in offline mode."); Console.ForegroundColor = ConsoleColor.Gray; result = MinecraftLoginResult.Success; sessionID = "0"; } else { Console.WriteLine("Connecting to Minecraft.net..."); try { result = MinecraftCom.GetLogin(ref Settings.Username, Settings.Password, ref sessionID, ref UUID); } catch(MinecraftAuthException ex) { DisplayError (ex); } } if (result == MinecraftLoginResult.Success) { if (Settings.ConsoleTitle != "") { Console.Title = Settings.ConsoleTitle.Replace("%username%", Settings.Username); } Console.WriteLine("Success. (session ID: " + sessionID + ')'); if (Settings.ServerIP == "") { Console.Write("Server IP : "); Settings.ServerIP = Console.ReadLine(); } //Get server version Console.WriteLine("Retrieving Server Info..."); int protocolversion = 0; string version = ""; if (MinecraftCom.GetServerInfo(Settings.ServerIP, ref protocolversion, ref version)) { //Supported protocol version ? int[] supportedVersions = { 4, 5 }; if (Array.IndexOf(supportedVersions, protocolversion) > -1) { //Load translations (Minecraft 1.6+) ChatParser.InitTranslations(); //Will handle the connection for this client Console.WriteLine("Version is supported."); MinecraftCom handler = new MinecraftCom(); ConsoleIO.SetAutoCompleteEngine(handler); handler.setVersion(protocolversion); //Load & initialize bots if needed if (Settings.AntiAFK_Enabled) { handler.BotLoad(new Bots.AntiAFK(Settings.AntiAFK_Delay)); } if (Settings.Hangman_Enabled) { handler.BotLoad(new Bots.Pendu(Settings.Hangman_English)); } if (Settings.Alerts_Enabled) { handler.BotLoad(new Bots.Alerts()); } if (Settings.ChatLog_Enabled) { handler.BotLoad(new Bots.ChatLog(Settings.ChatLog_File.Replace("%username%", Settings.Username), Settings.ChatLog_Filter, Settings.ChatLog_DateTime)); } if (Settings.PlayerLog_Enabled) { handler.BotLoad(new Bots.PlayerListLogger(Settings.PlayerLog_Delay, Settings.PlayerLog_File.Replace("%username%", Settings.Username))); } if (Settings.AutoRelog_Enabled) { handler.BotLoad(new Bots.AutoRelog(Settings.AutoRelog_Delay, Settings.AutoRelog_Retries)); } if (Settings.ScriptScheduler_Enabled) { handler.BotLoad(new Bots.ScriptScheduler(Settings.ScriptScheduler_TasksFile.Replace("%username%", Settings.Username))); } if (Settings.RemoteCtrl_Enabled) { handler.BotLoad(new Bots.RemoteControl()); } //Start the main TCP client if (Settings.SingleCommand != "") { Client = new McTcpClient(Settings.Username, UUID, sessionID, Settings.ServerIP, handler, Settings.SingleCommand); } else Client = new McTcpClient(Settings.Username, UUID, sessionID, Settings.ServerIP, handler); } else { Console.WriteLine("Cannot connect to the server : This version is not supported !"); ReadLineReconnect(); } } else { Console.WriteLine("Failed to ping this IP."); ReadLineReconnect(); } } else { Console.ForegroundColor = ConsoleColor.Gray; Console.Write("Connection failed : "); switch (result) { case MinecraftLoginResult.AccountMigrated: Console.WriteLine("Account migrated, use e-mail as username."); break; case MinecraftLoginResult.Blocked: Console.WriteLine("Too many failed logins. Please try again later."); break; case MinecraftLoginResult.ServiceUnavailable: Console.WriteLine("Login servers are unavailable. Please try again later."); break; case MinecraftLoginResult.WrongPassword: Console.WriteLine("Incorrect password."); break; case MinecraftLoginResult.NotPremium: Console.WriteLine("User not premium."); break; case MinecraftLoginResult.OtherError: Console.WriteLine("Network error."); break; case MinecraftLoginResult.SSLError: Console.WriteLine("SSL Error."); if (isUsingMono) { Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine("It 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"); Console.ForegroundColor = ConsoleColor.Gray; return; } break; } while (Console.KeyAvailable) { Console.ReadKey(false); } if (Settings.SingleCommand == "") { ReadLineReconnect(); } } }
public static bool GetServerInfo(string serverIP, ref int protocolversion, ref string version) { try { string host; int port; string[] sip = serverIP.Split(':'); host = sip[0]; if (sip.Length == 1) { port = 25565; } else { try { port = Convert.ToInt32(sip[1]); } catch (FormatException) { port = 25565; } } TcpClient tcp = new TcpClient(host, port); byte[] packet_id = getVarInt(0); byte[] protocol_version = getVarInt(4); byte[] server_adress_val = Encoding.UTF8.GetBytes(host); byte[] server_adress_len = getVarInt(server_adress_val.Length); byte[] server_port = BitConverter.GetBytes((ushort)port); Array.Reverse(server_port); byte[] next_state = getVarInt(1); byte[] packet = concatBytes(packet_id, protocol_version, server_adress_len, server_adress_val, server_port, next_state); byte[] tosend = concatBytes(getVarInt(packet.Length), packet); tcp.Client.Send(tosend, SocketFlags.None); byte[] status_request = getVarInt(0); byte[] request_packet = concatBytes(getVarInt(status_request.Length), status_request); tcp.Client.Send(request_packet, SocketFlags.None); MinecraftCom ComTmp = new MinecraftCom(); ComTmp.setClient(tcp); if (ComTmp.readNextVarInt() > 0) //Read Response length { if (ComTmp.readNextVarInt() == 0x00) //Read Packet ID { string result = ComTmp.readNextString(); //Get the Json data if (result[0] == '{' && result.Contains("protocol\":") && result.Contains("name\":\"")) { string[] tmp_ver = result.Split(new string[] { "protocol\":" }, StringSplitOptions.None); string[] tmp_name = result.Split(new string[] { "name\":\"" }, StringSplitOptions.None); if (tmp_ver.Length >= 2 && tmp_name.Length >= 2) { protocolversion = atoi(tmp_ver[1]); version = tmp_name[1].Split('"')[0]; Console.ForegroundColor = ConsoleColor.DarkGray; //Console.WriteLine(result); //Debug: show the full Json string Console.WriteLine("Server version : " + version + " (protocol v" + protocolversion + ")."); Console.ForegroundColor = ConsoleColor.Gray; return true; } } } } Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine("Unexpected answer from the server (is that a MC 1.7+ server ?)"); Console.ForegroundColor = ConsoleColor.Gray; return false; } catch { Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine("An error occured while attempting to connect to this IP."); Console.ForegroundColor = ConsoleColor.Gray; return false; } }