Refactor a lot of code + Add Guava v10 as shared library

This commit is contained in:
games647
2016-09-11 21:26:03 +02:00
parent 2cd50d23ad
commit 76f5ba7ed1
29 changed files with 128 additions and 150 deletions

View File

@ -1,9 +1,10 @@
######1.9 ######1.9
* Refactored/Cleaned up a lot of code
* [API] Deprecated platform specific authplugin. Please use AuthPlugin< platform specific player type > * [API] Deprecated platform specific authplugin. Please use AuthPlugin< platform specific player type >
* [API] Deprecated bukkit's password generator. Please use PasswordGenerator< platform specific player type > * [API] Deprecated bukkit's password generator. Please use PasswordGenerator< platform specific player type >
* Added second attempt login -> cracked login * Added second attempt login -> cracked login
* Add cracked whitelist (switch-mode -> switching to online-mode from offlinemode) * Added cracked whitelist (switch-mode -> switching to online-mode from offlinemode)
* Added configuration to disable auto logins for 2Factor authentication * Added configuration to disable auto logins for 2Factor authentication
* Add missing add-premium-other message * Add missing add-premium-other message
* Fix ProtocolSupport autoRegister * Fix ProtocolSupport autoRegister

View File

@ -104,6 +104,7 @@
<artifactId>LoginSecurity-2</artifactId> <artifactId>LoginSecurity-2</artifactId>
<!--Old version 2.0 --> <!--Old version 2.0 -->
<version>-9c09e73b7f-1</version> <version>-9c09e73b7f-1</version>
<optional>true</optional>
<exclusions> <exclusions>
<exclusion> <exclusion>
<groupId>*</groupId> <groupId>*</groupId>
@ -116,6 +117,7 @@
<groupId>com.lenis0012.bukkit</groupId> <groupId>com.lenis0012.bukkit</groupId>
<artifactId>loginsecurity</artifactId> <artifactId>loginsecurity</artifactId>
<version>2.1.3-SNAPSHOT</version> <version>2.1.3-SNAPSHOT</version>
<optional>true</optional>
<exclusions> <exclusions>
<exclusion> <exclusion>
<groupId>*</groupId> <groupId>*</groupId>
@ -128,6 +130,7 @@
<groupId>com.github.games647</groupId> <groupId>com.github.games647</groupId>
<artifactId>LogIt</artifactId> <artifactId>LogIt</artifactId>
<version>9e3581db27</version> <version>9e3581db27</version>
<optional>true</optional>
<scope>provided</scope> <scope>provided</scope>
<exclusions> <exclusions>
<exclusion> <exclusion>
@ -141,6 +144,7 @@
<groupId>com.github.RoyalDev</groupId> <groupId>com.github.RoyalDev</groupId>
<artifactId>RoyalAuth</artifactId> <artifactId>RoyalAuth</artifactId>
<version>-e21354a9b7-1</version> <version>-e21354a9b7-1</version>
<optional>true</optional>
<exclusions> <exclusions>
<exclusion> <exclusion>
<groupId>*</groupId> <groupId>*</groupId>

View File

@ -1,45 +1,24 @@
package com.github.games647.fastlogin.bukkit; package com.github.games647.fastlogin.bukkit;
import com.github.games647.fastlogin.core.FastLoginCore; import com.github.games647.fastlogin.core.shared.FastLoginCore;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import com.google.common.cache.CacheLoader;
import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.io.File; import java.io.File;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
public class BukkitCore extends FastLoginCore { public class BukkitCore extends FastLoginCore<Player> {
public static <K, V> ConcurrentMap<K, V> buildCache(int minutes, int maxSize) {
CompatibleCacheBuilder<Object, Object> builder = CompatibleCacheBuilder.newBuilder();
if (minutes > 0) {
builder.expireAfterWrite(minutes, TimeUnit.MINUTES);
}
if (maxSize > 0) {
builder.maximumSize(maxSize);
}
return builder.build(new CacheLoader<K, V>() {
@Override
public V load(K key) throws Exception {
throw new UnsupportedOperationException("Not supported yet.");
}
});
}
private final FastLoginBukkit plugin; private final FastLoginBukkit plugin;
public BukkitCore(FastLoginBukkit plugin) { public BukkitCore(FastLoginBukkit plugin) {
super(BukkitCore.<String, Object>buildCache(5, 0), plugin.getConfig().getValues(false)); super(plugin.getConfig().getValues(false));
this.plugin = plugin; this.plugin = plugin;
} }

View File

@ -14,13 +14,10 @@ import com.github.games647.fastlogin.bukkit.listener.protocollib.LoginSkinApplyL
import com.github.games647.fastlogin.bukkit.listener.protocollib.StartPacketListener; import com.github.games647.fastlogin.bukkit.listener.protocollib.StartPacketListener;
import com.github.games647.fastlogin.bukkit.listener.protocolsupport.ProtocolSupportListener; import com.github.games647.fastlogin.bukkit.listener.protocolsupport.ProtocolSupportListener;
import com.github.games647.fastlogin.bukkit.tasks.DelayedAuthHook; import com.github.games647.fastlogin.bukkit.tasks.DelayedAuthHook;
import com.github.games647.fastlogin.core.FastLoginCore; import com.github.games647.fastlogin.core.shared.FastLoginCore;
import com.google.common.collect.Sets;
import java.security.KeyPair; import java.security.KeyPair;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import java.util.logging.Level; import java.util.logging.Level;
@ -38,18 +35,13 @@ public class FastLoginBukkit extends JavaPlugin {
private final KeyPair keyPair = EncryptionUtil.generateKeyPair(); private final KeyPair keyPair = EncryptionUtil.generateKeyPair();
private boolean bungeeCord; private boolean bungeeCord;
private final FastLoginCore core = new BukkitCore(this); private final BukkitCore core = new BukkitCore(this);
private boolean serverStarted; private boolean serverStarted;
private final Set<UUID> pendingConfirms = Sets.newHashSet();
//this map is thread-safe for async access (Packet Listener)
//SafeCacheBuilder is used in order to be version independent
private final ConcurrentMap<String, BukkitLoginSession> session = BukkitCore.buildCache(1, -1);
//1 minutes should be enough as a timeout for bad internet connection (Server, Client and Mojang) //1 minutes should be enough as a timeout for bad internet connection (Server, Client and Mojang)
private final ConcurrentMap<String, BukkitLoginSession> session = FastLoginCore.buildCache(1, -1);
private BukkitAuthPlugin authPlugin; private BukkitAuthPlugin authPlugin;
private PasswordGenerator passwordGenerator = new DefaultPasswordGenerator();
@Override @Override
public void onEnable() { public void onEnable() {
@ -58,8 +50,7 @@ public class FastLoginBukkit extends JavaPlugin {
List<String> ipAddresses = getConfig().getStringList("ip-addresses"); List<String> ipAddresses = getConfig().getStringList("ip-addresses");
int requestLimit = getConfig().getInt("mojang-request-limit"); int requestLimit = getConfig().getInt("mojang-request-limit");
ConcurrentMap<Object, Object> requestCache = BukkitCore.buildCache(10, -1); MojangApiBukkit mojangApi = new MojangApiBukkit(getLogger(), ipAddresses, requestLimit);
MojangApiBukkit mojangApi = new MojangApiBukkit(requestCache, getLogger(), ipAddresses, requestLimit);
core.setMojangApiConnector(mojangApi); core.setMojangApiConnector(mojangApi);
try { try {
@ -67,8 +58,7 @@ public class FastLoginBukkit extends JavaPlugin {
bungeeCord = Class.forName("org.spigotmc.SpigotConfig").getDeclaredField("bungee").getBoolean(null); bungeeCord = Class.forName("org.spigotmc.SpigotConfig").getDeclaredField("bungee").getBoolean(null);
} }
} catch (Exception | NoSuchMethodError ex) { } catch (Exception | NoSuchMethodError ex) {
getLogger().warning("Cannot check bungeecord support. You use a non-spigot build"); getLogger().log(Level.WARNING, "Cannot check bungeecord support. You use a non-spigot build", ex);
ex.printStackTrace();
} }
if (getServer().getOnlineMode()) { if (getServer().getOnlineMode()) {
@ -86,15 +76,7 @@ public class FastLoginBukkit extends JavaPlugin {
getServer().getMessenger().registerOutgoingPluginChannel(this, getName()); getServer().getMessenger().registerOutgoingPluginChannel(this, getName());
//register listeners on success //register listeners on success
} else { } else {
String driver = getConfig().getString("driver"); if (!core.setupDatabase()) {
String host = getConfig().getString("host", "");
int port = getConfig().getInt("port", 3306);
String database = getConfig().getString("database");
String username = getConfig().getString("username", "");
String password = getConfig().getString("password", "");
if (!core.setupDatabase(driver, host, port, database, username, password)) {
setEnabled(false); setEnabled(false);
return; return;
} }
@ -143,16 +125,13 @@ public class FastLoginBukkit extends JavaPlugin {
} }
} }
public FastLoginCore getCore() { public BukkitCore getCore() {
return core; return core;
} }
public String generateStringPassword(Player player) { @Deprecated
return passwordGenerator.getRandomPassword(player);
}
public void setPasswordGenerator(PasswordGenerator passwordGenerator) { public void setPasswordGenerator(PasswordGenerator passwordGenerator) {
this.passwordGenerator = passwordGenerator; core.setPasswordGenerator(passwordGenerator);
} }
/** /**
@ -210,10 +189,6 @@ public class FastLoginBukkit extends JavaPlugin {
return serverStarted; return serverStarted;
} }
public Set<UUID> getPendingConfirms() {
return pendingConfirms;
}
public void setServerStarted() { public void setServerStarted() {
if (!this.serverStarted) { if (!this.serverStarted) {
this.serverStarted = true; this.serverStarted = true;

View File

@ -1,14 +1,13 @@
package com.github.games647.fastlogin.bukkit; package com.github.games647.fastlogin.bukkit;
import com.github.games647.fastlogin.core.FastLoginCore; import com.github.games647.fastlogin.core.shared.FastLoginCore;
import com.github.games647.fastlogin.core.MojangApiConnector; import com.github.games647.fastlogin.core.shared.MojangApiConnector;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -21,9 +20,8 @@ public class MojangApiBukkit extends MojangApiConnector {
//mojang api check to prove a player is logged in minecraft and made a join server request //mojang api check to prove a player is logged in minecraft and made a join server request
private static final String HAS_JOINED_URL = "https://sessionserver.mojang.com/session/minecraft/hasJoined?"; private static final String HAS_JOINED_URL = "https://sessionserver.mojang.com/session/minecraft/hasJoined?";
public MojangApiBukkit(ConcurrentMap<Object, Object> requests, Logger logger, List<String> localAddresses public MojangApiBukkit(Logger logger, List<String> localAddresses, int rateLimit) {
, int rateLimit) { super(logger, localAddresses, rateLimit);
super(requests, logger, localAddresses, rateLimit);
} }
@Override @Override

View File

@ -4,10 +4,10 @@ import org.bukkit.entity.Player;
/** /**
* *
* @deprecated please use com.github.games647.fastlogin.core.shared.PasswordGenerator<org.bukkit.entity.Player> * @deprecated please use com.github.games647.fastlogin.core.hooks.PasswordGenerator<org.bukkit.entity.Player>
*/ */
@Deprecated @Deprecated
public interface PasswordGenerator { public interface PasswordGenerator extends com.github.games647.fastlogin.core.hooks.PasswordGenerator<Player> {
String getRandomPassword(Player player); String getRandomPassword(Player player);
} }

View File

@ -2,7 +2,7 @@ package com.github.games647.fastlogin.bukkit.commands;
import com.github.games647.fastlogin.bukkit.FastLoginBukkit; import com.github.games647.fastlogin.bukkit.FastLoginBukkit;
import com.github.games647.fastlogin.core.AuthStorage; import com.github.games647.fastlogin.core.AuthStorage;
import com.github.games647.fastlogin.core.FastLoginCore; import com.github.games647.fastlogin.core.shared.FastLoginCore;
import com.github.games647.fastlogin.core.importer.ImportPlugin; import com.github.games647.fastlogin.core.importer.ImportPlugin;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;

View File

@ -44,13 +44,14 @@ public class PremiumCommand implements CommandExecutor {
} }
} else { } else {
UUID id = ((Player) sender).getUniqueId(); UUID id = ((Player) sender).getUniqueId();
if (plugin.getConfig().getBoolean("premium-warning") && !plugin.getPendingConfirms().contains(id)) { if (plugin.getConfig().getBoolean("premium-warning")
&& !plugin.getCore().getPendingConfirms().contains(id)) {
sender.sendMessage(plugin.getCore().getMessage("premium-warning")); sender.sendMessage(plugin.getCore().getMessage("premium-warning"));
plugin.getPendingConfirms().add(id); plugin.getCore().getPendingConfirms().add(id);
return true; return true;
} }
plugin.getPendingConfirms().remove(id); plugin.getCore().getPendingConfirms().remove(id);
//todo: load async //todo: load async
final PlayerProfile profile = plugin.getCore().getStorage().loadProfile(sender.getName()); final PlayerProfile profile = plugin.getCore().getStorage().loadProfile(sender.getName());
if (profile.isPremium()) { if (profile.isPremium()) {

View File

@ -1,11 +1,11 @@
package com.github.games647.fastlogin.bukkit.hooks; package com.github.games647.fastlogin.bukkit.hooks;
import com.github.games647.fastlogin.core.AuthPlugin; import com.github.games647.fastlogin.core.hooks.AuthPlugin;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
/** /**
* @deprecated please use com.github.games647.fastlogin.core.AuthPlugin<org.bukkit.entity.Player> * @deprecated please use com.github.games647.fastlogin.core.hooks.AuthPlugin<org.bukkit.entity.Player>
*/ */
@Deprecated @Deprecated
public interface BukkitAuthPlugin extends AuthPlugin<Player> { public interface BukkitAuthPlugin extends AuthPlugin<Player> {

View File

@ -48,6 +48,6 @@ public class BukkitJoinListener implements Listener {
public void onPlayerQuit(PlayerQuitEvent quitEvent) { public void onPlayerQuit(PlayerQuitEvent quitEvent) {
Player player = quitEvent.getPlayer(); Player player = quitEvent.getPlayer();
player.removeMetadata(plugin.getName(), plugin); player.removeMetadata(plugin.getName(), plugin);
plugin.getPendingConfirms().remove(player.getUniqueId()); plugin.getCore().getPendingConfirms().remove(player.getUniqueId());
} }
} }

View File

@ -88,7 +88,7 @@ public class ForceLoginTask implements Runnable {
private boolean forceRegister(BukkitAuthPlugin authPlugin, Player player) { private boolean forceRegister(BukkitAuthPlugin authPlugin, Player player) {
plugin.getLogger().log(Level.FINE, "Register player {0}", player.getName()); plugin.getLogger().log(Level.FINE, "Register player {0}", player.getName());
String generatedPassword = plugin.generateStringPassword(player); String generatedPassword = plugin.getCore().getPasswordGenerator().getRandomPassword(player);
boolean success = authPlugin.forceRegister(player, generatedPassword); boolean success = authPlugin.forceRegister(player, generatedPassword);
String message = plugin.getCore().getMessage("auto-register"); String message = plugin.getCore().getMessage("auto-register");
if (success && message != null) { if (success && message != null) {

View File

@ -44,13 +44,6 @@
<version>1.8-SNAPSHOT</version> <version>1.8-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- <dependency>
<groupId>io.github.waterfallmc</groupId>
<artifactId>waterfall-api</artifactId>
<version>1.9-SNAPSHOT</version>
<type>jar</type>
<scope>provided</scope>
</dependency>-->
<dependency> <dependency>
<groupId>com.github.MatteCarra</groupId> <groupId>com.github.MatteCarra</groupId>

View File

@ -1,7 +1,6 @@
package com.github.games647.fastlogin.bungee; package com.github.games647.fastlogin.bungee;
import com.github.games647.fastlogin.core.FastLoginCore; import com.github.games647.fastlogin.core.shared.FastLoginCore;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.common.util.concurrent.ThreadFactoryBuilder;
@ -12,23 +11,22 @@ import java.nio.file.Files;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.scheduler.GroupedThreadFactory; import net.md_5.bungee.api.scheduler.GroupedThreadFactory;
import net.md_5.bungee.config.Configuration; import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider; import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration; import net.md_5.bungee.config.YamlConfiguration;
public class BungeeCore extends FastLoginCore { public class BungeeCore extends FastLoginCore<ProxiedPlayer> {
private final FastLoginBungee plugin; private final FastLoginBungee plugin;
public BungeeCore(FastLoginBungee plugin) { public BungeeCore(FastLoginBungee plugin) {
super(CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES).<String, Object>build().asMap() super(generateConfigMap(plugin.getConfig()));
, generateConfigMap(plugin.getConfig()));
this.plugin = plugin; this.plugin = plugin;
} }

View File

@ -4,10 +4,6 @@ import com.github.games647.fastlogin.bungee.hooks.BungeeAuthHook;
import com.github.games647.fastlogin.bungee.hooks.BungeeAuthPlugin; import com.github.games647.fastlogin.bungee.hooks.BungeeAuthPlugin;
import com.github.games647.fastlogin.bungee.listener.PlayerConnectionListener; import com.github.games647.fastlogin.bungee.listener.PlayerConnectionListener;
import com.github.games647.fastlogin.bungee.listener.PluginMessageListener; import com.github.games647.fastlogin.bungee.listener.PluginMessageListener;
import com.github.games647.fastlogin.core.FastLoginCore;
import com.github.games647.fastlogin.core.shared.DefaultPasswordGenerator;
import com.github.games647.fastlogin.core.shared.PasswordGenerator;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
@ -17,11 +13,9 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level; import java.util.logging.Level;
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.plugin.Plugin; import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.config.Configuration; import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider; import net.md_5.bungee.config.ConfigurationProvider;
@ -32,11 +26,10 @@ import net.md_5.bungee.config.YamlConfiguration;
*/ */
public class FastLoginBungee extends Plugin { public class FastLoginBungee extends Plugin {
private final FastLoginCore loginCore = new BungeeCore(this); private final BungeeCore loginCore = new BungeeCore(this);
private BungeeAuthPlugin bungeeAuthPlugin; private BungeeAuthPlugin bungeeAuthPlugin;
private Configuration config; private Configuration config;
private final PasswordGenerator<ProxiedPlayer> passwordGenerator = new DefaultPasswordGenerator<>();
private final Set<UUID> pendingConfirms = Sets.newHashSet(); private final Set<UUID> pendingConfirms = Sets.newHashSet();
private final ConcurrentMap<PendingConnection, BungeeLoginSession> session = Maps.newConcurrentMap(); private final ConcurrentMap<PendingConnection, BungeeLoginSession> session = Maps.newConcurrentMap();
@ -52,19 +45,10 @@ public class FastLoginBungee extends Plugin {
List<String> ipAddresses = getConfig().getStringList("ip-addresses"); List<String> ipAddresses = getConfig().getStringList("ip-addresses");
int requestLimit = getConfig().getInt("mojang-request-limit"); int requestLimit = getConfig().getInt("mojang-request-limit");
ConcurrentMap<Object, Object> requestCache = CacheBuilder.newBuilder() MojangApiBungee mojangApi = new MojangApiBungee(getLogger(), ipAddresses, requestLimit);
.expireAfterWrite(10, TimeUnit.MINUTES).build().asMap();
MojangApiBungee mojangApi = new MojangApiBungee(requestCache, getLogger(), ipAddresses, requestLimit);
loginCore.setMojangApiConnector(mojangApi); loginCore.setMojangApiConnector(mojangApi);
String driver = config.getString("driver"); if (!loginCore.setupDatabase()) {
String host = config.getString("host", "");
int port = config.getInt("port", 3306);
String database = config.getString("database");
String username = config.getString("username", "");
String password = config.getString("password", "");
if (!loginCore.setupDatabase(driver, host, port, database, username, password)) {
return; return;
} }
} catch (IOException ioExc) { } catch (IOException ioExc) {
@ -85,16 +69,12 @@ public class FastLoginBungee extends Plugin {
registerHook(); registerHook();
} }
public String generatePassword(ProxiedPlayer player) {
return passwordGenerator.getRandomPassword(player);
}
@Override @Override
public void onDisable() { public void onDisable() {
loginCore.close(); loginCore.close();
} }
public FastLoginCore getCore() { public BungeeCore getCore() {
return loginCore; return loginCore;
} }
@ -110,10 +90,6 @@ public class FastLoginBungee extends Plugin {
return session; return session;
} }
public Set<UUID> getPendingConfirms() {
return pendingConfirms;
}
/** /**
* Get the auth plugin hook for BungeeCord * Get the auth plugin hook for BungeeCord
* *

View File

@ -2,7 +2,7 @@ package com.github.games647.fastlogin.bungee;
import com.github.games647.fastlogin.bungee.FastLoginBungee; import com.github.games647.fastlogin.bungee.FastLoginBungee;
import com.github.games647.fastlogin.core.AuthStorage; import com.github.games647.fastlogin.core.AuthStorage;
import com.github.games647.fastlogin.core.FastLoginCore; import com.github.games647.fastlogin.core.shared.FastLoginCore;
import com.github.games647.fastlogin.core.importer.ImportPlugin; import com.github.games647.fastlogin.core.importer.ImportPlugin;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;

View File

@ -1,20 +1,18 @@
package com.github.games647.fastlogin.bungee; package com.github.games647.fastlogin.bungee;
import com.github.games647.fastlogin.core.FastLoginCore; import com.github.games647.fastlogin.core.shared.FastLoginCore;
import com.github.games647.fastlogin.core.MojangApiConnector; import com.github.games647.fastlogin.core.shared.MojangApiConnector;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Logger; import java.util.logging.Logger;
import net.md_5.bungee.BungeeCord; import net.md_5.bungee.BungeeCord;
public class MojangApiBungee extends MojangApiConnector { public class MojangApiBungee extends MojangApiConnector {
public MojangApiBungee(ConcurrentMap<Object, Object> requests, Logger logger, List<String> localAddresses public MojangApiBungee(Logger logger, List<String> localAddresses, int rateLimit) {
, int rateLimit) { super(logger, localAddresses, rateLimit);
super(requests, logger, localAddresses, rateLimit);
} }
@Override @Override

View File

@ -1,10 +1,10 @@
package com.github.games647.fastlogin.bungee.hooks; package com.github.games647.fastlogin.bungee.hooks;
import com.github.games647.fastlogin.core.AuthPlugin; import com.github.games647.fastlogin.core.hooks.AuthPlugin;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
/** /**
* @deprecated please use com.github.games647.fastlogin.core.AuthPlugin<net.md_5.bungee.api.connection.ProxiedPlayer> * @deprecated please use com.github.games647.fastlogin.core.hooks.AuthPlugin<net.md_5.bungee.api.connection.ProxiedPlayer>
*/ */
@Deprecated @Deprecated
public interface BungeeAuthPlugin extends AuthPlugin<ProxiedPlayer> { public interface BungeeAuthPlugin extends AuthPlugin<ProxiedPlayer> {

View File

@ -103,6 +103,6 @@ public class PlayerConnectionListener implements Listener {
public void onDisconnect(PlayerDisconnectEvent disconnectEvent) { public void onDisconnect(PlayerDisconnectEvent disconnectEvent) {
ProxiedPlayer player = disconnectEvent.getPlayer(); ProxiedPlayer player = disconnectEvent.getPlayer();
plugin.getSession().remove(player.getPendingConnection()); plugin.getSession().remove(player.getPendingConnection());
plugin.getPendingConfirms().remove(player.getUniqueId()); plugin.getCore().getPendingConfirms().remove(player.getUniqueId());
} }
} }

View File

@ -55,14 +55,14 @@ public class PluginMessageListener implements Listener {
String playerName = dataInput.readUTF(); String playerName = dataInput.readUTF();
if (playerName.equals(forPlayer.getName()) && plugin.getConfig().getBoolean("premium-warning") if (playerName.equals(forPlayer.getName()) && plugin.getConfig().getBoolean("premium-warning")
&& !plugin.getPendingConfirms().contains(forPlayer.getUniqueId())) { && !plugin.getCore().getPendingConfirms().contains(forPlayer.getUniqueId())) {
String message = plugin.getCore().getMessage("premium-warning"); String message = plugin.getCore().getMessage("premium-warning");
forPlayer.sendMessage(TextComponent.fromLegacyText(message)); forPlayer.sendMessage(TextComponent.fromLegacyText(message));
plugin.getPendingConfirms().add(forPlayer.getUniqueId()); plugin.getCore().getPendingConfirms().add(forPlayer.getUniqueId());
return; return;
} }
plugin.getPendingConfirms().remove(forPlayer.getUniqueId()); plugin.getCore().getPendingConfirms().remove(forPlayer.getUniqueId());
AsyncToggleMessage task = new AsyncToggleMessage(plugin, forPlayer, playerName, true); AsyncToggleMessage task = new AsyncToggleMessage(plugin, forPlayer, playerName, true);
ProxyServer.getInstance().getScheduler().runAsync(plugin, task); ProxyServer.getInstance().getScheduler().runAsync(plugin, task);
} else if ("OFF".equals(subchannel)) { } else if ("OFF".equals(subchannel)) {

View File

@ -81,7 +81,7 @@ public class ForceLoginTask implements Runnable {
session.setAlreadyLogged(true); session.setAlreadyLogged(true);
String password = plugin.generatePassword(player); String password = plugin.getCore().getPasswordGenerator().getRandomPassword(player);
if (authPlugin.forceRegister(player, password)) { if (authPlugin.forceRegister(player, password)) {
//save will happen on success message from bukkit //save will happen on success message from bukkit
sendBukkitLoginNotification(true); sendBukkitLoginNotification(true);

View File

@ -28,5 +28,12 @@
<artifactId>slf4j-jdk14</artifactId> <artifactId>slf4j-jdk14</artifactId>
<version>1.7.21</version> <version>1.7.21</version>
</dependency> </dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>10.0</version>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,5 +1,6 @@
package com.github.games647.fastlogin.core; package com.github.games647.fastlogin.core;
import com.github.games647.fastlogin.core.shared.FastLoginCore;
import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;

View File

@ -1,4 +1,4 @@
package com.github.games647.fastlogin.bukkit; package com.github.games647.fastlogin.core;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
@ -10,14 +10,13 @@ import com.google.common.cache.CacheLoader;
import com.google.common.cache.RemovalListener; import com.google.common.cache.RemovalListener;
/** /**
* Represents a Guava CacheBuilder that is compatible with both Guava 10 and 13 * Represents a Guava CacheBuilder that is compatible with both Guava 10 (Minecraft 1.7.X) and 13
*/ */
public class CompatibleCacheBuilder<K, V> { public class CompatibleCacheBuilder<K, V> {
private static Method BUILD_METHOD; private static Method BUILD_METHOD;
private static Method AS_MAP_METHOD; private static Method AS_MAP_METHOD;
/** /**
* Construct a new safe cache builder. * Construct a new safe cache builder.
* *

View File

@ -1,4 +1,4 @@
package com.github.games647.fastlogin.core; package com.github.games647.fastlogin.core.hooks;
/** /**
* Represents a supporting authentication plugin in BungeeCord and Bukkit/Spigot/... servers * Represents a supporting authentication plugin in BungeeCord and Bukkit/Spigot/... servers

View File

@ -1,4 +1,4 @@
package com.github.games647.fastlogin.core.shared; package com.github.games647.fastlogin.core.hooks;
import java.util.Random; import java.util.Random;

View File

@ -1,4 +1,4 @@
package com.github.games647.fastlogin.core.shared; package com.github.games647.fastlogin.core.hooks;
public interface PasswordGenerator<T> { public interface PasswordGenerator<T> {

View File

@ -1,22 +1,50 @@
package com.github.games647.fastlogin.core; package com.github.games647.fastlogin.core.shared;
import com.github.games647.fastlogin.core.AuthStorage;
import com.github.games647.fastlogin.core.CompatibleCacheBuilder;
import com.github.games647.fastlogin.core.SharedConfig;
import com.github.games647.fastlogin.core.hooks.DefaultPasswordGenerator;
import com.github.games647.fastlogin.core.hooks.PasswordGenerator;
import com.github.games647.fastlogin.core.importer.AutoInImporter; import com.github.games647.fastlogin.core.importer.AutoInImporter;
import com.github.games647.fastlogin.core.importer.ImportPlugin; import com.github.games647.fastlogin.core.importer.ImportPlugin;
import com.github.games647.fastlogin.core.importer.Importer; import com.github.games647.fastlogin.core.importer.Importer;
import com.google.common.cache.CacheLoader;
import com.google.common.collect.Sets;
import java.io.File; import java.io.File;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
public abstract class FastLoginCore { public abstract class FastLoginCore<T> {
public static <K, V> ConcurrentMap<K, V> buildCache(int expireAfterWrite, int maxSize) {
CompatibleCacheBuilder<Object, Object> builder = CompatibleCacheBuilder.newBuilder();
if (expireAfterWrite > 0) {
builder.expireAfterWrite(expireAfterWrite, TimeUnit.MINUTES);
}
if (maxSize > 0) {
builder.maximumSize(maxSize);
}
return builder.build(new CacheLoader<K, V>() {
@Override
public V load(K key) throws Exception {
throw new UnsupportedOperationException("Not supported yet.");
}
});
}
public static UUID parseId(String withoutDashes) { public static UUID parseId(String withoutDashes) {
if (withoutDashes == null) { if (withoutDashes == null) {
@ -33,13 +61,15 @@ public abstract class FastLoginCore {
protected final Map<String, String> localeMessages = new ConcurrentHashMap<>(); protected final Map<String, String> localeMessages = new ConcurrentHashMap<>();
private final ConcurrentMap<String, Object> pendingLogins; private final ConcurrentMap<String, Object> pendingLogins;
private final Set<UUID> pendingConfirms = Sets.newHashSet();
private final SharedConfig sharedConfig; private final SharedConfig sharedConfig;
private MojangApiConnector mojangApiConnector; private MojangApiConnector mojangApiConnector;
private PasswordGenerator<T> passwordGenerator = new DefaultPasswordGenerator<>();
private AuthStorage storage; private AuthStorage storage;
public FastLoginCore(ConcurrentMap<String, Object> pendingLogins, Map<String, Object> config) { public FastLoginCore(Map<String, Object> config) {
this.pendingLogins = pendingLogins; this.pendingLogins = FastLoginCore.buildCache(5, 0);
this.sharedConfig = new SharedConfig(config); this.sharedConfig = new SharedConfig(config);
} }
@ -69,7 +99,15 @@ public abstract class FastLoginCore {
public abstract void loadConfig(); public abstract void loadConfig();
public boolean setupDatabase(String driver, String host, int port, String database, String user, String password) { public boolean setupDatabase() {
String driver = sharedConfig.get("driver");
String host = sharedConfig.get("host", "");
int port = sharedConfig.get("port", 3306);
String database = sharedConfig.get("database");
String user = sharedConfig.get("username", "");
String password = sharedConfig.get("password", "");
storage = new AuthStorage(this, driver, host, port, database, user, password); storage = new AuthStorage(this, driver, host, port, database, user, password);
try { try {
storage.createTables(); storage.createTables();
@ -124,10 +162,22 @@ public abstract class FastLoginCore {
return sharedConfig; return sharedConfig;
} }
public PasswordGenerator<T> getPasswordGenerator() {
return passwordGenerator;
}
public void setPasswordGenerator(PasswordGenerator<T> passwordGenerator) {
this.passwordGenerator = passwordGenerator;
}
public ConcurrentMap<String, Object> getPendingLogins() { public ConcurrentMap<String, Object> getPendingLogins() {
return pendingLogins; return pendingLogins;
} }
public Set<UUID> getPendingConfirms() {
return pendingConfirms;
}
public void close() { public void close() {
if (storage != null) { if (storage != null) {
storage.close(); storage.close();

View File

@ -1,7 +1,6 @@
package com.github.games647.fastlogin.core.shared; package com.github.games647.fastlogin.core.shared;
import com.github.games647.fastlogin.core.AuthPlugin; import com.github.games647.fastlogin.core.hooks.AuthPlugin;
import com.github.games647.fastlogin.core.FastLoginCore;
import com.github.games647.fastlogin.core.PlayerProfile; import com.github.games647.fastlogin.core.PlayerProfile;
import com.github.games647.fastlogin.core.SharedConfig; import com.github.games647.fastlogin.core.SharedConfig;
import java.util.UUID; import java.util.UUID;

View File

@ -1,5 +1,6 @@
package com.github.games647.fastlogin.core; package com.github.games647.fastlogin.core.shared;
import com.github.games647.fastlogin.core.BalancedSSLFactory;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@ -36,17 +37,15 @@ public abstract class MojangApiConnector {
//compile the pattern only on plugin enable -> and this have to be threadsafe //compile the pattern only on plugin enable -> and this have to be threadsafe
private final Pattern playernameMatcher = Pattern.compile(VALID_PLAYERNAME); private final Pattern playernameMatcher = Pattern.compile(VALID_PLAYERNAME);
private final ConcurrentMap<Object, Object> requests; private final ConcurrentMap<Object, Object> requests = FastLoginCore.buildCache(10, -1);
private final BalancedSSLFactory sslFactory; private final BalancedSSLFactory sslFactory;
private final int rateLimit; private final int rateLimit;
private long lastRateLimit; private long lastRateLimit;
protected final Logger logger; protected final Logger logger;
public MojangApiConnector(ConcurrentMap<Object, Object> requests, Logger logger, List<String> localAddresses public MojangApiConnector(Logger logger, List<String> localAddresses, int rateLimit) {
, int rateLimit) {
this.logger = logger; this.logger = logger;
this.requests = requests;
if (rateLimit > 600) { if (rateLimit > 600) {
this.rateLimit = 600; this.rateLimit = 600;