Skip to content

Commit

Permalink
Add direct-write config option to avoid plugins that modify packets
Browse files Browse the repository at this point in the history
  • Loading branch information
UserNugget committed Jul 19, 2024
1 parent 5dc2bba commit 53c8442
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/main/java/net/elytrium/fastmotd/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public static class MAIN {
@CommentValue("Set -1 to disable PNG recompression")
})
public double PNG_QUALITY = 0.0;
@Comment(@CommentValue("Write packets outside of Netty pipeline to avoid plugins that modify packets (e.g. PacketEvents)"))
public boolean DIRECT_WRITE = false;
public boolean LOG_PINGS = false;
public boolean LOG_IMPROPER_PINGS = false;
@Comment({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOutboundBuffer;
import java.net.InetSocketAddress;
import net.elytrium.fastmotd.FastMOTD;
import net.elytrium.fastmotd.Settings;
Expand Down Expand Up @@ -93,6 +94,26 @@ private void switchState(State oldState, State newState) {
this.state = newState;
}

private void sendPacket(ByteBuf packet, boolean constant) {
if (Settings.IMP.MAIN.DIRECT_WRITE) {
ChannelOutboundBuffer buffer = this.channel.unsafe().outboundBuffer();
if (buffer == null) {
packet.release(); // connection was closed already, no need to send the packet.
} else {
// .slice() constant packet to ensure that Netty do not modify its readerIndex
if (constant) {
packet = packet.slice();
}

// Send the packet
buffer.addMessage(packet, packet.readableBytes(), this.channel.voidPromise());
this.channel.flush();
}
} else {
this.channel.writeAndFlush(packet);
}
}

@Override
public boolean handle(LegacyPingPacket packet) {
this.connection.close();
Expand Down Expand Up @@ -154,11 +175,11 @@ public void handleGeneric(MinecraftPacket packet) {
buf.writeByte(9);
buf.writeByte(1);
packet.encode(buf, null, null);
this.channel.writeAndFlush(buf);
this.sendPacket(buf, false);
this.connection.close();
} else if (packet instanceof StatusRequestPacket) {
this.switchState(State.REQUEST, State.PING);
this.channel.writeAndFlush(this.plugin.getNext(this.protocolVersion, this.serverAddress));
this.sendPacket(this.plugin.getNext(this.protocolVersion, this.serverAddress), true);
} else {
this.original.handleGeneric(packet);
}
Expand Down

0 comments on commit 53c8442

Please sign in to comment.