Fix saving bug

This commit is contained in:
games647
2016-04-27 20:35:23 +02:00
parent b697dc6655
commit 6edd40742d
6 changed files with 112 additions and 104 deletions
@@ -51,58 +51,62 @@ public class BukkitJoinListener implements Listener {
@Override
public void run() {
if (player.isOnline()) {
//remove the bungeecord identifier
String id = '/' + player.getAddress().getAddress().getHostAddress() + ':'
+ player.getAddress().getPort();
PlayerSession session = plugin.getSessions().get(id);
if (!player.isOnline()) {
return;
}
//blacklist this target player for BungeeCord Id brute force attacks
player.setMetadata(plugin.getName(), new FixedMetadataValue(plugin, true));
//check if it's the same player as we checked before
//remove the bungeecord identifier
String id = '/' + player.getAddress().getAddress().getHostAddress() + ':'
+ player.getAddress().getPort();
PlayerSession session = plugin.getSessions().get(id);
//blacklist this target player for BungeeCord Id brute force attacks
player.setMetadata(plugin.getName(), new FixedMetadataValue(plugin, true));
//check if it's the same player as we checked before
if (session != null && player.getName().equals(session.getUsername())) {
final Storage storage = plugin.getStorage();
PlayerProfile playerProfile = null;
if (storage != null) {
playerProfile = storage.getProfile(session.getUsername(), false);
}
BukkitAuthPlugin authPlugin = plugin.getAuthPlugin();
if (session != null && player.getName().equals(session.getUsername()) && session.isVerified()
&& authPlugin != null) {
if (session.isVerified() && authPlugin != null) {
if (session.needsRegistration()) {
plugin.getLogger().log(Level.FINE, "Register player {0}", player.getName());
final Storage storage = plugin.getStorage();
if (storage != null) {
final PlayerProfile playerProfile = storage.getProfile(session.getUsername(), false);
playerProfile.setUuid(session.getUuid());
playerProfile.setPremium(true);
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
storage.save(playerProfile);
}
});
}
String generatedPassword = plugin.generateStringPassword();
authPlugin.forceRegister(player, generatedPassword);
player.sendMessage(ChatColor.DARK_GREEN + "Auto registered with password: "
+ generatedPassword);
player.sendMessage(ChatColor.DARK_GREEN + "You may want change it?");
if (playerProfile != null) {
playerProfile.setUuid(session.getUuid());
playerProfile.setPremium(true);
}
} else {
plugin.getLogger().log(Level.FINE, "Logging player {0} in", player.getName());
authPlugin.forceLogin(player);
player.sendMessage(ChatColor.DARK_GREEN + "Auto logged in");
final Storage storage = plugin.getStorage();
if (storage != null) {
final PlayerProfile playerProfile = storage.getProfile(session.getUsername(), false);
if (playerProfile != null) {
playerProfile.setUuid(session.getUuid());
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
storage.save(playerProfile);
}
});
playerProfile.setPremium(true);
}
}
}
final PlayerProfile toSave = playerProfile;
if (toSave != null) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
storage.save(toSave);
}
});
}
}
}
//Wait before auth plugin and we received a message from BungeeCord initializes the player
@@ -2,6 +2,7 @@ package com.github.games647.fastlogin.bukkit.listener;
import com.github.games647.fastlogin.bukkit.FastLoginBukkit;
import com.github.games647.fastlogin.bukkit.PlayerSession;
import com.github.games647.fastlogin.bukkit.hooks.BukkitAuthPlugin;
import com.google.common.base.Charsets;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
@@ -12,6 +13,7 @@ import java.io.IOException;
import java.util.UUID;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.plugin.messaging.PluginMessageListener;
@@ -45,33 +47,41 @@ public class BungeeCordListener implements PluginMessageListener {
String subchannel = dataInput.readUTF();
plugin.getLogger().log(Level.FINEST, "Received plugin message for subchannel {0} from {1}"
, new Object[]{subchannel, player});
if ("CHECKED".equalsIgnoreCase(subchannel)) {
//make sure the proxy is allowed to transfer data to us
String playerName = dataInput.readUTF();
//check if the player is still online or disconnected
Player checkedPlayer = plugin.getServer().getPlayerExact(playerName);
if (checkedPlayer != null && checkedPlayer.isOnline()
//fail if target player is blacklisted because already authed or wrong bungeecord id
&& !checkedPlayer.hasMetadata(plugin.getName())) {
//bungeecord UUID
long mostSignificantBits = dataInput.readLong();
long leastSignificantBits = dataInput.readLong();
UUID sourceId = new UUID(mostSignificantBits, leastSignificantBits);
final String playerName = dataInput.readUTF();
//fail if BungeeCord support is disabled (id = null)
if (sourceId.equals(proxyId)) {
PlayerSession playerSession = new PlayerSession(playerName);
//check if the player is still online or disconnected
final Player checkedPlayer = plugin.getServer().getPlayerExact(playerName);
//fail if target player is blacklisted because already authed or wrong bungeecord id
if (checkedPlayer != null && !checkedPlayer.hasMetadata(plugin.getName())) {
//bungeecord UUID
long mostSignificantBits = dataInput.readLong();
long leastSignificantBits = dataInput.readLong();
UUID sourceId = new UUID(mostSignificantBits, leastSignificantBits);
//fail if BungeeCord support is disabled (id = null)
if (sourceId.equals(proxyId)) {
final PlayerSession playerSession = new PlayerSession(playerName);
if ("AUTO_LOGIN".equalsIgnoreCase(subchannel)) {
playerSession.setVerified(true);
playerSession.setRegistered(true);
plugin.getSessions().put(checkedPlayer.getAddress().toString(), playerSession);
} else if ("AUTO_REGISTER".equalsIgnoreCase(subchannel)) {
playerSession.setVerified(true);
//put it only if the user doesn't has a session open
//so that the player have to send the bungeecord packet and cannot skip the verification then
plugin.getSessions().putIfAbsent(checkedPlayer.getAddress().toString(), playerSession);
} else {
//blacklist target for the current login
checkedPlayer.setMetadata(plugin.getName(), new FixedMetadataValue(plugin, true));
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
BukkitAuthPlugin authPlugin = plugin.getAuthPlugin();
//we need to check if the player is registered on Bukkit too
if (authPlugin != null && !authPlugin.isRegistered(playerName)) {
plugin.getSessions().put(checkedPlayer.getAddress().toString(), playerSession);
}
}
});
}
} else {
//blacklist target for the current login
checkedPlayer.setMetadata(plugin.getName(), new FixedMetadataValue(plugin, true));
}
}
}
@@ -36,8 +36,10 @@ public class ProtocolSupportListener implements Listener {
PlayerProfile playerProfile = plugin.getStorage().getProfile(username, true);
if (playerProfile != null) {
//user not exists in the db
if (!playerProfile.isPremium() && playerProfile.getUserId() == -1) {
if (playerProfile.isPremium()) {
startPremiumSession(username, loginStartEvent, true);
} else if (playerProfile.getUserId() == -1) {
//user not exists in the db
BukkitAuthPlugin authPlugin = plugin.getAuthPlugin();
if (plugin.getConfig().getBoolean("autoRegister") && !authPlugin.isRegistered(username)) {
UUID premiumUUID = plugin.getApiConnector().getPremiumUUID(username);
@@ -46,8 +48,6 @@ public class ProtocolSupportListener implements Listener {
startPremiumSession(username, loginStartEvent, false);
}
}
} else if (playerProfile.isPremium()) {
startPremiumSession(username, loginStartEvent, true);
}
}
}
@@ -77,8 +77,10 @@ public class StartPacketListener extends PacketAdapter {
PlayerProfile playerProfile = plugin.getStorage().getProfile(username, true);
if (playerProfile != null) {
//user not exists in the db
if (!playerProfile.isPremium() && playerProfile.getUserId() == -1) {
if (playerProfile.isPremium()) {
enablePremiumLogin(username, sessionKey, player, packetEvent, true);
} else if (playerProfile.getUserId() == -1) {
//user not exists in the db
BukkitAuthPlugin authPlugin = plugin.getAuthPlugin();
if (plugin.getConfig().getBoolean("autoRegister") && !authPlugin.isRegistered(username)) {
UUID premiumUUID = plugin.getApiConnector().getPremiumUUID(username);
@@ -87,8 +89,6 @@ public class StartPacketListener extends PacketAdapter {
enablePremiumLogin(username, sessionKey, player, packetEvent, false);
}
}
} else if (playerProfile.isPremium()) {
enablePremiumLogin(username, sessionKey, player, packetEvent, true);
}
}
}