Added a way to generate or use a predefined proxy uuid.

This commit is contained in:
juanmuscaria
2021-09-15 18:34:37 -03:00
parent 01632ec125
commit 4befb35af9
2 changed files with 52 additions and 8 deletions

View File

@ -49,9 +49,15 @@ import com.velocitypowered.api.proxy.server.RegisteredServer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.slf4j.Logger;
import java.io.*;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentMap;
@ -64,25 +70,26 @@ public class FastLoginVelocity implements PlatformPlugin<CommandSource> {
private FastLoginCore<Player, CommandSource, FastLoginVelocity> core;
private final ConcurrentMap<InetSocketAddress, VelocityLoginSession> session = new MapMaker().weakKeys().makeMap();
private AsyncScheduler scheduler;
private UUID proxyId;
private final String PROXY_ID_fILE = "proxyId.txt";
@Inject
public FastLoginVelocity(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) {
this.server = server;
this.logger = logger;
this.dataDirectory = dataDirectory;
logger.info("FastLogin velocity.");
}
@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
scheduler = new AsyncScheduler(logger, getThreadFactory());
core = new FastLoginCore<>(this);
core.load();
if (!core.setupDatabase()) {
loadOrGenerateProxyId();
if (!core.setupDatabase() || proxyId == null) {
return;
}
logger.info("Velocity uuid for allowed proxies:" + UUID.nameUUIDFromBytes("velocity".getBytes(StandardCharsets.UTF_8)));
server.getChannelRegistrar().register(MinecraftChannelIdentifier.create(getName(), ChangePremiumMessage.CHANGE_CHANNEL));
server.getChannelRegistrar().register(MinecraftChannelIdentifier.create(getName(), SuccessMessage.SUCCESS_CHANNEL));
server.getEventManager().register(this, new ConnectListener(this, core.getRateLimiter()));
@ -141,4 +148,43 @@ public class FastLoginVelocity implements PlatformPlugin<CommandSource> {
server.sendPluginMessage(channel, dataOutput.toByteArray());
}
}
private void loadOrGenerateProxyId() {
File idFile = new File(dataDirectory.toFile(), PROXY_ID_fILE);
boolean shouldGenerate = false;
if (idFile.exists()) {
try {
List<String> lines = Files.readAllLines(idFile.toPath(), StandardCharsets.UTF_8);
if (lines.isEmpty()) {
shouldGenerate = true;
} else {
proxyId = UUID.fromString(lines.get(0));
}
} catch (IOException e) {
e.printStackTrace();
logger.error("Unable to load proxy id from '{}'", idFile.getAbsolutePath());
logger.error("Detailed exception:", e);
} catch (IllegalArgumentException e) {
logger.error("'{}' contains an invalid uuid! FastLogin will not work without a valid id.", idFile.getAbsolutePath());
}
} else {
shouldGenerate = true;
}
if (shouldGenerate) {
proxyId = UUID.randomUUID();
try {
Files.write(idFile.toPath(), Collections.singletonList(proxyId.toString()),
StandardCharsets.UTF_8, StandardOpenOption.CREATE);
} catch (IOException e) {
logger.error("Unable to save proxy id to '{}'", idFile.getAbsolutePath());
logger.error("Detailed exception:", e);
}
}
}
public UUID getProxyId() {
return proxyId;
}
}

View File

@ -115,11 +115,9 @@ public class ForceLoginTask
if (session.needsRegistration()) {
type = Type.REGISTER;
}
//FIXME: Velocity does not have an alternative for this!
//UUID proxyId = UUID.fromString(ProxyServer.getInstance().getConfig().getUuid());
UUID proxyId = UUID.nameUUIDFromBytes("velocity".getBytes(StandardCharsets.UTF_8));
ChannelMessage loginMessage = new LoginActionMessage(type, player.getUsername(), proxyId);
UUID proxyId = core.getPlugin().getProxyId();
ChannelMessage loginMessage = new LoginActionMessage(type, player.getUsername(), proxyId);
core.getPlugin().sendPluginMessage(server, loginMessage);
}