From ad60397851e37a5d0c8ff174a5bf91e8a01db833 Mon Sep 17 00:00:00 2001 From: games647 Date: Thu, 25 Aug 2016 17:45:09 +0200 Subject: [PATCH] Added auto login importers --- CHANGELOG.md | 3 + .../fastlogin/bukkit/FastLoginBukkit.java | 2 + .../bukkit/commands/ImportCommand.java | 85 +++++++++++++++++++ bukkit/src/main/resources/plugin.yml | 7 +- .../fastlogin/bungee/FastLoginBungee.java | 3 + .../fastlogin/bungee/ImportCommand.java | 85 +++++++++++++++++++ .../games647/fastlogin/core/AuthStorage.java | 4 + .../fastlogin/core/FastLoginCore.java | 47 ++++++++++ .../core/importer/AutoInImporter.java | 64 +++++++++----- .../fastlogin/core/importer/BPAImporter.java | 43 +++++++--- .../core/importer/ElDziAuthImporter.java | 51 ++++++++--- .../fastlogin/core/importer/ImportPlugin.java | 20 +++++ .../fastlogin/core/importer/Importer.java | 5 +- pom.xml | 2 +- 14 files changed, 370 insertions(+), 51 deletions(-) create mode 100644 bukkit/src/main/java/com/github/games647/fastlogin/bukkit/commands/ImportCommand.java create mode 100644 bungee/src/main/java/com/github/games647/fastlogin/bungee/ImportCommand.java create mode 100644 core/src/main/java/com/github/games647/fastlogin/core/importer/ImportPlugin.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a4b4040..010fb1ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ######1.8 +* Added autoIn importer +* Added BFA importer +* Added ElDziAuth importer * Fix third-party not premium player detection * Fix ProtocolSupport BungeeCord * Fix duplicate logins for BungeeAuth users diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/FastLoginBukkit.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/FastLoginBukkit.java index b2f53235..9459b6dd 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/FastLoginBukkit.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/FastLoginBukkit.java @@ -4,6 +4,7 @@ import com.avaje.ebeaninternal.api.ClassUtil; import com.comphenix.protocol.AsynchronousManager; import com.comphenix.protocol.ProtocolLibrary; import com.github.games647.fastlogin.bukkit.commands.CrackedCommand; +import com.github.games647.fastlogin.bukkit.commands.ImportCommand; import com.github.games647.fastlogin.bukkit.commands.PremiumCommand; import com.github.games647.fastlogin.bukkit.hooks.BukkitAuthPlugin; import com.github.games647.fastlogin.bukkit.listener.BukkitJoinListener; @@ -125,6 +126,7 @@ public class FastLoginBukkit extends JavaPlugin { //register commands using a unique name getCommand("premium").setExecutor(new PremiumCommand(this)); getCommand("cracked").setExecutor(new CrackedCommand(this)); + getCommand("import-auth").setExecutor(new ImportCommand(this)); } @Override diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/commands/ImportCommand.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/commands/ImportCommand.java new file mode 100644 index 00000000..ecfedf4f --- /dev/null +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/commands/ImportCommand.java @@ -0,0 +1,85 @@ +package com.github.games647.fastlogin.bukkit.commands; + +import com.github.games647.fastlogin.bukkit.FastLoginBukkit; +import com.github.games647.fastlogin.core.AuthStorage; +import com.github.games647.fastlogin.core.FastLoginCore; +import com.github.games647.fastlogin.core.importer.ImportPlugin; +import org.bukkit.ChatColor; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; + +public class ImportCommand implements CommandExecutor { + + protected final FastLoginBukkit plugin; + + public ImportCommand(FastLoginBukkit plugin) { + this.plugin = plugin; + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (args.length < 2) { + sender.sendMessage(ChatColor.DARK_RED + "You need to specify the import plugin and database type"); + return true; + } + + ImportPlugin importPlugin; + switch (args[0].toLowerCase()) { + case "autoin": + importPlugin = ImportPlugin.AUTO_IN; + break; + case "bpa": + importPlugin = ImportPlugin.BPA; + break; + case "eldzi": + importPlugin = ImportPlugin.ELDZI; + break; + default: + sender.sendMessage(ChatColor.DARK_RED + "Unknown auto login plugin"); + return true; + } + + boolean sqlite; + switch (args[1].toLowerCase()) { + case "sqlite": + sqlite = true; + break; + case "mysql": + sqlite = false; + break; + default: + sender.sendMessage(ChatColor.DARK_RED + "Unknown storage type to import from. Either SQLite or MySQL"); + return true; + } + + String host = ""; + String database = ""; + String username = ""; + String password = ""; + if (!sqlite) { + if (args.length <= 5) { + sender.sendMessage(ChatColor.DARK_RED + "If importing from MySQL, you need to specify host database " + + "and username passowrd too"); + return true; + } + + host = args[2]; + database = args[3]; + username = args[4]; + password = args[5]; + } + + FastLoginCore core = plugin.getCore(); + AuthStorage storage = core.getStorage(); + boolean success = core.importDatabase(importPlugin, true, storage, host, database, username, password); + if (success) { + sender.sendMessage(ChatColor.DARK_GREEN + "Successful imported the data"); + } else { + sender.sendMessage(ChatColor.DARK_RED + "Failed to import the data. Check out the logs"); + } + + return true; + } +} diff --git a/bukkit/src/main/resources/plugin.yml b/bukkit/src/main/resources/plugin.yml index 7f0c3fba..059b721f 100644 --- a/bukkit/src/main/resources/plugin.yml +++ b/bukkit/src/main/resources/plugin.yml @@ -40,6 +40,11 @@ commands: usage: / [player] permission: ${project.artifactId}.command.unpremium + import-auth: + description: 'Imports the auth data from another auto login' + usage: / [player] + permission: ${project.artifactId}.command.import + permissions: ${project.artifactId}.command.premium: description: 'Label themselves as premium' @@ -54,7 +59,7 @@ permissions: description: 'Label themselves as cracked' default: true - ${project.artifactId}.command..cracked.other: + ${project.artifactId}.command.cracked.other: description: 'Label others as cracked' children: ${project.artifactId}.command.cracked: true \ No newline at end of file diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/FastLoginBungee.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/FastLoginBungee.java index 49114e20..06e9bee7 100644 --- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/FastLoginBungee.java +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/FastLoginBungee.java @@ -77,6 +77,9 @@ public class FastLoginBungee extends Plugin { getProxy().getPluginManager().registerListener(this, new PlayerConnectionListener(this)); getProxy().getPluginManager().registerListener(this, new PluginMessageListener(this)); + //bungee only commands + getProxy().getPluginManager().registerCommand(this, new ImportCommand(this)); + //this is required to listen to messages from the server getProxy().registerChannel(getDescription().getName()); diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/ImportCommand.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/ImportCommand.java new file mode 100644 index 00000000..f33a852c --- /dev/null +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/ImportCommand.java @@ -0,0 +1,85 @@ +package com.github.games647.fastlogin.bungee; + +import com.github.games647.fastlogin.core.AuthStorage; +import com.github.games647.fastlogin.core.FastLoginCore; +import com.github.games647.fastlogin.core.importer.ImportPlugin; + +import net.md_5.bungee.api.ChatColor; +import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.plugin.Command; + +public class ImportCommand extends Command { + + private final FastLoginBungee plugin; + + public ImportCommand(FastLoginBungee plugin) { + super("import-db"); + + this.plugin = plugin; + } + + @Override + public void execute(CommandSender sender, String[] args) { + if (args.length < 2) { + sender.sendMessage(ChatColor.DARK_RED + "You need to specify the import plugin and database type"); + return; + } + + ImportPlugin importPlugin; + switch (args[0].toLowerCase()) { + case "autoin": + importPlugin = ImportPlugin.AUTO_IN; + break; + case "bpa": + importPlugin = ImportPlugin.BPA; + break; + case "eldzi": + importPlugin = ImportPlugin.ELDZI; + break; + default: + sender.sendMessage(ChatColor.DARK_RED + "Unknown auto login plugin"); + return; + } + + boolean sqlite; + switch (args[1].toLowerCase()) { + case "sqlite": + sqlite = true; + break; + case "mysql": + sqlite = false; + break; + default: + sender.sendMessage(ChatColor.DARK_RED + "Unknown storage type to import from. Either SQLite or MySQL"); + return; + } + + String host = ""; + String database = ""; + String username = ""; + String password = ""; + if (!sqlite) { + if (args.length <= 5) { + sender.sendMessage(ChatColor.DARK_RED + "If importing from MySQL, you need to specify host database " + + "and username passowrd too"); + return; + } + + host = args[2]; + database = args[3]; + username = args[4]; + password = args[5]; + } + + FastLoginCore core = plugin.getCore(); + AuthStorage storage = core.getStorage(); + boolean success = core.importDatabase(importPlugin, true, storage, host, database, username, password); + if (success) { + sender.sendMessage(ChatColor.DARK_GREEN + "Successful imported the data"); + } else { + sender.sendMessage(ChatColor.DARK_RED + "Failed to import the data. Check out the logs"); + } + + return; + } +} diff --git a/core/src/main/java/com/github/games647/fastlogin/core/AuthStorage.java b/core/src/main/java/com/github/games647/fastlogin/core/AuthStorage.java index bebc31c7..2aaf93e5 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/AuthStorage.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/AuthStorage.java @@ -42,6 +42,10 @@ public class AuthStorage { this.dataSource = new HikariDataSource(databaseConfig); } + public HikariDataSource getDataSource() { + return dataSource; + } + public void createTables() throws SQLException { Connection con = null; Statement createStmt = null; diff --git a/core/src/main/java/com/github/games647/fastlogin/core/FastLoginCore.java b/core/src/main/java/com/github/games647/fastlogin/core/FastLoginCore.java index c0c57b0c..082af256 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/FastLoginCore.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/FastLoginCore.java @@ -1,6 +1,13 @@ package com.github.games647.fastlogin.core; +import com.github.games647.fastlogin.core.importer.AutoInImporter; +import com.github.games647.fastlogin.core.importer.ImportPlugin; +import com.github.games647.fastlogin.core.importer.Importer; + import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; import java.util.Map; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; @@ -59,6 +66,46 @@ public abstract class FastLoginCore { } } + public boolean importDatabase(ImportPlugin plugin, boolean sqlite, AuthStorage storage, String host, String database + , String username, String pass) { + if (sqlite && (plugin == ImportPlugin.BPA || plugin == ImportPlugin.ELDZI)) { + throw new IllegalArgumentException("These plugins doesn't support flat file databases"); + } + + Importer importer; + try { + importer = plugin.getImporter().newInstance(); + } catch (Exception ex) { + getLogger().log(Level.SEVERE, "Couldn't not setup importer class", ex); + return false; + } + + try { + if (sqlite && plugin == ImportPlugin.AUTO_IN) { + //load sqlite driver + Class.forName("org.sqlite.JDBC"); + + String jdbcUrl = "jdbc:sqlite:" + AutoInImporter.getSQLitePath(); + Connection con = DriverManager.getConnection(jdbcUrl); + importer.importData(con, storage.getDataSource(), storage); + return true; + } else { + Class.forName("com.mysql.jdbc.Driver"); + + String jdbcUrl = "jdbc:mysql://" + host + "/" + database; + Connection con = DriverManager.getConnection(jdbcUrl, username, pass); + importer.importData(con, storage.getDataSource(), storage); + return true; + } + } catch (ClassNotFoundException ex) { + getLogger().log(Level.SEVERE, "Cannot find SQL driver. Do you removed it?", ex); + } catch (SQLException ex) { + getLogger().log(Level.SEVERE, "Couldn't import data. Aborting...", ex); + } + + return false; + } + public void close() { if (storage != null) { storage.close(); diff --git a/core/src/main/java/com/github/games647/fastlogin/core/importer/AutoInImporter.java b/core/src/main/java/com/github/games647/fastlogin/core/importer/AutoInImporter.java index ed54882b..92b1d32b 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/importer/AutoInImporter.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/importer/AutoInImporter.java @@ -1,51 +1,73 @@ package com.github.games647.fastlogin.core.importer; +import com.github.games647.fastlogin.core.AuthStorage; +import com.github.games647.fastlogin.core.PlayerProfile; + import java.sql.Connection; +import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.util.UUID; import javax.sql.DataSource; public class AutoInImporter extends Importer { + private static final String PLUGIN_NAME = "AutoIn"; + + private static final String SQLITE_FILE = "plugins/" + PLUGIN_NAME + "/AutoIn_PlayerOptions.db"; + private static final String USER_TABLE = "nicknames"; private static final String UUID_TABLE = "uuids"; private static final String SESSION_TABLE = "sessions"; + public static String getSQLitePath() { + return SQLITE_FILE; + } + @Override - public int importData(DataSource source, DataSource target, String targetTable) throws SQLException { - Connection con = null; + public int importData(Connection source, DataSource target, AuthStorage storage) throws SQLException { Statement stmt = null; + ResultSet resultSet = null; try { - con = source.getConnection(); - stmt = con.createStatement(); - int importedRows = stmt.executeUpdate("INSERT INTO " + targetTable + " (Name, Premium, LastIp, UUID) SELECT" - + " name AS Name," - /* Enable premium authentication only for those who want to be auto logged in, so - they have their cracked protection disabled */ - + " !protection AND premium AS Premium," - + " '' AS LastIp," - /* Remove the dashes - returns null if puuid is null too */ - + " REPLACE(puuid, '-', '') AS UUID" - + " FROM " + USER_TABLE - /* Get the premium uuid */ + stmt = source.createStatement(); + resultSet = stmt.executeQuery("SELECT name, protection, premium, puuid FROM " + USER_TABLE + " LEFT JOIN " + " (" /* Prevent duplicates */ + "SELECT * FROM " + UUID_TABLE + " GROUP BY nickname_id" + ") uuids" + " ON " + USER_TABLE + ".id = uuids.nickname_id"); - /* FastLogin will also make lookups on the uuid column for name changes - the old 1.6.2 version won't check if those user have premium enabled + int rows = 0; + while (resultSet.next()) { + String name = resultSet.getString(1); + boolean protection = resultSet.getBoolean(2); + /* Enable premium authentication only for those who want to be auto logged in, + so they have their cracked protection disabled */ + boolean premium = !protection && resultSet.getBoolean(3); + String puuid = resultSet.getString(4); - so it could happen that a premium could steal the account if we don't do this + /* FastLogin will also make lookups on the uuid column for name changes + the old 1.6.2 version won't check if those user have premium enabled - It seems the uuid is saved on autoin too if the player is cracked */ - stmt.executeUpdate("UPDATE `premium` SET `UUID`=NULL WHERE PREMIUM=0"); - return importedRows; + so it could happen that a premium could steal the account if we don't do this + + It seems the uuid is saved on autoin too if the player is cracked */ + PlayerProfile profile; + if (premium) { + profile = new PlayerProfile(UUID.fromString(puuid), name, premium, ""); + } else { + profile = new PlayerProfile(null, name, premium, ""); + } + + storage.save(profile); + rows++; + } + + return rows; } finally { closeQuietly(stmt); - closeQuietly(con); + closeQuietly(resultSet); } } } diff --git a/core/src/main/java/com/github/games647/fastlogin/core/importer/BPAImporter.java b/core/src/main/java/com/github/games647/fastlogin/core/importer/BPAImporter.java index 9c11838b..ca32ff95 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/importer/BPAImporter.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/importer/BPAImporter.java @@ -1,8 +1,13 @@ package com.github.games647.fastlogin.core.importer; +import com.github.games647.fastlogin.core.AuthStorage; +import com.github.games647.fastlogin.core.PlayerProfile; + import java.sql.Connection; +import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.sql.Timestamp; import javax.sql.DataSource; @@ -11,23 +16,35 @@ public class BPAImporter extends Importer { private static final String DEFAULT_TABLE_NAME = "users"; @Override - public int importData(DataSource source, DataSource target, String targetTable) throws SQLException { - Connection con = null; + public int importData(Connection source, DataSource target, AuthStorage storage) throws SQLException { Statement stmt = null; + ResultSet resultSet = null; try { - con = source.getConnection(); - stmt = con.createStatement(); - int importedRows = stmt.executeUpdate("INSERT INTO " + targetTable + " SELECT" - + " nick AS Name," - + " NULL AS UUID," - + " checked AS Premium," - + " lastIP AS LastIp," - + " FROM_UNIXTIME(lastJoined * 0.001) AS LastLogin" - + " FROM " + DEFAULT_TABLE_NAME); - return importedRows; + stmt = source.createStatement(); + resultSet = stmt.executeQuery("SELECT " + + "nick, " + + "checked, " + + "lastIP, " + + "FROM_UNIXTIME(lastJoined * 0.001) AS LastLogin " + + "FROM " + DEFAULT_TABLE_NAME); + + int rows = 0; + while (resultSet.next()) { + String name = resultSet.getString(1); + boolean premium = resultSet.getBoolean(2); + String lastIP = resultSet.getString(3); + Timestamp lastLogin = resultSet.getTimestamp(4); + + //uuid doesn't exist here + PlayerProfile profile = new PlayerProfile(null, name, premium, lastIP); + storage.save(profile); + rows++; + } + + return rows; } finally { closeQuietly(stmt); - closeQuietly(con); + closeQuietly(resultSet); } } } diff --git a/core/src/main/java/com/github/games647/fastlogin/core/importer/ElDziAuthImporter.java b/core/src/main/java/com/github/games647/fastlogin/core/importer/ElDziAuthImporter.java index fd21f299..668a7933 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/importer/ElDziAuthImporter.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/importer/ElDziAuthImporter.java @@ -1,8 +1,14 @@ package com.github.games647.fastlogin.core.importer; +import com.github.games647.fastlogin.core.AuthStorage; +import com.github.games647.fastlogin.core.PlayerProfile; + import java.sql.Connection; +import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.sql.Timestamp; +import java.util.UUID; import javax.sql.DataSource; @@ -11,23 +17,42 @@ public class ElDziAuthImporter extends Importer { private static final String TABLE_NAME = "accounts"; @Override - public int importData(DataSource source, DataSource target, String targetTable) throws SQLException { - Connection con = null; + public int importData(Connection source, DataSource target, AuthStorage storage) throws SQLException { Statement stmt = null; + ResultSet resultSet = null; try { - con = source.getConnection(); - stmt = con.createStatement(); - int importedRows = stmt.executeUpdate("INSERT INTO " + targetTable + " SELECT" - + " nick AS Name," - + " uuid AS UUID," - + " premium AS Premium," - + " lastIp AS LastIp," - + " FROM_UNIXTIME(lastPlayed * 0.001) AS LastLogin" - + " FROM " + TABLE_NAME); - return importedRows; + stmt = source.createStatement(); + resultSet = stmt.executeQuery("SELECT " + + "nick, " + + "premium, " + + "lastIP, " + + "FROM_UNIXTIME(lastPlayed * 0.001) AS LastLogin " + + "FROM " + TABLE_NAME); + + int rows = 0; + while (resultSet.next()) { + String name = resultSet.getString(1); + boolean premium = resultSet.getBoolean(2); + String lastIP = resultSet.getString(3); + Timestamp lastLogin = resultSet.getTimestamp(4); + + String uuid = resultSet.getString(5); + + PlayerProfile profile; + if (premium) { + profile = new PlayerProfile(UUID.fromString(uuid), name, premium, lastIP); + } else { + profile = new PlayerProfile(null, name, premium, ""); + } + + storage.save(profile); + rows++; + } + + return rows; } finally { closeQuietly(stmt); - closeQuietly(con); + closeQuietly(resultSet); } } } diff --git a/core/src/main/java/com/github/games647/fastlogin/core/importer/ImportPlugin.java b/core/src/main/java/com/github/games647/fastlogin/core/importer/ImportPlugin.java new file mode 100644 index 00000000..fbf07892 --- /dev/null +++ b/core/src/main/java/com/github/games647/fastlogin/core/importer/ImportPlugin.java @@ -0,0 +1,20 @@ +package com.github.games647.fastlogin.core.importer; + +public enum ImportPlugin { + + AUTO_IN(AutoInImporter.class), + + BPA(BPAImporter.class), + + ELDZI(ElDziAuthImporter.class); + + private final Class importerClass; + + ImportPlugin(Class importer) { + this.importerClass = importer; + } + + public Class getImporter() { + return importerClass; + } +} diff --git a/core/src/main/java/com/github/games647/fastlogin/core/importer/Importer.java b/core/src/main/java/com/github/games647/fastlogin/core/importer/Importer.java index 60944125..15f2da79 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/importer/Importer.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/importer/Importer.java @@ -1,12 +1,13 @@ package com.github.games647.fastlogin.core.importer; +import com.github.games647.fastlogin.core.AuthStorage; +import java.sql.Connection; import java.sql.SQLException; - import javax.sql.DataSource; public abstract class Importer { - public abstract int importData(DataSource source, DataSource target, String targetTable) throws SQLException; + public abstract int importData(Connection source, DataSource target, AuthStorage storage) throws SQLException; protected void closeQuietly(AutoCloseable closeable) { if (closeable != null) { diff --git a/pom.xml b/pom.xml index 1d3ad65b..7c6049c3 100644 --- a/pom.xml +++ b/pom.xml @@ -60,7 +60,7 @@ org.apache.maven.plugins maven-jar-plugin - 3.0.0 + 3.0.2 ${outputDir}