From 3620b712b76af7abad87adc5d9cfd00b4a93b5dc Mon Sep 17 00:00:00 2001 From: DNx5 Date: Tue, 15 Sep 2015 14:38:55 +0700 Subject: [PATCH] improve DatabaseCalls, --- src/main/java/fr/xephi/authme/AuthMe.java | 55 +-- .../xephi/authme/cache/auth/PlayerCache.java | 2 +- .../authme/datasource/CacheDataSource.java | 19 +- .../xephi/authme/datasource/DataSource.java | 2 +- .../authme/datasource/DatabaseCalls.java | 345 +++--------------- .../fr/xephi/authme/datasource/MySQL.java | 13 +- .../fr/xephi/authme/process/Management.java | 6 +- 7 files changed, 109 insertions(+), 333 deletions(-) diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 0a1e35790..6f2bd2fdb 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -53,13 +53,14 @@ import java.util.zip.GZIPInputStream; public class AuthMe extends JavaPlugin { - private final Server server = getServer(); - private static Logger authmeLogger = Logger.getLogger("AuthMe"); private static AuthMe authme; + + private final Server server = getServer(); + private final Logger authmeLogger = Logger.getLogger("AuthMe"); public Management management; public NewAPI api; private Utils utils = Utils.getInstance(); - public SendMailSSL mail = null; + public SendMailSSL mail; private Settings settings; private Messages m; public DataManager dataManager; @@ -70,8 +71,8 @@ public class AuthMe extends JavaPlugin { public Permission permission; public Essentials ess; public Location essentialsSpawn; - public MultiverseCore multiverse = null; - public LookupService lookupService = null; + public MultiverseCore multiverse; + public LookupService lookupService; public CitizensCommunicator citizens; public boolean isCitizensActive = false; public boolean CombatTag = false; @@ -82,7 +83,6 @@ public class AuthMe extends JavaPlugin { public ConcurrentHashMap captcha = new ConcurrentHashMap<>(); public ConcurrentHashMap cap = new ConcurrentHashMap<>(); public ConcurrentHashMap realIp = new ConcurrentHashMap<>(); - protected static String vgUrl = "http://monitor-1.verygames.net/api/?action=ipclean-real-ip&out=raw&ip=%IP%&port=%PORT%"; public static AuthMe getInstance() { return authme; @@ -131,14 +131,6 @@ public class AuthMe extends JavaPlugin { // Setup otherAccounts file otherAccounts = OtherAccounts.getInstance(); - // Configuration Security Warnings - if (!Settings.isForceSingleSessionEnabled) { - ConsoleLogger.showError("WARNING!!! By disabling ForceSingleSession, your server protection is inadequate!"); - } - if (Settings.getSessionTimeout == 0 && Settings.isSessionsEnabled) { - ConsoleLogger.showError("WARNING!!! You set session timeout to 0, this may cause security issues!"); - } - // Setup messages m = Messages.getInstance(); @@ -148,8 +140,9 @@ public class AuthMe extends JavaPlugin { Metrics metrics = new Metrics(this); metrics.start(); ConsoleLogger.info("Metrics started successfully!"); - } catch (IOException e) { + } catch (Exception e) { // Failed to submit the metrics data + ConsoleLogger.writeStackTrace(e); ConsoleLogger.showError("Can't start Metrics! The plugin will work anyway..."); } @@ -258,8 +251,8 @@ public class AuthMe extends JavaPlugin { // Reload support hook if (Settings.reloadSupport) { - int playersOnline = Utils.getOnlinePlayers().size(); if (database != null) { + int playersOnline = Utils.getOnlinePlayers().size(); if (playersOnline < 1) { database.purgeLogged(); } else { @@ -281,15 +274,15 @@ public class AuthMe extends JavaPlugin { pm.registerEvents(new AuthMeServerListener(this), this); // Register commands - this.getCommand("authme").setExecutor(new AdminCommand(this)); - this.getCommand("register").setExecutor(new RegisterCommand(this)); - this.getCommand("login").setExecutor(new LoginCommand(this)); - this.getCommand("changepassword").setExecutor(new ChangePasswordCommand(this)); - this.getCommand("logout").setExecutor(new LogoutCommand(this)); - this.getCommand("unregister").setExecutor(new UnregisterCommand(this)); - this.getCommand("email").setExecutor(new EmailCommand(this)); - this.getCommand("captcha").setExecutor(new CaptchaCommand(this)); - this.getCommand("converter").setExecutor(new ConverterCommand(this)); + getCommand("authme").setExecutor(new AdminCommand(this)); + getCommand("register").setExecutor(new RegisterCommand(this)); + getCommand("login").setExecutor(new LoginCommand(this)); + getCommand("changepassword").setExecutor(new ChangePasswordCommand(this)); + getCommand("logout").setExecutor(new LogoutCommand(this)); + getCommand("unregister").setExecutor(new UnregisterCommand(this)); + getCommand("email").setExecutor(new EmailCommand(this)); + getCommand("captcha").setExecutor(new CaptchaCommand(this)); + getCommand("converter").setExecutor(new ConverterCommand(this)); // Purge on start if enabled autoPurge(); @@ -297,6 +290,14 @@ public class AuthMe extends JavaPlugin { // Start Email recall task if needed recallEmail(); + // Configuration Security Warnings + if (!Settings.isForceSingleSessionEnabled) { + ConsoleLogger.showError("WARNING!!! By disabling ForceSingleSession, your server protection is inadequate!"); + } + if (Settings.getSessionTimeout == 0 && Settings.isSessionsEnabled) { + ConsoleLogger.showError("WARNING!!! You set session timeout to 0, this may cause security issues!"); + } + // Sponsor messages ConsoleLogger.info("AuthMe hooks perfectly with the VERYGAMES server hosting!"); ConsoleLogger.info("Development builds are available on our jenkins, thanks to f14stelt."); @@ -331,6 +332,8 @@ public class AuthMe extends JavaPlugin { // Disabled correctly ConsoleLogger.info("AuthMe " + this.getDescription().getVersion() + " disabled!"); + + authme = null; } // Stop/unload the server/plugin as defined in the configuration @@ -789,7 +792,7 @@ public class AuthMe extends JavaPlugin { @Deprecated public String getVeryGamesIP(Player player) { String realIP = player.getAddress().getAddress().getHostAddress(); - String sUrl = vgUrl; + String sUrl = "http://monitor-1.verygames.net/api/?action=ipclean-real-ip&out=raw&ip=%IP%&port=%PORT%"; sUrl = sUrl.replace("%IP%", player.getAddress().getAddress().getHostAddress()).replace("%PORT%", "" + player.getAddress().getPort()); try { URL url = new URL(sUrl); diff --git a/src/main/java/fr/xephi/authme/cache/auth/PlayerCache.java b/src/main/java/fr/xephi/authme/cache/auth/PlayerCache.java index 8f39c0d36..491c33a4f 100644 --- a/src/main/java/fr/xephi/authme/cache/auth/PlayerCache.java +++ b/src/main/java/fr/xephi/authme/cache/auth/PlayerCache.java @@ -8,7 +8,7 @@ public class PlayerCache { private ConcurrentHashMap cache; private PlayerCache() { - cache = new ConcurrentHashMap(); + cache = new ConcurrentHashMap<>(); } public void addPlayer(PlayerAuth auth) { diff --git a/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java b/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java index 0b6dfe17d..f9ba8898a 100644 --- a/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java @@ -11,21 +11,24 @@ import java.util.concurrent.ConcurrentHashMap; public class CacheDataSource implements DataSource { - private DataSource source; - public AuthMe plugin; + private final DataSource source; private ConcurrentHashMap cache = new ConcurrentHashMap<>(); - public CacheDataSource(AuthMe plugin, DataSource source) { - this.plugin = plugin; - this.source = source; + public CacheDataSource(AuthMe plugin, DataSource src) { + this.source = src; /* * We need to load all players in cache ... It will took more time to * load the server, but it will be much easier to check for an * isAuthAvailable ! */ - for (PlayerAuth auth : source.getAllAuths()) { - cache.put(auth.getNickname().toLowerCase(), auth); - } + plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable(){ + @Override + public void run() { + for (PlayerAuth auth : source.getAllAuths()) { + cache.put(auth.getNickname().toLowerCase(), auth); + } + } + }); } @Override diff --git a/src/main/java/fr/xephi/authme/datasource/DataSource.java b/src/main/java/fr/xephi/authme/datasource/DataSource.java index 4592ec569..b89f13aaf 100644 --- a/src/main/java/fr/xephi/authme/datasource/DataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/DataSource.java @@ -6,7 +6,7 @@ import java.util.List; public interface DataSource { - public enum DataSourceType { + enum DataSourceType { MYSQL, FILE, SQLITE, diff --git a/src/main/java/fr/xephi/authme/datasource/DatabaseCalls.java b/src/main/java/fr/xephi/authme/datasource/DatabaseCalls.java index 1b3e0993e..6b84c7d73 100644 --- a/src/main/java/fr/xephi/authme/datasource/DatabaseCalls.java +++ b/src/main/java/fr/xephi/authme/datasource/DatabaseCalls.java @@ -1,67 +1,42 @@ package fr.xephi.authme.datasource; +import fr.xephi.authme.cache.auth.PlayerAuth; + import java.util.ArrayList; import java.util.List; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -import fr.xephi.authme.cache.auth.PlayerAuth; +import java.util.concurrent.*; public class DatabaseCalls implements DataSource { private DataSource database; + private final ExecutorService exec; public DatabaseCalls(DataSource database) { this.database = database; + this.exec = Executors.newCachedThreadPool(); } @Override public synchronized boolean isAuthAvailable(final String user) { - ExecutorService executor = Executors.newSingleThreadExecutor(); - Boolean result; try { - result = executor.submit(new Callable() { - + return exec.submit(new Callable() { public Boolean call() throws Exception { return database.isAuthAvailable(user); } }).get(); - } catch (InterruptedException e1) { - return false; - } catch (ExecutionException e1) { - return false; - } finally { - executor.shutdown(); - } - try { - return result.booleanValue(); } catch (Exception e) { - return (false); + return false; } } @Override public synchronized PlayerAuth getAuth(final String user) { - ExecutorService executor = Executors.newSingleThreadExecutor(); - PlayerAuth result; try { - result = executor.submit(new Callable() { - + return exec.submit(new Callable() { public PlayerAuth call() throws Exception { return database.getAuth(user); } }).get(); - } catch (InterruptedException e1) { - return null; - } catch (ExecutionException e1) { - return null; - } finally { - executor.shutdown(); - } - try { - return result; } catch (Exception e) { return null; } @@ -69,332 +44,183 @@ public class DatabaseCalls implements DataSource { @Override public synchronized boolean saveAuth(final PlayerAuth auth) { - ExecutorService executor = Executors.newSingleThreadExecutor(); - Boolean result; try { - result = executor.submit(new Callable() { - + return exec.submit(new Callable() { public Boolean call() throws Exception { return database.saveAuth(auth); } }).get(); - } catch (InterruptedException e1) { - return false; - } catch (ExecutionException e1) { - return false; - } finally { - executor.shutdown(); - } - try { - return result.booleanValue(); } catch (Exception e) { - return (false); + return false; } } @Override public synchronized boolean updateSession(final PlayerAuth auth) { - ExecutorService executor = Executors.newSingleThreadExecutor(); - Boolean result; try { - result = executor.submit(new Callable() { - + return exec.submit(new Callable() { public Boolean call() throws Exception { return database.updateSession(auth); } }).get(); - } catch (InterruptedException e1) { - return false; - } catch (ExecutionException e1) { - return false; - } finally { - executor.shutdown(); - } - try { - return result.booleanValue(); } catch (Exception e) { - return (false); + return false; } } @Override public synchronized boolean updatePassword(final PlayerAuth auth) { - ExecutorService executor = Executors.newSingleThreadExecutor(); - Boolean result; try { - result = executor.submit(new Callable() { - + return exec.submit(new Callable() { public Boolean call() throws Exception { return database.updatePassword(auth); } }).get(); - } catch (InterruptedException e1) { - return false; - } catch (ExecutionException e1) { - return false; - } finally { - executor.shutdown(); - } - try { - return result.booleanValue(); } catch (Exception e) { - return (false); + return false; } } @Override public synchronized int purgeDatabase(final long until) { - ExecutorService executor = Executors.newSingleThreadExecutor(); - Integer result; try { - result = executor.submit(new Callable() { - + return exec.submit(new Callable() { public Integer call() throws Exception { return database.purgeDatabase(until); } }).get(); - } catch (InterruptedException e1) { - return 0; - } catch (ExecutionException e1) { - return 0; - } finally { - executor.shutdown(); - } - try { - return result.intValue(); } catch (Exception e) { - return (0); + return -1; } } @Override public synchronized List autoPurgeDatabase(final long until) { - ExecutorService executor = Executors.newSingleThreadExecutor(); - List result; try { - result = executor.submit(new Callable>() { - + return exec.submit(new Callable>() { public List call() throws Exception { return database.autoPurgeDatabase(until); } }).get(); - } catch (InterruptedException e1) { - return new ArrayList(); - } catch (ExecutionException e1) { - return new ArrayList(); - } finally { - executor.shutdown(); - } - try { - return result; } catch (Exception e) { - return (new ArrayList()); + return new ArrayList<>(); } } @Override public synchronized boolean removeAuth(final String user) { - ExecutorService executor = Executors.newSingleThreadExecutor(); - Boolean result; try { - result = executor.submit(new Callable() { - + return exec.submit(new Callable() { public Boolean call() throws Exception { return database.removeAuth(user); } }).get(); - } catch (InterruptedException e1) { - return false; - } catch (ExecutionException e1) { - return false; - } finally { - executor.shutdown(); - } - try { - return result.booleanValue(); } catch (Exception e) { - return (false); + return false; } } @Override public synchronized boolean updateQuitLoc(final PlayerAuth auth) { - ExecutorService executor = Executors.newSingleThreadExecutor(); - Boolean result; try { - result = executor.submit(new Callable() { - + return exec.submit(new Callable() { public Boolean call() throws Exception { return database.updateQuitLoc(auth); } }).get(); - } catch (InterruptedException e1) { - return false; - } catch (ExecutionException e1) { - return false; - } finally { - executor.shutdown(); - } - try { - return result.booleanValue(); } catch (Exception e) { - return (false); + return false; } } @Override public synchronized int getIps(final String ip) { - ExecutorService executor = Executors.newSingleThreadExecutor(); - Integer result; try { - result = executor.submit(new Callable() { + return exec.submit(new Callable() { public Integer call() throws Exception { return database.getIps(ip); } }).get(); - } catch (InterruptedException e1) { - return 0; - } catch (ExecutionException e1) { - return 0; - } finally { - executor.shutdown(); - } - try { - return result.intValue(); } catch (Exception e) { - return (0); + return -1; } } @Override public synchronized List getAllAuthsByName(final PlayerAuth auth) { - ExecutorService executor = Executors.newSingleThreadExecutor(); - List result; try { - result = executor.submit(new Callable>() { - + return exec.submit(new Callable>() { public List call() throws Exception { return database.getAllAuthsByName(auth); } }).get(); - } catch (InterruptedException e1) { - return new ArrayList(); - } catch (ExecutionException e1) { - return new ArrayList(); - } finally { - executor.shutdown(); - } - try { - return result; } catch (Exception e) { - return (new ArrayList()); + return new ArrayList<>(); } } @Override public synchronized List getAllAuthsByIp(final String ip) { - ExecutorService executor = Executors.newSingleThreadExecutor(); - List result; try { - result = executor.submit(new Callable>() { - + return exec.submit(new Callable>() { public List call() throws Exception { return database.getAllAuthsByIp(ip); } }).get(); - } catch (InterruptedException e1) { - return new ArrayList(); - } catch (ExecutionException e1) { - return new ArrayList(); - } finally { - executor.shutdown(); - } - try { - return result; } catch (Exception e) { - return (new ArrayList()); + return new ArrayList<>(); } } @Override public synchronized List getAllAuthsByEmail(final String email) { - ExecutorService executor = Executors.newSingleThreadExecutor(); - List result; try { - result = executor.submit(new Callable>() { - + return exec.submit(new Callable>() { public List call() throws Exception { return database.getAllAuthsByEmail(email); } }).get(); - } catch (InterruptedException e1) { - return new ArrayList(); - } catch (ExecutionException e1) { - return new ArrayList(); - } finally { - executor.shutdown(); - } - try { - return result; } catch (Exception e) { - return (new ArrayList()); + return new ArrayList<>(); } } @Override public synchronized boolean updateEmail(final PlayerAuth auth) { - ExecutorService executor = Executors.newSingleThreadExecutor(); - Boolean result; try { - result = executor.submit(new Callable() { - + return exec.submit(new Callable() { public Boolean call() throws Exception { return database.updateEmail(auth); } }).get(); - } catch (InterruptedException e1) { - return false; - } catch (ExecutionException e1) { - return false; - } finally { - executor.shutdown(); - } - try { - return result.booleanValue(); } catch (Exception e) { - return (false); + return false; } } @Override public synchronized boolean updateSalt(final PlayerAuth auth) { - ExecutorService executor = Executors.newSingleThreadExecutor(); - Boolean result; try { - result = executor.submit(new Callable() { - + return exec.submit(new Callable() { public Boolean call() throws Exception { return database.updateSalt(auth); } }).get(); - } catch (InterruptedException e1) { - return false; - } catch (ExecutionException e1) { - return false; - } finally { - executor.shutdown(); - } - try { - return result.booleanValue(); } catch (Exception e) { - return (false); + return false; } } @Override public synchronized void close() { - database.close(); + try { + exec.shutdown(); + exec.awaitTermination(10, TimeUnit.SECONDS); + database.close(); + } catch (Exception e) { + e.printStackTrace(); + } } @Override @@ -405,8 +231,6 @@ public class DatabaseCalls implements DataSource { @Override public synchronized void purgeBanned(final List banned) { new Thread(new Runnable() { - - @Override public synchronized void run() { database.purgeBanned(banned); } @@ -420,139 +244,90 @@ public class DatabaseCalls implements DataSource { @Override public synchronized boolean isLogged(final String user) { - ExecutorService executor = Executors.newSingleThreadExecutor(); - Boolean result; try { - result = executor.submit(new Callable() { - + return exec.submit(new Callable() { public Boolean call() throws Exception { return database.isLogged(user); } }).get(); - } catch (InterruptedException e1) { - return false; - } catch (ExecutionException e1) { - return false; - } finally { - executor.shutdown(); - } - try { - return result.booleanValue(); } catch (Exception e) { - return (false); + return false; } } @Override public synchronized void setLogged(final String user) { - new Thread(new Runnable() { - - @Override + exec.execute(new Runnable() { public synchronized void run() { database.setLogged(user); } - }).start(); + }); } @Override public synchronized void setUnlogged(final String user) { - new Thread(new Runnable() { - - @Override + exec.execute(new Runnable() { public synchronized void run() { database.setUnlogged(user); } - }).start(); + }); } @Override public synchronized void purgeLogged() { - new Thread(new Runnable() { - - @Override + exec.execute(new Runnable() { public synchronized void run() { database.purgeLogged(); } - }).start(); + }); } @Override public synchronized int getAccountsRegistered() { - ExecutorService executor = Executors.newSingleThreadExecutor(); - Integer result; try { - result = executor.submit(new Callable() { - + return exec.submit(new Callable() { public Integer call() throws Exception { return database.getAccountsRegistered(); } }).get(); - } catch (InterruptedException e1) { - return 0; - } catch (ExecutionException e1) { - return 0; - } finally { - executor.shutdown(); - } - try { - return result.intValue(); } catch (Exception e) { - return (0); + return -1; } } @Override - public synchronized void updateName(final String oldone, - final String newone) { - new Thread(new Runnable() { - - @Override + public synchronized void updateName(final String oldone, final String newone) { + exec.execute(new Runnable() { public synchronized void run() { database.updateName(oldone, newone); } - }).start(); + }); } @Override public synchronized List getAllAuths() { - ExecutorService executor = Executors.newSingleThreadExecutor(); - List result; try { - result = executor.submit(new Callable>() { - + return exec.submit(new Callable>() { public List call() throws Exception { return database.getAllAuths(); } }).get(); - } catch (InterruptedException e1) { - return (new ArrayList()); - } catch (ExecutionException e1) { - return (new ArrayList()); - } finally { - executor.shutdown(); + } catch (Exception e) { + return new ArrayList<>(); } - return result; } @Override public List getLoggedPlayers() { - ExecutorService executor = Executors.newSingleThreadExecutor(); - List result; try { - result = executor.submit(new Callable>() { - + return exec.submit(new Callable>() { public List call() throws Exception { return database.getLoggedPlayers(); } }).get(); - } catch (InterruptedException e1) { - return (new ArrayList()); - } catch (ExecutionException e1) { - return (new ArrayList()); - } finally { - executor.shutdown(); + } catch (Exception e) { + return new ArrayList<>(); } - return result; } } diff --git a/src/main/java/fr/xephi/authme/datasource/MySQL.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java index ee58973fe..41adbeb2c 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -235,14 +235,13 @@ public class MySQL implements DataSource { } } - @SuppressWarnings("resource") @Override public synchronized PlayerAuth getAuth(String user) { Connection con = null; PreparedStatement pst = null; ResultSet rs = null; PlayerAuth pAuth = null; - int id = -1; + int id; try { con = getConnection(); pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + columnName + ")=LOWER(?);"); @@ -1019,7 +1018,7 @@ public class MySQL implements DataSource { @Override public List getAllAuths() { - List auths = new ArrayList(); + List auths = new ArrayList<>(); Connection con = null; PreparedStatement pst = null; ResultSet rs = null; @@ -1028,7 +1027,7 @@ public class MySQL implements DataSource { pst = con.prepareStatement("SELECT * FROM " + tableName + ";"); rs = pst.executeQuery(); while (rs.next()) { - PlayerAuth pAuth = null; + PlayerAuth pAuth; int id = rs.getInt(columnID); if (rs.getString(columnIp).isEmpty() && rs.getString(columnIp) != null) { pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), "192.168.0.1", rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); @@ -1052,11 +1051,9 @@ public class MySQL implements DataSource { byte[] bytes = blob.getBytes(1, (int) blob.length()); pAuth.setHash(new String(bytes)); } - if (rsid != null) - rsid.close(); + rsid.close(); } - if (pAuth != null) - auths.add(pAuth); + auths.add(pAuth); } } catch (Exception ex) { ConsoleLogger.showError(ex.getMessage()); diff --git a/src/main/java/fr/xephi/authme/process/Management.java b/src/main/java/fr/xephi/authme/process/Management.java index 479790e58..209e12a9a 100644 --- a/src/main/java/fr/xephi/authme/process/Management.java +++ b/src/main/java/fr/xephi/authme/process/Management.java @@ -30,8 +30,7 @@ public class Management { this.pm = plugin.getServer().getPluginManager(); } - public void performLogin(final Player player, final String password, - final boolean forceLogin) { + public void performLogin(final Player player, final String password, final boolean forceLogin) { Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { @Override @@ -41,8 +40,7 @@ public class Management { }); } - public void performRegister(final Player player, final String password, - final String email) { + public void performRegister(final Player player, final String password, final String email) { Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { @Override