From 0b709997a4dea53f6f4a4d7c15c58d48149dbdd6 Mon Sep 17 00:00:00 2001 From: games647 Date: Tue, 7 Jun 2016 16:46:18 +0200 Subject: [PATCH] Fix duplicate premium uuid check in BungeeCord --- .../fastlogin/bungee/AsyncPremiumCheck.java | 70 -------------- .../listener/PlayerConnectionListener.java | 4 +- .../listener/PluginMessageListener.java | 5 +- .../bungee/tasks/AsyncPremiumCheck.java | 91 +++++++++++++++++++ .../AsyncToggleMessage.java} | 6 +- .../bungee/{ => tasks}/ForceLoginTask.java | 6 +- 6 files changed, 102 insertions(+), 80 deletions(-) delete mode 100644 bungee/src/main/java/com/github/games647/fastlogin/bungee/AsyncPremiumCheck.java create mode 100644 bungee/src/main/java/com/github/games647/fastlogin/bungee/tasks/AsyncPremiumCheck.java rename bungee/src/main/java/com/github/games647/fastlogin/bungee/{listener/AsyncStatusMessage.java => tasks/AsyncToggleMessage.java} (93%) rename bungee/src/main/java/com/github/games647/fastlogin/bungee/{ => tasks}/ForceLoginTask.java (96%) diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/AsyncPremiumCheck.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/AsyncPremiumCheck.java deleted file mode 100644 index 8a7019a0..00000000 --- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/AsyncPremiumCheck.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.github.games647.fastlogin.bungee; - -import com.github.games647.fastlogin.bungee.FastLoginBungee; -import com.github.games647.fastlogin.bungee.hooks.BungeeAuthPlugin; -import com.github.games647.fastlogin.core.LoginSession; -import com.github.games647.fastlogin.core.PlayerProfile; - -import java.util.UUID; -import java.util.logging.Level; - -import net.md_5.bungee.api.connection.PendingConnection; -import net.md_5.bungee.api.event.PreLoginEvent; - -public class AsyncPremiumCheck implements Runnable { - - private final FastLoginBungee plugin; - private final PreLoginEvent preLoginEvent; - - public AsyncPremiumCheck(FastLoginBungee plugin, PreLoginEvent preLoginEvent) { - this.plugin = plugin; - this.preLoginEvent = preLoginEvent; - } - - @Override - public void run() { - PendingConnection connection = preLoginEvent.getConnection(); - plugin.getSession().remove(connection); - - String username = connection.getName(); - try { - PlayerProfile profile = plugin.getCore().getStorage().loadProfile(username); - if (profile != null) { - if (profile.isPremium()) { - if (profile.getUserId() != -1) { - plugin.getSession().put(connection, new LoginSession(username, true, profile)); - connection.setOnlineMode(true); - } - } else if (profile.getUserId() == -1) { - //user not exists in the db - BungeeAuthPlugin authPlugin = plugin.getBungeeAuthPlugin(); - if (plugin.getConfiguration().getBoolean("nameChangeCheck")) { - UUID premiumUUID = plugin.getCore().getMojangApiConnector().getPremiumUUID(username); - if (premiumUUID != null) { - profile = plugin.getCore().getStorage().loadProfile(premiumUUID); - if (profile != null) { - plugin.getLogger().log(Level.FINER, "Player {0} changed it's username", premiumUUID); - connection.setOnlineMode(true); - plugin.getSession().put(connection, new LoginSession(username, false, profile)); - } - } - } - - if (plugin.getConfiguration().getBoolean("autoRegister") - && (authPlugin == null || !authPlugin.isRegistered(username))) { - UUID premiumUUID = plugin.getCore().getMojangApiConnector().getPremiumUUID(username); - if (premiumUUID != null) { - plugin.getLogger().log(Level.FINER, "Player {0} uses a premium username", username); - connection.setOnlineMode(true); - plugin.getSession().put(connection, new LoginSession(username, false, profile)); - } - } - } - } - } catch (Exception ex) { - plugin.getLogger().log(Level.SEVERE, "Failed to check premium state", ex); - } finally { - preLoginEvent.completeIntent(plugin); - } - } -} diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/PlayerConnectionListener.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/PlayerConnectionListener.java index 456a7c1b..4fddd05c 100644 --- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/PlayerConnectionListener.java +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/PlayerConnectionListener.java @@ -1,8 +1,8 @@ package com.github.games647.fastlogin.bungee.listener; -import com.github.games647.fastlogin.bungee.AsyncPremiumCheck; +import com.github.games647.fastlogin.bungee.tasks.AsyncPremiumCheck; import com.github.games647.fastlogin.bungee.FastLoginBungee; -import com.github.games647.fastlogin.bungee.ForceLoginTask; +import com.github.games647.fastlogin.bungee.tasks.ForceLoginTask; import com.github.games647.fastlogin.core.LoginSession; import com.github.games647.fastlogin.core.PlayerProfile; import com.google.common.base.Charsets; diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/PluginMessageListener.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/PluginMessageListener.java index 17f34c88..cea9a268 100644 --- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/PluginMessageListener.java +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/PluginMessageListener.java @@ -1,5 +1,6 @@ package com.github.games647.fastlogin.bungee.listener; +import com.github.games647.fastlogin.bungee.tasks.AsyncToggleMessage; import com.github.games647.fastlogin.bungee.FastLoginBungee; import com.github.games647.fastlogin.core.PlayerProfile; import com.google.common.io.ByteArrayDataInput; @@ -46,12 +47,12 @@ public class PluginMessageListener implements Listener { if ("ON".equals(subchannel)) { String playerName = dataInput.readUTF(); - AsyncStatusMessage task = new AsyncStatusMessage(plugin, fromPlayer, playerName, true); + AsyncToggleMessage task = new AsyncToggleMessage(plugin, fromPlayer, playerName, true); ProxyServer.getInstance().getScheduler().runAsync(plugin, task); } else if ("OFF".equals(subchannel)) { String playerName = dataInput.readUTF(); - AsyncStatusMessage task = new AsyncStatusMessage(plugin, fromPlayer, playerName, false); + AsyncToggleMessage task = new AsyncToggleMessage(plugin, fromPlayer, playerName, false); ProxyServer.getInstance().getScheduler().runAsync(plugin, task); } else if ("SUCCESS".equals(subchannel)) { if (fromPlayer.getPendingConnection().isOnlineMode()) { diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/tasks/AsyncPremiumCheck.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/tasks/AsyncPremiumCheck.java new file mode 100644 index 00000000..f62eb615 --- /dev/null +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/tasks/AsyncPremiumCheck.java @@ -0,0 +1,91 @@ +package com.github.games647.fastlogin.bungee.tasks; + +import com.github.games647.fastlogin.bungee.FastLoginBungee; +import com.github.games647.fastlogin.bungee.hooks.BungeeAuthPlugin; +import com.github.games647.fastlogin.core.LoginSession; +import com.github.games647.fastlogin.core.PlayerProfile; + +import java.util.UUID; +import java.util.logging.Level; + +import net.md_5.bungee.api.connection.PendingConnection; +import net.md_5.bungee.api.event.PreLoginEvent; + +public class AsyncPremiumCheck implements Runnable { + + private final FastLoginBungee plugin; + private final PreLoginEvent preLoginEvent; + + public AsyncPremiumCheck(FastLoginBungee plugin, PreLoginEvent preLoginEvent) { + this.plugin = plugin; + this.preLoginEvent = preLoginEvent; + } + + @Override + public void run() { + PendingConnection connection = preLoginEvent.getConnection(); + plugin.getSession().remove(connection); + + String username = connection.getName(); + try { + PlayerProfile profile = plugin.getCore().getStorage().loadProfile(username); + if (profile == null) { + return; + } + + if (profile.getUserId() == -1) { + UUID premiumUUID = null; + if (plugin.getConfiguration().getBoolean("nameChangeCheck") + || plugin.getConfiguration().getBoolean("autoRegister")) { + premiumUUID = plugin.getCore().getMojangApiConnector().getPremiumUUID(username); + } + + if (premiumUUID == null + || checkNameChange(premiumUUID, connection, username) + || checkPremiumName(username, connection, profile)) { + //nothing detected the player as premium -> start a cracked session + plugin.getSession().put(connection, new LoginSession(username, false, profile)); + } + } else if (profile.isPremium()) { + requestPremiumLogin(connection, profile, username, true); + } + } catch (Exception ex) { + plugin.getLogger().log(Level.SEVERE, "Failed to check premium state", ex); + } finally { + preLoginEvent.completeIntent(plugin); + } + } + + private boolean checkPremiumName(String username, PendingConnection connection, PlayerProfile profile) + throws Exception { + BungeeAuthPlugin authPlugin = plugin.getBungeeAuthPlugin(); + if (plugin.getConfiguration().getBoolean("autoRegister") + && (authPlugin == null || !authPlugin.isRegistered(username))) { + plugin.getLogger().log(Level.FINER, "Player {0} uses a premium username", username); + requestPremiumLogin(connection, profile, username, false); + return true; + } + + return false; + } + + private boolean checkNameChange(UUID premiumUUID, PendingConnection connection, String username) { + //user not exists in the db + if (plugin.getConfiguration().getBoolean("nameChangeCheck")) { + PlayerProfile profile = plugin.getCore().getStorage().loadProfile(premiumUUID); + if (profile != null) { + //uuid exists in the database + plugin.getLogger().log(Level.FINER, "Player {0} changed it's username", premiumUUID); + requestPremiumLogin(connection, profile, username, false); + return true; + } + } + + return false; + } + + private void requestPremiumLogin(PendingConnection con, PlayerProfile profile, String username, boolean register) { + con.setOnlineMode(true); + plugin.getSession().put(con, new LoginSession(username, register, profile)); + } +} diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/AsyncStatusMessage.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/tasks/AsyncToggleMessage.java similarity index 93% rename from bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/AsyncStatusMessage.java rename to bungee/src/main/java/com/github/games647/fastlogin/bungee/tasks/AsyncToggleMessage.java index 3a5d36bf..9d817966 100644 --- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/AsyncStatusMessage.java +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/tasks/AsyncToggleMessage.java @@ -1,4 +1,4 @@ -package com.github.games647.fastlogin.bungee.listener; +package com.github.games647.fastlogin.bungee.tasks; import com.github.games647.fastlogin.bungee.FastLoginBungee; import com.github.games647.fastlogin.core.PlayerProfile; @@ -7,14 +7,14 @@ import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.connection.ProxiedPlayer; -public class AsyncStatusMessage implements Runnable { +public class AsyncToggleMessage implements Runnable { private final FastLoginBungee plugin; private final ProxiedPlayer fromPlayer; private final String targetPlayer; private final boolean toPremium; - public AsyncStatusMessage(FastLoginBungee plugin, ProxiedPlayer fromPlayer, String targetPlayer + public AsyncToggleMessage(FastLoginBungee plugin, ProxiedPlayer fromPlayer, String targetPlayer , boolean toPremium) { this.plugin = plugin; this.fromPlayer = fromPlayer; diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/ForceLoginTask.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/tasks/ForceLoginTask.java similarity index 96% rename from bungee/src/main/java/com/github/games647/fastlogin/bungee/ForceLoginTask.java rename to bungee/src/main/java/com/github/games647/fastlogin/bungee/tasks/ForceLoginTask.java index 6eed0d3f..fe0d1113 100644 --- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/ForceLoginTask.java +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/tasks/ForceLoginTask.java @@ -1,5 +1,6 @@ -package com.github.games647.fastlogin.bungee; +package com.github.games647.fastlogin.bungee.tasks; +import com.github.games647.fastlogin.bungee.FastLoginBungee; import com.github.games647.fastlogin.bungee.hooks.BungeeAuthPlugin; import com.github.games647.fastlogin.core.LoginSession; import com.github.games647.fastlogin.core.PlayerProfile; @@ -7,8 +8,8 @@ import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteStreams; import java.util.UUID; -import net.md_5.bungee.api.connection.PendingConnection; +import net.md_5.bungee.api.connection.PendingConnection; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.Server; @@ -49,7 +50,6 @@ public class ForceLoginTask implements Runnable { } } else { //cracked player - //update only on success to prevent corrupt data playerProfile.setPremium(false); plugin.getCore().getStorage().save(playerProfile); }