[#746] Made Server UUID generation random

- Refactored ServerInfo classes a bit in this commit, made them use
  Optional<Server> instead of Optional<Integer> (server id)
- Removed all uses of FetchOperations#getServerId
This commit is contained in:
Rsl1122 2019-02-15 09:20:57 +02:00
parent f0f32f3ec4
commit 029c1378f3
14 changed files with 73 additions and 78 deletions

View File

@ -63,9 +63,9 @@ public class BungeeServerInfo extends ServerInfo {
try { try {
Database database = dbSystem.getDatabase(); Database database = dbSystem.getDatabase();
Optional<Server> bungeeInfo = database.fetch().getBungeeInformation(); Optional<Server> proxyInfo = database.query(ServerQueries.fetchProxyServerInformation());
if (bungeeInfo.isPresent()) { if (proxyInfo.isPresent()) {
server = bungeeInfo.get(); server = proxyInfo.get();
updateServerInfo(database); updateServerInfo(database);
} else { } else {
server = registerBungeeInfo(database); server = registerBungeeInfo(database);
@ -97,22 +97,13 @@ public class BungeeServerInfo extends ServerInfo {
UUID serverUUID = generateNewUUID(); UUID serverUUID = generateNewUUID();
String accessAddress = webServer.get().getAccessAddress(); String accessAddress = webServer.get().getAccessAddress();
Server bungeeCord = new Server(-1, serverUUID, "BungeeCord", accessAddress, serverProperties.getMaxPlayers()); Server proxy = new Server(-1, serverUUID, "BungeeCord", accessAddress, serverProperties.getMaxPlayers());
db.executeTransaction(new StoreServerInformationTransaction(bungeeCord)); db.executeTransaction(new StoreServerInformationTransaction(proxy));
Optional<Server> bungeeInfo = db.query(ServerQueries.fetchProxyServerInformation()); Optional<Server> proxyInfo = db.query(ServerQueries.fetchProxyServerInformation());
if (bungeeInfo.isPresent()) { if (proxyInfo.isPresent()) {
return bungeeInfo.get(); return proxyInfo.get();
} }
throw new EnableException("BungeeCord registration failed (DB)"); throw new EnableException("BungeeCord registration failed (DB)");
} }
private UUID generateNewUUID() {
String seed = serverProperties.getName() +
serverProperties.getIp() +
serverProperties.getPort() +
serverProperties.getVersion() +
serverProperties.getImplVersion();
return UUID.nameUUIDFromBytes(seed.getBytes());
}
} }

View File

@ -134,7 +134,7 @@ public class AnalyzeCommand extends CommandNode {
private Optional<Server> getServer(String[] args) { private Optional<Server> getServer(String[] args) {
if (args.length >= 1 && connectionSystem.isServerAvailable()) { if (args.length >= 1 && connectionSystem.isServerAvailable()) {
String serverIdentifier = getGivenIdentifier(args); String serverIdentifier = getGivenIdentifier(args);
return dbSystem.getDatabase().query(ServerQueries.fetchMatchingServerIdentifier(serverIdentifier)) return dbSystem.getDatabase().query(ServerQueries.fetchServerMatchingIdentifier(serverIdentifier))
.filter(server -> !server.isProxy()); .filter(server -> !server.isProxy());
} }
return Optional.empty(); return Optional.empty();

View File

@ -103,7 +103,7 @@ public class ManageUninstalledCommand extends CommandNode {
private Optional<Server> getServer(String[] args) { private Optional<Server> getServer(String[] args) {
if (args.length >= 1) { if (args.length >= 1) {
String serverIdentifier = getGivenIdentifier(args); String serverIdentifier = getGivenIdentifier(args);
return dbSystem.getDatabase().query(ServerQueries.fetchMatchingServerIdentifier(serverIdentifier)) return dbSystem.getDatabase().query(ServerQueries.fetchServerMatchingIdentifier(serverIdentifier))
.filter(Server::isNotProxy); .filter(Server::isNotProxy);
} }
return Optional.empty(); return Optional.empty();

View File

@ -57,7 +57,7 @@ public class ServerContainerQuery implements Query<ServerContainer> {
public ServerContainer executeQuery(SQLDB db) { public ServerContainer executeQuery(SQLDB db) {
ServerContainer container = new ServerContainer(); ServerContainer container = new ServerContainer();
Optional<Server> serverInfo = db.query(ServerQueries.fetchMatchingServerIdentifier(serverUUID)); Optional<Server> serverInfo = db.query(ServerQueries.fetchServerMatchingIdentifier(serverUUID));
if (!serverInfo.isPresent()) { if (!serverInfo.isPresent()) {
return container; return container;
} }

View File

@ -73,11 +73,11 @@ public class ServerQueries {
return db -> db.query(fetchPlanServerInformation()).values(); return db -> db.query(fetchPlanServerInformation()).values();
} }
public static Query<Optional<Server>> fetchMatchingServerIdentifier(UUID serverUUID) { public static Query<Optional<Server>> fetchServerMatchingIdentifier(UUID serverUUID) {
return fetchMatchingServerIdentifier(serverUUID.toString()); return fetchServerMatchingIdentifier(serverUUID.toString());
} }
public static Query<Optional<Server>> fetchMatchingServerIdentifier(String identifier) { public static Query<Optional<Server>> fetchServerMatchingIdentifier(String identifier) {
String sql = "SELECT * FROM " + ServerTable.TABLE_NAME + String sql = "SELECT * FROM " + ServerTable.TABLE_NAME +
" WHERE (LOWER(" + ServerTable.SERVER_UUID + ") LIKE LOWER(?)" + " WHERE (LOWER(" + ServerTable.SERVER_UUID + ") LIKE LOWER(?)" +
" OR LOWER(" + ServerTable.NAME + ") LIKE LOWER(?)" + " OR LOWER(" + ServerTable.NAME + ") LIKE LOWER(?)" +
@ -110,6 +110,6 @@ public class ServerQueries {
} }
public static Query<Optional<Server>> fetchProxyServerInformation() { public static Query<Optional<Server>> fetchProxyServerInformation() {
return db -> db.query(fetchMatchingServerIdentifier("BungeeCord")); return db -> db.query(fetchServerMatchingIdentifier("BungeeCord"));
} }
} }

View File

@ -17,7 +17,9 @@
package com.djrapitops.plan.db.patches; package com.djrapitops.plan.db.patches;
import com.djrapitops.plan.db.SQLDB; import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.queries.objects.ServerQueries;
import com.djrapitops.plan.db.sql.tables.*; import com.djrapitops.plan.db.sql.tables.*;
import com.djrapitops.plan.system.info.server.Server;
import java.util.Optional; import java.util.Optional;
@ -38,11 +40,10 @@ public class Version10Patch extends Patch {
@Override @Override
protected void applyPatch() { protected void applyPatch() {
Optional<Integer> fetchedServerID = db.getServerTable().getServerID(getServerUUID()); Optional<Server> server = db.query(ServerQueries.fetchServerMatchingIdentifier(getServerUUID()));
if (!fetchedServerID.isPresent()) { serverID = server.map(Server::getId)
throw new IllegalStateException("Server UUID was not registered, try rebooting the plugin."); .orElseThrow(() -> new IllegalStateException("Server UUID was not registered, try rebooting the plugin."));
}
serverID = fetchedServerID.get();
alterTablesToV10(); alterTablesToV10();
} }

View File

@ -79,7 +79,7 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
@Override @Override
public Optional<UUID> getServerUUID(String serverName) { public Optional<UUID> getServerUUID(String serverName) {
return db.query(ServerQueries.fetchMatchingServerIdentifier(serverName)) return db.query(ServerQueries.fetchServerMatchingIdentifier(serverName))
.map(Server::getUuid); .map(Server::getUuid);
} }
@ -130,7 +130,7 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
@Override @Override
public Optional<String> getServerName(UUID serverUUID) { public Optional<String> getServerName(UUID serverUUID) {
return db.query(ServerQueries.fetchMatchingServerIdentifier(serverUUID)).map(Server::getName); return db.query(ServerQueries.fetchServerMatchingIdentifier(serverUUID)).map(Server::getName);
} }
@Override @Override
@ -146,7 +146,7 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
@Override @Override
public Optional<Integer> getServerID(UUID serverUUID) { public Optional<Integer> getServerID(UUID serverUUID) {
return serverTable.getServerID(serverUUID); return db.query(ServerQueries.fetchServerMatchingIdentifier(serverUUID)).map(Server::getId);
} }
@Override @Override

View File

@ -61,7 +61,7 @@ public class ConnectionIn {
UUID serverUUID = getServerUUID(); UUID serverUUID = getServerUUID();
try { try {
if (!database.query(ServerQueries.fetchMatchingServerIdentifier(serverUUID)).isPresent()) { if (!database.query(ServerQueries.fetchServerMatchingIdentifier(serverUUID)).isPresent()) {
return; return;
} }
} catch (DBOpException e) { } catch (DBOpException e) {

View File

@ -55,6 +55,10 @@ public class Server implements Comparable<Server> {
return name; return name;
} }
public void setName(String name) {
this.name = name;
}
public String getWebAddress() { public String getWebAddress() {
return webAddress; return webAddress;
} }
@ -105,4 +109,8 @@ public class Server implements Comparable<Server> {
public boolean isNotProxy() { public boolean isNotProxy() {
return !isProxy(); return !isProxy();
} }
public void setMaxPlayers(int maxPlayers) {
this.maxPlayers = maxPlayers;
}
} }

View File

@ -63,4 +63,8 @@ public abstract class ServerInfo implements SubSystem {
public void disable() { public void disable() {
} }
protected UUID generateNewUUID() {
return UUID.randomUUID();
}
} }

View File

@ -19,6 +19,7 @@ package com.djrapitops.plan.system.info.server;
import com.djrapitops.plan.api.exceptions.EnableException; import com.djrapitops.plan.api.exceptions.EnableException;
import com.djrapitops.plan.api.exceptions.database.DBOpException; import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.db.Database; import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.access.queries.objects.ServerQueries;
import com.djrapitops.plan.db.access.transactions.StoreServerInformationTransaction; import com.djrapitops.plan.db.access.transactions.StoreServerInformationTransaction;
import com.djrapitops.plan.system.database.DBSystem; import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.info.server.properties.ServerProperties; import com.djrapitops.plan.system.info.server.properties.ServerProperties;
@ -88,18 +89,24 @@ public class ServerServerInfo extends ServerInfo {
private Server updateDbInfo(UUID serverUUID) throws IOException { private Server updateDbInfo(UUID serverUUID) throws IOException {
Database db = dbSystem.getDatabase(); Database db = dbSystem.getDatabase();
Optional<Integer> serverID = db.fetch().getServerID(serverUUID);
if (!serverID.isPresent()) { Optional<Server> foundServer = db.query(ServerQueries.fetchServerMatchingIdentifier(serverUUID));
if (!foundServer.isPresent()) {
return registerServer(serverUUID); return registerServer(serverUUID);
} }
String name = config.get(PluginSettings.SERVER_NAME).replaceAll("[^a-zA-Z0-9_\\s]", "_"); Server server = foundServer.get();
String webAddress = webServer.get().getAccessAddress();
if ("plan".equalsIgnoreCase(name)) {
name = "Server " + serverID.get();
}
int maxPlayers = serverProperties.getMaxPlayers();
Server server = new Server(serverID.get(), serverUUID, name, webAddress, maxPlayers); // Update information
String name = config.get(PluginSettings.SERVER_NAME).replaceAll("[^a-zA-Z0-9_\\s]", "_");
server.setName("plan".equalsIgnoreCase(name) ? "Server " + server.getId() : name);
String webAddress = webServer.get().getAccessAddress();
server.setWebAddress(webAddress);
int maxPlayers = serverProperties.getMaxPlayers();
server.setMaxPlayers(maxPlayers);
// Save
db.executeTransaction(new StoreServerInformationTransaction(server)); db.executeTransaction(new StoreServerInformationTransaction(server));
return server; return server;
} }
@ -109,30 +116,23 @@ public class ServerServerInfo extends ServerInfo {
} }
private Server registerServer(UUID serverUUID) throws IOException { private Server registerServer(UUID serverUUID) throws IOException {
Database db = dbSystem.getDatabase();
// Create the server object
String webAddress = webServer.get().getAccessAddress(); String webAddress = webServer.get().getAccessAddress();
String name = config.get(PluginSettings.SERVER_NAME).replaceAll("[^a-zA-Z0-9_\\s]", "_"); String name = config.get(PluginSettings.SERVER_NAME).replaceAll("[^a-zA-Z0-9_\\s]", "_");
int maxPlayers = serverProperties.getMaxPlayers(); int maxPlayers = serverProperties.getMaxPlayers();
Server server = new Server(-1, serverUUID, name, webAddress, maxPlayers); Server server = new Server(-1, serverUUID, name, webAddress, maxPlayers);
Database db = dbSystem.getDatabase(); // Save
db.executeTransaction(new StoreServerInformationTransaction(server)); db.executeTransaction(new StoreServerInformationTransaction(server));
Optional<Integer> serverID = db.fetch().getServerID(serverUUID); // Load from database
int id = serverID.orElseThrow(() -> new IllegalStateException("Failed to Register Server (ID not found)")); server = db.query(ServerQueries.fetchServerMatchingIdentifier(serverUUID))
server.setId(id); .orElseThrow(() -> new IllegalStateException("Failed to Register Server (ID not found)"));
// Store the UUID in ServerInfoFile
serverInfoFile.saveServerUUID(serverUUID); serverInfoFile.saveServerUUID(serverUUID);
return server; return server;
} }
private UUID generateNewUUID() {
String seed = serverProperties.getServerId() +
serverProperties.getName() +
serverProperties.getIp() +
serverProperties.getPort() +
serverProperties.getVersion() +
serverProperties.getImplVersion();
return UUID.nameUUIDFromBytes(seed.getBytes());
}
} }

View File

@ -112,7 +112,7 @@ public class ServerPageHandler implements PageHandler {
try { try {
String serverName = target.get(0); String serverName = target.get(0);
Optional<UUID> serverUUIDOptional = dbSystem.getDatabase() Optional<UUID> serverUUIDOptional = dbSystem.getDatabase()
.query(ServerQueries.fetchMatchingServerIdentifier(serverName)) .query(ServerQueries.fetchServerMatchingIdentifier(serverName))
.map(Server::getUuid); .map(Server::getUuid);
if (serverUUIDOptional.isPresent()) { if (serverUUIDOptional.isPresent()) {
serverUUID = serverUUIDOptional.get(); serverUUID = serverUUIDOptional.get();

View File

@ -28,6 +28,7 @@ import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import utilities.OptionalAssert;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -76,9 +77,8 @@ public class SQLiteTest extends CommonDBTest {
assertTrue(bungeeInfo.isPresent()); assertTrue(bungeeInfo.isPresent());
assertEquals(bungeeCord, bungeeInfo.get()); assertEquals(bungeeCord, bungeeInfo.get());
Optional<Integer> serverID = serverTable.getServerID(bungeeUUID); Optional<Server> found = db.query(ServerQueries.fetchServerMatchingIdentifier(bungeeUUID));
assertTrue(serverID.isPresent()); OptionalAssert.equals(2, found.map(Server::getId));
assertEquals(2, (int) serverID.get());
} }
@Test @Test

View File

@ -63,9 +63,9 @@ public class VelocityServerInfo extends ServerInfo {
try { try {
Database database = dbSystem.getDatabase(); Database database = dbSystem.getDatabase();
Optional<Server> bungeeInfo = database.fetch().getBungeeInformation(); Optional<Server> proxyInfo = database.query(ServerQueries.fetchProxyServerInformation());
if (bungeeInfo.isPresent()) { if (proxyInfo.isPresent()) {
server = bungeeInfo.get(); server = proxyInfo.get();
updateServerInfo(database); updateServerInfo(database);
} else { } else {
server = registerVelocityInfo(database); server = registerVelocityInfo(database);
@ -98,22 +98,13 @@ public class VelocityServerInfo extends ServerInfo {
String accessAddress = webServer.get().getAccessAddress(); String accessAddress = webServer.get().getAccessAddress();
// TODO Rework to allow Velocity as name. // TODO Rework to allow Velocity as name.
Server bungeeCord = new Server(-1, serverUUID, "BungeeCord", accessAddress, serverProperties.getMaxPlayers()); Server proxy = new Server(-1, serverUUID, "BungeeCord", accessAddress, serverProperties.getMaxPlayers());
db.executeTransaction(new StoreServerInformationTransaction(bungeeCord)); db.executeTransaction(new StoreServerInformationTransaction(proxy));
Optional<Server> bungeeInfo = db.query(ServerQueries.fetchProxyServerInformation()); Optional<Server> proxyInfo = db.query(ServerQueries.fetchProxyServerInformation());
if (bungeeInfo.isPresent()) { if (proxyInfo.isPresent()) {
return bungeeInfo.get(); return proxyInfo.get();
} }
throw new EnableException("Velocity registration failed (DB)"); throw new EnableException("Velocity registration failed (DB)");
} }
private UUID generateNewUUID() {
String seed = serverProperties.getName() +
serverProperties.getIp() +
serverProperties.getPort() +
serverProperties.getVersion() +
serverProperties.getImplVersion();
return UUID.nameUUIDFromBytes(seed.getBytes());
}
} }