diff --git a/velocity/src/main/java/com/github/games647/fastlogin/velocity/FastLoginVelocity.java b/velocity/src/main/java/com/github/games647/fastlogin/velocity/FastLoginVelocity.java index a7bd5c9d..f1f7b040 100644 --- a/velocity/src/main/java/com/github/games647/fastlogin/velocity/FastLoginVelocity.java +++ b/velocity/src/main/java/com/github/games647/fastlogin/velocity/FastLoginVelocity.java @@ -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 { private FastLoginCore core; private final ConcurrentMap 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 { 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 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; + } } diff --git a/velocity/src/main/java/com/github/games647/fastlogin/velocity/task/ForceLoginTask.java b/velocity/src/main/java/com/github/games647/fastlogin/velocity/task/ForceLoginTask.java index 44e4f034..4c75bf8d 100644 --- a/velocity/src/main/java/com/github/games647/fastlogin/velocity/task/ForceLoginTask.java +++ b/velocity/src/main/java/com/github/games647/fastlogin/velocity/task/ForceLoginTask.java @@ -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); }