From 1380f33bc8e5fdf1dd468c012cf8194035910f36 Mon Sep 17 00:00:00 2001 From: Michel Elkenwaat Date: Thu, 16 Nov 2023 16:49:52 +0100 Subject: [PATCH 1/4] fix: optimize ConcurrentHashMap usage in Fallback --- .../main/java/xyz/jonesdev/sonar/api/fallback/Fallback.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sonar-api/src/main/java/xyz/jonesdev/sonar/api/fallback/Fallback.java b/sonar-api/src/main/java/xyz/jonesdev/sonar/api/fallback/Fallback.java index dcd8a60df..b4aaa2c1d 100644 --- a/sonar-api/src/main/java/xyz/jonesdev/sonar/api/fallback/Fallback.java +++ b/sonar-api/src/main/java/xyz/jonesdev/sonar/api/fallback/Fallback.java @@ -40,11 +40,10 @@ public final class Fallback { public static final Fallback INSTANCE = new Fallback(); private static final @NotNull Sonar SONAR = Objects.requireNonNull(Sonar.get()); - private final Map connected = new ConcurrentHashMap<>(); + private final Map connected = new ConcurrentHashMap<>(64, 0.75f); // Only block the player for a few minutes to avoid issues private final ExpiringCache blacklisted = Cappuccino.buildExpiring( - 10L, TimeUnit.MINUTES, 5000L - ); + 10L, TimeUnit.MINUTES, 5000L); private final @NotNull FallbackQueue queue = FallbackQueue.INSTANCE; private final @NotNull FallbackRatelimiter ratelimiter = FallbackRatelimiter.INSTANCE; From 4212d0d21250ae339cdebc130dcaab83b422941d Mon Sep 17 00:00:00 2001 From: Michel Elkenwaat Date: Thu, 16 Nov 2023 16:50:22 +0100 Subject: [PATCH 2/4] feat: remove max-verifying-players option from config --- .../jonesdev/sonar/api/config/SonarConfiguration.java | 6 ------ .../sonar/bungee/fallback/FallbackInitialHandler.java | 7 ------- .../sonar/velocity/fallback/FallbackListener.java | 10 ---------- 3 files changed, 23 deletions(-) diff --git a/sonar-api/src/main/java/xyz/jonesdev/sonar/api/config/SonarConfiguration.java b/sonar-api/src/main/java/xyz/jonesdev/sonar/api/config/SonarConfiguration.java index 4ebc2c5db..e34def691 100644 --- a/sonar-api/src/main/java/xyz/jonesdev/sonar/api/config/SonarConfiguration.java +++ b/sonar-api/src/main/java/xyz/jonesdev/sonar/api/config/SonarConfiguration.java @@ -131,7 +131,6 @@ public enum Gamemode { private int maxBrandLength; private int maxMovementTicks; private int maxIgnoredTicks; - private int maxVerifyingPlayers; private int maxLoginPackets; private int maxPing; private int readTimeout; @@ -480,11 +479,6 @@ public void load() { "Amount of time that has to pass before a player times out"); verification.readTimeout = clamp(generalConfig.getInt("verification.read-timeout", 3500), 500, 30000); - generalConfig.getYaml().setComment("verification.max-players", - "Maximum number of players verifying at the same time"); - verification.maxVerifyingPlayers = clamp(generalConfig.getInt("verification.max-players", 1024), 1, - Short.MAX_VALUE); - generalConfig.getYaml().setComment("verification.rejoin-delay", "Minimum number of rejoin delay during verification"); verification.reconnectDelay = clamp(generalConfig.getInt("verification.rejoin-delay", 8000), 0, 100000); diff --git a/sonar-bungee/src/main/java/xyz/jonesdev/sonar/bungee/fallback/FallbackInitialHandler.java b/sonar-bungee/src/main/java/xyz/jonesdev/sonar/bungee/fallback/FallbackInitialHandler.java index 0bf207ed8..15a46fcb1 100644 --- a/sonar-bungee/src/main/java/xyz/jonesdev/sonar/bungee/fallback/FallbackInitialHandler.java +++ b/sonar-bungee/src/main/java/xyz/jonesdev/sonar/bungee/fallback/FallbackInitialHandler.java @@ -215,13 +215,6 @@ public void handle(final LoginRequest loginRequest) throws Exception { return; } - // We cannot allow too many players on our Fallback server - // There's technically no reason for limiting this, but we'll better stay safe. - if (FALLBACK.getConnected().size() > Sonar.get().getConfig().getVerification().getMaxVerifyingPlayers()) { - closeWith(getKickPacket(Sonar.get().getConfig().getVerification().getTooManyPlayers())); - return; - } - // Check if the IP address is currently being rate-limited if (!FALLBACK.getRatelimiter().attempt(inetAddress)) { closeWith(getKickPacket(Sonar.get().getConfig().getVerification().getTooFastReconnect())); diff --git a/sonar-velocity/src/main/java/xyz/jonesdev/sonar/velocity/fallback/FallbackListener.java b/sonar-velocity/src/main/java/xyz/jonesdev/sonar/velocity/fallback/FallbackListener.java index 68c655520..aad208c4a 100644 --- a/sonar-velocity/src/main/java/xyz/jonesdev/sonar/velocity/fallback/FallbackListener.java +++ b/sonar-velocity/src/main/java/xyz/jonesdev/sonar/velocity/fallback/FallbackListener.java @@ -220,16 +220,6 @@ public void handle(final @NotNull PreLoginEvent event) throws Throwable { return; } - // We cannot allow too many players on our Fallback server - // There's technically no reason for limiting this, but we'll better stay safe. - if (fallback.getConnected().size() > Sonar.get().getConfig().getVerification().getMaxVerifyingPlayers()) { - initialConnection.getConnection().closeWith(Disconnect.create( - Sonar.get().getConfig().getVerification().getTooManyPlayers(), - inboundConnection.getProtocolVersion() - )); - return; - } - // Check if the IP address is currently being rate-limited if (!fallback.getRatelimiter().attempt(inetAddress)) { initialConnection.getConnection().closeWith(Disconnect.create( From 2ab4e35981b46cd94e5a7a952ee4660eb35b3928 Mon Sep 17 00:00:00 2001 From: Michel Elkenwaat Date: Thu, 16 Nov 2023 16:54:33 +0100 Subject: [PATCH 3/4] fix: use hashset for whitelist and blacklisted protocol ids --- .../xyz/jonesdev/sonar/api/config/SonarConfiguration.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sonar-api/src/main/java/xyz/jonesdev/sonar/api/config/SonarConfiguration.java b/sonar-api/src/main/java/xyz/jonesdev/sonar/api/config/SonarConfiguration.java index e34def691..7a69b5673 100644 --- a/sonar-api/src/main/java/xyz/jonesdev/sonar/api/config/SonarConfiguration.java +++ b/sonar-api/src/main/java/xyz/jonesdev/sonar/api/config/SonarConfiguration.java @@ -135,8 +135,8 @@ public enum Gamemode { private int maxPing; private int readTimeout; private int reconnectDelay; - private final Collection whitelistedProtocols = new Vector<>(0); - private final Collection blacklistedProtocols = new Vector<>(0); + private final Collection whitelistedProtocols = new HashSet<>(0); + private final Collection blacklistedProtocols = new HashSet<>(0); private Component tooManyPlayers; private Component tooFastReconnect; From 39e564e0a09323ac9955fe31f4c4a5f8090fb6ff Mon Sep 17 00:00:00 2001 From: Michel Elkenwaat Date: Thu, 16 Nov 2023 16:56:05 +0100 Subject: [PATCH 4/4] fix: yaml configuration formatting error on empty collections --- .../xyz/jonesdev/sonar/api/config/SonarConfiguration.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sonar-api/src/main/java/xyz/jonesdev/sonar/api/config/SonarConfiguration.java b/sonar-api/src/main/java/xyz/jonesdev/sonar/api/config/SonarConfiguration.java index 7a69b5673..a58a77e66 100644 --- a/sonar-api/src/main/java/xyz/jonesdev/sonar/api/config/SonarConfiguration.java +++ b/sonar-api/src/main/java/xyz/jonesdev/sonar/api/config/SonarConfiguration.java @@ -489,12 +489,12 @@ public void load() { + LINE_SEPARATOR + "https://wiki.vg/Protocol_version_numbers" + LINE_SEPARATOR + "For example, Minecraft 1.20 has the ID 763."); verification.whitelistedProtocols.clear(); - verification.whitelistedProtocols.addAll(generalConfig.getIntList("verification.whitelisted-protocols", Collections.emptyList())); + verification.whitelistedProtocols.addAll(generalConfig.getIntList("verification.whitelisted-protocols", new ArrayList<>(0))); generalConfig.getYaml().setComment("verification.blacklisted-protocols", "List of protocol IDs which are unable to join the server at all"); verification.blacklistedProtocols.clear(); - verification.blacklistedProtocols.addAll(generalConfig.getIntList("verification.blacklisted-protocols", Collections.emptyList())); + verification.blacklistedProtocols.addAll(generalConfig.getIntList("verification.blacklisted-protocols", new ArrayList<>(0))); generalConfig.getYaml().setComment("webhook", "Bot attack notifications can also be sent to your Discord server using webhooks");