Exemplo n.º 1
0
        /// <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);

            packetReadWriter.WritePacket(0x00, handshake_packet);

            byte[] login_packet = dataTypes.GetString(player.GetUsername());

            packetReadWriter.WritePacket(0x00, login_packet);

            while (true)
            {
                Packet packet = packetReadWriter.ReadNext();
                if (packet.id == 0x00) //Login rejected
                {
                    handler.OnConnectionLost(DisconnectReason.LoginRejected, ChatParser.ParseText(dataTypes.ReadNextString(packet.data)));
                    return(false);
                }
                else if (packet.id == 0x01) //Encryption request
                {
                    string serverID  = dataTypes.ReadNextString(packet.data);
                    byte[] Serverkey = dataTypes.ReadNextByteArray(packet.data);
                    byte[] token     = dataTypes.ReadNextByteArray(packet.data);
                    return(StartEncryption(player.GetUserUUID(), handler.GetSessionID(), token, serverID, Serverkey));
                }
                else if (packet.id == 0x02) //Login successful
                {
                    ConsoleIO.WriteLineFormatted("§8Server is in offline mode.");
                    login_phase = false;

                    if (!pForge.CompleteForgeHandshake())
                    {
                        return(false);
                    }

                    readNet = true;
                    //StartUpdating();
                    return(true); //No need to check session or start encryption
                }
                else
                {
                    HandlePacket(packet);
                }
            }
        }
        private void Updater()
        {
            try
            {
                do
                {
                    Thread.Sleep(100);
                }while (Update());
            }
            catch (System.IO.IOException) { }
            catch (SocketException) { }
            catch (ObjectDisposedException) { }

            handler.OnConnectionLost(ChatBot.DisconnectReason.ConnectionLost, "");
        }
        /// <summary>
        /// Completes the Minecraft Forge handshake.
        /// </summary>
        /// <returns>Whether the handshake was successful.</returns>
        public bool CompleteForgeHandshake()
        {
            if (ForgeEnabled())
            {
                int         packetID   = -1;
                List <byte> packetData = new List <byte>();

                while (fmlHandshakeState != FMLHandshakeClientState.DONE)
                {
                    protocol18.ReadNextPacket(ref packetID, packetData);

                    if (packetID == 0x40) // Disconnect
                    {
                        mcHandler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, ChatParser.ParseText(dataTypes.ReadNextString(packetData)));
                        return(false);
                    }
                    else
                    {
                        // Send back regular packet to the vanilla protocol handler
                        protocol18.HandlePacket(packetID, packetData);
                    }
                }
            }
            return(true);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Separate thread. Network reading loop.
        /// </summary>

        private void Updater()
        {
            int keep_alive_interval = 100;
            int keep_alive_timer    = 100;

            try
            {
                do
                {
                    Thread.Sleep(100);
                    keep_alive_timer--;
                    if (keep_alive_timer <= 0)
                    {
                        Send(getPaddingPacket());
                        keep_alive_timer = keep_alive_interval;
                    }
                }while (Update());
            }
            catch (System.IO.IOException) { }
            catch (SocketException) { }
            catch (ObjectDisposedException) { }

            handler.OnConnectionLost(ChatBot.DisconnectReason.ConnectionLost, "");
        }
        /// <summary>
        /// Completes the Minecraft Forge handshake.
        /// </summary>
        /// <returns>Whether the handshake was successful.</returns>
        public bool CompleteForgeHandshake()
        {
            if (ForgeEnabled())
            {
                while (fmlHandshakeState != FMLHandshakeClientState.DONE)
                {
                    Packet packet = packetReadWriter.ReadNext();

                    if (packet.id == 0x40) // Disconnect
                    {
                        mcHandler.OnConnectionLost(DisconnectReason.LoginRejected, ChatParser.ParseText(dataTypes.ReadNextString(packet.data)));
                        return(false);
                    }
                    else
                    {
                        // Send back regular packet to the vanilla protocol handler
                        protocol18.HandlePacket(packet);
                    }
                }
            }
            return(true);
        }
 public bool HandlePacket(PacketIncomingType packetType, List <byte> packetData)
 {
     handler.OnConnectionLost(DisconnectReason.InGameKick, ChatParser.ParseText(dataTypes.ReadNextString(packetData)));
     return(false);
 }