forked from TuxCoding/FastLogin
Fix thread-safety in async forcelogin task
This commit is contained in:
@ -4,8 +4,12 @@ import com.github.games647.fastlogin.bukkit.hooks.BukkitAuthPlugin;
|
|||||||
import com.google.common.io.ByteArrayDataOutput;
|
import com.google.common.io.ByteArrayDataOutput;
|
||||||
import com.google.common.io.ByteStreams;
|
import com.google.common.io.ByteStreams;
|
||||||
|
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.metadata.FixedMetadataValue;
|
import org.bukkit.metadata.FixedMetadataValue;
|
||||||
@ -13,7 +17,7 @@ import org.bukkit.metadata.FixedMetadataValue;
|
|||||||
public class ForceLoginTask implements Runnable {
|
public class ForceLoginTask implements Runnable {
|
||||||
|
|
||||||
protected final FastLoginBukkit plugin;
|
protected final FastLoginBukkit plugin;
|
||||||
private final Player player;
|
protected final Player player;
|
||||||
|
|
||||||
public ForceLoginTask(FastLoginBukkit plugin, Player player) {
|
public ForceLoginTask(FastLoginBukkit plugin, Player player) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
@ -22,7 +26,7 @@ public class ForceLoginTask implements Runnable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
if (!player.isOnline()) {
|
if (!isOnlineThreadSafe()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,7 +60,7 @@ public class ForceLoginTask implements Runnable {
|
|||||||
sendSuccessNotification();
|
sendSuccessNotification();
|
||||||
} else {
|
} else {
|
||||||
boolean success = false;
|
boolean success = false;
|
||||||
if (session.isVerified()) {
|
if (isOnlineThreadSafe() && session.isVerified()) {
|
||||||
if (session.needsRegistration()) {
|
if (session.needsRegistration()) {
|
||||||
success = forceRegister(authPlugin, player);
|
success = forceRegister(authPlugin, player);
|
||||||
} else {
|
} else {
|
||||||
@ -104,4 +108,21 @@ public class ForceLoginTask implements Runnable {
|
|||||||
player.sendPluginMessage(plugin, plugin.getName(), dataOutput.toByteArray());
|
player.sendPluginMessage(plugin, plugin.getName(), dataOutput.toByteArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isOnlineThreadSafe() {
|
||||||
|
//the playerlist isn't thread-safe
|
||||||
|
Future<Boolean> onlineFuture = Bukkit.getScheduler().callSyncMethod(plugin, new Callable<Boolean>() {
|
||||||
|
@Override
|
||||||
|
public Boolean call() throws Exception {
|
||||||
|
return player.isOnline();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
return onlineFuture.get();
|
||||||
|
} catch (InterruptedException | ExecutionException ex) {
|
||||||
|
plugin.getLogger().log(Level.SEVERE, "Failed to perform thread-safe online check", ex);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ public class CrazyLoginHook implements BukkitAuthPlugin {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
LoginPlayerData result = future.get();
|
LoginPlayerData result = future.get();
|
||||||
if (result != null) {
|
if (result != null && result.isLoggedIn()) {
|
||||||
//SQL-Queries should run async
|
//SQL-Queries should run async
|
||||||
crazyLoginPlugin.getCrazyDatabase().saveWithoutPassword(result);
|
crazyLoginPlugin.getCrazyDatabase().saveWithoutPassword(result);
|
||||||
return true;
|
return true;
|
||||||
@ -88,7 +88,7 @@ public class CrazyLoginHook implements BukkitAuthPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean forceRegister(final Player player, String password) {
|
public boolean forceRegister(final Player player, String password) {
|
||||||
final CrazyLoginDataDatabase crazyDatabase = crazyLoginPlugin.getCrazyDatabase();
|
CrazyLoginDataDatabase crazyDatabase = crazyLoginPlugin.getCrazyDatabase();
|
||||||
|
|
||||||
//this executes a sql query and accesses only thread safe collections so we can run it async
|
//this executes a sql query and accesses only thread safe collections so we can run it async
|
||||||
LoginPlayerData playerData = crazyLoginPlugin.getPlayerData(player.getName());
|
LoginPlayerData playerData = crazyLoginPlugin.getPlayerData(player.getName());
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -81,6 +81,12 @@ public class LoginSecurityHook implements BukkitAuthPlugin {
|
|||||||
|
|
||||||
//this executes a sql query without interacting with other parts so we can run it async.
|
//this executes a sql query without interacting with other parts so we can run it async.
|
||||||
dataManager.register(uuidString, passwordHash, securityPlugin.hasher.getTypeId(), ipAddress.toString());
|
dataManager.register(uuidString, passwordHash, securityPlugin.hasher.getTypeId(), ipAddress.toString());
|
||||||
return forceLogin(player);
|
String storedPassword = dataManager.getPassword(uuidString);
|
||||||
|
if (storedPassword != null && storedPassword.equals(passwordHash)) {
|
||||||
|
//the register method silents any excpetion so check if our entry was saved
|
||||||
|
return forceLogin(player);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ public class RoyalAuthHook implements BukkitAuthPlugin {
|
|||||||
//not thread-safe
|
//not thread-safe
|
||||||
authPlayer.login();
|
authPlayer.login();
|
||||||
|
|
||||||
return true;
|
return authPlayer.isLoggedIn();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -32,11 +32,10 @@ public class xAuthHook implements BukkitAuthPlugin {
|
|||||||
xAuthPlayer xAuthPlayer = xAuthPlugin.getPlayerManager().getPlayer(player);
|
xAuthPlayer xAuthPlayer = xAuthPlugin.getPlayerManager().getPlayer(player);
|
||||||
if (xAuthPlayer != null) {
|
if (xAuthPlayer != null) {
|
||||||
//we checked that the player is premium (paid account)
|
//we checked that the player is premium (paid account)
|
||||||
//unprotect the inventory, op status...
|
|
||||||
xAuthPlayer.setPremium(true);
|
xAuthPlayer.setPremium(true);
|
||||||
|
|
||||||
xAuthPlugin.getPlayerManager().doLogin(xAuthPlayer);
|
//unprotect the inventory, op status...
|
||||||
return true;
|
return xAuthPlugin.getPlayerManager().doLogin(xAuthPlayer);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
Reference in New Issue
Block a user