[#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 {
Database database = dbSystem.getDatabase();
Optional<Server> bungeeInfo = database.fetch().getBungeeInformation();
if (bungeeInfo.isPresent()) {
server = bungeeInfo.get();
Optional<Server> proxyInfo = database.query(ServerQueries.fetchProxyServerInformation());
if (proxyInfo.isPresent()) {
server = proxyInfo.get();
updateServerInfo(database);
} else {
server = registerBungeeInfo(database);
@ -97,22 +97,13 @@ public class BungeeServerInfo extends ServerInfo {
UUID serverUUID = generateNewUUID();
String accessAddress = webServer.get().getAccessAddress();
Server bungeeCord = new Server(-1, serverUUID, "BungeeCord", accessAddress, serverProperties.getMaxPlayers());
db.executeTransaction(new StoreServerInformationTransaction(bungeeCord));
Server proxy = new Server(-1, serverUUID, "BungeeCord", accessAddress, serverProperties.getMaxPlayers());
db.executeTransaction(new StoreServerInformationTransaction(proxy));
Optional<Server> bungeeInfo = db.query(ServerQueries.fetchProxyServerInformation());
if (bungeeInfo.isPresent()) {
return bungeeInfo.get();
Optional<Server> proxyInfo = db.query(ServerQueries.fetchProxyServerInformation());
if (proxyInfo.isPresent()) {
return proxyInfo.get();
}
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) {
if (args.length >= 1 && connectionSystem.isServerAvailable()) {
String serverIdentifier = getGivenIdentifier(args);
return dbSystem.getDatabase().query(ServerQueries.fetchMatchingServerIdentifier(serverIdentifier))
return dbSystem.getDatabase().query(ServerQueries.fetchServerMatchingIdentifier(serverIdentifier))
.filter(server -> !server.isProxy());
}
return Optional.empty();

View File

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

View File

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

View File

@ -73,11 +73,11 @@ public class ServerQueries {
return db -> db.query(fetchPlanServerInformation()).values();
}
public static Query<Optional<Server>> fetchMatchingServerIdentifier(UUID serverUUID) {
return fetchMatchingServerIdentifier(serverUUID.toString());
public static Query<Optional<Server>> fetchServerMatchingIdentifier(UUID serverUUID) {
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 +
" WHERE (LOWER(" + ServerTable.SERVER_UUID + ") LIKE LOWER(?)" +
" OR LOWER(" + ServerTable.NAME + ") LIKE LOWER(?)" +
@ -110,6 +110,6 @@ public class ServerQueries {
}
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;
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.system.info.server.Server;
import java.util.Optional;
@ -38,11 +40,10 @@ public class Version10Patch extends Patch {
@Override
protected void applyPatch() {
Optional<Integer> fetchedServerID = db.getServerTable().getServerID(getServerUUID());
if (!fetchedServerID.isPresent()) {
throw new IllegalStateException("Server UUID was not registered, try rebooting the plugin.");
}
serverID = fetchedServerID.get();
Optional<Server> server = db.query(ServerQueries.fetchServerMatchingIdentifier(getServerUUID()));
serverID = server.map(Server::getId)
.orElseThrow(() -> new IllegalStateException("Server UUID was not registered, try rebooting the plugin."));
alterTablesToV10();
}

View File

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

View File

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

View File

@ -55,6 +55,10 @@ public class Server implements Comparable<Server> {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebAddress() {
return webAddress;
}
@ -105,4 +109,8 @@ public class Server implements Comparable<Server> {
public boolean isNotProxy() {
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() {
}
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.database.DBOpException;
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.system.database.DBSystem;
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 {
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);
}
String name = config.get(PluginSettings.SERVER_NAME).replaceAll("[^a-zA-Z0-9_\\s]", "_");
String webAddress = webServer.get().getAccessAddress();
if ("plan".equalsIgnoreCase(name)) {
name = "Server " + serverID.get();
}
int maxPlayers = serverProperties.getMaxPlayers();
Server server = foundServer.get();
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));
return server;
}
@ -109,30 +116,23 @@ public class ServerServerInfo extends ServerInfo {
}
private Server registerServer(UUID serverUUID) throws IOException {
Database db = dbSystem.getDatabase();
// Create the server object
String webAddress = webServer.get().getAccessAddress();
String name = config.get(PluginSettings.SERVER_NAME).replaceAll("[^a-zA-Z0-9_\\s]", "_");
int maxPlayers = serverProperties.getMaxPlayers();
Server server = new Server(-1, serverUUID, name, webAddress, maxPlayers);
Database db = dbSystem.getDatabase();
// Save
db.executeTransaction(new StoreServerInformationTransaction(server));
Optional<Integer> serverID = db.fetch().getServerID(serverUUID);
int id = serverID.orElseThrow(() -> new IllegalStateException("Failed to Register Server (ID not found)"));
server.setId(id);
// Load from database
server = db.query(ServerQueries.fetchServerMatchingIdentifier(serverUUID))
.orElseThrow(() -> new IllegalStateException("Failed to Register Server (ID not found)"));
// Store the UUID in ServerInfoFile
serverInfoFile.saveServerUUID(serverUUID);
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 {
String serverName = target.get(0);
Optional<UUID> serverUUIDOptional = dbSystem.getDatabase()
.query(ServerQueries.fetchMatchingServerIdentifier(serverName))
.query(ServerQueries.fetchServerMatchingIdentifier(serverName))
.map(Server::getUuid);
if (serverUUIDOptional.isPresent()) {
serverUUID = serverUUIDOptional.get();

View File

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

View File

@ -63,9 +63,9 @@ public class VelocityServerInfo extends ServerInfo {
try {
Database database = dbSystem.getDatabase();
Optional<Server> bungeeInfo = database.fetch().getBungeeInformation();
if (bungeeInfo.isPresent()) {
server = bungeeInfo.get();
Optional<Server> proxyInfo = database.query(ServerQueries.fetchProxyServerInformation());
if (proxyInfo.isPresent()) {
server = proxyInfo.get();
updateServerInfo(database);
} else {
server = registerVelocityInfo(database);
@ -98,22 +98,13 @@ public class VelocityServerInfo extends ServerInfo {
String accessAddress = webServer.get().getAccessAddress();
// TODO Rework to allow Velocity as name.
Server bungeeCord = new Server(-1, serverUUID, "BungeeCord", accessAddress, serverProperties.getMaxPlayers());
db.executeTransaction(new StoreServerInformationTransaction(bungeeCord));
Server proxy = new Server(-1, serverUUID, "BungeeCord", accessAddress, serverProperties.getMaxPlayers());
db.executeTransaction(new StoreServerInformationTransaction(proxy));
Optional<Server> bungeeInfo = db.query(ServerQueries.fetchProxyServerInformation());
if (bungeeInfo.isPresent()) {
return bungeeInfo.get();
Optional<Server> proxyInfo = db.query(ServerQueries.fetchProxyServerInformation());
if (proxyInfo.isPresent()) {
return proxyInfo.get();
}
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());
}
}