diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..18f0443 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +target +*.iml \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..ed46f3a --- /dev/null +++ b/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + com.glyart.authmevelocity + parent + pom + 1.0.0 + + + spigot + proxy + + + + 8 + 8 + + + + AuthMeVelocity-${project.name} + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + org.apache.maven.plugins + maven-shade-plugin + 3.1.0 + + + package + + shade + + + false + + + + + + + + src/main/resources + true + + + + + \ No newline at end of file diff --git a/proxy/pom.xml b/proxy/pom.xml new file mode 100644 index 0000000..4ee49fe --- /dev/null +++ b/proxy/pom.xml @@ -0,0 +1,49 @@ + + + + parent + com.glyart.authmevelocity + 1.0.0 + + 4.0.0 + + proxy + + + 8 + 8 + + + + + velocity + https://nexus.velocitypowered.com/repository/maven-public/ + + + + + + com.velocitypowered + velocity-api + 1.1.4 + provided + + + + org.projectlombok + lombok + 1.18.18 + provided + + + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + 2.12.1 + compile + + + + \ No newline at end of file diff --git a/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java b/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java new file mode 100644 index 0000000..280a3d4 --- /dev/null +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java @@ -0,0 +1,56 @@ +package com.glyart.authmevelocity.proxy; + +import com.glyart.authmevelocity.proxy.config.AuthMeConfig; +import com.glyart.authmevelocity.proxy.listener.ProxyListener; +import com.google.inject.Inject; +import com.velocitypowered.api.event.Subscribe; +import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; +import com.velocitypowered.api.plugin.Plugin; +import com.velocitypowered.api.plugin.annotation.DataDirectory; +import com.velocitypowered.api.proxy.ProxyServer; +import com.velocitypowered.api.proxy.messages.LegacyChannelIdentifier; +import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier; +import lombok.Getter; +import org.slf4j.Logger; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.UUID; + +@Plugin(id = "authmevelocity", name = "AuthMeVelocity", version = "1.0.0", url = "https://github.com/Glyart/AuthMeVelocity", description = "This plugin adds the support for AuthMeReloaded to Velocity.", authors = {"xQuickGlare"}) +@Getter +public class AuthMeVelocityPlugin { + + private final ProxyServer server; + private final Logger logger; + private final Path dataFolder; + + private final List loggedPlayers = Collections.synchronizedList(new ArrayList<>()); + + private AuthMeConfig config; + + @Inject + public AuthMeVelocityPlugin(ProxyServer server, Logger logger, @DataDirectory Path dataFolder) { + this.server = server; + this.logger = logger; + this.dataFolder = dataFolder; + } + + @Subscribe + public void onProxyInitialize(ProxyInitializeEvent event) { + try { + config = AuthMeConfig.loadConfig(dataFolder); + } catch (IOException e) { + logger.error("An error occurred while enabling AuthMeVelocity.", e); + return; + } + + server.getChannelRegistrar().register(new LegacyChannelIdentifier("authmevelocity:main"), MinecraftChannelIdentifier.create("authmevelocity", "main")); + server.getEventManager().register(this, new ProxyListener(this)); + logger.info("AuthMeVelocity enabled."); + } + +} diff --git a/proxy/src/main/java/com/glyart/authmevelocity/proxy/config/AuthMeConfig.java b/proxy/src/main/java/com/glyart/authmevelocity/proxy/config/AuthMeConfig.java new file mode 100644 index 0000000..382fbd0 --- /dev/null +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/config/AuthMeConfig.java @@ -0,0 +1,36 @@ +package com.glyart.authmevelocity.proxy.config; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import lombok.Getter; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; + +@Getter +public class AuthMeConfig { + + private List authServers = Arrays.asList("auth-1", "auth-2"); + + public static AuthMeConfig loadConfig(Path folder) throws IOException { + File folderFile = folder.toFile(); + File file = new File(folderFile, "config.yml"); + ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); + if (!folderFile.exists()) + folderFile.mkdir(); + + if (!file.exists()) { + file.createNewFile(); + + AuthMeConfig config = new AuthMeConfig(); + mapper.writeValue(file, config); + return config; + } + + return mapper.readValue(file, AuthMeConfig.class); + } + +} diff --git a/proxy/src/main/java/com/glyart/authmevelocity/proxy/listener/ProxyListener.java b/proxy/src/main/java/com/glyart/authmevelocity/proxy/listener/ProxyListener.java new file mode 100644 index 0000000..02018fc --- /dev/null +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/listener/ProxyListener.java @@ -0,0 +1,101 @@ +package com.glyart.authmevelocity.proxy.listener; + +import com.glyart.authmevelocity.proxy.AuthMeVelocityPlugin; +import com.glyart.authmevelocity.proxy.config.AuthMeConfig; +import com.google.common.io.ByteArrayDataInput; +import com.velocitypowered.api.event.Subscribe; +import com.velocitypowered.api.event.command.CommandExecuteEvent; +import com.velocitypowered.api.event.connection.DisconnectEvent; +import com.velocitypowered.api.event.connection.PluginMessageEvent; +import com.velocitypowered.api.event.player.PlayerChatEvent; +import com.velocitypowered.api.event.player.ServerPreConnectEvent; +import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.ProxyServer; +import com.velocitypowered.api.proxy.ServerConnection; +import com.velocitypowered.api.proxy.server.RegisteredServer; + +import java.util.Optional; +import java.util.UUID; + +public class ProxyListener { + + private final AuthMeVelocityPlugin plugin; + private final ProxyServer server; + private final AuthMeConfig config; + + public ProxyListener(AuthMeVelocityPlugin plugin) { + this.plugin = plugin; + server = plugin.getServer(); + config = plugin.getConfig(); + } + + @Subscribe + public void onPluginMessage(PluginMessageEvent event) { + if (!(event.getSource() instanceof ServerConnection)) + return; + + if (!event.getIdentifier().getId().equals("authmevelocity:main")) + return; + + ByteArrayDataInput input = event.dataAsDataStream(); + String sChannel = input.readUTF(); + if (!sChannel.equals("LOGIN")) + return; + + String user = input.readUTF(); + Optional player = server.getPlayer(UUID.fromString(user)); + if (!player.isPresent()) + return; + + plugin.getLoggedPlayers().add(player.get().getUniqueId()); + } + + @Subscribe + public void onDisconnect(DisconnectEvent event) { + plugin.getLoggedPlayers().remove(event.getPlayer().getUniqueId()); + } + + @Subscribe + public void onCommandExecute(CommandExecuteEvent event) { + if (!(event.getCommandSource() instanceof Player)) + return; + + Player player = (Player) event.getCommandSource(); + if (plugin.getLoggedPlayers().contains(player.getUniqueId())) + return; + + Optional server = player.getCurrentServer(); + boolean isAuthServer = server.isPresent() && config.getAuthServers().contains(server.get().getServerInfo().getName()); + if (isAuthServer) + event.setResult(CommandExecuteEvent.CommandResult.forwardToServer()); + else + event.setResult(CommandExecuteEvent.CommandResult.denied()); + } + + @Subscribe + public void onPlayerChat(PlayerChatEvent event) { + Player player = event.getPlayer(); + if (plugin.getLoggedPlayers().contains(player.getUniqueId())) + return; + + Optional server = player.getCurrentServer(); + if (server.isPresent() && config.getAuthServers().contains(server.get().getServerInfo().getName())) + return; + + event.setResult(PlayerChatEvent.ChatResult.denied()); + } + + @Subscribe + public void onServerPreConnect(ServerPreConnectEvent event) { + Player player = event.getPlayer(); + if (plugin.getLoggedPlayers().contains(player.getUniqueId())) + return; + + Optional server = event.getResult().getServer(); + if (server.isPresent() && config.getAuthServers().contains(server.get().getServerInfo().getName())) + return; + + event.setResult(ServerPreConnectEvent.ServerResult.denied()); + } + +} diff --git a/spigot/pom.xml b/spigot/pom.xml new file mode 100644 index 0000000..ec0f920 --- /dev/null +++ b/spigot/pom.xml @@ -0,0 +1,47 @@ + + + + parent + com.glyart.authmevelocity + 1.0.0 + + 4.0.0 + + spigot + + + 8 + 8 + + + + + papermc + https://papermc.io/repo/repository/maven-public/ + + + + codemc-repo + https://repo.codemc.org/repository/maven-public/ + + + + + + com.destroystokyo.paper + paper-api + 1.16.5-R0.1-SNAPSHOT + provided + + + + fr.xephi + authme + 5.6.0-SNAPSHOT + provided + + + + \ No newline at end of file diff --git a/spigot/src/main/java/com/glyart/authmevelocity/spigot/AuthMeVelocityPlugin.java b/spigot/src/main/java/com/glyart/authmevelocity/spigot/AuthMeVelocityPlugin.java new file mode 100644 index 0000000..740e36c --- /dev/null +++ b/spigot/src/main/java/com/glyart/authmevelocity/spigot/AuthMeVelocityPlugin.java @@ -0,0 +1,32 @@ +package com.glyart.authmevelocity.spigot; + +import com.glyart.authmevelocity.spigot.listeners.AuthMeListener; +import com.google.common.io.ByteArrayDataOutput; +import com.google.common.io.ByteStreams; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; + +public class AuthMeVelocityPlugin extends JavaPlugin { + + @Override + public void onEnable() { + getServer().getMessenger().registerOutgoingPluginChannel(this, "authmevelocity:main"); + getServer().getPluginManager().registerEvents(new AuthMeListener(this), this); + + getLogger().info("AuthMeVelocity enabled."); + } + + @Override + public void onDisable() { + + } + + public void sendLoginToProxy(Player player) { + ByteArrayDataOutput out = ByteStreams.newDataOutput(); + out.writeUTF("LOGIN"); + out.writeUTF(player.getUniqueId().toString()); + + player.sendPluginMessage(this, "authmevelocity:main", out.toByteArray()); + } + +} diff --git a/spigot/src/main/java/com/glyart/authmevelocity/spigot/listeners/AuthMeListener.java b/spigot/src/main/java/com/glyart/authmevelocity/spigot/listeners/AuthMeListener.java new file mode 100644 index 0000000..eddc46d --- /dev/null +++ b/spigot/src/main/java/com/glyart/authmevelocity/spigot/listeners/AuthMeListener.java @@ -0,0 +1,21 @@ +package com.glyart.authmevelocity.spigot.listeners; + +import com.glyart.authmevelocity.spigot.AuthMeVelocityPlugin; +import fr.xephi.authme.events.LoginEvent; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; + +public class AuthMeListener implements Listener { + + private final AuthMeVelocityPlugin plugin; + + public AuthMeListener(AuthMeVelocityPlugin plugin) { + this.plugin = plugin; + } + + @EventHandler + public void onLogin(LoginEvent event) { + plugin.sendLoginToProxy(event.getPlayer()); + } + +} diff --git a/spigot/src/main/resources/plugin.yml b/spigot/src/main/resources/plugin.yml new file mode 100644 index 0000000..23e5cf2 --- /dev/null +++ b/spigot/src/main/resources/plugin.yml @@ -0,0 +1,5 @@ +name: AuthMeVelocity +author: xQuickGlare +version: ${project.version} +main: com.glyart.authmevelocity.spigot.AuthMeVelocityPlugin +depend: [AuthMe] \ No newline at end of file