From 4858049c2a14145c540b82de0883e6ed2735a180 Mon Sep 17 00:00:00 2001 From: games647 Date: Sat, 14 Oct 2017 18:25:12 +0200 Subject: [PATCH] Use direct proxies instead of ssl factories for multiple IP-addresses --- CHANGELOG.md | 4 ++ .../bukkit/commands/CrackedCommand.java | 2 +- .../bukkit/hooks/LoginSecurityHook.java | 7 +- .../bukkit/tasks/DelayedAuthHook.java | 59 ++++++++++------- .../bungee/listener/ConnectListener.java | 2 +- .../bungee/tasks/AsyncToggleMessage.java | 2 +- .../bungee/tasks/ForceLoginTask.java | 1 - core/pom.xml | 2 +- .../games647/fastlogin/core/AuthStorage.java | 2 +- .../fastlogin/core/BalancedSSLFactory.java | 66 ------------------- .../fastlogin/core/PlayerProfile.java | 5 +- .../core/mojang/MojangApiConnector.java | 32 ++------- .../core/shared/ForceLoginManagement.java | 4 +- 13 files changed, 59 insertions(+), 129 deletions(-) delete mode 100644 core/src/main/java/com/github/games647/fastlogin/core/BalancedSSLFactory.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ff1bcd7..7049d90d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ### 1.11 +* Use direct proxies instead of ssl factories for multiple IP-addresses +* Remove local address check for multiple IP-addresses +* Fix parsing of local IP-addresses +* Fix address rotating for contacting the Mojang API * Optimize issue template * Use Instant for timestamps * Migrate SLF4J logging (Fixes #177) diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/commands/CrackedCommand.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/commands/CrackedCommand.java index 3fd15979..5923d590 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/commands/CrackedCommand.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/commands/CrackedCommand.java @@ -36,7 +36,7 @@ public class CrackedCommand implements CommandExecutor { plugin.getCore().sendLocaleMessage("remove-premium", sender); profile.setPremium(false); - profile.setUuid(null); + profile.setUUID(null); Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { plugin.getCore().getStorage().save(profile); }); diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/LoginSecurityHook.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/LoginSecurityHook.java index 61b4bff1..c2f822e5 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/LoginSecurityHook.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/LoginSecurityHook.java @@ -9,7 +9,6 @@ import com.lenis0012.bukkit.loginsecurity.session.action.LoginAction; import com.lenis0012.bukkit.loginsecurity.session.action.RegisterAction; import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; /** * Github: https://github.com/lenis0012/LoginSecurity-2 Project page: @@ -19,7 +18,11 @@ import org.bukkit.plugin.java.JavaPlugin; */ public class LoginSecurityHook implements AuthPlugin { - private final FastLoginBukkit plugin = JavaPlugin.getPlugin(FastLoginBukkit.class); + private final FastLoginBukkit plugin; + + public LoginSecurityHook(FastLoginBukkit plugin) { + this.plugin = plugin; + } @Override public boolean forceLogin(Player player) { diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/tasks/DelayedAuthHook.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/tasks/DelayedAuthHook.java index 78385500..78abbffb 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/tasks/DelayedAuthHook.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/tasks/DelayedAuthHook.java @@ -8,8 +8,9 @@ import com.github.games647.fastlogin.bukkit.hooks.LoginSecurityHook; import com.github.games647.fastlogin.bukkit.hooks.UltraAuthHook; import com.github.games647.fastlogin.bukkit.hooks.xAuthHook; import com.github.games647.fastlogin.core.hooks.AuthPlugin; -import com.google.common.collect.Lists; +import java.lang.reflect.Constructor; +import java.util.Arrays; import java.util.List; import org.bukkit.Bukkit; @@ -41,28 +42,7 @@ public class DelayedAuthHook implements Runnable { } private boolean registerHooks() { - AuthPlugin authPluginHook = null; - - try { - @SuppressWarnings("unchecked") - List>> supportedHooks = Lists.newArrayList(AuthMeHook.class - , CrazyLoginHook.class, LogItHook.class, LoginSecurityHook.class, UltraAuthHook.class - , xAuthHook.class); - - for (Class> clazz : supportedHooks) { - String pluginName = clazz.getSimpleName().replace("Hook", ""); - //uses only member classes which uses AuthPlugin interface (skip interfaces) - if (Bukkit.getPluginManager().isPluginEnabled(pluginName)) { - //check only for enabled plugins. A single plugin could be disabled by plugin managers - authPluginHook = clazz.newInstance(); - plugin.getLog().info("Hooking into auth plugin: {}", pluginName); - break; - } - } - } catch (InstantiationException | IllegalAccessException ex) { - plugin.getLog().error("Couldn't load the integration class", ex); - } - + AuthPlugin authPluginHook = getAuthHook(); if (authPluginHook == null) { //run this check for exceptions (errors) and not found plugins plugin.getLog().warn("No support offline Auth plugin found. "); @@ -70,9 +50,42 @@ public class DelayedAuthHook implements Runnable { } if (plugin.getCore().getAuthPluginHook() == null) { + plugin.getLog().info("Hooking into auth plugin: {}", authPluginHook.getClass().getSimpleName()); plugin.getCore().setAuthPluginHook(authPluginHook); } return true; } + + private AuthPlugin getAuthHook() { + try { + @SuppressWarnings("unchecked") + List>> hooks = Arrays.asList(AuthMeHook.class, + CrazyLoginHook.class, LogItHook.class, LoginSecurityHook.class, UltraAuthHook.class, + xAuthHook.class); + + for (Class> clazz : hooks) { + String pluginName = clazz.getSimpleName().replace("Hook", ""); + //uses only member classes which uses AuthPlugin interface (skip interfaces) + if (Bukkit.getPluginManager().isPluginEnabled(pluginName)) { + //check only for enabled plugins. A single plugin could be disabled by plugin managers + return newInstance(clazz); + } + } + } catch (ReflectiveOperationException ex) { + plugin.getLog().error("Couldn't load the integration class", ex); + } + + return null; + } + + private AuthPlugin newInstance(Class> clazz) + throws ReflectiveOperationException { + try { + Constructor> cons = clazz.getConstructor(FastLoginBukkit.class); + return cons.newInstance(plugin); + } catch (NoSuchMethodException noMethodEx) { + return clazz.newInstance(); + } + } } diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/ConnectListener.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/ConnectListener.java index 632e7d32..da797b32 100644 --- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/ConnectListener.java +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/ConnectListener.java @@ -69,7 +69,7 @@ public class ConnectListener implements Listener { session.setUuid(connection.getUniqueId()); PlayerProfile playerProfile = session.getProfile(); - playerProfile.setUuid(connection.getUniqueId()); + playerProfile.setUUID(connection.getUniqueId()); //bungeecord will do this automatically so override it on disabled option if (!plugin.getCore().getConfig().get("premiumUuid", true)) { diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/tasks/AsyncToggleMessage.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/tasks/AsyncToggleMessage.java index 55b3435b..f4e47036 100644 --- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/tasks/AsyncToggleMessage.java +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/tasks/AsyncToggleMessage.java @@ -44,7 +44,7 @@ public class AsyncToggleMessage implements Runnable { } playerProfile.setPremium(false); - playerProfile.setUuid(null); + playerProfile.setUUID(null); core.getStorage().save(playerProfile); sendMessage("remove-premium"); } diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/tasks/ForceLoginTask.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/tasks/ForceLoginTask.java index 4fda50f5..79acbb55 100644 --- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/tasks/ForceLoginTask.java +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/tasks/ForceLoginTask.java @@ -31,7 +31,6 @@ public class ForceLoginTask public void run() { PendingConnection pendingConnection = player.getPendingConnection(); session = core.getPlugin().getSession().get(pendingConnection); - if (session == null) { return; } diff --git a/core/pom.xml b/core/pom.xml index ab0279a1..38e055bc 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -28,7 +28,7 @@ com.zaxxer HikariCP - 2.7.1 + 2.7.2 diff --git a/core/src/main/java/com/github/games647/fastlogin/core/AuthStorage.java b/core/src/main/java/com/github/games647/fastlogin/core/AuthStorage.java index d00f2435..7107bd1d 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/AuthStorage.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/AuthStorage.java @@ -138,7 +138,7 @@ public class AuthStorage { public void save(PlayerProfile playerProfile) { try (Connection con = dataSource.getConnection()) { - UUID uuid = playerProfile.getUuid(); + UUID uuid = playerProfile.getUUID(); if (playerProfile.getUserId() == -1) { try (PreparedStatement saveStmt = con.prepareStatement(INSERT_PROFILE, RETURN_GENERATED_KEYS)) { diff --git a/core/src/main/java/com/github/games647/fastlogin/core/BalancedSSLFactory.java b/core/src/main/java/com/github/games647/fastlogin/core/BalancedSSLFactory.java deleted file mode 100644 index 4ce69953..00000000 --- a/core/src/main/java/com/github/games647/fastlogin/core/BalancedSSLFactory.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.github.games647.fastlogin.core; - -import com.google.common.collect.Iterables; - -import java.io.IOException; -import java.net.InetAddress; -import java.net.Socket; -import java.util.Iterator; - -import javax.net.ssl.SSLSocketFactory; - -public class BalancedSSLFactory extends SSLSocketFactory { - - //in order to be thread-safe - private final Iterator iterator; - private final SSLSocketFactory oldFactory; - - public BalancedSSLFactory(SSLSocketFactory oldFactory, Iterable localAddresses) { - this.oldFactory = oldFactory; - this.iterator = Iterables.cycle(localAddresses).iterator(); - } - - @Override - public String[] getDefaultCipherSuites() { - return oldFactory.getDefaultCipherSuites(); - } - - @Override - public String[] getSupportedCipherSuites() { - return oldFactory.getSupportedCipherSuites(); - } - - @Override - public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException { - return oldFactory.createSocket(host, port, getNextLocalAddress(), 0); - } - - @Override - public Socket createSocket(String host, int port) throws IOException { - return oldFactory.createSocket(host, port, getNextLocalAddress(), 0); - } - - @Override - public Socket createSocket(String host, int port, InetAddress localAddress, int localPort) - throws IOException { - //default - return oldFactory.createSocket(host, port, localAddress, localPort); - } - - @Override - public Socket createSocket(InetAddress host, int port) throws IOException { - return oldFactory.createSocket(host, port, getNextLocalAddress(), 0); - } - - @Override - public Socket createSocket(InetAddress host, int port, InetAddress local, int localPort) throws IOException { - //Default - return oldFactory.createSocket(host, port, local, localPort); - } - - private InetAddress getNextLocalAddress() { - synchronized (iterator) { - return iterator.next(); - } - } -} diff --git a/core/src/main/java/com/github/games647/fastlogin/core/PlayerProfile.java b/core/src/main/java/com/github/games647/fastlogin/core/PlayerProfile.java index 410f4d41..0e921116 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/PlayerProfile.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/PlayerProfile.java @@ -43,11 +43,12 @@ public class PlayerProfile { this.userId = generatedId; } - public synchronized UUID getUuid() { + //todo: this should be optional + public synchronized UUID getUUID() { return uuid; } - public synchronized void setUuid(UUID uuid) { + public synchronized void setUUID(UUID uuid) { this.uuid = uuid; } diff --git a/core/src/main/java/com/github/games647/fastlogin/core/mojang/MojangApiConnector.java b/core/src/main/java/com/github/games647/fastlogin/core/mojang/MojangApiConnector.java index 0bab1810..5699c221 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/mojang/MojangApiConnector.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/mojang/MojangApiConnector.java @@ -1,11 +1,9 @@ package com.github.games647.fastlogin.core.mojang; -import com.github.games647.fastlogin.core.BalancedSSLFactory; import com.github.games647.fastlogin.core.CommonUtil; import com.github.games647.fastlogin.core.shared.LoginSession; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; -import com.google.common.collect.Sets; import com.google.common.net.HostAndPort; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -14,12 +12,10 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; -import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.Proxy.Type; import java.net.URL; -import java.net.UnknownHostException; import java.time.Duration; import java.time.Instant; import java.time.temporal.ChronoUnit; @@ -28,12 +24,10 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.Set; import java.util.UUID; import java.util.regex.Pattern; import javax.net.ssl.HttpsURLConnection; -import javax.net.ssl.SSLSocketFactory; import org.slf4j.Logger; @@ -53,7 +47,6 @@ public class MojangApiConnector { private final Iterator proxies; private final Map requests = CommonUtil.buildCache(10, -1); - private final SSLSocketFactory sslFactory; private final int rateLimit; private Instant lastRateLimit = Instant.now().minus(10, ChronoUnit.MINUTES); @@ -65,13 +58,16 @@ public class MojangApiConnector { , Iterable proxies) { this.logger = logger; this.rateLimit = Math.max(rateLimit, 600); - this.sslFactory = buildAddresses(logger, localAddresses); List proxyBuilder = Lists.newArrayList(); for (HostAndPort proxy : proxies) { proxyBuilder.add(new Proxy(Type.HTTP, new InetSocketAddress(proxy.getHostText(), proxy.getPort()))); } + for (String address : localAddresses) { + proxyBuilder.add(new Proxy(Type.DIRECT, new InetSocketAddress(address.replace('-', '.'), 0))); + } + this.proxies = Iterables.cycle(proxyBuilder).iterator(); } @@ -142,8 +138,6 @@ public class MojangApiConnector { connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("User-Agent", USER_AGENT); - connection.setSSLSocketFactory(sslFactory); - //this connection doesn't need to be closed. So can make use of keep alive in java return connection; } @@ -151,22 +145,4 @@ public class MojangApiConnector { protected HttpsURLConnection getConnection(String url) throws IOException { return getConnection(url, Proxy.NO_PROXY); } - - private SSLSocketFactory buildAddresses(Logger logger, Iterable localAddresses) { - Set addresses = Sets.newHashSet(); - for (String localAddress : localAddresses) { - try { - InetAddress address = InetAddress.getByName(localAddress.replace('-', '.')); - addresses.add(address); - } catch (UnknownHostException ex) { - logger.error("IP-Address is unknown to us", ex); - } - } - - if (addresses.isEmpty()) { - return HttpsURLConnection.getDefaultSSLSocketFactory(); - } - - return new BalancedSSLFactory(HttpsURLConnection.getDefaultSSLSocketFactory(), addresses); - } } diff --git a/core/src/main/java/com/github/games647/fastlogin/core/shared/ForceLoginManagement.java b/core/src/main/java/com/github/games647/fastlogin/core/shared/ForceLoginManagement.java index ea874286..6631a770 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/shared/ForceLoginManagement.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/shared/ForceLoginManagement.java @@ -49,7 +49,7 @@ public abstract class ForceLoginManagement