/// <summary> /// Do the Minecraft login. /// </summary> /// <returns>True if login successful</returns> public bool Login() { byte[] protocol_version = dataTypes.GetVarInt(protocolversion); string server_address = pForge.GetServerAddress(handler.GetServerHost()); byte[] server_port = BitConverter.GetBytes((ushort)handler.GetServerPort()); Array.Reverse(server_port); byte[] next_state = dataTypes.GetVarInt(2); byte[] handshake_packet = dataTypes.ConcatBytes(protocol_version, dataTypes.GetString(server_address), server_port, next_state); SendPacket(0x00, handshake_packet); byte[] login_packet = dataTypes.GetString(handler.GetUsername()); SendPacket(0x00, login_packet); int packetID = -1; List <byte> packetData = new List <byte>(); while (true) { ReadNextPacket(ref packetID, packetData); if (packetID == 0x00) //Login rejected { handler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, ChatParser.ParseText(dataTypes.ReadNextString(packetData))); return(false); } else if (packetID == 0x01) //Encryption request { string serverID = dataTypes.ReadNextString(packetData); byte[] Serverkey = dataTypes.ReadNextByteArray(packetData); byte[] token = dataTypes.ReadNextByteArray(packetData); return(StartEncryption(handler.GetUserUUID(), handler.GetSessionID(), token, serverID, Serverkey)); } else if (packetID == 0x02) //Login successful { ConsoleIO.WriteLineFormatted("§8Server is in offline mode."); login_phase = false; if (!pForge.CompleteForgeHandshake()) { return(false); } StartUpdating(); return(true); //No need to check session or start encryption } else { HandlePacket(packetID, packetData); } } }
/// <summary> /// Handle Forge plugin messages /// </summary> /// <param name="channel">Plugin message channel</param> /// <param name="packetData">Plugin message data</param> /// <param name="currentDimension">Current world dimension</param> /// <returns>TRUE if the plugin message was recognized and handled</returns> public bool HandlePluginMessage(string channel, List <byte> packetData, ref int currentDimension) { if (ForgeEnabled() && fmlHandshakeState != FMLHandshakeClientState.DONE) { if (channel == "FML|HS") { FMLHandshakeDiscriminator discriminator = (FMLHandshakeDiscriminator)dataTypes.ReadNextByte(packetData); if (discriminator == FMLHandshakeDiscriminator.HandshakeReset) { fmlHandshakeState = FMLHandshakeClientState.START; return(true); } switch (fmlHandshakeState) { case FMLHandshakeClientState.START: if (discriminator != FMLHandshakeDiscriminator.ServerHello) { return(false); } // Send the plugin channel registration. // REGISTER is somewhat special in that it doesn't actually include length information, // and is also \0-separated. // Also, yes, "FML" is there twice. Don't ask me why, but that's the way forge does it. string[] channels = { "FML|HS", "FML", "FML|MP", "FML", "FORGE" }; protocol18.SendPluginChannelPacket("REGISTER", Encoding.UTF8.GetBytes(string.Join("\0", channels))); byte fmlProtocolVersion = dataTypes.ReadNextByte(packetData); if (Settings.DebugMessages) { ConsoleIO.WriteLineFormatted("§8Forge protocol version : " + fmlProtocolVersion); } if (fmlProtocolVersion >= 1) { currentDimension = dataTypes.ReadNextInt(packetData); } // Tell the server we're running the same version. SendForgeHandshakePacket(FMLHandshakeDiscriminator.ClientHello, new byte[] { fmlProtocolVersion }); // Then tell the server that we're running the same mods. if (Settings.DebugMessages) { ConsoleIO.WriteLineFormatted("§8Sending falsified mod list to server..."); } byte[][] mods = new byte[forgeInfo.Mods.Count][]; for (int i = 0; i < forgeInfo.Mods.Count; i++) { ForgeInfo.ForgeMod mod = forgeInfo.Mods[i]; mods[i] = dataTypes.ConcatBytes(dataTypes.GetString(mod.ModID), dataTypes.GetString(mod.Version)); } SendForgeHandshakePacket(FMLHandshakeDiscriminator.ModList, dataTypes.ConcatBytes(dataTypes.GetVarInt(forgeInfo.Mods.Count), dataTypes.ConcatBytes(mods))); fmlHandshakeState = FMLHandshakeClientState.WAITINGSERVERDATA; return(true); case FMLHandshakeClientState.WAITINGSERVERDATA: if (discriminator != FMLHandshakeDiscriminator.ModList) { return(false); } Thread.Sleep(2000); if (Settings.DebugMessages) { ConsoleIO.WriteLineFormatted("§8Accepting server mod list..."); } // Tell the server that yes, we are OK with the mods it has // even though we don't actually care what mods it has. SendForgeHandshakePacket(FMLHandshakeDiscriminator.HandshakeAck, new byte[] { (byte)FMLHandshakeClientState.WAITINGSERVERDATA }); fmlHandshakeState = FMLHandshakeClientState.WAITINGSERVERCOMPLETE; return(false); case FMLHandshakeClientState.WAITINGSERVERCOMPLETE: // The server now will tell us a bunch of registry information. // We need to read it all, though, until it says that there is no more. if (discriminator != FMLHandshakeDiscriminator.RegistryData) { return(false); } if (protocolversion < Protocol18Handler.MC18Version) { // 1.7.10 and below have one registry // with blocks and items. int registrySize = dataTypes.ReadNextVarInt(packetData); if (Settings.DebugMessages) { ConsoleIO.WriteLineFormatted("§8Received registry with " + registrySize + " entries"); } fmlHandshakeState = FMLHandshakeClientState.PENDINGCOMPLETE; } else { // 1.8+ has more than one registry. bool hasNextRegistry = dataTypes.ReadNextBool(packetData); string registryName = dataTypes.ReadNextString(packetData); int registrySize = dataTypes.ReadNextVarInt(packetData); if (Settings.DebugMessages) { ConsoleIO.WriteLineFormatted("§8Received registry " + registryName + " with " + registrySize + " entries"); } if (!hasNextRegistry) { fmlHandshakeState = FMLHandshakeClientState.PENDINGCOMPLETE; } } return(false); case FMLHandshakeClientState.PENDINGCOMPLETE: // The server will ask us to accept the registries. // Just say yes. if (discriminator != FMLHandshakeDiscriminator.HandshakeAck) { return(false); } if (Settings.DebugMessages) { ConsoleIO.WriteLineFormatted("§8Accepting server registries..."); } SendForgeHandshakePacket(FMLHandshakeDiscriminator.HandshakeAck, new byte[] { (byte)FMLHandshakeClientState.PENDINGCOMPLETE }); fmlHandshakeState = FMLHandshakeClientState.COMPLETE; return(true); case FMLHandshakeClientState.COMPLETE: // One final "OK". On the actual forge source, a packet is sent from // the client to the client saying that the connection was complete, but // we don't need to do that. SendForgeHandshakePacket(FMLHandshakeDiscriminator.HandshakeAck, new byte[] { (byte)FMLHandshakeClientState.COMPLETE }); if (Settings.DebugMessages) { ConsoleIO.WriteLine("Forge server connection complete!"); } fmlHandshakeState = FMLHandshakeClientState.DONE; return(true); } } } return(false); }
/// <summary> /// Ping a Minecraft server to get information about the server /// </summary> /// <returns>True if ping was successful</returns> public static bool doPing(string host, int port, ref int protocolversion, ref ForgeInfo forgeInfo) { string version = ""; TcpClient tcp = ProxyHandler.newTcpClient(host, port); tcp.ReceiveBufferSize = 1024 * 1024; SocketWrapper socketWrapper = new SocketWrapper(tcp); DataTypes dataTypes = new DataTypes(MC18Version); byte[] packet_id = dataTypes.GetVarInt(0); byte[] protocol_version = dataTypes.GetVarInt(-1); byte[] server_port = BitConverter.GetBytes((ushort)port); Array.Reverse(server_port); byte[] next_state = dataTypes.GetVarInt(1); byte[] packet = dataTypes.ConcatBytes(packet_id, protocol_version, dataTypes.GetString(host), server_port, next_state); byte[] tosend = dataTypes.ConcatBytes(dataTypes.GetVarInt(packet.Length), packet); socketWrapper.SendDataRAW(tosend); byte[] status_request = dataTypes.GetVarInt(0); byte[] request_packet = dataTypes.ConcatBytes(dataTypes.GetVarInt(status_request.Length), status_request); socketWrapper.SendDataRAW(request_packet); int packetLength = dataTypes.ReadNextVarIntRAW(socketWrapper); if (packetLength > 0) //Read Response length { List <byte> packetData = new List <byte>(socketWrapper.ReadDataRAW(packetLength)); if (dataTypes.ReadNextVarInt(packetData) == 0x00) //Read Packet ID { string result = dataTypes.ReadNextString(packetData); //Get the Json data if (!String.IsNullOrEmpty(result) && result.StartsWith("{") && result.EndsWith("}")) { Json.JSONData jsonData = Json.ParseJson(result); if (jsonData.Type == Json.JSONData.DataType.Object && jsonData.Properties.ContainsKey("version")) { Json.JSONData versionData = jsonData.Properties["version"]; //Retrieve display name of the Minecraft version if (versionData.Properties.ContainsKey("name")) { version = versionData.Properties["name"].StringValue; } //Retrieve protocol version number for handling this server if (versionData.Properties.ContainsKey("protocol")) { protocolversion = dataTypes.Atoi(versionData.Properties["protocol"].StringValue); } // Check for forge on the server. Protocol18Forge.ServerInfoCheckForge(jsonData, ref forgeInfo); ConsoleIO.WriteLineFormatted("§8Server version : " + version + " (protocol v" + protocolversion + (forgeInfo != null ? ", with Forge)." : ").")); return(true); } } } } return(false); }