Skip to content

Commit

Permalink
kick players even if user is null & they throw an exception, kick use…
Browse files Browse the repository at this point in the history
…r for exceptions also in encoder.
  • Loading branch information
retrooper committed Aug 18, 2024
1 parent 5203b3a commit f4bb1dd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ public class SpigotChannelInjector implements ChannelInjector {
public final Set<Channel> injectedConnectionChannels = new HashSet<>();
public List<Object> networkManagers;
private int connectionChannelsListIndex = -1;
public boolean inboundAheadProtocolTranslation = false;
public boolean outboundAheadProtocolTranslation = false;

public void updatePlayer(User user, Object player) {
PacketEvents.getAPI().getEventManager().callEvent(new UserLoginEvent(user, player));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,33 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
super.exceptionCaught(ctx, cause);
//Check if the minecraft server will already print our exception for us.
//Don't print errors during handshake
if (ExceptionUtil.isException(cause, PacketProcessException.class)
&& !SpigotReflectionUtil.isMinecraftServerInstanceDebugging()
&& (user != null && user.getDecoderState() != ConnectionState.HANDSHAKING)) {
if (PacketEvents.getAPI().getSettings().isFullStackTraceEnabled()) {
cause.printStackTrace();
} else {
PacketEvents.getAPI().getLogManager().warn(cause.getMessage());
boolean didWeCauseThis = ExceptionUtil.isException(cause, PacketProcessException.class);
if (didWeCauseThis
&& (user == null || user.getDecoderState() != ConnectionState.HANDSHAKING)) {
if (!SpigotReflectionUtil.isMinecraftServerInstanceDebugging()) {
if (PacketEvents.getAPI().getSettings().isFullStackTraceEnabled()) {
cause.printStackTrace();
} else {
PacketEvents.getAPI().getLogManager().warn(cause.getMessage());
}
}

if (PacketEvents.getAPI().getSettings().isKickOnPacketExceptionEnabled()) {
try {
user.sendPacket(new WrapperPlayServerDisconnect(Component.text("Invalid packet")));
if (user != null) {
user.sendPacket(new WrapperPlayServerDisconnect(Component.text("Invalid packet")));
}
} catch (Exception ignored) { // There may (?) be an exception if the player is in the wrong state...
// Do nothing.
}
user.closeConnection();
ctx.channel().close();
if (player != null) {
FoliaScheduler.getEntityScheduler().runDelayed(player, (Plugin) PacketEvents.getAPI().getPlugin(), (o) -> player.kickPlayer("Invalid packet"), null, 1);
}

PacketEvents.getAPI().getLogManager().warn("Disconnected " + user.getProfile().getName() + " due to invalid packet!");
if (user != null) {
PacketEvents.getAPI().getLogManager().warn("Disconnected " + user.getProfile().getName() + " due to invalid packet!");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,20 @@
import com.github.retrooper.packetevents.util.ExceptionUtil;
import com.github.retrooper.packetevents.util.PacketEventsImplHelper;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerDisconnect;
import io.github.retrooper.packetevents.injector.connection.ServerConnectionInitializer;
import io.github.retrooper.packetevents.util.SpigotReflectionUtil;
import io.github.retrooper.packetevents.util.folia.FoliaScheduler;
import io.github.retrooper.packetevents.util.viaversion.CustomPipelineUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.MessageToMessageEncoder;
import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
Expand Down Expand Up @@ -114,15 +119,32 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
}

boolean didWeCauseThis = ExceptionUtil.isException(cause, PacketProcessException.class);

if (didWeCauseThis && user != null && user.getEncoderState() != ConnectionState.HANDSHAKING) {
// Ignore handshaking exceptions
if (PacketEvents.getAPI().getSettings().isFullStackTraceEnabled()) {
cause.printStackTrace();
} else {
PacketEvents.getAPI().getLogManager().warn(cause.getMessage());
if (didWeCauseThis
&& (user == null || user.getEncoderState() != ConnectionState.HANDSHAKING)) {
if (!SpigotReflectionUtil.isMinecraftServerInstanceDebugging()) {
if (PacketEvents.getAPI().getSettings().isFullStackTraceEnabled()) {
cause.printStackTrace();
} else {
PacketEvents.getAPI().getLogManager().warn(cause.getMessage());
}
}
return;

if (PacketEvents.getAPI().getSettings().isKickOnPacketExceptionEnabled()) {
try {
if (user != null) {
user.sendPacket(new WrapperPlayServerDisconnect(Component.text("Invalid packet")));
}
} catch (Exception ignored) { // There may (?) be an exception if the player is in the wrong state...
// Do nothing.
}
ctx.channel().close();
if (player != null) {
FoliaScheduler.getEntityScheduler().runDelayed(player, (Plugin) PacketEvents.getAPI().getPlugin(), (o) -> player.kickPlayer("Invalid packet"), null, 1);
}

if (user != null) {
PacketEvents.getAPI().getLogManager().warn("Disconnected " + user.getProfile().getName() + " due to invalid packet!");
}}
}

super.exceptionCaught(ctx, cause);
Expand Down

0 comments on commit f4bb1dd

Please sign in to comment.