From 44ba945b5837329bd6c26130c93caeb4b6d6ba87 Mon Sep 17 00:00:00 2001 From: Gravita Date: Tue, 31 May 2022 17:07:37 +0700 Subject: [PATCH] [FEATURE] DebugCommand --- .../command/basic/DebugCommand.java | 43 +++++++++++ .../command/handler/CommandHandler.java | 8 +- .../launchserver/socket/WebSocketService.java | 77 ++++++++++++++++--- .../handlers/WebSocketFrameHandler.java | 3 + 4 files changed, 115 insertions(+), 16 deletions(-) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/command/basic/DebugCommand.java diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/basic/DebugCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/basic/DebugCommand.java new file mode 100644 index 00000000..42dd2f5a --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/basic/DebugCommand.java @@ -0,0 +1,43 @@ +package pro.gravit.launchserver.command.basic; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.LoggerConfig; +import pro.gravit.launchserver.LaunchServer; +import pro.gravit.launchserver.command.Command; + +public class DebugCommand extends Command { + private transient Logger logger = LogManager.getLogger(); + public DebugCommand(LaunchServer server) { + super(server); + } + + @Override + public String getArgsDescription() { + return "[true/false]"; + } + + @Override + public String getUsageDescription() { + return "Enable log level TRACE in LaunchServer"; + } + + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args, 1); + boolean value = Boolean.parseBoolean(args[0]); + LoggerContext ctx = (LoggerContext) LogManager.getContext(false); + Configuration config = ctx.getConfiguration(); + LoggerConfig loggerConfig = config.getLoggerConfig("pro.gravit"); + loggerConfig.setLevel(value ? Level.TRACE : Level.DEBUG); + ctx.updateLoggers(); + if(value) { + logger.info("Log level TRACE enabled"); + } else { + logger.info("Log level TRACE disabled"); + } + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java index 99e1add5..300b4916 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java @@ -1,17 +1,13 @@ package pro.gravit.launchserver.command.handler; import pro.gravit.launchserver.LaunchServer; -import pro.gravit.launchserver.command.basic.BuildCommand; -import pro.gravit.launchserver.command.basic.RestartCommand; -import pro.gravit.launchserver.command.basic.StopCommand; -import pro.gravit.launchserver.command.basic.VersionCommand; +import pro.gravit.launchserver.command.basic.*; import pro.gravit.launchserver.command.hash.*; import pro.gravit.launchserver.command.modules.LoadModuleCommand; import pro.gravit.launchserver.command.modules.ModulesCommand; import pro.gravit.launchserver.command.service.*; import pro.gravit.utils.command.BaseCommandCategory; import pro.gravit.utils.command.basic.ClearCommand; -import pro.gravit.utils.command.basic.DebugCommand; import pro.gravit.utils.command.basic.GCCommand; import pro.gravit.utils.command.basic.HelpCommand; @@ -25,7 +21,7 @@ public static void registerCommands(pro.gravit.utils.command.CommandHandler hand basic.registerCommand("build", new BuildCommand(server)); basic.registerCommand("stop", new StopCommand(server)); basic.registerCommand("restart", new RestartCommand(server)); - basic.registerCommand("debug", new DebugCommand()); + basic.registerCommand("debug", new DebugCommand(server)); basic.registerCommand("clear", new ClearCommand(handler)); basic.registerCommand("gc", new GCCommand()); basic.registerCommand("loadModule", new LoadModuleCommand(server)); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java index 113d02bb..10d04a25 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java @@ -173,31 +173,71 @@ public void registerClient(Channel channel) { channels.add(channel); } + public static String getIPFromContext(ChannelHandlerContext ctx) { + var handler = ctx.pipeline().get(WebSocketFrameHandler.class); + if(handler == null || handler.context == null || handler.context.ip == null) { + return IOHelper.getIP(ctx.channel().remoteAddress()); + } + return handler.context.ip; + } + + public static String getIPFromChannel(Channel channel) { + var handler = channel.pipeline().get(WebSocketFrameHandler.class); + if(handler == null || handler.context == null || handler.context.ip == null) { + return IOHelper.getIP(channel.remoteAddress()); + } + return handler.context.ip; + } + public void sendObject(ChannelHandlerContext ctx, Object obj) { - ctx.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, WebSocketEvent.class)), ctx.voidPromise()); + String msg = gson.toJson(obj, WebSocketEvent.class); + if(logger.isTraceEnabled()) { + logger.trace("Send to {}: {}", getIPFromContext(ctx), msg); + } + ctx.writeAndFlush(new TextWebSocketFrame(msg), ctx.voidPromise()); } public void sendObject(ChannelHandlerContext ctx, Object obj, Type type) { - ctx.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type)), ctx.voidPromise()); + String msg = gson.toJson(obj, type); + if(logger.isTraceEnabled()) { + logger.trace("Send to {}: {}", getIPFromContext(ctx), msg); + } + ctx.writeAndFlush(new TextWebSocketFrame(msg), ctx.voidPromise()); } public void sendObject(Channel channel, Object obj) { - channel.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, WebSocketEvent.class)), channel.voidPromise()); + String msg = gson.toJson(obj, WebSocketEvent.class); + if(logger.isTraceEnabled()) { + logger.trace("Send to channel {}: {}", getIPFromChannel(channel), msg); + } + channel.writeAndFlush(new TextWebSocketFrame(msg), channel.voidPromise()); } public void sendObject(Channel channel, Object obj, Type type) { - channel.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type)), channel.voidPromise()); + String msg = gson.toJson(obj, type); + if(logger.isTraceEnabled()) { + logger.trace("Send to channel {}: {}", getIPFromChannel(channel), msg); + } + channel.writeAndFlush(new TextWebSocketFrame(msg), channel.voidPromise()); } public void sendObjectAll(Object obj) { + String msg = gson.toJson(obj, WebSocketEvent.class); + if(logger.isTraceEnabled()) { + logger.trace("Send to all: {}", msg); + } for (Channel ch : channels) { - ch.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, WebSocketEvent.class)), ch.voidPromise()); + ch.writeAndFlush(new TextWebSocketFrame(msg), ch.voidPromise()); } } public void sendObjectAll(Object obj, Type type) { + String msg = gson.toJson(obj, type); + if(logger.isTraceEnabled()) { + logger.trace("Send to all: {}", msg); + } for (Channel ch : channels) { - ch.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type)), ch.voidPromise()); + ch.writeAndFlush(new TextWebSocketFrame(msg), ch.voidPromise()); } } @@ -208,7 +248,11 @@ public void sendObjectToUUID(UUID userUuid, Object obj, Type type) { if (wsHandler == null) continue; Client client = wsHandler.getClient(); if (client == null || !userUuid.equals(client.uuid)) continue; - ch.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type)), ch.voidPromise()); + String msg = gson.toJson(obj, type); + if(logger.isTraceEnabled()) { + logger.trace("Send to {}({}): {}", getIPFromChannel(ch), userUuid, msg); + } + ch.writeAndFlush(new TextWebSocketFrame(msg), ch.voidPromise()); } } @@ -272,15 +316,28 @@ public boolean kickByIP(String ip, boolean isClose) { } public void sendObjectAndClose(ChannelHandlerContext ctx, Object obj) { - ctx.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, WebSocketEvent.class))).addListener(ChannelFutureListener.CLOSE); + String msg = gson.toJson(obj, WebSocketEvent.class); + if(logger.isTraceEnabled()) { + logger.trace("Send and close {}: {}", getIPFromContext(ctx), msg); + } + ctx.writeAndFlush(new TextWebSocketFrame(msg)).addListener(ChannelFutureListener.CLOSE); } public void sendObjectAndClose(ChannelHandlerContext ctx, Object obj, Type type) { - ctx.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type))).addListener(ChannelFutureListener.CLOSE); + String msg = gson.toJson(obj, type); + if(logger.isTraceEnabled()) { + logger.trace("Send and close {}: {}", getIPFromContext(ctx), msg); + } + ctx.writeAndFlush(new TextWebSocketFrame(msg)).addListener(ChannelFutureListener.CLOSE); } + @Deprecated public void sendEvent(EventResult obj) { - channels.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj)), ChannelMatchers.all(), true); + String msg = gson.toJson(obj, WebSocketEvent.class); + if(logger.isTraceEnabled()) { + logger.trace("Send event: {}", msg); + } + channels.writeAndFlush(new TextWebSocketFrame(msg), ChannelMatchers.all(), true); } public static class WebSocketRequestContext { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/WebSocketFrameHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/WebSocketFrameHandler.java index ea9eb7b0..3a4fb5af 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/WebSocketFrameHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/WebSocketFrameHandler.java @@ -64,6 +64,9 @@ protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) { logger.error("WebSocket frame handler hook error", ex); } if (frame instanceof TextWebSocketFrame) { + if(logger.isTraceEnabled()) { + logger.trace("Message from {}: {}", context.ip == null ? IOHelper.getIP(ctx.channel().remoteAddress()) : context.ip, ((TextWebSocketFrame) frame).text()); + } try { service.process(ctx, (TextWebSocketFrame) frame, client, context.ip); } catch (Throwable ex) {