Skip to content

Commit

Permalink
Improve trace logging for offline handler
Browse files Browse the repository at this point in the history
  • Loading branch information
SupremeMortal committed Jul 24, 2024
1 parent 241c262 commit e241646
Showing 1 changed file with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.cloudburstmc.netty.channel.raknet.RakChildChannel;
import org.cloudburstmc.netty.channel.raknet.RakPing;
import org.cloudburstmc.netty.channel.raknet.RakServerChannel;
import org.cloudburstmc.netty.channel.raknet.config.RakChannelOption;
import org.cloudburstmc.netty.channel.raknet.config.RakServerChannelConfig;
import org.cloudburstmc.netty.channel.raknet.config.RakServerMetrics;
import org.cloudburstmc.netty.handler.codec.raknet.AdvancedChannelInboundHandler;
import org.cloudburstmc.netty.util.RakUtils;
Expand Down Expand Up @@ -90,7 +90,7 @@ protected boolean acceptInboundMessage(ChannelHandlerContext ctx, Object msg) th
}
case ID_OPEN_CONNECTION_REQUEST_1:
case ID_OPEN_CONNECTION_REQUEST_2:
ByteBuf magicBuf = ctx.channel().config().getOption(RakChannelOption.RAK_UNCONNECTED_MAGIC);
ByteBuf magicBuf = ((RakServerChannelConfig) ctx.channel().config()).getUnconnectedMagic();
return buf.isReadable(magicBuf.readableBytes()) && ByteBufUtil.equals(buf.readSlice(magicBuf.readableBytes()), magicBuf);
default:
return false;
Expand All @@ -105,8 +105,8 @@ protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) th
ByteBuf buf = packet.content();
short packetId = buf.readUnsignedByte();

ByteBuf magicBuf = ctx.channel().config().getOption(RakChannelOption.RAK_UNCONNECTED_MAGIC);
long guid = ctx.channel().config().getOption(RakChannelOption.RAK_GUID);
ByteBuf magicBuf = ((RakServerChannelConfig) ctx.channel().config()).getUnconnectedMagic();
long guid = ((RakServerChannelConfig) ctx.channel().config()).getGuid();

RakServerMetrics metrics = this.channel.config().getMetrics();

Expand All @@ -129,13 +129,13 @@ protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) th
private void onUnconnectedPing(ChannelHandlerContext ctx, DatagramPacket packet, ByteBuf magicBuf, long guid) {
long pingTime = packet.content().readLong();

boolean handlePing = ctx.channel().config().getOption(RakChannelOption.RAK_HANDLE_PING);
boolean handlePing = ((RakServerChannelConfig) ctx.channel().config()).getHandlePing();
if (handlePing) {
ctx.fireChannelRead(new RakPing(pingTime, packet.sender()));
return;
}

ByteBuf advertisement = ctx.channel().config().getOption(RakChannelOption.RAK_ADVERTISEMENT);
ByteBuf advertisement = ((RakServerChannelConfig) ctx.channel().config()).getAdvertisement();

int packetLength = 35 + (advertisement != null ? advertisement.readableBytes() : -2);

Expand Down Expand Up @@ -163,7 +163,7 @@ private void onOpenConnectionRequest1(ChannelHandlerContext ctx, DatagramPacket
// 1 (Packet ID), (Magic), 1 (Protocol Version), 20/40 (IP Header)
int mtu = buffer.readableBytes() + 1 + magicBuf.readableBytes() + 1 + (sender.getAddress() instanceof Inet6Address ? 40 : 20) + UDP_HEADER_SIZE;

int[] supportedProtocols = ctx.channel().config().getOption(RakChannelOption.RAK_SUPPORTED_PROTOCOLS);
int[] supportedProtocols = ((RakServerChannelConfig) ctx.channel().config()).getSupportedProtocols();
if (supportedProtocols != null && Arrays.binarySearch(supportedProtocols, protocolVersion) < 0) {
int latestVersion = supportedProtocols[supportedProtocols.length - 1];
this.sendIncompatibleVersion(ctx, packet.sender(), latestVersion, magicBuf, guid);
Expand All @@ -174,7 +174,7 @@ private void onOpenConnectionRequest1(ChannelHandlerContext ctx, DatagramPacket
// TODO: max connections check?


boolean sendCookie = ctx.channel().config().getOption(RakChannelOption.RAK_SEND_COOKIE);
boolean sendCookie = ((RakServerChannelConfig) ctx.channel().config()).getSendCookie();
int cookie;

if (sendCookie) {
Expand All @@ -198,7 +198,8 @@ private void onOpenConnectionRequest1(ChannelHandlerContext ctx, DatagramPacket
if (sendCookie) {
replyBuffer.writeInt(cookie);
}
replyBuffer.writeShort(RakUtils.clamp(mtu, ctx.channel().config().getOption(RakChannelOption.RAK_MIN_MTU), ctx.channel().config().getOption(RakChannelOption.RAK_MAX_MTU)));
replyBuffer.writeShort(RakUtils.clamp(mtu, ((RakServerChannelConfig) ctx.channel().config()).getMinMtu(),
((RakServerChannelConfig) ctx.channel().config()).getMaxMtu()));
ctx.writeAndFlush(new DatagramPacket(replyBuffer, sender));
}

Expand All @@ -212,19 +213,20 @@ private void onOpenConnectionRequest2(ChannelHandlerContext ctx, DatagramPacket
PendingConnection connection = this.pendingConnections.remove(sender);
if (connection == null) {
if (log.isTraceEnabled()) {
log.trace("Received open connection request 2 from {} without open connection request 1", sender);
log.trace("[{}] Received ID_OPEN_CONNECTION_REQUEST_2 without open connection request 1", sender);
}
// Don't respond yet as we cannot verify the connection source IP
return;
}

boolean sendCookie = ctx.channel().config().getOption(RakChannelOption.RAK_SEND_COOKIE);
boolean sendCookie = ((RakServerChannelConfig) ctx.channel().config()).getSendCookie();
if (sendCookie) {
int cookie = buffer.readInt();
int expectedCookie = connection.getCookie();
if (expectedCookie != cookie) {
if (log.isTraceEnabled()) {
log.trace("Received open connection request 2 from {} with invalid cookie (expected {}, but received {})", sender, expectedCookie, cookie);
log.trace("[{}] Received ID_OPEN_CONNECTION_REQUEST_2 with invalid cookie (expected {}, but received {})",
sender, expectedCookie, cookie);
}
// Incorrect cookie provided
// This is likely source IP spoofing so we will not reply
Expand All @@ -238,7 +240,13 @@ private void onOpenConnectionRequest2(ChannelHandlerContext ctx, DatagramPacket
int mtu = buffer.readUnsignedShort();
long clientGuid = buffer.readLong();

if (mtu < ctx.channel().config().getOption(RakChannelOption.RAK_MIN_MTU) || mtu > ctx.channel().config().getOption(RakChannelOption.RAK_MAX_MTU)) {
int minMtu = ((RakServerChannelConfig) ctx.channel().config()).getMinMtu();
int maxMtu = ((RakServerChannelConfig) ctx.channel().config()).getMaxMtu();
if (mtu < minMtu || mtu > maxMtu) {
if (log.isTraceEnabled()) {
log.trace("[{}] Received ID_OPEN_CONNECTION_REQUEST_2, but the MTU was {}. Expecting a value between {} - {}",
sender, mtu, minMtu, maxMtu);
}
// The client should have already negotiated a valid MTU
this.sendAlreadyConnected(ctx, sender, magicBuf, guid);
return;
Expand All @@ -247,6 +255,10 @@ private void onOpenConnectionRequest2(ChannelHandlerContext ctx, DatagramPacket
RakServerChannel serverChannel = (RakServerChannel) ctx.channel();
RakChildChannel channel = serverChannel.createChildChannel(sender, clientGuid, connection.getProtocolVersion(), mtu);
if (channel == null) {
if (log.isTraceEnabled()) {
log.trace("[{}] Received ID_OPEN_CONNECTION_REQUEST_2, but a channel already exists for this socket address",
sender);
}
// Already connected
this.sendAlreadyConnected(ctx, sender, magicBuf, guid);
return;
Expand Down

0 comments on commit e241646

Please sign in to comment.