|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: MIT |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2015-2021 <Your name and contributors> |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in all |
| 16 | + * copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | + * SOFTWARE. |
| 25 | + */ |
| 26 | +package com.github.games647.fastlogin.velocity; |
| 27 | + |
| 28 | +import com.github.games647.fastlogin.core.AsyncScheduler; |
| 29 | +import com.github.games647.fastlogin.core.message.ChangePremiumMessage; |
| 30 | +import com.github.games647.fastlogin.core.message.ChannelMessage; |
| 31 | +import com.github.games647.fastlogin.core.message.SuccessMessage; |
| 32 | +import com.github.games647.fastlogin.core.shared.FastLoginCore; |
| 33 | +import com.github.games647.fastlogin.core.shared.PlatformPlugin; |
| 34 | +import com.github.games647.fastlogin.velocity.listener.ConnectListener; |
| 35 | +import com.github.games647.fastlogin.velocity.listener.PluginMessageListener; |
| 36 | +import com.google.common.collect.MapMaker; |
| 37 | +import com.google.common.io.ByteArrayDataOutput; |
| 38 | +import com.google.common.io.ByteStreams; |
| 39 | +import com.google.inject.Inject; |
| 40 | +import com.velocitypowered.api.command.CommandSource; |
| 41 | +import com.velocitypowered.api.event.Subscribe; |
| 42 | +import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; |
| 43 | +import com.velocitypowered.api.event.proxy.ProxyShutdownEvent; |
| 44 | +import com.velocitypowered.api.plugin.Plugin; |
| 45 | +import com.velocitypowered.api.plugin.annotation.DataDirectory; |
| 46 | +import com.velocitypowered.api.proxy.Player; |
| 47 | +import com.velocitypowered.api.proxy.ProxyServer; |
| 48 | +import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier; |
| 49 | +import com.velocitypowered.api.proxy.server.RegisteredServer; |
| 50 | + |
| 51 | +import java.io.IOException; |
| 52 | +import java.net.InetSocketAddress; |
| 53 | +import java.nio.charset.StandardCharsets; |
| 54 | +import java.nio.file.Files; |
| 55 | +import java.nio.file.Path; |
| 56 | +import java.nio.file.StandardOpenOption; |
| 57 | +import java.util.Collections; |
| 58 | +import java.util.List; |
| 59 | +import java.util.UUID; |
| 60 | +import java.util.concurrent.ConcurrentMap; |
| 61 | + |
| 62 | +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; |
| 63 | + |
| 64 | +import org.slf4j.Logger; |
| 65 | + |
| 66 | +//TODO: Support for floodgate |
| 67 | +@Plugin(id = PomData.NAME, name = PomData.DISPLAY_NAME, description = PomData.DESCRIPTION, url = PomData.URL, |
| 68 | + version = PomData.VERSION, authors = {"games647", "https://github.com/games647/FastLogin/graphs/contributors"}) |
| 69 | +public class FastLoginVelocity implements PlatformPlugin<CommandSource> { |
| 70 | + |
| 71 | + private final ProxyServer server; |
| 72 | + private final Path dataDirectory; |
| 73 | + private final Logger logger; |
| 74 | + private final ConcurrentMap<InetSocketAddress, VelocityLoginSession> session = new MapMaker().weakKeys().makeMap(); |
| 75 | + private static final String PROXY_ID_fILE = "proxyId.txt"; |
| 76 | + |
| 77 | + private FastLoginCore<Player, CommandSource, FastLoginVelocity> core; |
| 78 | + private AsyncScheduler scheduler; |
| 79 | + private UUID proxyId; |
| 80 | + |
| 81 | + @Inject |
| 82 | + public FastLoginVelocity(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) { |
| 83 | + this.server = server; |
| 84 | + this.logger = logger; |
| 85 | + this.dataDirectory = dataDirectory; |
| 86 | + } |
| 87 | + |
| 88 | + @Subscribe |
| 89 | + public void onProxyInitialization(ProxyInitializeEvent event) { |
| 90 | + scheduler = new AsyncScheduler(logger, getThreadFactory()); |
| 91 | + core = new FastLoginCore<>(this); |
| 92 | + core.load(); |
| 93 | + loadOrGenerateProxyId(); |
| 94 | + if (!core.setupDatabase() || proxyId == null) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + server.getEventManager().register(this, new ConnectListener(this, core.getRateLimiter())); |
| 99 | + server.getEventManager().register(this, new PluginMessageListener(this)); |
| 100 | + server.getChannelRegistrar().register(MinecraftChannelIdentifier.create(getName(), ChangePremiumMessage.CHANGE_CHANNEL)); |
| 101 | + server.getChannelRegistrar().register(MinecraftChannelIdentifier.create(getName(), SuccessMessage.SUCCESS_CHANNEL)); |
| 102 | + } |
| 103 | + |
| 104 | + @Subscribe |
| 105 | + public void onProxyShutdown(ProxyShutdownEvent event) { |
| 106 | + if (core != null) { |
| 107 | + core.close(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public String getName() { |
| 113 | + return PomData.NAME; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public Path getPluginFolder() { |
| 118 | + return dataDirectory; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public Logger getLog() { |
| 123 | + return logger; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void sendMessage(CommandSource receiver, String message) { |
| 128 | + receiver.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(message)); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public AsyncScheduler getScheduler() { |
| 133 | + return scheduler; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public boolean isPluginInstalled(String name) { |
| 138 | + return server.getPluginManager().isLoaded(name); |
| 139 | + } |
| 140 | + |
| 141 | + public FastLoginCore<Player, CommandSource, FastLoginVelocity> getCore() { |
| 142 | + return core; |
| 143 | + } |
| 144 | + |
| 145 | + public ConcurrentMap<InetSocketAddress, VelocityLoginSession> getSession() { |
| 146 | + return session; |
| 147 | + } |
| 148 | + |
| 149 | + public ProxyServer getProxy() { |
| 150 | + return server; |
| 151 | + } |
| 152 | + |
| 153 | + public void sendPluginMessage(RegisteredServer server, ChannelMessage message) { |
| 154 | + if (server != null) { |
| 155 | + ByteArrayDataOutput dataOutput = ByteStreams.newDataOutput(); |
| 156 | + message.writeTo(dataOutput); |
| 157 | + |
| 158 | + MinecraftChannelIdentifier channel = MinecraftChannelIdentifier.create(getName(), message.getChannelName()); |
| 159 | + server.sendPluginMessage(channel, dataOutput.toByteArray()); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private void loadOrGenerateProxyId() { |
| 164 | + Path idFile = dataDirectory.resolve(PROXY_ID_fILE); |
| 165 | + boolean shouldGenerate = false; |
| 166 | + |
| 167 | + if (Files.exists(idFile)) { |
| 168 | + try { |
| 169 | + List<String> lines = Files.readAllLines(idFile, StandardCharsets.UTF_8); |
| 170 | + if (lines.isEmpty()) { |
| 171 | + shouldGenerate = true; |
| 172 | + } else { |
| 173 | + proxyId = UUID.fromString(lines.get(0)); |
| 174 | + } |
| 175 | + } catch (IOException e) { |
| 176 | + logger.error("Unable to load proxy id from '{}'", idFile.toAbsolutePath()); |
| 177 | + logger.error("Detailed exception:", e); |
| 178 | + } catch (IllegalArgumentException e) { |
| 179 | + logger.error("'{}' contains an invalid uuid! FastLogin will not work without a valid id.", idFile.toAbsolutePath()); |
| 180 | + } |
| 181 | + } else { |
| 182 | + shouldGenerate = true; |
| 183 | + } |
| 184 | + |
| 185 | + if (shouldGenerate) { |
| 186 | + proxyId = UUID.randomUUID(); |
| 187 | + try { |
| 188 | + Files.write(idFile, Collections.singletonList(proxyId.toString()), StandardOpenOption.CREATE); |
| 189 | + } catch (IOException e) { |
| 190 | + logger.error("Unable to save proxy id to '{}'", idFile.toAbsolutePath()); |
| 191 | + logger.error("Detailed exception:", e); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + public UUID getProxyId() { |
| 197 | + return proxyId; |
| 198 | + } |
| 199 | +} |
0 commit comments