Пример #1
0
        // Chart for all error password-related error codes were provided by kojasou@ on
        // https://github.com/hybrasyl/server/pull/11.
        private void PacketHandler_0x26_ChangePassword(Client client, ClientPacket packet)
        {
            var name        = packet.ReadString8();
            var currentPass = packet.ReadString8();
            // Clientside validation ensures that the same string is typed twice for the new
            // password, and the new password is only sent to the server once. We can assume
            // that they matched if 0x26 request is sent from the client.
            var newPass = packet.ReadString8();

            using (var ctx = new hybrasylEntities(Constants.ConnectionString))
            {
                var player = ctx.players.Where(p => p.name == name).SingleOrDefault();

                // Check that `name` exists. If not, return a message indicating that to the user.
                if (player == null)
                {
                    client.LoginMessage(GetPasswordError(0x0E), 0x0E);
                    Logger.DebugFormat("Password change attempt on invalid player `{0}`", name);
                }
                // If the player does exist, validate the current and new passwords before updating.
                else
                {
                    // Check that the current password is correct and the new password is different
                    // than the current password.
                    if (VerifyPassword(currentPass, player))
                    {
                        // Check if the password is valid.
                        byte err = 0x00;
                        if (ValidPassword(newPass, out err))
                        {
                            player.password_hash = HashPassword(newPass);
                            ctx.SaveChanges();

                            // Let the user know the good news.
                            client.LoginMessage("Your password has been changed successfully.", 0x0);
                            Logger.InfoFormat("Password successfully changed for `{0}`", name);
                        }
                        else
                        {
                            client.LoginMessage(GetPasswordError(err), err);
                            Logger.ErrorFormat("Invalid new password proposed during password change attempt for `{0}`", name);
                        }
                    }
                    // The current password is incorrect. Don't allow any changes to happen.
                    else
                    {
                        client.LoginMessage(GetPasswordError(0x0F), 0x0F);
                        Logger.ErrorFormat("Invalid current password during password change attempt for `{0}`", name);
                    }
                }
            }
        }
Пример #2
0
        private void PacketHandler_0x03_Login(Client client, ClientPacket packet)
        {
            var name     = packet.ReadString8();
            var password = packet.ReadString8();

            Logger.DebugFormat("cid {0}: Login request for {1}", client.ConnectionId, name);

            using (var ctx = new hybrasylEntities(Constants.ConnectionString))
            {
                var result = ctx.players.Where(player => player.name == name).SingleOrDefault();

                if (result == null)
                {
                    client.LoginMessage("That character does not exist", 3);
                    Logger.InfoFormat("cid {0}: attempt to login as nonexistent character {1}", client.ConnectionId, name);
                }
                else
                {
                    if (VerifyPassword(password, result))
                    {
                        Logger.DebugFormat("cid {0}: password verified for {1}", client.ConnectionId, name);

                        if (Game.World.ActiveUsersByName.ContainsKey(name))
                        {
                            Logger.InfoFormat("cid {0}: {1} logging on again, disconnecting previous connection",
                                              client.ConnectionId, name);
                            World.MessageQueue.Add(new HybrasylControlMessage(ControlOpcodes.LogoffUser, name));
                        }

                        Logger.DebugFormat("cid {0} ({1}): logging in", client.ConnectionId, name);
                        client.LoginMessage("\0", 0);
                        client.SendMessage("Welcome to Hybrasyl!", 3);
                        Logger.DebugFormat("cid {0} ({1}): sending redirect to world", client.ConnectionId, name);

                        var redirect = new Redirect(client, this, Game.World, name, client.EncryptionSeed,
                                                    client.EncryptionKey);
                        Logger.InfoFormat("cid {0} ({1}): login successful, redirecting to world server",
                                          client.ConnectionId, name);
                        client.Redirect(redirect);
                    }
                    else
                    {
                        Logger.WarnFormat("cid {0} ({1}): password incorrect", client.ConnectionId, name);
                        client.LoginMessage("Incorrect password", 3);
                    }
                }
            }
        }
Пример #3
0
        private void PacketHandler_0x04_CreateB(Client client, ClientPacket packet)
        {
            if (string.IsNullOrEmpty(client.NewCharacterName) || string.IsNullOrEmpty(client.NewCharacterPassword))
            {
                return;
            }

            var hairStyle = packet.ReadByte();
            var sex       = packet.ReadByte();
            var hairColor = packet.ReadByte();

            if (hairStyle < 1)
            {
                hairStyle = 1;
            }

            if (hairStyle > 17)
            {
                hairStyle = 17;
            }

            if (hairColor > 13)
            {
                hairColor = 13;
            }

            if (sex < 1)
            {
                sex = 1;
            }

            if (sex > 2)
            {
                sex = 2;
            }

            if (!Game.World.PlayerExists(client.NewCharacterName))
            {
                using (var ctx = new hybrasylEntities(Constants.ConnectionString))
                {
                    player newplayer = new player
                    {
                        name          = client.NewCharacterName,
                        password_hash = client.NewCharacterPassword,
                        sex           = (Sex)sex,
                        hairstyle     = hairStyle,
                        haircolor     = hairColor,
                        map_id        = 136,
                        map_x         = 10,
                        map_y         = 10,
                        direction     = 1,
                        class_type    = 0,
                        level         = 1,
                        exp           = 0,
                        ab            = 0,
                        gold          = 0,
                        ab_exp        = 0,
                        max_hp        = 50,
                        max_mp        = 50,
                        cur_hp        = 50,
                        cur_mp        = 35,
                        str           = 3,
                        @int          = 3,
                        wis           = 3,
                        con           = 3,
                        dex           = 3,
                        inventory     = "[]",
                        equipment     = "[]",
                        created_at    = DateTime.Now
                    };
                    try
                    {
                        ctx.players.Add(newplayer);
                        ctx.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        Logger.ErrorFormat("Error saving new player!");
                        Logger.ErrorFormat(e.ToString());
                        client.LoginMessage("Unknown error. Contact [email protected]", 3);
                    }
                    client.LoginMessage("\0", 0);
                }
            }
        }