forked from TuxCoding/FastLogin
Fix saving bug
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -41,6 +41,10 @@ public class FastLoginBungee extends Plugin {
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
if (!getDataFolder().exists()) {
|
||||
getDataFolder().mkdir();
|
||||
}
|
||||
|
||||
File configFile = new File(getDataFolder(), "config.yml");
|
||||
if (!configFile.exists()) {
|
||||
try (InputStream in = getResourceAsStream("config.yml")) {
|
||||
@ -53,13 +57,13 @@ public class FastLoginBungee extends Plugin {
|
||||
try {
|
||||
configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile);
|
||||
|
||||
String driver = configuration.getString("storage.driver");
|
||||
String host = configuration.getString("storage.host", "");
|
||||
int port = configuration.getInt("storage.port", 3306);
|
||||
String database = configuration.getString("storage.database");
|
||||
String driver = configuration.getString("driver");
|
||||
String host = configuration.getString("host", "");
|
||||
int port = configuration.getInt("port", 3306);
|
||||
String database = configuration.getString("database");
|
||||
|
||||
String username = configuration.getString("storage.username", "");
|
||||
String password = configuration.getString("storage.password", "");
|
||||
String username = configuration.getString("username", "");
|
||||
String password = configuration.getString("password", "");
|
||||
storage = new Storage(this, driver, host, port, database, username, password);
|
||||
try {
|
||||
storage.createTables();
|
||||
|
@ -1,7 +1,6 @@
|
||||
package com.github.games647.fastlogin.bungee;
|
||||
|
||||
import com.github.games647.fastlogin.bungee.hooks.BungeeAuthPlugin;
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
@ -18,14 +17,10 @@ import net.md_5.bungee.api.chat.TextComponent;
|
||||
import net.md_5.bungee.api.connection.PendingConnection;
|
||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||
import net.md_5.bungee.api.connection.Server;
|
||||
import net.md_5.bungee.api.event.LoginEvent;
|
||||
import net.md_5.bungee.api.event.PluginMessageEvent;
|
||||
import net.md_5.bungee.api.event.PreLoginEvent;
|
||||
import net.md_5.bungee.api.event.ServerConnectedEvent;
|
||||
import net.md_5.bungee.api.plugin.Listener;
|
||||
import net.md_5.bungee.connection.InitialHandler;
|
||||
import net.md_5.bungee.connection.LoginResult;
|
||||
import net.md_5.bungee.connection.LoginResult.Property;
|
||||
import net.md_5.bungee.event.EventHandler;
|
||||
|
||||
/**
|
||||
@ -57,10 +52,13 @@ public class PlayerConnectionListener 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()) {
|
||||
connection.setOnlineMode(true);
|
||||
} else if (playerProfile.getUserId() == -1) {
|
||||
//user not exists in the db
|
||||
BungeeAuthPlugin authPlugin = plugin.getBungeeAuthPlugin();
|
||||
if (plugin.getConfiguration().getBoolean("autoRegister") && !authPlugin.isRegistered(username)) {
|
||||
if (plugin.getConfiguration().getBoolean("autoRegister")
|
||||
&& (authPlugin == null || !authPlugin.isRegistered(username))) {
|
||||
UUID premiumUUID = plugin.getMojangApiConnector().getPremiumUUID(username);
|
||||
if (premiumUUID != null) {
|
||||
plugin.getLogger().log(Level.FINER, "Player {0} uses a premium username", username);
|
||||
@ -68,30 +66,6 @@ public class PlayerConnectionListener implements Listener {
|
||||
pendingAutoRegister.put(connection, new Object());
|
||||
}
|
||||
}
|
||||
} else if (playerProfile.isPremium()) {
|
||||
connection.setOnlineMode(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onLogin(LoginEvent loginEvent) {
|
||||
PendingConnection connection = loginEvent.getConnection();
|
||||
String username = connection.getName();
|
||||
if (connection.isOnlineMode()) {
|
||||
//bungeecord will do this automatically so override it on disabled option
|
||||
if (!plugin.getConfiguration().getBoolean("premiumUuid")) {
|
||||
UUID offlineUUID = UUID.nameUUIDFromBytes(("OfflinePlayer:" + username).getBytes(Charsets.UTF_8));
|
||||
connection.setUniqueId(offlineUUID);
|
||||
}
|
||||
|
||||
if (!plugin.getConfiguration().getBoolean("forwardSkin")) {
|
||||
InitialHandler initialHandler = (InitialHandler) connection;
|
||||
//this is null on offline mode
|
||||
LoginResult loginProfile = initialHandler.getLoginProfile();
|
||||
if (loginProfile != null) {
|
||||
loginProfile.setProperties(new Property[]{});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -100,12 +74,29 @@ public class PlayerConnectionListener implements Listener {
|
||||
public void onServerConnected(ServerConnectedEvent serverConnectedEvent) {
|
||||
ProxiedPlayer player = serverConnectedEvent.getPlayer();
|
||||
//send message even when the online mode is activated by default
|
||||
|
||||
final PlayerProfile playerProfile = plugin.getStorage().getProfile(player.getName(), false);
|
||||
if (playerProfile.getUserId() == -1) {
|
||||
ProxyServer.getInstance().getScheduler().runAsync(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
plugin.getStorage().save(playerProfile);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (player.getPendingConnection().isOnlineMode()) {
|
||||
Server server = serverConnectedEvent.getServer();
|
||||
|
||||
boolean autoRegister = pendingAutoRegister.remove(player.getPendingConnection()) != null;
|
||||
|
||||
ByteArrayDataOutput dataOutput = ByteStreams.newDataOutput();
|
||||
//subchannel name
|
||||
dataOutput.writeUTF("CHECKED");
|
||||
if (autoRegister) {
|
||||
dataOutput.writeUTF("AUTO_REGISTER");
|
||||
} else {
|
||||
dataOutput.writeUTF("AUTO_LOGIN");
|
||||
}
|
||||
|
||||
//Data is sent through a random player. We have to tell the Bukkit version of this plugin the target
|
||||
dataOutput.writeUTF(player.getName());
|
||||
@ -119,12 +110,11 @@ public class PlayerConnectionListener implements Listener {
|
||||
|
||||
BungeeAuthPlugin authPlugin = plugin.getBungeeAuthPlugin();
|
||||
if (authPlugin != null) {
|
||||
Object existed = pendingAutoRegister.remove(player.getPendingConnection());
|
||||
if (existed == null) {
|
||||
authPlugin.forceLogin(player);
|
||||
} else {
|
||||
if (autoRegister) {
|
||||
String password = plugin.generateStringPassword();
|
||||
authPlugin.forceRegister(player, password);
|
||||
} else {
|
||||
authPlugin.forceLogin(player);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user