Skip to content

Commit 53c8442

Browse files
committed
Add direct-write config option to avoid plugins that modify packets
1 parent 5dc2bba commit 53c8442

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/main/java/net/elytrium/fastmotd/Settings.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public static class MAIN {
6666
@CommentValue("Set -1 to disable PNG recompression")
6767
})
6868
public double PNG_QUALITY = 0.0;
69+
@Comment(@CommentValue("Write packets outside of Netty pipeline to avoid plugins that modify packets (e.g. PacketEvents)"))
70+
public boolean DIRECT_WRITE = false;
6971
public boolean LOG_PINGS = false;
7072
public boolean LOG_IMPROPER_PINGS = false;
7173
@Comment({

src/main/java/net/elytrium/fastmotd/injection/HandshakeSessionHandlerHook.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import io.netty.buffer.ByteBuf;
3434
import io.netty.buffer.Unpooled;
3535
import io.netty.channel.Channel;
36+
import io.netty.channel.ChannelOutboundBuffer;
3637
import java.net.InetSocketAddress;
3738
import net.elytrium.fastmotd.FastMOTD;
3839
import net.elytrium.fastmotd.Settings;
@@ -93,6 +94,26 @@ private void switchState(State oldState, State newState) {
9394
this.state = newState;
9495
}
9596

97+
private void sendPacket(ByteBuf packet, boolean constant) {
98+
if (Settings.IMP.MAIN.DIRECT_WRITE) {
99+
ChannelOutboundBuffer buffer = this.channel.unsafe().outboundBuffer();
100+
if (buffer == null) {
101+
packet.release(); // connection was closed already, no need to send the packet.
102+
} else {
103+
// .slice() constant packet to ensure that Netty do not modify its readerIndex
104+
if (constant) {
105+
packet = packet.slice();
106+
}
107+
108+
// Send the packet
109+
buffer.addMessage(packet, packet.readableBytes(), this.channel.voidPromise());
110+
this.channel.flush();
111+
}
112+
} else {
113+
this.channel.writeAndFlush(packet);
114+
}
115+
}
116+
96117
@Override
97118
public boolean handle(LegacyPingPacket packet) {
98119
this.connection.close();
@@ -154,11 +175,11 @@ public void handleGeneric(MinecraftPacket packet) {
154175
buf.writeByte(9);
155176
buf.writeByte(1);
156177
packet.encode(buf, null, null);
157-
this.channel.writeAndFlush(buf);
178+
this.sendPacket(buf, false);
158179
this.connection.close();
159180
} else if (packet instanceof StatusRequestPacket) {
160181
this.switchState(State.REQUEST, State.PING);
161-
this.channel.writeAndFlush(this.plugin.getNext(this.protocolVersion, this.serverAddress));
182+
this.sendPacket(this.plugin.getNext(this.protocolVersion, this.serverAddress), true);
162183
} else {
163184
this.original.handleGeneric(packet);
164185
}

0 commit comments

Comments
 (0)