From 9b692480dac00c74dc78941bc9bac6c52eb706f4 Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+Rsl1122@users.noreply.github.com> Date: Fri, 1 Jan 2021 11:26:07 +0200 Subject: [PATCH] Patch proxy server detection - Added a field is_proxy to plan_servers - Patch to populate the new field - Field is set to true when proxy enables to fix any broken data - Allows any name for a proxy - Updated server save queries - Changed isProxy method in Server.java to match the change - Changed query for fetching the proxy server Affects issues: - Close #1678 --- .../plan/identification/BungeeServerInfo.java | 4 +- .../plan/identification/Server.java | 12 +++- .../storage/ServerFileLoader.java | 4 +- .../plan/storage/database/SQLDB.java | 3 +- .../database/queries/LargeStoreQueries.java | 12 ++-- .../queries/objects/ServerQueries.java | 36 ++++++++++-- .../database/sql/tables/ServerTable.java | 7 ++- .../StoreServerInformationTransaction.java | 6 +- .../patches/ServerIsProxyPatch.java | 56 +++++++++++++++++++ .../database/queries/ServerQueriesTest.java | 1 + .../identification/VelocityServerInfo.java | 6 +- 11 files changed, 123 insertions(+), 24 deletions(-) create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/ServerIsProxyPatch.java diff --git a/Plan/bungeecord/src/main/java/com/djrapitops/plan/identification/BungeeServerInfo.java b/Plan/bungeecord/src/main/java/com/djrapitops/plan/identification/BungeeServerInfo.java index 15737d19b..5751d2fdc 100644 --- a/Plan/bungeecord/src/main/java/com/djrapitops/plan/identification/BungeeServerInfo.java +++ b/Plan/bungeecord/src/main/java/com/djrapitops/plan/identification/BungeeServerInfo.java @@ -67,6 +67,8 @@ public class BungeeServerInfo extends ServerInfo { this.server = fromFile.load(null).orElseGet(() -> fromDatabase.load(null) .orElseGet(this::registerServer)); + this.server.setProxy(true); // Ensure isProxy if loaded from file + processing.submitNonCritical(this::updateStorage); } @@ -112,6 +114,6 @@ public class BungeeServerInfo extends ServerInfo { private Server createServerObject() { UUID serverUUID = generateNewUUID(); String accessAddress = addresses.getAccessAddress().orElseThrow(() -> new EnableException("Velocity can not have '0.0.0.0' or '' as an address. Set up 'Server.IP' setting.")); - return new Server(-1, serverUUID, "BungeeCord", accessAddress); + return new Server(-1, serverUUID, "BungeeCord", accessAddress, true); } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/identification/Server.java b/Plan/common/src/main/java/com/djrapitops/plan/identification/Server.java index dc0760266..f656fbb26 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/identification/Server.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/identification/Server.java @@ -30,16 +30,18 @@ public class Server implements Comparable { private Integer id; private String name; private String webAddress; + private boolean proxy; public Server(UUID uuid, String name, String webAddress) { - this(null, uuid, name, webAddress); + this(null, uuid, name, webAddress, false); } - public Server(Integer id, UUID uuid, String name, String webAddress) { + public Server(Integer id, UUID uuid, String name, String webAddress, boolean proxy) { this.id = id; this.uuid = uuid; this.name = name; this.webAddress = webAddress; + this.proxy = proxy; } public Optional getId() { @@ -105,7 +107,11 @@ public class Server implements Comparable { } public boolean isProxy() { - return "BungeeCord".equals(name); + return proxy; + } + + public void setProxy(boolean proxy) { + this.proxy = proxy; } public boolean isNotProxy() { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/identification/storage/ServerFileLoader.java b/Plan/common/src/main/java/com/djrapitops/plan/identification/storage/ServerFileLoader.java index c515675da..2f048849d 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/identification/storage/ServerFileLoader.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/identification/storage/ServerFileLoader.java @@ -72,10 +72,10 @@ public class ServerFileLoader extends Config implements ServerLoader { UUID serverUUID = UUID.fromString(serverUUIDString); String name = config.getNode(PluginSettings.SERVER_NAME.getPath()) .map(ConfigNode::getString) - .orElse("BungeeCord"); + .orElse("Proxy"); String address = getString("Server.Web_address"); - return Optional.of(new Server(id, serverUUID, name, address)); + return Optional.of(new Server(id, serverUUID, name, address, false)); } catch (IOException e) { throw new EnableException("Failed to read ServerInfoFile.yml: " + e.getMessage()); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/SQLDB.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/SQLDB.java index f2509d95b..8152ef900 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/SQLDB.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/SQLDB.java @@ -173,7 +173,8 @@ public abstract class SQLDB extends AbstractDatabase { new BadNukkitRegisterValuePatch(), new LinkedToSecurityTablePatch(), new LinkUsersToPlayersSecurityTablePatch(), - new LitebansTableHeaderPatch() + new LitebansTableHeaderPatch(), + new ServerIsProxyPatch() }; } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/LargeStoreQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/LargeStoreQueries.java index 8c960ec8c..9378901c5 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/LargeStoreQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/LargeStoreQueries.java @@ -151,19 +151,17 @@ public class LargeStoreQueries { return new ExecBatchStatement(ServerTable.INSERT_STATEMENT) { @Override public void prepare(PreparedStatement statement) throws SQLException { - for (Server info : servers) { - UUID uuid = info.getUuid(); - String name = info.getName(); - String webAddress = info.getWebAddress(); - + for (Server server : servers) { + UUID uuid = server.getUuid(); if (uuid == null) { continue; } statement.setString(1, uuid.toString()); - statement.setString(2, name); - statement.setString(3, webAddress); + statement.setString(2, server.getName()); + statement.setString(3, server.getWebAddress()); statement.setBoolean(4, true); + statement.setBoolean(5, server.isProxy()); statement.addBatch(); } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/ServerQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/ServerQueries.java index 58b731e8a..d8b6f5cb2 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/ServerQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/ServerQueries.java @@ -65,7 +65,9 @@ public class ServerQueries { set.getInt(ServerTable.SERVER_ID), serverUUID, set.getString(ServerTable.NAME), - set.getString(ServerTable.WEB_ADDRESS))); + set.getString(ServerTable.WEB_ADDRESS), + set.getBoolean(ServerTable.PROXY) + )); } return servers; } @@ -106,7 +108,8 @@ public class ServerQueries { set.getInt(ServerTable.SERVER_ID), UUID.fromString(set.getString(ServerTable.SERVER_UUID)), set.getString(ServerTable.NAME), - set.getString(ServerTable.WEB_ADDRESS) + set.getString(ServerTable.WEB_ADDRESS), + set.getBoolean(ServerTable.PROXY) )); } return Optional.empty(); @@ -115,7 +118,31 @@ public class ServerQueries { } public static Query> fetchProxyServerInformation() { - return db -> db.query(fetchServerMatchingIdentifier("BungeeCord")); + String sql = SELECT + '*' + FROM + ServerTable.TABLE_NAME + + WHERE + ServerTable.INSTALLED + "=?" + + AND + ServerTable.PROXY + "=?" + + " LIMIT 1"; + return new QueryStatement>(sql) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setBoolean(1, true); + statement.setBoolean(2, true); + } + + @Override + public Optional processResults(ResultSet set) throws SQLException { + if (set.next()) { + return Optional.of(new Server( + set.getInt(ServerTable.SERVER_ID), + UUID.fromString(set.getString(ServerTable.SERVER_UUID)), + set.getString(ServerTable.NAME), + set.getString(ServerTable.WEB_ADDRESS), + set.getBoolean(ServerTable.PROXY) + )); + } + return Optional.empty(); + } + }; } public static Query> fetchServerNames() { @@ -165,7 +192,8 @@ public class ServerQueries { set.getInt(ServerTable.SERVER_ID), UUID.fromString(set.getString(ServerTable.SERVER_UUID)), set.getString(ServerTable.NAME), - set.getString(ServerTable.WEB_ADDRESS) + set.getString(ServerTable.WEB_ADDRESS), + set.getBoolean(ServerTable.PROXY) )); } return matches; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/sql/tables/ServerTable.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/sql/tables/ServerTable.java index cdfbd0e7c..bbd64aeff 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/sql/tables/ServerTable.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/sql/tables/ServerTable.java @@ -40,18 +40,20 @@ public class ServerTable { public static final String NAME = "name"; public static final String WEB_ADDRESS = "web_address"; public static final String INSTALLED = "is_installed"; + public static final String PROXY = "is_proxy"; @Deprecated public static final String MAX_PLAYERS = "max_players"; public static final String INSERT_STATEMENT = Insert.values(TABLE_NAME, SERVER_UUID, NAME, - WEB_ADDRESS, INSTALLED); + WEB_ADDRESS, INSTALLED, PROXY); public static final String UPDATE_STATEMENT = Update.values(TABLE_NAME, SERVER_UUID, NAME, WEB_ADDRESS, - INSTALLED) + INSTALLED, + PROXY) .where(SERVER_UUID + "=?") .toString(); @@ -71,6 +73,7 @@ public class ServerTable { .column(NAME, Sql.varchar(100)) .column(WEB_ADDRESS, Sql.varchar(100)) .column(INSTALLED, Sql.BOOL).notNull().defaultValue(true) + .column(PROXY, Sql.BOOL).notNull().defaultValue(false) .column(MAX_PLAYERS, Sql.INT).notNull().defaultValue("-1") .toString(); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/StoreServerInformationTransaction.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/StoreServerInformationTransaction.java index 3bf1e3129..4ea284de4 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/StoreServerInformationTransaction.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/StoreServerInformationTransaction.java @@ -25,7 +25,7 @@ import static com.djrapitops.plan.storage.database.sql.tables.ServerTable.INSERT import static com.djrapitops.plan.storage.database.sql.tables.ServerTable.UPDATE_STATEMENT; /** - * Transaction for keeping Plan Server serverrmation up to date in the database. + * Transaction for keeping Plan Server information up to date in the database. * * @author Rsl1122 */ @@ -53,7 +53,8 @@ public class StoreServerInformationTransaction extends Transaction { statement.setString(2, server.getName()); statement.setString(3, server.getWebAddress()); statement.setBoolean(4, true); - statement.setString(5, serverUUIDString); + statement.setBoolean(5, server.isProxy()); + statement.setString(6, serverUUIDString); } }; } @@ -66,6 +67,7 @@ public class StoreServerInformationTransaction extends Transaction { statement.setString(2, server.getName()); statement.setString(3, server.getWebAddress()); statement.setBoolean(4, true); + statement.setBoolean(5, server.isProxy()); } }; } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/ServerIsProxyPatch.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/ServerIsProxyPatch.java new file mode 100644 index 000000000..3f0392231 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/ServerIsProxyPatch.java @@ -0,0 +1,56 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.storage.database.transactions.patches; + +import com.djrapitops.plan.storage.database.sql.building.Sql; +import com.djrapitops.plan.storage.database.sql.tables.ServerTable; +import com.djrapitops.plan.storage.database.transactions.ExecStatement; + +import java.sql.PreparedStatement; +import java.sql.SQLException; + +import static com.djrapitops.plan.storage.database.sql.building.Sql.WHERE; + +/** + * Adds a is_proxy field to remove technical debt assuming name field "BungeeCord" being the proxy. + *

+ * See https://github.com/plan-player-analytics/Plan/issues/1678 for more details + * + * @author Rsl1122 + */ +public class ServerIsProxyPatch extends Patch { + + @Override + public boolean hasBeenApplied() { + return hasColumn(ServerTable.TABLE_NAME, ServerTable.PROXY); + } + + @Override + protected void applyPatch() { + addColumn(ServerTable.TABLE_NAME, ServerTable.PROXY + ' ' + Sql.BOOL + " DEFAULT 0"); + + String populateFieldSql = "UPDATE " + ServerTable.TABLE_NAME + " SET " + ServerTable.PROXY + "=?" + + WHERE + ServerTable.NAME + "=?"; + execute(new ExecStatement(populateFieldSql) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setBoolean(1, true); + statement.setString(2, "BungeeCord"); + } + }); + } +} diff --git a/Plan/common/src/test/java/com/djrapitops/plan/storage/database/queries/ServerQueriesTest.java b/Plan/common/src/test/java/com/djrapitops/plan/storage/database/queries/ServerQueriesTest.java index 123cd3337..2b97ca0b0 100644 --- a/Plan/common/src/test/java/com/djrapitops/plan/storage/database/queries/ServerQueriesTest.java +++ b/Plan/common/src/test/java/com/djrapitops/plan/storage/database/queries/ServerQueriesTest.java @@ -57,6 +57,7 @@ public interface ServerQueriesTest extends DatabaseTestPreparer { UUID bungeeUUID = UUID.randomUUID(); Server bungeeCord = new Server(bungeeUUID, "BungeeCord", "Random:1234"); + bungeeCord.setProxy(true); db().executeTransaction(new StoreServerInformationTransaction(bungeeCord)); forcePersistenceCheck(); diff --git a/Plan/velocity/src/main/java/com/djrapitops/plan/identification/VelocityServerInfo.java b/Plan/velocity/src/main/java/com/djrapitops/plan/identification/VelocityServerInfo.java index e8c7b4ef7..9b6f2c041 100644 --- a/Plan/velocity/src/main/java/com/djrapitops/plan/identification/VelocityServerInfo.java +++ b/Plan/velocity/src/main/java/com/djrapitops/plan/identification/VelocityServerInfo.java @@ -66,6 +66,8 @@ public class VelocityServerInfo extends ServerInfo { this.server = fromFile.load(null).orElseGet(() -> fromDatabase.load(null) .orElseGet(this::registerServer)); + this.server.setProxy(true); // Ensure isProxy if loaded from file + processing.submitNonCritical(this::updateStorage); } @@ -108,7 +110,7 @@ public class VelocityServerInfo extends ServerInfo { private Server createServerObject() { UUID serverUUID = generateNewUUID(); String accessAddress = addresses.getAccessAddress().orElseThrow(() -> new EnableException("Velocity can not have '0.0.0.0' or '' as an address. Set up 'Server.IP' setting.")); - // TODO Rework to allow Velocity as a name - return new Server(-1, serverUUID, "BungeeCord", accessAddress); + + return new Server(-1, serverUUID, "Velocity", accessAddress, true); } }