mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-29 04:28:21 +01:00
Added max packet size condition
This commit is contained in:
parent
373a1cf3a7
commit
674e1079dd
@ -107,15 +107,14 @@ public final class MinecraftServer {
|
|||||||
@Setter
|
@Setter
|
||||||
private static boolean hardcoreLook = false;
|
private static boolean hardcoreLook = false;
|
||||||
|
|
||||||
//Extras
|
// Extras
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
private static boolean fixLighting = true;
|
private static boolean fixLighting = true;
|
||||||
|
|
||||||
//Rate Limiting
|
// Rate Limiting
|
||||||
private static int rateLimit = 0;
|
private static int rateLimit = 0;
|
||||||
// TODO
|
private static int maxPacketSize = 30_000;
|
||||||
public static final int MAX_PACKET_SIZE = 300_000;
|
|
||||||
|
|
||||||
private static PacketListenerManager packetListenerManager;
|
private static PacketListenerManager packetListenerManager;
|
||||||
private static NettyServer nettyServer;
|
private static NettyServer nettyServer;
|
||||||
@ -250,7 +249,7 @@ public final class MinecraftServer {
|
|||||||
/**
|
/**
|
||||||
* Gets the max number of packets a client can send over 1 second.
|
* Gets the max number of packets a client can send over 1 second.
|
||||||
*
|
*
|
||||||
* @return the packet count limit over 1 second
|
* @return the packet count limit over 1 second, 0 if not enabled
|
||||||
*/
|
*/
|
||||||
public static int getRateLimit() {
|
public static int getRateLimit() {
|
||||||
return rateLimit;
|
return rateLimit;
|
||||||
@ -265,6 +264,24 @@ public final class MinecraftServer {
|
|||||||
MinecraftServer.rateLimit = rateLimit;
|
MinecraftServer.rateLimit = rateLimit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the maximum packet size (in bytes) that a client can send without getting disconnected.
|
||||||
|
*
|
||||||
|
* @return the maximum packet size
|
||||||
|
*/
|
||||||
|
public static int getMaxPacketSize() {
|
||||||
|
return maxPacketSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Changes the maximum packet size (in bytes) that a client can send without getting disconnected.
|
||||||
|
*
|
||||||
|
* @param maxPacketSize the new max packet size
|
||||||
|
*/
|
||||||
|
public static void setMaxPacketSize(int maxPacketSize) {
|
||||||
|
MinecraftServer.maxPacketSize = maxPacketSize;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the server difficulty showed in game option.
|
* Gets the server difficulty showed in game option.
|
||||||
*
|
*
|
||||||
|
@ -29,7 +29,7 @@ public class NettyServer {
|
|||||||
private String address;
|
private String address;
|
||||||
private int port;
|
private int port;
|
||||||
|
|
||||||
public NettyServer(PacketProcessor packetProcessor) {
|
public NettyServer(@NotNull PacketProcessor packetProcessor) {
|
||||||
Class<? extends ServerChannel> channel;
|
Class<? extends ServerChannel> channel;
|
||||||
|
|
||||||
if (Epoll.isAvailable()) {
|
if (Epoll.isAvailable()) {
|
||||||
@ -55,10 +55,11 @@ public class NettyServer {
|
|||||||
|
|
||||||
ChannelPipeline pipeline = ch.pipeline();
|
ChannelPipeline pipeline = ch.pipeline();
|
||||||
|
|
||||||
|
// First check should verify if the packet is a legacy ping (from 1.6 version and earlier)
|
||||||
pipeline.addLast("legacy-ping", new LegacyPingHandler());
|
pipeline.addLast("legacy-ping", new LegacyPingHandler());
|
||||||
|
|
||||||
// Adds packetLength at start | Reads framed bytebuf
|
// Adds packetLength at start | Reads framed bytebuf
|
||||||
pipeline.addLast("framer", new PacketFramer());
|
pipeline.addLast("framer", new PacketFramer(packetProcessor));
|
||||||
|
|
||||||
// Reads bytebuf and creating inbound packet
|
// Reads bytebuf and creating inbound packet
|
||||||
pipeline.addLast("decoder", new PacketDecoder());
|
pipeline.addLast("decoder", new PacketDecoder());
|
||||||
|
@ -4,12 +4,26 @@ import io.netty.buffer.ByteBuf;
|
|||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import io.netty.handler.codec.ByteToMessageCodec;
|
import io.netty.handler.codec.ByteToMessageCodec;
|
||||||
import io.netty.handler.codec.CorruptedFrameException;
|
import io.netty.handler.codec.CorruptedFrameException;
|
||||||
|
import net.minestom.server.MinecraftServer;
|
||||||
|
import net.minestom.server.entity.Player;
|
||||||
|
import net.minestom.server.network.PacketProcessor;
|
||||||
|
import net.minestom.server.network.player.PlayerConnection;
|
||||||
import net.minestom.server.utils.Utils;
|
import net.minestom.server.utils.Utils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class PacketFramer extends ByteToMessageCodec<ByteBuf> {
|
public class PacketFramer extends ByteToMessageCodec<ByteBuf> {
|
||||||
|
|
||||||
|
public final static Logger LOGGER = LoggerFactory.getLogger(PacketFramer.class);
|
||||||
|
|
||||||
|
private final PacketProcessor packetProcessor;
|
||||||
|
|
||||||
|
public PacketFramer(PacketProcessor packetProcessor) {
|
||||||
|
this.packetProcessor = packetProcessor;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void encode(ChannelHandlerContext ctx, ByteBuf from, ByteBuf to) {
|
protected void encode(ChannelHandlerContext ctx, ByteBuf from, ByteBuf to) {
|
||||||
final int packetSize = from.readableBytes();
|
final int packetSize = from.readableBytes();
|
||||||
@ -40,14 +54,29 @@ public class PacketFramer extends ByteToMessageCodec<ByteBuf> {
|
|||||||
if (b >= 0) {
|
if (b >= 0) {
|
||||||
buf.resetReaderIndex();
|
buf.resetReaderIndex();
|
||||||
|
|
||||||
final int j = Utils.readVarInt(buf);
|
final int packetSize = Utils.readVarInt(buf);
|
||||||
|
|
||||||
if (buf.readableBytes() < j) {
|
// Max packet size check
|
||||||
|
if (packetSize >= MinecraftServer.getMaxPacketSize()) {
|
||||||
|
final PlayerConnection playerConnection = packetProcessor.getPlayerConnection(ctx);
|
||||||
|
if (playerConnection != null) {
|
||||||
|
final Player player = playerConnection.getPlayer();
|
||||||
|
final String identifier = player != null ?
|
||||||
|
player.getUsername() :
|
||||||
|
playerConnection.getRemoteAddress().toString();
|
||||||
|
LOGGER.warn("An user (" + identifier + ") sent a packet over the maximum size (" + packetSize + ")");
|
||||||
|
} else {
|
||||||
|
LOGGER.warn("An unregistered user sent a packet over the maximum size (" + packetSize + ")");
|
||||||
|
}
|
||||||
|
ctx.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (buf.readableBytes() < packetSize) {
|
||||||
buf.resetReaderIndex();
|
buf.resetReaderIndex();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
out.add(buf.readRetainedSlice(j));
|
out.add(buf.readRetainedSlice(packetSize));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
package net.minestom.server.network.netty.packet;
|
package net.minestom.server.network.netty.packet;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
public class InboundPacket {
|
public class InboundPacket {
|
||||||
|
|
||||||
public final int packetId;
|
public final int packetId;
|
||||||
public final ByteBuf body;
|
public final ByteBuf body;
|
||||||
|
|
||||||
public InboundPacket(int id, ByteBuf body) {
|
public InboundPacket(int id, @NotNull ByteBuf body) {
|
||||||
this.packetId = id;
|
this.packetId = id;
|
||||||
this.body = body;
|
this.body = body;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user