improve DatabaseCalls,

This commit is contained in:
DNx5 2015-09-15 14:38:55 +07:00
parent 91d6ead466
commit 3620b712b7
7 changed files with 109 additions and 333 deletions

View File

@ -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<String, Integer> captcha = new ConcurrentHashMap<>();
public ConcurrentHashMap<String, String> cap = new ConcurrentHashMap<>();
public ConcurrentHashMap<String, String> 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);

View File

@ -8,7 +8,7 @@ public class PlayerCache {
private ConcurrentHashMap<String, PlayerAuth> cache;
private PlayerCache() {
cache = new ConcurrentHashMap<String, PlayerAuth>();
cache = new ConcurrentHashMap<>();
}
public void addPlayer(PlayerAuth auth) {

View File

@ -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<String, PlayerAuth> 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

View File

@ -6,7 +6,7 @@ import java.util.List;
public interface DataSource {
public enum DataSourceType {
enum DataSourceType {
MYSQL,
FILE,
SQLITE,

View File

@ -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<Boolean>() {
return exec.submit(new Callable<Boolean>() {
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<PlayerAuth>() {
return exec.submit(new Callable<PlayerAuth>() {
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<Boolean>() {
return exec.submit(new Callable<Boolean>() {
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<Boolean>() {
return exec.submit(new Callable<Boolean>() {
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<Boolean>() {
return exec.submit(new Callable<Boolean>() {
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<Integer>() {
return exec.submit(new Callable<Integer>() {
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<String> autoPurgeDatabase(final long until) {
ExecutorService executor = Executors.newSingleThreadExecutor();
List<String> result;
try {
result = executor.submit(new Callable<List<String>>() {
return exec.submit(new Callable<List<String>>() {
public List<String> call() throws Exception {
return database.autoPurgeDatabase(until);
}
}).get();
} catch (InterruptedException e1) {
return new ArrayList<String>();
} catch (ExecutionException e1) {
return new ArrayList<String>();
} finally {
executor.shutdown();
}
try {
return result;
} catch (Exception e) {
return (new ArrayList<String>());
return new ArrayList<>();
}
}
@Override
public synchronized boolean removeAuth(final String user) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Boolean result;
try {
result = executor.submit(new Callable<Boolean>() {
return exec.submit(new Callable<Boolean>() {
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<Boolean>() {
return exec.submit(new Callable<Boolean>() {
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<Integer>() {
return exec.submit(new Callable<Integer>() {
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<String> getAllAuthsByName(final PlayerAuth auth) {
ExecutorService executor = Executors.newSingleThreadExecutor();
List<String> result;
try {
result = executor.submit(new Callable<List<String>>() {
return exec.submit(new Callable<List<String>>() {
public List<String> call() throws Exception {
return database.getAllAuthsByName(auth);
}
}).get();
} catch (InterruptedException e1) {
return new ArrayList<String>();
} catch (ExecutionException e1) {
return new ArrayList<String>();
} finally {
executor.shutdown();
}
try {
return result;
} catch (Exception e) {
return (new ArrayList<String>());
return new ArrayList<>();
}
}
@Override
public synchronized List<String> getAllAuthsByIp(final String ip) {
ExecutorService executor = Executors.newSingleThreadExecutor();
List<String> result;
try {
result = executor.submit(new Callable<List<String>>() {
return exec.submit(new Callable<List<String>>() {
public List<String> call() throws Exception {
return database.getAllAuthsByIp(ip);
}
}).get();
} catch (InterruptedException e1) {
return new ArrayList<String>();
} catch (ExecutionException e1) {
return new ArrayList<String>();
} finally {
executor.shutdown();
}
try {
return result;
} catch (Exception e) {
return (new ArrayList<String>());
return new ArrayList<>();
}
}
@Override
public synchronized List<String> getAllAuthsByEmail(final String email) {
ExecutorService executor = Executors.newSingleThreadExecutor();
List<String> result;
try {
result = executor.submit(new Callable<List<String>>() {
return exec.submit(new Callable<List<String>>() {
public List<String> call() throws Exception {
return database.getAllAuthsByEmail(email);
}
}).get();
} catch (InterruptedException e1) {
return new ArrayList<String>();
} catch (ExecutionException e1) {
return new ArrayList<String>();
} finally {
executor.shutdown();
}
try {
return result;
} catch (Exception e) {
return (new ArrayList<String>());
return new ArrayList<>();
}
}
@Override
public synchronized boolean updateEmail(final PlayerAuth auth) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Boolean result;
try {
result = executor.submit(new Callable<Boolean>() {
return exec.submit(new Callable<Boolean>() {
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<Boolean>() {
return exec.submit(new Callable<Boolean>() {
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<String> 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<Boolean>() {
return exec.submit(new Callable<Boolean>() {
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<Integer>() {
return exec.submit(new Callable<Integer>() {
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<PlayerAuth> getAllAuths() {
ExecutorService executor = Executors.newSingleThreadExecutor();
List<PlayerAuth> result;
try {
result = executor.submit(new Callable<List<PlayerAuth>>() {
return exec.submit(new Callable<List<PlayerAuth>>() {
public List<PlayerAuth> call() throws Exception {
return database.getAllAuths();
}
}).get();
} catch (InterruptedException e1) {
return (new ArrayList<PlayerAuth>());
} catch (ExecutionException e1) {
return (new ArrayList<PlayerAuth>());
} finally {
executor.shutdown();
} catch (Exception e) {
return new ArrayList<>();
}
return result;
}
@Override
public List<PlayerAuth> getLoggedPlayers() {
ExecutorService executor = Executors.newSingleThreadExecutor();
List<PlayerAuth> result;
try {
result = executor.submit(new Callable<List<PlayerAuth>>() {
return exec.submit(new Callable<List<PlayerAuth>>() {
public List<PlayerAuth> call() throws Exception {
return database.getLoggedPlayers();
}
}).get();
} catch (InterruptedException e1) {
return (new ArrayList<PlayerAuth>());
} catch (ExecutionException e1) {
return (new ArrayList<PlayerAuth>());
} finally {
executor.shutdown();
} catch (Exception e) {
return new ArrayList<>();
}
return result;
}
}

View File

@ -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<PlayerAuth> getAllAuths() {
List<PlayerAuth> auths = new ArrayList<PlayerAuth>();
List<PlayerAuth> 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());

View File

@ -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