Re-added Jansi, moved ClientMessage logging.

This commit is contained in:
Mike 2020-05-05 00:20:07 +02:00
parent 89ff26f21b
commit a0582c09ee
13 changed files with 115 additions and 85 deletions

View File

@ -154,6 +154,12 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>1.18</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>

View File

@ -1,6 +1,8 @@
package com.eu.habbo;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.ConsoleAppender;
import com.eu.habbo.core.*;
import com.eu.habbo.core.consolecommands.ConsoleCommand;
import com.eu.habbo.database.Database;
@ -30,6 +32,8 @@ import java.util.concurrent.ThreadLocalRandom;
public final class Emulator {
private static final Logger LOGGER = LoggerFactory.getLogger(Emulator.class);
private static final String OS_NAME = System.getProperty("os.name");
private static final String CLASS_PATH = System.getProperty("java.class.path");
public final static int MAJOR = 2;
public final static int MINOR = 4;
@ -81,6 +85,17 @@ public final class Emulator {
public static void main(String[] args) throws Exception {
try {
// Check if running on Windows and not in IntelliJ.
// If so, we need to reconfigure the console appender and enable Jansi for colors.
if (OS_NAME.startsWith("Windows") && !CLASS_PATH.contains("idea_rt.jar")) {
ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
ConsoleAppender<ILoggingEvent> appender = (ConsoleAppender<ILoggingEvent>) root.getAppender("Console");
appender.stop();
appender.setWithJansi(true);
appender.start();
}
Locale.setDefault(new Locale("en"));
setBuild();
Emulator.stopped = false;

View File

@ -1,10 +1,9 @@
package com.eu.habbo.messages;
import com.eu.habbo.util.PacketUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.nio.charset.Charset;
public class ClientMessage {
private final int header;
private final ByteBuf buffer;
@ -65,13 +64,7 @@ public class ClientMessage {
}
public String getMessageBody() {
String consoleText = this.buffer.toString(Charset.defaultCharset());
for (int i = -1; i < 31; i++) {
consoleText = consoleText.replace(Character.toString((char) i), "[" + i + "]");
}
return consoleText;
return PacketUtils.formatPacket(this.buffer);
}
public int bytesAvailable() {

View File

@ -1,7 +1,6 @@
package com.eu.habbo.messages;
import com.eu.habbo.Emulator;
import com.eu.habbo.core.CreditsScheduler;
import com.eu.habbo.habbohotel.gameclients.GameClient;
import com.eu.habbo.messages.incoming.Incoming;
import com.eu.habbo.messages.incoming.MessageHandler;
@ -53,9 +52,9 @@ import com.eu.habbo.messages.incoming.rooms.items.jukebox.*;
import com.eu.habbo.messages.incoming.rooms.items.lovelock.LoveLockStartConfirmEvent;
import com.eu.habbo.messages.incoming.rooms.items.rentablespace.RentSpaceCancelEvent;
import com.eu.habbo.messages.incoming.rooms.items.rentablespace.RentSpaceEvent;
import com.eu.habbo.messages.incoming.rooms.items.youtube.YoutubeRequestStateChange;
import com.eu.habbo.messages.incoming.rooms.items.youtube.YoutubeRequestPlaylists;
import com.eu.habbo.messages.incoming.rooms.items.youtube.YoutubeRequestPlaylistChange;
import com.eu.habbo.messages.incoming.rooms.items.youtube.YoutubeRequestPlaylists;
import com.eu.habbo.messages.incoming.rooms.items.youtube.YoutubeRequestStateChange;
import com.eu.habbo.messages.incoming.rooms.pets.*;
import com.eu.habbo.messages.incoming.rooms.promotions.BuyRoomPromotionEvent;
import com.eu.habbo.messages.incoming.rooms.promotions.RequestPromotionRoomsEvent;
@ -169,7 +168,7 @@ public class PacketManager {
if (client.getHabbo() == null && !handlerClass.isAnnotationPresent(NoAuthMessage.class)) {
if (DEBUG_SHOW_PACKETS) {
LOGGER.debug("[CLIENT][NOT LOGGED IN][{}] => {}", packet.getMessageId(), packet.getMessageBody());
LOGGER.warn("Client packet {} requires an authenticated session.", packet.getMessageId());
}
return;
@ -180,7 +179,7 @@ public class PacketManager {
if (handler.getRatelimit() > 0) {
if (client.messageTimestamps.containsKey(handlerClass) && System.currentTimeMillis() - client.messageTimestamps.get(handlerClass) < handler.getRatelimit()) {
if (PacketManager.DEBUG_SHOW_PACKETS) {
LOGGER.debug("[CLIENT][{}][RATELIMITED] => {}", packet.getMessageId(), packet.getMessageBody());
LOGGER.warn("Client packet {} was ratelimited.", packet.getMessageId());
}
return;
@ -189,12 +188,8 @@ public class PacketManager {
}
}
if (PacketManager.DEBUG_SHOW_PACKETS) {
LOGGER.debug("[CLIENT][{}] => {}", packet.getMessageId(), packet.getMessageBody());
}
if (logList.contains(packet.getMessageId()) && client.getHabbo() != null) {
LOGGER.debug("[CLIENT][{}][{}] => {}", client.getHabbo().getHabboInfo().getUsername(), packet.getMessageId(), packet.getMessageBody());
LOGGER.info("User {} sent packet {} with body {}", client.getHabbo().getHabboInfo().getUsername(), packet.getMessageId(), packet.getMessageBody());
}
handler.client = client;
@ -209,10 +204,6 @@ public class PacketManager {
if (!handler.isCancelled) {
handler.handle();
}
} else {
if (PacketManager.DEBUG_SHOW_PACKETS) {
LOGGER.debug("[CLIENT][UNDEFINED][{}] => {}", packet.getMessageId(), packet.getMessageBody());
}
}
} catch (Exception e) {
LOGGER.error("Caught exception", e);

View File

@ -1,10 +1,9 @@
package com.eu.habbo.messages;
import com.eu.habbo.util.HexUtils;
import com.eu.habbo.util.PacketUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.buffer.Unpooled;
import io.netty.util.IllegalReferenceCountException;
import io.netty.util.ReferenceCounted;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -173,13 +172,7 @@ public class ServerMessage implements ReferenceCounted {
}
public String getBodyString() {
byte[] data = this.get().array();
if (data.length == 0) {
return "";
}
return HexUtils.pretty(data);
return PacketUtils.formatPacket(this.channelBuffer);
}
public int getHeader() {

View File

@ -8,6 +8,7 @@ import io.netty.channel.EventLoopGroup;
import io.netty.channel.FixedRecvByteBufAllocator;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.concurrent.DefaultThreadFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -29,8 +30,10 @@ public abstract class Server {
this.host = host;
this.port = port;
this.bossGroup = new NioEventLoopGroup(bossGroupThreads);
this.workerGroup = new NioEventLoopGroup(workerGroupThreads);
String threadName = name.replace("Server", "").replace(" ", "");
this.bossGroup = new NioEventLoopGroup(bossGroupThreads, new DefaultThreadFactory(threadName + "Boss"));
this.workerGroup = new NioEventLoopGroup(workerGroupThreads, new DefaultThreadFactory(threadName + "Worker"));
this.serverBootstrap = new ServerBootstrap();
}
@ -40,8 +43,8 @@ public abstract class Server {
this.serverBootstrap.childOption(ChannelOption.TCP_NODELAY, true);
this.serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
this.serverBootstrap.childOption(ChannelOption.SO_REUSEADDR, true);
this.serverBootstrap.childOption(ChannelOption.SO_RCVBUF, 5120);
this.serverBootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(5120));
this.serverBootstrap.childOption(ChannelOption.SO_RCVBUF, 4096);
this.serverBootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(4096));
this.serverBootstrap.childOption(ChannelOption.ALLOCATOR, new UnpooledByteBufAllocator(false));
}

View File

@ -5,7 +5,7 @@ import com.eu.habbo.habbohotel.gameclients.GameClientManager;
import com.eu.habbo.messages.PacketManager;
import com.eu.habbo.networking.Server;
import com.eu.habbo.networking.gameserver.decoders.*;
import com.eu.habbo.networking.gameserver.encoders.ServerMessageEncoder;
import com.eu.habbo.networking.gameserver.encoders.GameServerMessageEncoder;
import com.eu.habbo.networking.gameserver.encoders.GameServerMessageLogger;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
@ -30,17 +30,23 @@ public class GameServer extends Server {
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("logger", new LoggingHandler());
// Logger.
// Decoders.
ch.pipeline().addLast(
new GamePolicyDecoder(),
new GameByteFrameDecoder(),
new GameByteDecoder(),
new GameMessageRateLimit(),
new GameMessageHandler()
);
ch.pipeline().addLast(new GamePolicyDecoder());
ch.pipeline().addLast(new GameByteFrameDecoder());
ch.pipeline().addLast(new GameByteDecoder());
if (PacketManager.DEBUG_SHOW_PACKETS) {
ch.pipeline().addLast(new GameClientMessageLogger());
}
ch.pipeline().addLast(new GameMessageRateLimit());
ch.pipeline().addLast(new GameMessageHandler());
// Encoders.
ch.pipeline().addLast(new ServerMessageEncoder());
ch.pipeline().addLast(new GameServerMessageEncoder());
if (PacketManager.DEBUG_SHOW_PACKETS) {
ch.pipeline().addLast(new GameServerMessageLogger());

View File

@ -0,0 +1,23 @@
package com.eu.habbo.networking.gameserver.decoders;
import com.eu.habbo.messages.ClientMessage;
import com.eu.habbo.util.ANSI;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
public class GameClientMessageLogger extends MessageToMessageDecoder<ClientMessage> {
private static final Logger LOGGER = LoggerFactory.getLogger(GameClientMessageLogger.class);
@Override
protected void decode(ChannelHandlerContext ctx, ClientMessage message, List<Object> out) {
LOGGER.debug(String.format("[" + ANSI.GREEN + "CLIENT" + ANSI.DEFAULT + "][%-4d] => %s", message.getMessageId(), message.getMessageBody()));
out.add(message);
}
}

View File

@ -4,9 +4,8 @@ import com.eu.habbo.messages.ServerMessage;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.IllegalReferenceCountException;
public class ServerMessageEncoder extends MessageToByteEncoder<ServerMessage> {
public class GameServerMessageEncoder extends MessageToByteEncoder<ServerMessage> {
@Override
protected void encode(ChannelHandlerContext ctx, ServerMessage message, ByteBuf out) {

View File

@ -1,6 +1,7 @@
package com.eu.habbo.networking.gameserver.encoders;
import com.eu.habbo.messages.ServerMessage;
import com.eu.habbo.util.ANSI;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import org.slf4j.Logger;
@ -13,15 +14,10 @@ public class GameServerMessageLogger extends MessageToMessageEncoder<ServerMessa
private static final Logger LOGGER = LoggerFactory.getLogger(GameServerMessageLogger.class);
@Override
protected void encode(ChannelHandlerContext ctx, ServerMessage message, List<Object> out) throws Exception {
LOGGER.debug("[SERVER][{}]", message.getHeader());
String body = message.getBodyString();
if (body == null || body.length() == 0) {
LOGGER.debug("\n" + message.getBodyString());
}
protected void encode(ChannelHandlerContext ctx, ServerMessage message, List<Object> out) {
LOGGER.debug(String.format("[" + ANSI.BLUE + "SERVER" + ANSI.DEFAULT + "][%-4d] => %s", message.getHeader(), message.getBodyString()));
out.add(message.retain());
}
}
}

View File

@ -0,0 +1,16 @@
package com.eu.habbo.util;
import ch.qos.logback.core.pattern.color.ANSIConstants;
public class ANSI {
public static final String RED = "\u001B[" + ANSIConstants.RED_FG + "m";
public static final String GREEN = "\u001B[" + ANSIConstants.GREEN_FG + "m";
public static final String YELLOW = "\u001B[" + ANSIConstants.YELLOW_FG + "m";
public static final String BLUE = "\u001B[" + ANSIConstants.BLUE_FG + "m";
public static final String MAGENTA = "\u001B[" + ANSIConstants.MAGENTA_FG + "m";
public static final String CYAN = "\u001B[" + ANSIConstants.CYAN_FG + "m";
public static final String WHITE = "\u001B[" + ANSIConstants.WHITE_FG + "m";
public static final String DEFAULT = "\u001B[" + ANSIConstants.DEFAULT_FG + "m";
}

View File

@ -1,7 +1,5 @@
package com.eu.habbo.util;
import java.nio.charset.StandardCharsets;
public class HexUtils {
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
@ -26,32 +24,4 @@ public class HexUtils {
return data;
}
public static String pretty(byte[] array) {
final int width = 32;
StringBuilder builder = new StringBuilder();
for (int rowOffset = 0; rowOffset < array.length; rowOffset += width) {
builder.append(String.format("%06d: ", rowOffset));
for (int index = 0; index < width; index++) {
if (rowOffset + index < array.length) {
builder.append(String.format("%02x ", array[rowOffset + index]));
} else {
builder.append(" ");
}
}
int asciiWidth = Math.min(width, array.length - rowOffset);
builder.append(" | ");
builder.append(new String(array, rowOffset, asciiWidth, StandardCharsets.UTF_8).replaceAll("\r\n", " ").replaceAll("\n", " "));
if (rowOffset + width < array.length) {
builder.append(String.format("%n"));
}
}
return builder.toString();
}
}

View File

@ -0,0 +1,19 @@
package com.eu.habbo.util;
import io.netty.buffer.ByteBuf;
import java.nio.charset.Charset;
public class PacketUtils {
public static String formatPacket(ByteBuf buffer) {
String result = buffer.toString(Charset.defaultCharset());
for (int i = -1; i < 31; i++) {
result = result.replace(Character.toString((char) i), "[" + i + "]");
}
return result;
}
}