forked from TuxCoding/FastLogin
Added auto login importers
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
+43
-21
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+38
-13
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<? extends Importer> importerClass;
|
||||
|
||||
ImportPlugin(Class<? extends Importer> importer) {
|
||||
this.importerClass = importer;
|
||||
}
|
||||
|
||||
public Class<? extends Importer> getImporter() {
|
||||
return importerClass;
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user