From a520bc9416b50e677c5adcfeca5b8921c37c1c35 Mon Sep 17 00:00:00 2001 From: Dang Hoang Phuc Date: Fri, 7 Oct 2022 22:07:20 +0700 Subject: [PATCH] Fix showing ban message box (#1826) * fix: getplayertoken ban message popup * feat: insure no packet handle when banned * feat: using session state instead of account --- .../server/game/GameServerPacketHandler.java | 7 +++- .../grasscutter/server/game/GameSession.java | 39 +++++++++++-------- .../packet/recv/HandlerGetPlayerTokenReq.java | 8 ++-- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/main/java/emu/grasscutter/server/game/GameServerPacketHandler.java b/src/main/java/emu/grasscutter/server/game/GameServerPacketHandler.java index 94fe3e232..2808e6e85 100644 --- a/src/main/java/emu/grasscutter/server/game/GameServerPacketHandler.java +++ b/src/main/java/emu/grasscutter/server/game/GameServerPacketHandler.java @@ -4,6 +4,7 @@ import static emu.grasscutter.config.Configuration.*; import java.util.Set; +import emu.grasscutter.game.Account; import emu.grasscutter.server.event.game.ReceivePacketEvent; import org.reflections.Reflections; @@ -68,6 +69,9 @@ public class GameServerPacketHandler { if (state != SessionState.WAITING_FOR_TOKEN) { return; } + } else if (state == SessionState.ACCOUNT_BANNED) { + session.close(); + return; } else if (opcode == PacketOpcodes.PlayerLoginReq) { if (state != SessionState.WAITING_FOR_LOGIN) { return; @@ -83,7 +87,8 @@ public class GameServerPacketHandler { } // Invoke event. - ReceivePacketEvent event = new ReceivePacketEvent(session, opcode, payload); event.call(); + ReceivePacketEvent event = new ReceivePacketEvent(session, opcode, payload); + event.call(); if (!event.isCanceled()) // If event is not canceled, continue. handler.handle(session, header, event.getPacketData()); } catch (Exception ex) { diff --git a/src/main/java/emu/grasscutter/server/game/GameSession.java b/src/main/java/emu/grasscutter/server/game/GameSession.java index bbb9c625b..11254f872 100644 --- a/src/main/java/emu/grasscutter/server/game/GameSession.java +++ b/src/main/java/emu/grasscutter/server/game/GameSession.java @@ -50,7 +50,7 @@ public class GameSession implements GameSessionManager.KcpChannel { public InetSocketAddress getAddress() { try { return tunnel.getAddress(); - }catch (Throwable ignore) { + } catch (Throwable ignore) { return null; } } @@ -96,10 +96,11 @@ public class GameSession implements GameSessionManager.KcpChannel { send(basePacket); } - public void logPacket( String sendOrRecv, int opcode, byte[] payload) { + public void logPacket(String sendOrRecv, int opcode, byte[] payload) { Grasscutter.getLogger().info(sendOrRecv + ": " + PacketOpcodesUtils.getOpcodeName(opcode) + " (" + opcode + ")"); System.out.println(Utils.bytesToHex(payload)); } + public void send(BasePacket packet) { // Test if (packet.getOpcode() <= 0) { @@ -125,21 +126,23 @@ public class GameSession implements GameSessionManager.KcpChannel { logPacket("SEND", packet.getOpcode(), packet.getData()); } } - case WHITELIST-> { + case WHITELIST -> { if (SERVER.debugWhitelist.contains(packet.getOpcode())) { logPacket("SEND", packet.getOpcode(), packet.getData()); } } - case BLACKLIST-> { + case BLACKLIST -> { if (!SERVER.debugBlacklist.contains(packet.getOpcode())) { logPacket("SEND", packet.getOpcode(), packet.getData()); } } - default -> {} + default -> { + } } // Invoke event. - SendPacketEvent event = new SendPacketEvent(this, packet); event.call(); + SendPacketEvent event = new SendPacketEvent(this, packet); + event.call(); if (!event.isCanceled()) { // If event is not cancelled, continue. tunnel.writeData(event.getPacket().build()); } @@ -172,7 +175,7 @@ public class GameSession implements GameSessionManager.KcpChannel { int const1 = packet.readShort(); if (const1 != 17767) { if (allDebug) { - Grasscutter.getLogger().error("Bad Data Package Received: got {} ,expect 17767",const1); + Grasscutter.getLogger().error("Bad Data Package Received: got {} ,expect 17767", const1); } return; // Bad packet } @@ -189,7 +192,7 @@ public class GameSession implements GameSessionManager.KcpChannel { int const2 = packet.readShort(); if (const2 != -30293) { if (allDebug) { - Grasscutter.getLogger().error("Bad Data Package Received: got {} ,expect -30293",const2); + Grasscutter.getLogger().error("Bad Data Package Received: got {} ,expect -30293", const2); } return; // Bad packet } @@ -198,20 +201,21 @@ public class GameSession implements GameSessionManager.KcpChannel { switch (GAME_INFO.logPackets) { case ALL -> { if (!PacketOpcodesUtils.LOOP_PACKETS.contains(opcode)) { - logPacket("RECV",opcode, payload); + logPacket("RECV", opcode, payload); } } - case WHITELIST-> { + case WHITELIST -> { if (SERVER.debugWhitelist.contains(opcode)) { - logPacket("RECV",opcode, payload); + logPacket("RECV", opcode, payload); } } - case BLACKLIST-> { + case BLACKLIST -> { if (!(SERVER.debugBlacklist.contains(opcode))) { - logPacket("RECV",opcode, payload); + logPacket("RECV", opcode, payload); } } - default -> {} + default -> { + } } // Handle @@ -238,8 +242,8 @@ public class GameSession implements GameSessionManager.KcpChannel { } try { send(new BasePacket(PacketOpcodes.ServerDisconnectClientNotify)); - }catch (Throwable ignore) { - Grasscutter.getLogger().warn("closing {} error",getAddress().getAddress().getHostAddress()); + } catch (Throwable ignore) { + Grasscutter.getLogger().warn("closing {} error", getAddress().getAddress().getHostAddress()); } tunnel = null; } @@ -257,6 +261,7 @@ public class GameSession implements GameSessionManager.KcpChannel { WAITING_FOR_TOKEN, WAITING_FOR_LOGIN, PICKING_CHARACTER, - ACTIVE + ACTIVE, + ACCOUNT_BANNED } } diff --git a/src/main/java/emu/grasscutter/server/packet/recv/HandlerGetPlayerTokenReq.java b/src/main/java/emu/grasscutter/server/packet/recv/HandlerGetPlayerTokenReq.java index 0b54a4f9a..5c26eb452 100644 --- a/src/main/java/emu/grasscutter/server/packet/recv/HandlerGetPlayerTokenReq.java +++ b/src/main/java/emu/grasscutter/server/packet/recv/HandlerGetPlayerTokenReq.java @@ -67,7 +67,8 @@ public class HandlerGetPlayerTokenReq extends PacketHandler { } // Call creation event. - PlayerCreationEvent event = new PlayerCreationEvent(session, Player.class); event.call(); + PlayerCreationEvent event = new PlayerCreationEvent(session, Player.class); + event.call(); // Get player. Player player = DatabaseHelper.getPlayerByAccount(account, event.getPlayerClass()); @@ -87,8 +88,8 @@ public class HandlerGetPlayerTokenReq extends PacketHandler { // Checks if the player is banned if (session.getAccount().isBanned()) { + session.setState(SessionState.ACCOUNT_BANNED); session.send(new PacketGetPlayerTokenRsp(session, 21, "FORBID_CHEATING_PLUGINS", session.getAccount().getBanEndTime())); - session.close(); return; } @@ -132,8 +133,7 @@ public class HandlerGetPlayerTokenReq extends PacketHandler { session.send(new PacketGetPlayerTokenRsp(session, base64str, "bm90aGluZyBoZXJl")); } - } - else { + } else { // Send packet session.send(new PacketGetPlayerTokenRsp(session)); }