Add message-specific ratelimiting

This commit is contained in:
Alejandro 2020-02-27 21:06:36 +02:00
parent 70d20ff2dd
commit 4c77c76bdc
5 changed files with 45 additions and 5 deletions

View File

@ -5,6 +5,7 @@ import com.eu.habbo.core.Logging;
import com.eu.habbo.habbohotel.users.Habbo;
import com.eu.habbo.messages.PacketManager;
import com.eu.habbo.messages.ServerMessage;
import com.eu.habbo.messages.incoming.MessageHandler;
import com.eu.habbo.messages.outgoing.MessageComposer;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
@ -17,12 +18,12 @@ import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
public class GameClient {
public final ConcurrentHashMap<Integer, Integer> incomingPacketCounter = new ConcurrentHashMap<>(25);
private final Channel channel;
public long lastPacketCounterCleared = Emulator.getIntUnixTimestamp();
private Habbo habbo;
private String machineId = "";
public final ConcurrentHashMap<Class<? extends MessageHandler>, Long> messageTimestamps = new ConcurrentHashMap<>();
public GameClient(Channel channel) {
this.channel = channel;

View File

@ -291,7 +291,7 @@ public class RoomUnit {
if (item != null) {
if (item != habboItem || !RoomLayout.pointInSquare(item.getX(), item.getY(), item.getX() + item.getBaseItem().getWidth() - 1, item.getY() + item.getBaseItem().getLength() - 1, this.getX(), this.getY())) {
if (item.canWalkOn(this, room, null)) {
item.onWalkOn(this, room, null);
item.onWalkOn(this, room, new Object[]{this.getCurrentLocation(), next});
} else if (item instanceof InteractionGuildGate || item instanceof InteractionHabboClubGate) {
this.setRotation(oldRotation);
this.tilesWalked--;
@ -306,7 +306,7 @@ public class RoomUnit {
return false;
}
} else {
item.onWalk(this, room, null);
item.onWalk(this, room, new Object[]{this.getCurrentLocation(), next});
}
zHeight += item.getZ();

View File

@ -171,6 +171,20 @@ public class PacketManager {
return;
}
final MessageHandler handler = handlerClass.newInstance();
if (handler.getRatelimit() > 0) {
if (client.messageTimestamps.containsKey(handlerClass) && System.currentTimeMillis() - client.messageTimestamps.get(handlerClass) < handler.getRatelimit()) {
if (PacketManager.DEBUG_SHOW_PACKETS) {
Emulator.getLogging().logPacketLine("[" + Logging.ANSI_CYAN + "CLIENT" + Logging.ANSI_RESET + "][" + packet.getMessageId() + "][" + Logging.ANSI_RED + "RATELIMITED" + Logging.ANSI_RESET + "] => " + packet.getMessageBody());
}
return;
} else {
client.messageTimestamps.put(handlerClass, System.currentTimeMillis());
}
}
if (PacketManager.DEBUG_SHOW_PACKETS)
Emulator.getLogging().logPacketLine("[" + Logging.ANSI_CYAN + "CLIENT" + Logging.ANSI_RESET + "][" + packet.getMessageId() + "] => " + packet.getMessageBody());
@ -178,8 +192,6 @@ public class PacketManager {
System.out.println(("[" + Logging.ANSI_CYAN + "CLIENT" + Logging.ANSI_RESET + "][" + client.getHabbo().getHabboInfo().getUsername() + "][" + packet.getMessageId() + "] => " + packet.getMessageBody()));
}
final MessageHandler handler = handlerClass.newInstance();
handler.client = client;
handler.packet = packet;

View File

@ -9,4 +9,8 @@ public abstract class MessageHandler {
public boolean isCancelled = false;
public abstract void handle() throws Exception;
public int getRatelimit() {
return 0;
}
}

View File

@ -11,8 +11,14 @@ import com.eu.habbo.habbohotel.users.HabboItem;
import com.eu.habbo.messages.incoming.MessageHandler;
import com.eu.habbo.messages.outgoing.rooms.users.RoomUnitOnRollerComposer;
import com.eu.habbo.plugin.events.users.UserIdleEvent;
import gnu.trove.set.hash.THashSet;
public class RoomUserWalkEvent extends MessageHandler {
@Override
public int getRatelimit() {
return 500;
}
@Override
public void handle() throws Exception {
if (this.client.getHabbo().getHabboInfo().getCurrentRoom() != null) {
@ -115,6 +121,23 @@ public class RoomUserWalkEvent extends MessageHandler {
}
}
THashSet<HabboItem> items = room.getItemsAt(tile);
if (items.size() > 0) {
for (HabboItem item : items) {
RoomTile overriddenTile = item.getOverrideGoalTile(roomUnit, room, tile);
if (overriddenTile == null) {
return; // null cancels the entire event
}
if (!overriddenTile.equals(tile) && overriddenTile.isWalkable()) {
tile = overriddenTile;
break;
}
}
}
// This is where we set the end location and begin finding a path
if (tile.isWalkable() || room.canSitOrLayAt(tile.x, tile.y)) {
if (roomUnit.getMoveBlockingTask() != null) roomUnit.getMoveBlockingTask().get();