Esempio n. 1
0
        /// <summary>
        /// Do the Minecraft login.
        /// </summary>
        /// <returns>True if login successful</returns>

        public bool Login()
        {
            byte[] protocol_version  = getVarInt(protocolversion);
            byte[] server_adress_val = Encoding.UTF8.GetBytes(handler.getServerHost());
            byte[] server_adress_len = getVarInt(server_adress_val.Length);
            byte[] server_port       = BitConverter.GetBytes((ushort)handler.getServerPort()); Array.Reverse(server_port);
            byte[] next_state        = getVarInt(2);
            byte[] handshake_packet  = concatBytes(protocol_version, server_adress_len, server_adress_val, server_port, next_state);

            SendPacket(0x00, handshake_packet);

            byte[] username_val = Encoding.UTF8.GetBytes(handler.getUsername());
            byte[] username_len = getVarInt(username_val.Length);
            byte[] login_packet = concatBytes(username_len, username_val);

            SendPacket(0x00, login_packet);

            int packetID = -1;

            byte[] packetData = new byte[] { };
            while (true)
            {
                readNextPacket(ref packetID, ref packetData);
                if (packetID == 0x00) //Login rejected
                {
                    handler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, ChatParser.ParseText(readNextString(ref packetData)));
                    return(false);
                }
                else if (packetID == 0x01) //Encryption request
                {
                    string serverID  = readNextString(ref packetData);
                    byte[] Serverkey = readNextByteArray(ref packetData);
                    byte[] token     = readNextByteArray(ref 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;
                    StartUpdating();
                    return(true); //No need to check session or start encryption
                }
                else
                {
                    handlePacket(packetID, packetData);
                }
            }
        }
 public bool Login()
 {
     if (Handshake(handler.getUserUUID(), handler.getUsername(), handler.getSessionID(), handler.getServerHost(), handler.getServerPort()))
     {
         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);
                         StartUpdating();
                         return(true); //The Server accepted the request
                     }
                     else if (pid[0] == (byte)0xFF)
                     {
                         string reason = readNextString();
                         handler.OnConnectionLost(ChatBot.DisconnectReason.LoginRejected, reason);
                         return(false);
                     }
                 }
             }
             catch
             {
                 //Connection failed
                 return(false);
             }
         }
         catch
         {
             handler.OnConnectionLost(ChatBot.DisconnectReason.ConnectionLost, "");
             return(false);
         }
         return(false); //Login was unsuccessful (received a kick...)
     }
     else
     {
         return(false);
     }
 }