mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-19 14:41:34 +01:00
Rate Limiting
This commit is contained in:
parent
23e82e0ae2
commit
8ecb90f359
@ -90,6 +90,10 @@ public class MinecraftServer {
|
||||
@Setter
|
||||
private static boolean fixLighting = true;
|
||||
|
||||
//Rate Limiting
|
||||
@Getter @Setter
|
||||
private static int rateLimit = 0;
|
||||
|
||||
// Networking
|
||||
private static PacketProcessor packetProcessor;
|
||||
private static PacketListenerManager packetListenerManager;
|
||||
|
@ -46,6 +46,9 @@ public class PacketProcessor {
|
||||
channel, c -> new NettyPlayerConnection((SocketChannel) channel.channel())
|
||||
);
|
||||
|
||||
if (MinecraftServer.getRateLimit() > 0)
|
||||
playerConnection.getPacketCounter().incrementAndGet();
|
||||
|
||||
final ConnectionState connectionState = playerConnection.getConnectionState();
|
||||
|
||||
//if (!printBlackList.contains(id)) {
|
||||
|
@ -3,11 +3,16 @@ package net.minestom.server.network.player;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.chat.ColoredText;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.network.ConnectionState;
|
||||
import net.minestom.server.network.packet.server.ServerPacket;
|
||||
import net.minestom.server.network.packet.server.login.LoginDisconnect;
|
||||
import net.minestom.server.network.packet.server.play.DisconnectPacket;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* A PlayerConnection is an object needed for all created player
|
||||
@ -27,11 +32,38 @@ public abstract class PlayerConnection {
|
||||
private ConnectionState connectionState;
|
||||
private boolean online;
|
||||
|
||||
private static final ColoredText rateLimitKickMessage = ColoredText.of("Too Many Packets");
|
||||
|
||||
//Connection Stats
|
||||
@Getter
|
||||
private final AtomicInteger packetCounter = new AtomicInteger(0);
|
||||
private short tickCounter = 0;
|
||||
|
||||
public PlayerConnection() {
|
||||
this.online = true;
|
||||
this.connectionState = ConnectionState.UNKNOWN;
|
||||
}
|
||||
|
||||
public void updateStats() {
|
||||
tickCounter++;
|
||||
if (tickCounter % 20 == 0 && tickCounter > 0) {
|
||||
tickCounter = 0;
|
||||
int i = packetCounter.get();
|
||||
packetCounter.set(0);
|
||||
if (i > MinecraftServer.getRateLimit()) {
|
||||
if (connectionState == ConnectionState.LOGIN) {
|
||||
sendPacket(new LoginDisconnect("Too Many Packets"));
|
||||
} else {
|
||||
DisconnectPacket disconnectPacket = new DisconnectPacket();
|
||||
disconnectPacket.message = rateLimitKickMessage;
|
||||
sendPacket(disconnectPacket);
|
||||
}
|
||||
disconnect();
|
||||
refreshOnline(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void enableCompression(int threshold);
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user