From 68ded7ca7f9a9a5a5f4f5acf6ea0561ca28117cc Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 3 Aug 2018 16:56:19 -0400 Subject: [PATCH] Allow tweaking compression. --- .../proxy/config/VelocityConfiguration.java | 31 +++++++++++++++++-- .../proxy/connection/MinecraftConnection.java | 5 +-- .../client/LoginSessionHandler.java | 5 +-- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java b/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java index 04c9f3657..d128bc89b 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java @@ -29,12 +29,15 @@ public class VelocityConfiguration { private final IPForwardingMode ipForwardingMode; private final Map servers; private final List attemptConnectionOrder; + private final int compressionThreshold; + private final int compressionLevel; private Component motdAsComponent; private VelocityConfiguration(String bind, String motd, int showMaxPlayers, boolean onlineMode, IPForwardingMode ipForwardingMode, Map servers, - List attemptConnectionOrder) { + List attemptConnectionOrder, int compressionThreshold, + int compressionLevel) { this.bind = bind; this.motd = motd; this.showMaxPlayers = showMaxPlayers; @@ -42,6 +45,8 @@ public class VelocityConfiguration { this.ipForwardingMode = ipForwardingMode; this.servers = servers; this.attemptConnectionOrder = attemptConnectionOrder; + this.compressionThreshold = compressionThreshold; + this.compressionLevel = compressionLevel; } public boolean validate() { @@ -102,6 +107,18 @@ public class VelocityConfiguration { valid = false; } + if (compressionLevel < -1 || compressionLevel > 9) { + logger.error("Invalid compression level {}", compressionLevel); + } else if (compressionLevel == 0) { + logger.warn("ALL packets going through the proxy are going to be uncompressed. This will increase bandwidth usage."); + } + + if (compressionThreshold < -1) { + logger.error("Invalid compression threshold {}", compressionLevel); + } else if (compressionThreshold == 0) { + logger.warn("ALL packets going through the proxy are going to be compressed. This may hurt performance."); + } + return valid; } @@ -144,6 +161,14 @@ public class VelocityConfiguration { return attemptConnectionOrder; } + public int getCompressionThreshold() { + return compressionThreshold; + } + + public int getCompressionLevel() { + return compressionLevel; + } + @Override public String toString() { return "VelocityConfiguration{" + @@ -179,7 +204,9 @@ public class VelocityConfiguration { toml.getBoolean("online-mode"), IPForwardingMode.valueOf(toml.getString("ip-forwarding").toUpperCase()), ImmutableMap.copyOf(servers), - toml.getTable("servers").getList("try")); + toml.getTable("servers").getList("try"), + toml.getTable("advanced").getLong("compression-threshold", 1024L).intValue(), + toml.getTable("advanced").getLong("compression-level", -1L).intValue()); } } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java index fa8b51268..050d145d5 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java @@ -3,6 +3,7 @@ package com.velocitypowered.proxy.connection; import com.google.common.base.Preconditions; import com.velocitypowered.natives.compression.VelocityCompressor; import com.velocitypowered.natives.util.Natives; +import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.protocol.PacketWrapper; import com.velocitypowered.proxy.protocol.StateRegistry; import com.velocitypowered.natives.encryption.JavaVelocityCipher; @@ -20,7 +21,6 @@ import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.security.GeneralSecurityException; -import java.util.zip.Deflater; import static com.velocitypowered.network.Connections.CIPHER_DECODER; import static com.velocitypowered.network.Connections.CIPHER_ENCODER; @@ -193,7 +193,8 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter { return; } - VelocityCompressor compressor = Natives.compressor.get().create(Deflater.DEFAULT_COMPRESSION); + int level = VelocityServer.getServer().getConfiguration().getCompressionLevel(); + VelocityCompressor compressor = Natives.compressor.get().create(level); MinecraftCompressEncoder encoder = new MinecraftCompressEncoder(threshold, compressor); MinecraftCompressDecoder decoder = new MinecraftCompressDecoder(threshold, compressor); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java index 4362991f8..b4437e535 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java @@ -125,8 +125,9 @@ public class LoginSessionHandler implements MinecraftSessionHandler { return; } - inbound.write(new SetCompression(256)); - inbound.setCompressionThreshold(256); + int threshold = VelocityServer.getServer().getConfiguration().getCompressionThreshold(); + inbound.write(new SetCompression(threshold)); + inbound.setCompressionThreshold(threshold); ServerLoginSuccess success = new ServerLoginSuccess(); success.setUsername(profile.getName());