mirror of
https://github.com/PaperMC/Waterfall.git
synced 2025-02-10 17:01:22 +01:00
226 lines
8.9 KiB
Diff
226 lines
8.9 KiB
Diff
From 4ee0d7b23287f5d557f54cb1072fc3ab9f931182 Mon Sep 17 00:00:00 2001
|
|
From: Shane Freeder <theboyetronic@gmail.com>
|
|
Date: Mon, 25 Nov 2019 19:54:06 +0000
|
|
Subject: [PATCH] Speed up some common exceptions
|
|
|
|
|
|
diff --git a/api/src/main/java/io/github/waterfallmc/waterfall/utils/FastException.java b/api/src/main/java/io/github/waterfallmc/waterfall/utils/FastException.java
|
|
new file mode 100644
|
|
index 00000000..11e103cb
|
|
--- /dev/null
|
|
+++ b/api/src/main/java/io/github/waterfallmc/waterfall/utils/FastException.java
|
|
@@ -0,0 +1,19 @@
|
|
+package io.github.waterfallmc.waterfall.utils;
|
|
+
|
|
+// This is basically a copy of QuietException
|
|
+public class FastException extends RuntimeException {
|
|
+
|
|
+ public FastException(String message) {
|
|
+ super(message);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public synchronized Throwable initCause(Throwable cause) {
|
|
+ return this;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public synchronized Throwable fillInStackTrace() {
|
|
+ return this;
|
|
+ }
|
|
+}
|
|
diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/BadPacketException.java b/protocol/src/main/java/net/md_5/bungee/protocol/BadPacketException.java
|
|
index 6c0ef4df..076ddd70 100644
|
|
--- a/protocol/src/main/java/net/md_5/bungee/protocol/BadPacketException.java
|
|
+++ b/protocol/src/main/java/net/md_5/bungee/protocol/BadPacketException.java
|
|
@@ -12,4 +12,24 @@ public class BadPacketException extends RuntimeException
|
|
{
|
|
super( message, cause );
|
|
}
|
|
+
|
|
+ // Waterfall start
|
|
+ @Override
|
|
+ public Throwable initCause(Throwable cause)
|
|
+ {
|
|
+ if (DefinedPacket.PROCESS_TRACES) {
|
|
+ return super.initCause(cause);
|
|
+ }
|
|
+ return this;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public Throwable fillInStackTrace()
|
|
+ {
|
|
+ if (DefinedPacket.PROCESS_TRACES) {
|
|
+ return super.fillInStackTrace();
|
|
+ }
|
|
+ return this;
|
|
+ }
|
|
+ // Waterfall end
|
|
}
|
|
diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/DefinedPacket.java b/protocol/src/main/java/net/md_5/bungee/protocol/DefinedPacket.java
|
|
index 6cd9e6a4..dffd3b7a 100644
|
|
--- a/protocol/src/main/java/net/md_5/bungee/protocol/DefinedPacket.java
|
|
+++ b/protocol/src/main/java/net/md_5/bungee/protocol/DefinedPacket.java
|
|
@@ -47,6 +47,9 @@ public abstract class DefinedPacket
|
|
}
|
|
}
|
|
|
|
+ public static final boolean PROCESS_TRACES = Boolean.getBoolean("waterfall.bad-packet-traces");
|
|
+ private static final OverflowPacketException OVERSIZED_VAR_INT_EXCEPTION = new OverflowPacketException( "VarInt too big" );
|
|
+ private static final BadPacketException NO_MORE_BYTES_EXCEPTION = new BadPacketException("No more bytes reading varint");
|
|
public static void writeString(String s, ByteBuf buf)
|
|
{
|
|
writeString( s, buf, Short.MAX_VALUE );
|
|
@@ -242,13 +245,18 @@ public abstract class DefinedPacket
|
|
byte in;
|
|
while ( true )
|
|
{
|
|
+ // Waterfall start
|
|
+ if (input.readableBytes() == 0) {
|
|
+ throw PROCESS_TRACES ? new BadPacketException("No more bytes reading varint") : NO_MORE_BYTES_EXCEPTION;
|
|
+ }
|
|
+ // Waterfall end
|
|
in = input.readByte();
|
|
|
|
out |= ( in & 0x7F ) << ( bytes++ * 7 );
|
|
|
|
if ( bytes > maxBytes )
|
|
{
|
|
- throw new OverflowPacketException( "VarInt too big (max " + maxBytes + ")" );
|
|
+ throw PROCESS_TRACES ? new OverflowPacketException( "VarInt too big (max " + maxBytes + ")" ) : OVERSIZED_VAR_INT_EXCEPTION;
|
|
}
|
|
|
|
if ( ( in & 0x80 ) != 0x80 )
|
|
diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/FastDecoderException.java b/protocol/src/main/java/net/md_5/bungee/protocol/FastDecoderException.java
|
|
new file mode 100644
|
|
index 00000000..2583aa2c
|
|
--- /dev/null
|
|
+++ b/protocol/src/main/java/net/md_5/bungee/protocol/FastDecoderException.java
|
|
@@ -0,0 +1,26 @@
|
|
+package net.md_5.bungee.protocol;
|
|
+
|
|
+import io.netty.handler.codec.DecoderException;
|
|
+
|
|
+public class FastDecoderException extends DecoderException {
|
|
+
|
|
+ public FastDecoderException(String message, Throwable cause) {
|
|
+ super(message, cause);
|
|
+ }
|
|
+
|
|
+ public FastDecoderException(String message) {
|
|
+ super(message);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public Throwable initCause(Throwable cause)
|
|
+ {
|
|
+ return this;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public Throwable fillInStackTrace()
|
|
+ {
|
|
+ return this;
|
|
+ }
|
|
+}
|
|
diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftDecoder.java b/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftDecoder.java
|
|
index 655bcd46..52f76cd9 100644
|
|
--- a/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftDecoder.java
|
|
+++ b/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftDecoder.java
|
|
@@ -80,7 +80,7 @@ public class MinecraftDecoder extends MessageToMessageDecoder<ByteBuf>
|
|
} else {
|
|
packetTypeStr = "unknown";
|
|
}
|
|
- throw new DecoderException("Error decoding packet " + packetTypeStr + " with contents:\n" + ByteBufUtil.prettyHexDump(slice), e);
|
|
+ throw new FastDecoderException("Error decoding packet " + packetTypeStr + " with contents:\n" + ByteBufUtil.prettyHexDump(slice), e); // Waterfall
|
|
} finally
|
|
{
|
|
if ( slice != null )
|
|
diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/OverflowPacketException.java b/protocol/src/main/java/net/md_5/bungee/protocol/OverflowPacketException.java
|
|
index 237955ab..d0bd4d75 100644
|
|
--- a/protocol/src/main/java/net/md_5/bungee/protocol/OverflowPacketException.java
|
|
+++ b/protocol/src/main/java/net/md_5/bungee/protocol/OverflowPacketException.java
|
|
@@ -2,9 +2,28 @@ package net.md_5.bungee.protocol;
|
|
|
|
public class OverflowPacketException extends RuntimeException
|
|
{
|
|
-
|
|
public OverflowPacketException(String message)
|
|
{
|
|
super( message );
|
|
}
|
|
+
|
|
+ // Waterfall start
|
|
+ @Override
|
|
+ public Throwable initCause(Throwable cause)
|
|
+ {
|
|
+ if (DefinedPacket.PROCESS_TRACES) {
|
|
+ return super.initCause(cause);
|
|
+ }
|
|
+ return this;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public Throwable fillInStackTrace()
|
|
+ {
|
|
+ if (DefinedPacket.PROCESS_TRACES) {
|
|
+ return super.fillInStackTrace();
|
|
+ }
|
|
+ return this;
|
|
+ }
|
|
+ // Waterfall end
|
|
}
|
|
diff --git a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java
|
|
index d6872035..a7241527 100644
|
|
--- a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java
|
|
+++ b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java
|
|
@@ -23,6 +23,8 @@ import javax.crypto.SecretKey;
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.Data;
|
|
import lombok.EqualsAndHashCode;
|
|
+import javax.crypto.spec.SecretKeySpec;
|
|
+
|
|
import lombok.Getter;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.ToString;
|
|
@@ -518,6 +520,14 @@ public class InitialHandler extends PacketHandler implements PendingConnection
|
|
Preconditions.checkState( EncryptionUtil.check( loginRequest.getPublicKey(), encryptResponse, request ), "Invalid verification" );
|
|
|
|
SecretKey sharedKey = EncryptionUtil.getSecret( encryptResponse, request );
|
|
+ // Waterfall start
|
|
+ if (sharedKey instanceof SecretKeySpec) {
|
|
+ if (sharedKey.getEncoded().length != 16) {
|
|
+ this.ch.close();
|
|
+ return;
|
|
+ }
|
|
+ }
|
|
+ // Waterfall end
|
|
BungeeCipher decrypt = EncryptionUtil.getCipher( false, sharedKey );
|
|
ch.addBefore( PipelineUtils.FRAME_DECODER, PipelineUtils.DECRYPT_HANDLER, new CipherDecoder( decrypt ) );
|
|
BungeeCipher encrypt = EncryptionUtil.getCipher( true, sharedKey );
|
|
diff --git a/query/src/main/java/net/md_5/bungee/query/QueryHandler.java b/query/src/main/java/net/md_5/bungee/query/QueryHandler.java
|
|
index ac99d02c..0c1ecfb8 100644
|
|
--- a/query/src/main/java/net/md_5/bungee/query/QueryHandler.java
|
|
+++ b/query/src/main/java/net/md_5/bungee/query/QueryHandler.java
|
|
@@ -32,6 +32,7 @@ public class QueryHandler extends SimpleChannelInboundHandler<DatagramPacket>
|
|
/*========================================================================*/
|
|
private final Random random = new Random();
|
|
private final Cache<InetAddress, QuerySession> sessions = CacheBuilder.newBuilder().expireAfterWrite( 30, TimeUnit.SECONDS ).build();
|
|
+ private static io.github.waterfallmc.waterfall.utils.FastException cachedNoSessionException = new io.github.waterfallmc.waterfall.utils.FastException("No Session!");
|
|
|
|
private void writeShort(ByteBuf buf, int s)
|
|
{
|
|
@@ -96,7 +97,7 @@ public class QueryHandler extends SimpleChannelInboundHandler<DatagramPacket>
|
|
QuerySession session = sessions.getIfPresent( msg.sender().getAddress() );
|
|
if ( session == null || session.getToken() != challengeToken )
|
|
{
|
|
- throw new IllegalStateException( "No session!" );
|
|
+ throw cachedNoSessionException; // Waterfall
|
|
}
|
|
|
|
// Waterfall start
|
|
--
|
|
2.47.0
|
|
|