Beispiel #1
0
        public override void ReadPacket()
        {
            SharedSecretLength = Read <VarInt>();
            SharedSecret       = ReadArray <byte>(SharedSecretLength);
            VerifyTokenLength  = Read <VarInt>();
            VerifyToken        = ReadArray <byte>(VerifyTokenLength);

            VerifyToken = PacketCryptography.Decrypt(VerifyToken);

            if (!VerifyToken.SequenceEqual(PacketCryptography.VerifyToken))
            {
                Client.Send(new DisconnectLoginPaket(Client)
                {
                    Reason = new ChatMessage()
                    {
                        Text = "Verification Failed"
                    }
                });
                return;
            }

            SharedSecret = PacketCryptography.Decrypt(SharedSecret);

            var v = Utils.GetUUID(Player, (Utils.GetHash(Utils.BaseServerHash,
                                                         SharedSecret,
                                                         PacketCryptography.PublicKeyToAsn1(Utils.ServerKey))));

            Client.Crypto = PacketCryptography.GenerateAes(SharedSecret);

            var v2 = v.GetAwaiter().GetResult();

            if (v2 == null)
            {
                Client.Send(new DisconnectLoginPaket(Client)
                {
                    Reason = new ChatMessage()
                    {
                        Text = "Coud not get UUID"
                    }
                });
                return;
            }
            else
            {
                Player.UUID = v2;
            }

            Client.Send(new LoginSuccessPacket(Client));
        }
        public override void Read()
        {
            if (Buffer == null)
            {
                return;
            }
            var length       = Buffer.ReadVarInt();
            var sharedsecret = Buffer.Read(length);

            length = Buffer.ReadVarInt();
            var verifytoken = Buffer.Read(length);

            Client.SharedKey = PacketCryptography.Decrypt(sharedsecret);

            var recv = PacketCryptography.GenerateAes((byte[])Client.SharedKey.Clone());
            var send = PacketCryptography.GenerateAes((byte[])Client.SharedKey.Clone());

            var packetToken = PacketCryptography.Decrypt(verifytoken);

            if (!packetToken.SequenceEqual(PacketCryptography.VerifyToken))
            {
                //Wrong token! :(
                ConsoleFunctions.WriteWarningLine("Wrong token!");
                return;
            }

            Client.Decryptor = recv.CreateDecryptor();
            Client.Encryptor = send.CreateEncryptor();

            Client.EncryptionEnabled = true;
            Client.Player            = new Player(Globals.LevelManager.MainLevel)
            {
                Uuid     = GetUuid(Client.Username),
                Username = Client.Username,
                Wrapper  = Client,
                Gamemode = Globals.LevelManager.MainLevel.DefaultGamemode
            };

            if (Client.Player.IsAuthenticated())
            {
                new LoginSucces(Client)
                {
                    Username = Client.Username, Uuid = Client.Player.Uuid
                }.Write();
                Client.PacketMode = PacketMode.Play;

                new SetCompression(Client).Write();

                new JoinGame(Client)
                {
                    Player = Client.Player
                }.Write();
                new SpawnPosition(Client).Write();

                Client.Player.InitializePlayer();
            }
            else
            {
                new LoginSucces(Client)
                {
                    Username = Client.Username, Uuid = Client.Player.Uuid
                }.Write();
                new Disconnect(Client)
                {
                    Reason = new ChatText("Authentication failed! Try restarting your client.")
                }.Write();
            }
        }