diff --git a/common/src/main/java/me/confuser/banmanager/common/commands/CommonCommand.java b/common/src/main/java/me/confuser/banmanager/common/commands/CommonCommand.java index 36d3ed9a..d4e6f834 100644 --- a/common/src/main/java/me/confuser/banmanager/common/commands/CommonCommand.java +++ b/common/src/main/java/me/confuser/banmanager/common/commands/CommonCommand.java @@ -5,6 +5,7 @@ import me.confuser.banmanager.common.BanManagerPlugin; import me.confuser.banmanager.common.CommonPlayer; import me.confuser.banmanager.common.configs.PluginInfo; +import me.confuser.banmanager.common.data.DuplicatePlayerData; import me.confuser.banmanager.common.data.PlayerData; import me.confuser.banmanager.common.data.PlayerNoteData; import me.confuser.banmanager.common.ipaddr.IPAddress; @@ -129,12 +130,12 @@ public static IPAddress getIp(String ipStr) { IPAddress ip = null; if (isName) { - List players = BanManagerPlugin.getInstance().getDuplicatePlayerStorage().retrieve(ipStr); + List players = BanManagerPlugin.getInstance().getDuplicatePlayerStorage().retrieve(ipStr); if (players.isEmpty()) { return null; } - PlayerData player = players.get(0); + DuplicatePlayerData player = players.get(0); if (player == null) { return null; diff --git a/common/src/main/java/me/confuser/banmanager/common/configs/LocalDatabaseConfig.java b/common/src/main/java/me/confuser/banmanager/common/configs/LocalDatabaseConfig.java index 43d272ff..9155460e 100644 --- a/common/src/main/java/me/confuser/banmanager/common/configs/LocalDatabaseConfig.java +++ b/common/src/main/java/me/confuser/banmanager/common/configs/LocalDatabaseConfig.java @@ -10,7 +10,7 @@ public class LocalDatabaseConfig extends DatabaseConfig { public static HashMap types = new HashMap() {{ put("players", PlayerData.class); - put("duplicatePlayers", PlayerData.class); + put("duplicatePlayers", DuplicatePlayerData.class); put("playerBans", PlayerBanData.class); put("playerABans", PlayerBanData.class); diff --git a/common/src/main/java/me/confuser/banmanager/common/data/DuplicatePlayerData.java b/common/src/main/java/me/confuser/banmanager/common/data/DuplicatePlayerData.java new file mode 100644 index 00000000..d55f4693 --- /dev/null +++ b/common/src/main/java/me/confuser/banmanager/common/data/DuplicatePlayerData.java @@ -0,0 +1,78 @@ +package me.confuser.banmanager.common.data; + +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; +import me.confuser.banmanager.common.ipaddr.AddressStringException; +import me.confuser.banmanager.common.ipaddr.IPAddress; +import me.confuser.banmanager.common.ipaddr.IPAddressString; +import me.confuser.banmanager.common.ormlite.field.DatabaseField; +import me.confuser.banmanager.common.ormlite.table.DatabaseTable; +import me.confuser.banmanager.common.storage.mysql.ByteArray; +import me.confuser.banmanager.common.storage.mysql.IpAddress; +import me.confuser.banmanager.common.util.UUIDUtils; + +import java.util.UUID; + +@DatabaseTable +@ToString +public class DuplicatePlayerData { + + @DatabaseField(id = true, persisterClass = ByteArray.class, columnDefinition = "BINARY(16) NOT NULL") + @Getter + private byte[] id; + @DatabaseField(index = true, width = 16, columnDefinition = "VARCHAR(16) NOT NULL") + @Getter + @Setter + private String name; + @Getter + @DatabaseField(index = true, persisterClass = IpAddress.class, columnDefinition = "VARBINARY(16) NOT NULL") + private IPAddress ip; + @Getter + @DatabaseField(columnDefinition = "BIGINT UNSIGNED NOT NULL") + private long lastSeen = System.currentTimeMillis() / 1000L; + + private UUID uuid = null; + + DuplicatePlayerData() { + + } + + public DuplicatePlayerData(UUID uuid, String name) { + this.uuid = uuid; + this.id = UUIDUtils.toBytes(uuid); + this.name = name; + + try { + this.ip = new IPAddressString("127.0.0.1").toAddress(); + } catch (AddressStringException e) { + e.printStackTrace(); + } + + this.lastSeen = System.currentTimeMillis() / 1000L; + } + + public DuplicatePlayerData(UUID uuid, String name, IPAddress ip) { + this.uuid = uuid; + this.id = UUIDUtils.toBytes(uuid); + this.name = name; + this.ip = ip; + this.lastSeen = System.currentTimeMillis() / 1000L; + } + + public DuplicatePlayerData(UUID uuid, String name, IPAddress ip, long lastSeen) { + this.uuid = uuid; + this.id = UUIDUtils.toBytes(uuid); + this.name = name; + this.ip = ip; + this.lastSeen = lastSeen; + } + + public UUID getUUID() { + if (uuid == null) { + uuid = UUIDUtils.fromBytes(id); + } + + return uuid; + } +} diff --git a/common/src/main/java/me/confuser/banmanager/common/listeners/CommonJoinListener.java b/common/src/main/java/me/confuser/banmanager/common/listeners/CommonJoinListener.java index 04ed2113..f5e1f436 100755 --- a/common/src/main/java/me/confuser/banmanager/common/listeners/CommonJoinListener.java +++ b/common/src/main/java/me/confuser/banmanager/common/listeners/CommonJoinListener.java @@ -3,14 +3,7 @@ import me.confuser.banmanager.common.BanManagerPlugin; import me.confuser.banmanager.common.CommonPlayer; import me.confuser.banmanager.common.commands.NotesCommand; -import me.confuser.banmanager.common.data.IpBanData; -import me.confuser.banmanager.common.data.IpRangeBanData; -import me.confuser.banmanager.common.data.NameBanData; -import me.confuser.banmanager.common.data.PlayerBanData; -import me.confuser.banmanager.common.data.PlayerData; -import me.confuser.banmanager.common.data.PlayerMuteData; -import me.confuser.banmanager.common.data.PlayerNoteData; -import me.confuser.banmanager.common.data.PlayerWarnData; +import me.confuser.banmanager.common.data.*; import me.confuser.banmanager.common.google.guava.cache.Cache; import me.confuser.banmanager.common.google.guava.cache.CacheBuilder; import me.confuser.banmanager.common.ipaddr.IPAddress; @@ -319,7 +312,7 @@ public void onServerJoin(CommonPlayer player, InetAddress address, String server String name = player.getName(); IPAddress ip = IPUtils.toIPAddress(address); - PlayerData playerData = new PlayerData(id, name, ip); + DuplicatePlayerData playerData = new DuplicatePlayerData(id, name, ip); try { plugin.getDuplicatePlayerStorage().createOrUpdate(playerData); diff --git a/common/src/main/java/me/confuser/banmanager/common/storage/DuplicatePlayerStorage.java b/common/src/main/java/me/confuser/banmanager/common/storage/DuplicatePlayerStorage.java index f674d3fd..cbe256c7 100644 --- a/common/src/main/java/me/confuser/banmanager/common/storage/DuplicatePlayerStorage.java +++ b/common/src/main/java/me/confuser/banmanager/common/storage/DuplicatePlayerStorage.java @@ -2,30 +2,29 @@ import lombok.Getter; import me.confuser.banmanager.common.BanManagerPlugin; +import me.confuser.banmanager.common.data.DuplicatePlayerData; import me.confuser.banmanager.common.data.PlayerData; import me.confuser.banmanager.common.ipaddr.IPAddress; import me.confuser.banmanager.common.ormlite.dao.BaseDaoImpl; import me.confuser.banmanager.common.ormlite.dao.CloseableIterator; -import me.confuser.banmanager.common.ormlite.stmt.QueryBuilder; +import me.confuser.banmanager.common.ormlite.field.DataType; import me.confuser.banmanager.common.ormlite.stmt.SelectArg; -import me.confuser.banmanager.common.ormlite.stmt.Where; import me.confuser.banmanager.common.ormlite.support.ConnectionSource; import me.confuser.banmanager.common.ormlite.table.DatabaseTableConfig; import me.confuser.banmanager.common.ormlite.table.TableUtils; -import me.confuser.banmanager.common.util.UUIDProfile; import me.confuser.banmanager.common.util.UUIDUtils; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -public class DuplicatePlayerStorage extends BaseDaoImpl { +public class DuplicatePlayerStorage extends BaseDaoImpl { @Getter private BanManagerPlugin plugin; public DuplicatePlayerStorage(BanManagerPlugin plugin) throws SQLException { - super(plugin.getLocalConn(), (DatabaseTableConfig) plugin.getConfig().getLocalDb() + super(plugin.getLocalConn(), (DatabaseTableConfig) plugin.getConfig().getLocalDb() .getTable("duplicatePlayers")); this.plugin = plugin; @@ -36,10 +35,10 @@ public DuplicatePlayerStorage(BanManagerPlugin plugin) throws SQLException { } public DuplicatePlayerStorage(ConnectionSource connection, DatabaseTableConfig table) throws SQLException { - super(connection, (DatabaseTableConfig) table); + super(connection, (DatabaseTableConfig) table); } - public List retrieve(String name) { + public List retrieve(String name) { try { return queryForEq("name", new SelectArg(name)); } catch (SQLException e) { @@ -49,51 +48,44 @@ public List retrieve(String name) { return null; } - public List getDuplicatesInTime(IPAddress ip, long timeDiff) { - ArrayList players = new ArrayList<>(); + public List getDuplicatesInTime(IPAddress address, long timeDiff) { + List players = new ArrayList<>(); - if (plugin.getConfig().getBypassPlayerIps().contains(ip.toString())) { + if (plugin.getConfig().getBypassPlayerIps().contains(address.toString())) { return players; } - QueryBuilder query = queryBuilder(); try { - query.leftJoin(plugin.getPlayerBanStorage().queryBuilder()); - - Where where = query.where(); - - where.eq("ip", ip); - - if (timeDiff != 0) { - long currentTime = System.currentTimeMillis() / 1000L; + CloseableIterator itr = null; + try { + String playerTableName = getTableName(); + String banTableName = plugin.getPlayerBanStorage().getTableName(); + itr = queryRaw("SELECT *\n" + + "FROM " + playerTableName + " \n" + + "LEFT JOIN " + banTableName + " ON " + playerTableName + ".id = " + banTableName + ".player_id\n" + + "WHERE " + playerTableName + ".ip = UNHEX('" + address.toHexString(false) + "') \n" + + "LIMIT 300;", new DataType[]{DataType.BYTE_ARRAY, DataType.STRING, DataType.BYTE_ARRAY, DataType.LONG}).closeableIterator(); + + while (itr.hasNext()) { + Object[] data = itr.next(); + PlayerData player = new PlayerData(UUIDUtils.fromBytes((byte[]) data[0]), (String) data[1], address, (Long) data[3]); + + if (!plugin.getExemptionsConfig().isExempt(player, "alts")) { + players.add(player); + } + } - where.and().ge("lastSeen", (currentTime - timeDiff)); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + if (itr != null) itr.closeQuietly(); } - query.setWhere(where); - } catch (SQLException e) { + } catch (Exception e) { e.printStackTrace(); return players; } - CloseableIterator itr = null; - try { - itr = query.limit(300L).iterator(); - - while (itr.hasNext()) { - PlayerData player = itr.next(); - - if (!plugin.getExemptionsConfig().isExempt(player, "alts")) { - players.add(player); - } - } - - } catch (SQLException e) { - e.printStackTrace(); - } finally { - if (itr != null) itr.closeQuietly(); - } - return players; } } diff --git a/common/src/main/java/me/confuser/banmanager/common/storage/PlayerABanStorage.java b/common/src/main/java/me/confuser/banmanager/common/storage/PlayerABanStorage.java index 15ae7739..bc1105cd 100644 --- a/common/src/main/java/me/confuser/banmanager/common/storage/PlayerABanStorage.java +++ b/common/src/main/java/me/confuser/banmanager/common/storage/PlayerABanStorage.java @@ -8,6 +8,7 @@ import me.confuser.banmanager.common.ipaddr.IPAddress; import me.confuser.banmanager.common.ormlite.dao.BaseDaoImpl; import me.confuser.banmanager.common.ormlite.dao.CloseableIterator; +import me.confuser.banmanager.common.ormlite.field.DataType; import me.confuser.banmanager.common.ormlite.stmt.QueryBuilder; import me.confuser.banmanager.common.ormlite.stmt.StatementBuilder; import me.confuser.banmanager.common.ormlite.stmt.Where; @@ -259,40 +260,58 @@ public CloseableIterator findBans(long fromTime) throws SQLExcept } - public List getDuplicates(IPAddress ip) { + public List getDuplicates(IPAddress address) { ArrayList players = new ArrayList<>(); - if (plugin.getConfig().getBypassPlayerIps().contains(ip.toString())) { + if (plugin.getConfig().getBypassPlayerIps().contains(address.toString())) { return players; } - QueryBuilder query = queryBuilder(); try { - QueryBuilder playerQuery = plugin.getDuplicatePlayerStorage().queryBuilder(); + CloseableIterator itr = null; + try { + String banTableName = getTableName(); + String playerTableName = plugin.getDuplicatePlayerStorage().getTableName(); + itr = queryRaw("SELECT *\n" + + "FROM " + banTableName + " \n" + + "LEFT JOIN " + playerTableName + " ON " + playerTableName + ".id = " + banTableName + ".player_id\n" + + "WHERE " + playerTableName + ".ip = UNHEX('" + address.toHexString(false) + "') \n" + + ";", new DataType[]{ + DataType.INTEGER, + DataType.BYTE_ARRAY, + DataType.STRING, + DataType.BYTE_ARRAY, + DataType.LONG, + DataType.LONG, + DataType.LONG, + DataType.BOOLEAN, + DataType.BYTE_ARRAY, + DataType.STRING, + DataType.BYTE_ARRAY, + DataType.LONG + }).closeableIterator(); + + while (itr.hasNext()) { + Object[] data = itr.next(); + + PlayerData player = new PlayerData(UUIDUtils.fromBytes((byte[]) data[9]), (String) data[9], address, (Long) data[11]); + + if (!plugin.getExemptionsConfig().isExempt(player, "alts")) { + players.add(player); + } + } - Where where = playerQuery.where(); - where.eq("ip", ip); - playerQuery.setWhere(where); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + if (itr != null) itr.closeQuietly(); + } - query.leftJoin(playerQuery); - } catch (SQLException e) { + } catch (Exception e) { e.printStackTrace(); return players; } - CloseableIterator itr = null; - try { - itr = query.iterator(); - - while (itr.hasNext()) { - players.add(itr.next().getPlayer()); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - if (itr != null) itr.closeQuietly(); - } - return players; } diff --git a/common/src/main/java/me/confuser/banmanager/common/storage/PlayerBanStorage.java b/common/src/main/java/me/confuser/banmanager/common/storage/PlayerBanStorage.java index 2ac5ae82..56c057f4 100644 --- a/common/src/main/java/me/confuser/banmanager/common/storage/PlayerBanStorage.java +++ b/common/src/main/java/me/confuser/banmanager/common/storage/PlayerBanStorage.java @@ -9,6 +9,7 @@ import me.confuser.banmanager.common.ipaddr.IPAddress; import me.confuser.banmanager.common.ormlite.dao.BaseDaoImpl; import me.confuser.banmanager.common.ormlite.dao.CloseableIterator; +import me.confuser.banmanager.common.ormlite.field.DataType; import me.confuser.banmanager.common.ormlite.stmt.QueryBuilder; import me.confuser.banmanager.common.ormlite.stmt.StatementBuilder; import me.confuser.banmanager.common.ormlite.stmt.Where; @@ -286,40 +287,58 @@ public CloseableIterator findBans(long fromTime) throws SQLExcept } - public List getDuplicates(IPAddress ip) { - ArrayList players = new ArrayList<>(); + public List getDuplicates(IPAddress address) { + List players = new ArrayList<>(); - if (plugin.getConfig().getBypassPlayerIps().contains(ip.toString())) { + if (plugin.getConfig().getBypassPlayerIps().contains(address.toString())) { return players; } - QueryBuilder query = queryBuilder(); try { - QueryBuilder playerQuery = plugin.getDuplicatePlayerStorage().queryBuilder(); + CloseableIterator itr = null; + try { + String banTableName = getTableName(); + String playerTableName = plugin.getDuplicatePlayerStorage().getTableName(); + itr = queryRaw("SELECT *\n" + + "FROM " + banTableName + " \n" + + "LEFT JOIN " + playerTableName + " ON " + playerTableName + ".id = " + banTableName + ".player_id\n" + + "WHERE " + playerTableName + ".ip = UNHEX('" + address.toHexString(false) + "') \n" + + ";", new DataType[]{ + DataType.INTEGER, + DataType.BYTE_ARRAY, + DataType.STRING, + DataType.BYTE_ARRAY, + DataType.LONG, + DataType.LONG, + DataType.LONG, + DataType.BOOLEAN, + DataType.BYTE_ARRAY, + DataType.STRING, + DataType.BYTE_ARRAY, + DataType.LONG + }).closeableIterator(); + + while (itr.hasNext()) { + Object[] data = itr.next(); + + PlayerData player = new PlayerData(UUIDUtils.fromBytes((byte[]) data[8]), (String) data[9], address, (Long) data[11]); + + if (!plugin.getExemptionsConfig().isExempt(player, "alts")) { + players.add(player); + } + } - Where where = playerQuery.where(); - where.eq("ip", ip); - playerQuery.setWhere(where); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + if (itr != null) itr.closeQuietly(); + } - query.leftJoin(playerQuery); - } catch (SQLException e) { + } catch (Exception e) { e.printStackTrace(); return players; } - CloseableIterator itr = null; - try { - itr = query.iterator(); - - while (itr.hasNext()) { - players.add(itr.next().getPlayer()); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - if (itr != null) itr.closeQuietly(); - } - return players; }