From f1b780c39865479431669b7ea56f7c5d40a46bfe Mon Sep 17 00:00:00 2001 From: games647 Date: Sat, 11 May 2024 11:15:13 +0200 Subject: [PATCH] Clarify pending logins methods --- .../fastlogin/bukkit/FastLoginBukkit.java | 22 ++++++++++++++----- .../listener/protocollib/NameCheckTask.java | 2 +- .../ProtocolSupportListener.java | 2 +- .../fastlogin/bungee/FastLoginBungee.java | 2 +- .../bungee/task/AsyncPremiumCheck.java | 2 +- .../games647/fastlogin/core/CommonUtil.java | 8 +++---- .../fastlogin/core/shared/FastLoginCore.java | 20 +++++++++++++---- .../fastlogin/core/shared/JoinManagement.java | 7 +----- .../fastlogin/velocity/FastLoginVelocity.java | 2 +- .../velocity/task/AsyncPremiumCheck.java | 2 +- 10 files changed, 43 insertions(+), 26 deletions(-) diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/FastLoginBukkit.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/FastLoginBukkit.java index 2caaaed7..2f58615d 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/FastLoginBukkit.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/FastLoginBukkit.java @@ -36,6 +36,7 @@ import com.github.games647.fastlogin.bukkit.listener.protocolsupport.ProtocolSup import com.github.games647.fastlogin.bukkit.task.DelayedAuthHook; import com.github.games647.fastlogin.core.CommonUtil; import com.github.games647.fastlogin.core.PremiumStatus; +import com.github.games647.fastlogin.core.antibot.AntiBotService; import com.github.games647.fastlogin.core.hooks.bedrock.BedrockService; import com.github.games647.fastlogin.core.hooks.bedrock.FloodgateService; import com.github.games647.fastlogin.core.hooks.bedrock.GeyserService; @@ -53,6 +54,7 @@ import org.slf4j.Logger; import java.net.InetSocketAddress; import java.nio.file.Path; +import java.time.Duration; import java.util.Map; import java.util.Optional; import java.util.UUID; @@ -65,7 +67,10 @@ import java.util.concurrent.ConcurrentMap; public class FastLoginBukkit extends JavaPlugin implements PlatformPlugin { //1 minutes should be enough as a timeout for bad internet connection (Server, Client and Mojang) - private final ConcurrentMap loginSession = CommonUtil.buildCache(1, -1); + private final ConcurrentMap loginSession = CommonUtil.buildCache( + Duration.ofMinutes(1), -1 + ); + private final Map premiumPlayers = new ConcurrentHashMap<>(); private final Logger logger; @@ -111,10 +116,11 @@ public class FastLoginBukkit extends JavaPlugin implements PlatformPlugin c.setExecutor(new PremiumCommand(this))); - Optional.ofNullable(getCommand("cracked")).ifPresent(c -> c.setExecutor(new CrackedCommand(this))); + registerCommands(); if (pluginManager.isPluginEnabled("PlaceholderAPI")) { premiumPlaceholder = new PremiumPlaceholder(this); @@ -147,6 +151,12 @@ public class FastLoginBukkit extends JavaPlugin implements PlatformPlugin c.setExecutor(new PremiumCommand(this))); + Optional.ofNullable(getCommand("cracked")).ifPresent(c -> c.setExecutor(new CrackedCommand(this))); + } + private boolean initializeFloodgate() { if (getServer().getPluginManager().getPlugin("Geyser-Spigot") != null) { geyserService = new GeyserService(GeyserImpl.getInstance(), core); diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/NameCheckTask.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/NameCheckTask.java index 4c13c386..05c23e62 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/NameCheckTask.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/NameCheckTask.java @@ -97,7 +97,7 @@ public class NameCheckTask extends JoinManagement ConcurrentMap buildCache(int expireAfterWrite, int maxSize) { + public static ConcurrentMap buildCache(Duration expireAfterWrite, int maxSize) { CacheBuilder builder = CacheBuilder.newBuilder(); - if (expireAfterWrite > 0) { - builder.expireAfterWrite(expireAfterWrite, TimeUnit.MINUTES); + if (expireAfterWrite != null) { + builder.expireAfterWrite(expireAfterWrite); } if (maxSize > 0) { diff --git a/core/src/main/java/com/github/games647/fastlogin/core/shared/FastLoginCore.java b/core/src/main/java/com/github/games647/fastlogin/core/shared/FastLoginCore.java index bfec89ff..c1fca7db 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/shared/FastLoginCore.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/shared/FastLoginCore.java @@ -55,6 +55,7 @@ import java.net.Proxy.Type; import java.net.UnknownHostException; import java.nio.file.Files; import java.nio.file.Path; +import java.time.Duration; import java.util.Collection; import java.util.HashSet; import java.util.Map; @@ -78,7 +79,10 @@ public class FastLoginCore

> { private static final long MAX_EXPIRE_RATE = 1_000_000; private final Map localeMessages = new ConcurrentHashMap<>(); - private final ConcurrentMap pendingLogin = CommonUtil.buildCache(5, -1); + private final ConcurrentMap pendingLogin = CommonUtil.buildCache( + Duration.ofMinutes(5), -1 + ); + private final Collection pendingConfirms = new HashSet<>(); private final T plugin; @@ -272,8 +276,16 @@ public class FastLoginCore

> { this.passwordGenerator = passwordGenerator; } - public ConcurrentMap getPendingLogin() { - return pendingLogin; + public void addLoginAttempt(String ip, String username) { + pendingLogin.put(ip + username, new Object()); + } + + public boolean hasFailedLogin(String ip, String username) { + if (!config.get("secondAttemptCracked", false)) { + return false; + } + + return pendingLogin.remove(ip + username) != null; } public Collection getPendingConfirms() { @@ -284,7 +296,7 @@ public class FastLoginCore

> { return authPlugin; } - public AntiBotService getAntiBot() { + public AntiBotService getAntiBotService() { return antiBot; } diff --git a/core/src/main/java/com/github/games647/fastlogin/core/shared/JoinManagement.java b/core/src/main/java/com/github/games647/fastlogin/core/shared/JoinManagement.java index 539ab247..aca74e0c 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/shared/JoinManagement.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/shared/JoinManagement.java @@ -48,8 +48,6 @@ public abstract class JoinManagement

{ } public void onLogin(String username, S source) { - core.getPlugin().getLog().info("Handling player {}", username); - //check if the player is connecting through Bedrock Edition if (bedrockService != null && bedrockService.isBedrockConnection(username)) { //perform Bedrock specific checks and skip Java checks if no longer needed @@ -59,7 +57,6 @@ public abstract class JoinManagement

{ } StoredProfile profile = core.getStorage().loadProfile(username); - //can't be a premium Java player, if it's not saved in the database if (profile == null) { return; @@ -78,7 +75,6 @@ public abstract class JoinManagement

{ } callFastLoginPreLoginEvent(username, source, profile); - Configuration config = core.getConfig(); String ip = source.getAddress().getAddress().getHostAddress(); @@ -94,7 +90,7 @@ public abstract class JoinManagement

{ } } } else { - if (core.getPendingLogin().remove(ip + username) != null && config.get("secondAttemptCracked", false)) { + if (core.hasFailedLogin(ip, username)) { core.getPlugin().getLog().info("Second attempt login -> cracked {}", username); //first login request failed so make a cracked session @@ -124,7 +120,6 @@ public abstract class JoinManagement

{ + " server issued more than 600 Name -> UUID requests within 10 minutes. After those 10" + " minutes we can make requests again.", username); } catch (Exception ex) { - core.getPlugin().getLog().error("Failed to check premium state for {}", username, ex); core.getPlugin().getLog().error("Failed to check premium state of {}", username, ex); } } diff --git a/velocity/src/main/java/com/github/games647/fastlogin/velocity/FastLoginVelocity.java b/velocity/src/main/java/com/github/games647/fastlogin/velocity/FastLoginVelocity.java index 71c76076..2dd1f2a4 100644 --- a/velocity/src/main/java/com/github/games647/fastlogin/velocity/FastLoginVelocity.java +++ b/velocity/src/main/java/com/github/games647/fastlogin/velocity/FastLoginVelocity.java @@ -109,7 +109,7 @@ public class FastLoginVelocity implements PlatformPlugin { geyserService = new GeyserService(GeyserImpl.getInstance(), core); } - server.getEventManager().register(this, new ConnectListener(this, core.getAntiBot())); + server.getEventManager().register(this, new ConnectListener(this, core.getAntiBotService())); server.getEventManager().register(this, new PluginMessageListener(this)); ChannelRegistrar channelRegistry = server.getChannelRegistrar(); diff --git a/velocity/src/main/java/com/github/games647/fastlogin/velocity/task/AsyncPremiumCheck.java b/velocity/src/main/java/com/github/games647/fastlogin/velocity/task/AsyncPremiumCheck.java index 7eb7e9b3..28d26eff 100644 --- a/velocity/src/main/java/com/github/games647/fastlogin/velocity/task/AsyncPremiumCheck.java +++ b/velocity/src/main/java/com/github/games647/fastlogin/velocity/task/AsyncPremiumCheck.java @@ -85,7 +85,7 @@ public class AsyncPremiumCheck extends JoinManagement