feat(velocity): Implement Velocity debug mode

This commit is contained in:
Adrian3d04 2022-08-13 14:25:02 +00:00
parent 315f6b043b
commit 1c7c50675a
8 changed files with 59 additions and 25 deletions

View File

@ -7,8 +7,6 @@ import org.slf4j.Logger;
import org.spongepowered.configurate.CommentedConfigurationNode; import org.spongepowered.configurate.CommentedConfigurationNode;
import org.spongepowered.configurate.ConfigurateException; import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.hocon.HoconConfigurationLoader; import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment;
public final class Loader { public final class Loader {

View File

@ -69,7 +69,7 @@ public final class AuthMeVelocityPlugin extends JavaPlugin {
public void logDebug(String debug) { public void logDebug(String debug) {
if (config.get().debug()) { if (config.get().debug()) {
getSLF4JLogger().info("[DEBUG] "+debug); getSLF4JLogger().info("[DEBUG] {}", debug);
} }
} }
} }

View File

@ -77,6 +77,7 @@ public final class AuthMeVelocityPlugin implements AuthMeVelocityAPI {
new VelocityLibraryManager<>( new VelocityLibraryManager<>(
logger, pluginDirectory, proxy.getPluginManager(), this)); logger, pluginDirectory, proxy.getPluginManager(), this));
libraries.loadLibraries(); libraries.loadLibraries();
logDebug("Loaded plugin libraries");
this.config = Loader.loadMainConfig(pluginDirectory, ProxyConfiguration.class, logger); this.config = Loader.loadMainConfig(pluginDirectory, ProxyConfiguration.class, logger);
@ -90,10 +91,12 @@ public final class AuthMeVelocityPlugin implements AuthMeVelocityAPI {
proxy.getEventManager().register(this, listener)); proxy.getEventManager().register(this, listener));
if (proxy.getPluginManager().isLoaded("fastlogin")) { if (proxy.getPluginManager().isLoaded("fastlogin")) {
logDebug("Register FastLogin compatibility");
proxy.getEventManager().register(this, new FastLoginListener(proxy, this)); proxy.getEventManager().register(this, new FastLoginListener(proxy, this));
} }
if (proxy.getPluginManager().isLoaded("miniplaceholders")) { if (proxy.getPluginManager().isLoaded("miniplaceholders")) {
logDebug("Register MiniPlaceholders compatibility");
AuthMePlaceholders.getExpansion(this).register(); AuthMePlaceholders.getExpansion(this).register();
} }
@ -166,4 +169,10 @@ public final class AuthMeVelocityPlugin implements AuthMeVelocityAPI {
public boolean isAuthServer(String server){ public boolean isAuthServer(String server){
return config.get().authServers().contains(server); return config.get().authServers().contains(server);
} }
public void logDebug(String msg) {
if (config.get().debug()) {
logger.info("[DEBUG] {}", msg);
}
}
} }

View File

@ -34,12 +34,14 @@ public class ConnectListener {
public void onInitialServer(PlayerChooseInitialServerEvent event, Continuation continuation){ public void onInitialServer(PlayerChooseInitialServerEvent event, Continuation continuation){
if(!plugin.config().get().ensureAuthServer().ensureFirstServerIsAuthServer()) { if(!plugin.config().get().ensureAuthServer().ensureFirstServerIsAuthServer()) {
continuation.resume(); continuation.resume();
plugin.logDebug("PlayerChooseInitialServerEvent | Not enabled");
return; return;
} }
Optional<RegisteredServer> optionalSV = event.getInitialServer(); Optional<RegisteredServer> optionalSV = event.getInitialServer();
if(optionalSV.isPresent() && plugin.isAuthServer(optionalSV.get())){ if(optionalSV.isPresent() && plugin.isAuthServer(optionalSV.get())){
continuation.resume(); continuation.resume();
plugin.logDebug("PlayerChooseInitialServerEvent | Player is in auth server");
return; return;
} }
@ -48,19 +50,22 @@ public class ConnectListener {
event.setInitialServer(server); event.setInitialServer(server);
continuation.resume(); continuation.resume();
if (server == null) { if (server == null) {
plugin.logDebug("PlayerChooseInitialServerEvent | Null server");
logger.error("Cannot send the player {} to an auth server", event.getPlayer().getUsername()); logger.error("Cannot send the player {} to an auth server", event.getPlayer().getUsername());
} }
} }
@Subscribe @Subscribe
public void onServerPreConnect(ServerPreConnectEvent event, Continuation continuation) { public void onServerPreConnect(ServerPreConnectEvent event, Continuation continuation) {
if (!event.getResult().isAllowed() && plugin.isLogged(event.getPlayer())) { if (!event.getResult().isAllowed() || plugin.isLogged(event.getPlayer())) {
plugin.logDebug("ServerPreConnectEvent | Not allowed or player not logged");
continuation.resume(); continuation.resume();
return; return;
} }
// this should be present, "event.getResult().isAllowed()" is the "isPresent" check // this should be present, "event.getResult().isAllowed()" is the "isPresent" check
if(!plugin.isAuthServer(event.getResult().getServer().get())) { if(!plugin.isAuthServer(event.getResult().getServer().orElse(null))) {
plugin.logDebug("ServerPreConnectEvent | Server is not an auth server");
event.setResult(ServerPreConnectEvent.ServerResult.denied()); event.setResult(ServerPreConnectEvent.ServerResult.denied());
} }
continuation.resume(); continuation.resume();
@ -70,7 +75,8 @@ public class ConnectListener {
public void onServerPostConnect(ServerPostConnectEvent event) { public void onServerPostConnect(ServerPostConnectEvent event) {
final Player player = event.getPlayer(); final Player player = event.getPlayer();
if (plugin.isLogged(player) && plugin.isInAuthServer(player)) { if (plugin.isLogged(player) && plugin.isInAuthServer(player)) {
ByteArrayDataOutput buf = ByteStreams.newDataOutput(); plugin.logDebug("ServerPostConnectEvent | Already logged player and connected to an Auth server");
final ByteArrayDataOutput buf = ByteStreams.newDataOutput();
buf.writeUTF("LOGIN"); buf.writeUTF("LOGIN");
player.getCurrentServer().ifPresent(sv -> player.getCurrentServer().ifPresent(sv ->
sv.sendPluginMessage(AuthMeVelocityPlugin.AUTHMEVELOCITY_CHANNEL, buf.toByteArray())); sv.sendPluginMessage(AuthMeVelocityPlugin.AUTHMEVELOCITY_CHANNEL, buf.toByteArray()));

View File

@ -4,18 +4,18 @@ import com.github.games647.fastlogin.velocity.event.VelocityFastLoginAutoLoginEv
import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import me.adrianed.authmevelocity.api.velocity.AuthMeVelocityAPI; import me.adrianed.authmevelocity.velocity.AuthMeVelocityPlugin;
public class FastLoginListener { public class FastLoginListener {
private final ProxyServer proxy; private final ProxyServer proxy;
private final AuthMeVelocityAPI api; private final AuthMeVelocityPlugin plugin;
public FastLoginListener(ProxyServer proxy, AuthMeVelocityAPI api){ public FastLoginListener(ProxyServer proxy, AuthMeVelocityPlugin plugin){
this.proxy = proxy; this.proxy = proxy;
this.api = api; this.plugin = plugin;
} }
@Subscribe @Subscribe
public void onAutoLogin(VelocityFastLoginAutoLoginEvent event){ public void onAutoLogin(VelocityFastLoginAutoLoginEvent event){
proxy.getPlayer(event.getProfile().getName()) plugin.logDebug("VelocityFastLoginAutoLoginEvent | Attempt to auto register player");
.ifPresent(api::addPlayer); proxy.getPlayer(event.getProfile().getName()).ifPresent(plugin::addPlayer);
} }
} }

View File

@ -43,14 +43,16 @@ public class PluginMessageListener {
@Subscribe @Subscribe
public void onPluginMessage(final PluginMessageEvent event, Continuation continuation) { public void onPluginMessage(final PluginMessageEvent event, Continuation continuation) {
final boolean cancelled = !(event.getSource() instanceof ServerConnection) final boolean cancelled = !event.getResult().isAllowed()
||!(event.getSource() instanceof ServerConnection)
|| !event.getIdentifier().equals(AuthMeVelocityPlugin.AUTHMEVELOCITY_CHANNEL); || !event.getIdentifier().equals(AuthMeVelocityPlugin.AUTHMEVELOCITY_CHANNEL);
if (cancelled) { if (cancelled) {
continuation.resume(); continuation.resume();
plugin.logDebug("PluginMessageEvent | Not allowed");
return; return;
} }
ServerConnection connection = ((ServerConnection)event.getSource()); final ServerConnection connection = ((ServerConnection)event.getSource());
event.setResult(PluginMessageEvent.ForwardResult.handled()); event.setResult(PluginMessageEvent.ForwardResult.handled());
@ -63,26 +65,39 @@ public class PluginMessageListener {
switch (type) { switch (type) {
case LOGIN -> { case LOGIN -> {
plugin.logDebug("PluginMessageEvent | Login type");
if (player != null && plugin.addPlayer(player)){ if (player != null && plugin.addPlayer(player)){
proxy.getEventManager().fireAndForget(new ProxyLoginEvent(player)); proxy.getEventManager().fireAndForget(new ProxyLoginEvent(player));
this.createServerConnectionRequest(player, connection); this.createServerConnectionRequest(player, connection);
plugin.logDebug("PluginMessageEvent | Player not null");
} }
} }
case LOGOUT -> { case LOGOUT -> {
plugin.logDebug("PluginMessageEvent | Logout type");
if (player != null && plugin.removePlayer(player)){ if (player != null && plugin.removePlayer(player)){
proxy.getEventManager().fireAndForget(new ProxyLogoutEvent(player)); proxy.getEventManager().fireAndForget(new ProxyLogoutEvent(player));
plugin.logDebug("PluginMessageEvent | Player not null");
} }
} }
case REGISTER -> { case REGISTER -> {
if (player != null) plugin.logDebug("PluginMessageEvent | Register");
if (player != null) {
proxy.getEventManager().fireAndForget(new ProxyRegisterEvent(player)); proxy.getEventManager().fireAndForget(new ProxyRegisterEvent(player));
plugin.logDebug("PluginMessageEvent | Player not null");
}
} }
case UNREGISTER -> { case UNREGISTER -> {
if(player != null) plugin.logDebug("PluginMessageEvent | Unregister type");
if(player != null) {
plugin.logDebug("PluginMessageEvent | Player not null");
proxy.getEventManager().fireAndForget(new ProxyUnregisterEvent(player)); proxy.getEventManager().fireAndForget(new ProxyUnregisterEvent(player));
}
} }
case FORCE_UNREGISTER -> case FORCE_UNREGISTER -> {
proxy.getEventManager().fireAndForget(new ProxyForcedUnregisterEvent(player)); proxy.getEventManager().fireAndForget(new ProxyForcedUnregisterEvent(player));
plugin.logDebug("PluginMessageEvent | Forced Unregister type");
}
} }
continuation.resume(); continuation.resume();
} }
@ -100,14 +115,14 @@ public class PluginMessageListener {
.thenApply(PreSendOnLoginEvent::getResult) .thenApply(PreSendOnLoginEvent::getResult)
.thenApply(GenericResult::isAllowed) .thenApply(GenericResult::isAllowed)
.thenAcceptAsync(allowed -> { .thenAcceptAsync(allowed -> {
if (!allowed) { if (!allowed.booleanValue()) {
return; return;
} }
player.createConnectionRequest(server) player.createConnectionRequest(server)
.connect() .connect()
.thenApply(Result::isSuccessful) .thenApply(Result::isSuccessful)
.thenAcceptAsync(result -> { .thenAcceptAsync(result -> {
if(!result) { if(!result.booleanValue()) {
logger.info("Unable to connect the player {} to the server {}", logger.info("Unable to connect the player {} to the server {}",
player.getUsername(), player.getUsername(),
server.getServerInfo().getName()); server.getServerInfo().getName());

View File

@ -28,25 +28,28 @@ public final class ProxyListener {
@Subscribe(order = PostOrder.FIRST) @Subscribe(order = PostOrder.FIRST)
public void onCommandExecute(final CommandExecuteEvent event, Continuation continuation) { public void onCommandExecute(final CommandExecuteEvent event, Continuation continuation) {
if (!(event.getCommandSource() instanceof Player)){ if (!(event.getCommandSource() instanceof final Player player)){
plugin.logDebug("CommandExecuteEvent | CommandSource is not a player");
continuation.resume(); continuation.resume();
return; return;
} }
Player player = (Player)event.getCommandSource();
if (plugin.isLogged(player)) { if (plugin.isLogged(player)) {
plugin.logDebug("CommandExecuteEvent | Player is not logged");
continuation.resume(); continuation.resume();
return; return;
} }
if (plugin.isInAuthServer(player)) { if (plugin.isInAuthServer(player)) {
plugin.logDebug("CommandExecuteEvent | Player is in Auth Server");
String command = AuthmeUtils.getFirstArgument(event.getCommand()); String command = AuthmeUtils.getFirstArgument(event.getCommand());
if (!plugin.config().get().commands().allowedCommands().contains(command)) { if (!plugin.config().get().commands().allowedCommands().contains(command)) {
plugin.logDebug("CommandExecuteEvent | Player executed an blocked command");
sendBlockedMessage(player); sendBlockedMessage(player);
event.setResult(CommandExecuteEvent.CommandResult.denied()); event.setResult(CommandExecuteEvent.CommandResult.denied());
} }
} else { } else {
plugin.logDebug("CommandExecuteEven | Player is not in auth server");
sendBlockedMessage(player); sendBlockedMessage(player);
event.setResult(CommandExecuteEvent.CommandResult.denied()); event.setResult(CommandExecuteEvent.CommandResult.denied());
} }
@ -56,6 +59,7 @@ public final class ProxyListener {
@Subscribe(order = PostOrder.FIRST) @Subscribe(order = PostOrder.FIRST)
public void onPlayerChat(final PlayerChatEvent event) { public void onPlayerChat(final PlayerChatEvent event) {
if (plugin.isNotLogged(event.getPlayer())) { if (plugin.isNotLogged(event.getPlayer())) {
plugin.logDebug("PlayerChatEvent | Player is not logged");
event.setResult(PlayerChatEvent.ChatResult.denied()); event.setResult(PlayerChatEvent.ChatResult.denied());
} }
} }
@ -63,6 +67,7 @@ public final class ProxyListener {
@Subscribe(order = PostOrder.FIRST) @Subscribe(order = PostOrder.FIRST)
public void onTabComplete(TabCompleteEvent event){ public void onTabComplete(TabCompleteEvent event){
if (plugin.isLogged(event.getPlayer())) { if (plugin.isLogged(event.getPlayer())) {
plugin.logDebug("TabCompleteEvent | Player is already logged");
return; return;
} }
@ -73,6 +78,7 @@ public final class ProxyListener {
} }
} }
plugin.logDebug("TabCompleteEvent | Not allowed tabcompletion");
event.getSuggestions().clear(); event.getSuggestions().clear();
} }

View File

@ -11,12 +11,12 @@ public class AuthmeUtils {
* @param string the string * @param string the string
* @return the first argument * @return the first argument
*/ */
public static @NotNull String getFirstArgument(@NotNull String string){ public static @NotNull String getFirstArgument(final @NotNull String string){
int index = Objects.requireNonNull(string).indexOf(" "); final int index = Objects.requireNonNull(string).indexOf(' ');
if (index == -1) { if (index == -1) {
return string; return string;
} }
return string.substring(0, index); return string.substring(0, index);
} }
private AuthmeUtils(){} private AuthmeUtils() {}
} }