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
This commit is contained in:
Dang Hoang Phuc 2022-10-07 22:07:20 +07:00 committed by GitHub
parent 0b9cab5ad5
commit a520bc9416
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 22 deletions

View File

@ -4,6 +4,7 @@ import static emu.grasscutter.config.Configuration.*;
import java.util.Set; import java.util.Set;
import emu.grasscutter.game.Account;
import emu.grasscutter.server.event.game.ReceivePacketEvent; import emu.grasscutter.server.event.game.ReceivePacketEvent;
import org.reflections.Reflections; import org.reflections.Reflections;
@ -68,6 +69,9 @@ public class GameServerPacketHandler {
if (state != SessionState.WAITING_FOR_TOKEN) { if (state != SessionState.WAITING_FOR_TOKEN) {
return; return;
} }
} else if (state == SessionState.ACCOUNT_BANNED) {
session.close();
return;
} else if (opcode == PacketOpcodes.PlayerLoginReq) { } else if (opcode == PacketOpcodes.PlayerLoginReq) {
if (state != SessionState.WAITING_FOR_LOGIN) { if (state != SessionState.WAITING_FOR_LOGIN) {
return; return;
@ -83,7 +87,8 @@ public class GameServerPacketHandler {
} }
// Invoke event. // 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. if (!event.isCanceled()) // If event is not canceled, continue.
handler.handle(session, header, event.getPacketData()); handler.handle(session, header, event.getPacketData());
} catch (Exception ex) { } catch (Exception ex) {

View File

@ -50,7 +50,7 @@ public class GameSession implements GameSessionManager.KcpChannel {
public InetSocketAddress getAddress() { public InetSocketAddress getAddress() {
try { try {
return tunnel.getAddress(); return tunnel.getAddress();
}catch (Throwable ignore) { } catch (Throwable ignore) {
return null; return null;
} }
} }
@ -96,10 +96,11 @@ public class GameSession implements GameSessionManager.KcpChannel {
send(basePacket); 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 + ")"); Grasscutter.getLogger().info(sendOrRecv + ": " + PacketOpcodesUtils.getOpcodeName(opcode) + " (" + opcode + ")");
System.out.println(Utils.bytesToHex(payload)); System.out.println(Utils.bytesToHex(payload));
} }
public void send(BasePacket packet) { public void send(BasePacket packet) {
// Test // Test
if (packet.getOpcode() <= 0) { if (packet.getOpcode() <= 0) {
@ -125,21 +126,23 @@ public class GameSession implements GameSessionManager.KcpChannel {
logPacket("SEND", packet.getOpcode(), packet.getData()); logPacket("SEND", packet.getOpcode(), packet.getData());
} }
} }
case WHITELIST-> { case WHITELIST -> {
if (SERVER.debugWhitelist.contains(packet.getOpcode())) { if (SERVER.debugWhitelist.contains(packet.getOpcode())) {
logPacket("SEND", packet.getOpcode(), packet.getData()); logPacket("SEND", packet.getOpcode(), packet.getData());
} }
} }
case BLACKLIST-> { case BLACKLIST -> {
if (!SERVER.debugBlacklist.contains(packet.getOpcode())) { if (!SERVER.debugBlacklist.contains(packet.getOpcode())) {
logPacket("SEND", packet.getOpcode(), packet.getData()); logPacket("SEND", packet.getOpcode(), packet.getData());
} }
} }
default -> {} default -> {
}
} }
// Invoke event. // 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. if (!event.isCanceled()) { // If event is not cancelled, continue.
tunnel.writeData(event.getPacket().build()); tunnel.writeData(event.getPacket().build());
} }
@ -172,7 +175,7 @@ public class GameSession implements GameSessionManager.KcpChannel {
int const1 = packet.readShort(); int const1 = packet.readShort();
if (const1 != 17767) { if (const1 != 17767) {
if (allDebug) { 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 return; // Bad packet
} }
@ -189,7 +192,7 @@ public class GameSession implements GameSessionManager.KcpChannel {
int const2 = packet.readShort(); int const2 = packet.readShort();
if (const2 != -30293) { if (const2 != -30293) {
if (allDebug) { 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 return; // Bad packet
} }
@ -198,20 +201,21 @@ public class GameSession implements GameSessionManager.KcpChannel {
switch (GAME_INFO.logPackets) { switch (GAME_INFO.logPackets) {
case ALL -> { case ALL -> {
if (!PacketOpcodesUtils.LOOP_PACKETS.contains(opcode)) { if (!PacketOpcodesUtils.LOOP_PACKETS.contains(opcode)) {
logPacket("RECV",opcode, payload); logPacket("RECV", opcode, payload);
} }
} }
case WHITELIST-> { case WHITELIST -> {
if (SERVER.debugWhitelist.contains(opcode)) { if (SERVER.debugWhitelist.contains(opcode)) {
logPacket("RECV",opcode, payload); logPacket("RECV", opcode, payload);
} }
} }
case BLACKLIST-> { case BLACKLIST -> {
if (!(SERVER.debugBlacklist.contains(opcode))) { if (!(SERVER.debugBlacklist.contains(opcode))) {
logPacket("RECV",opcode, payload); logPacket("RECV", opcode, payload);
} }
} }
default -> {} default -> {
}
} }
// Handle // Handle
@ -238,8 +242,8 @@ public class GameSession implements GameSessionManager.KcpChannel {
} }
try { try {
send(new BasePacket(PacketOpcodes.ServerDisconnectClientNotify)); send(new BasePacket(PacketOpcodes.ServerDisconnectClientNotify));
}catch (Throwable ignore) { } catch (Throwable ignore) {
Grasscutter.getLogger().warn("closing {} error",getAddress().getAddress().getHostAddress()); Grasscutter.getLogger().warn("closing {} error", getAddress().getAddress().getHostAddress());
} }
tunnel = null; tunnel = null;
} }
@ -257,6 +261,7 @@ public class GameSession implements GameSessionManager.KcpChannel {
WAITING_FOR_TOKEN, WAITING_FOR_TOKEN,
WAITING_FOR_LOGIN, WAITING_FOR_LOGIN,
PICKING_CHARACTER, PICKING_CHARACTER,
ACTIVE ACTIVE,
ACCOUNT_BANNED
} }
} }

View File

@ -67,7 +67,8 @@ public class HandlerGetPlayerTokenReq extends PacketHandler {
} }
// Call creation event. // Call creation event.
PlayerCreationEvent event = new PlayerCreationEvent(session, Player.class); event.call(); PlayerCreationEvent event = new PlayerCreationEvent(session, Player.class);
event.call();
// Get player. // Get player.
Player player = DatabaseHelper.getPlayerByAccount(account, event.getPlayerClass()); Player player = DatabaseHelper.getPlayerByAccount(account, event.getPlayerClass());
@ -87,8 +88,8 @@ public class HandlerGetPlayerTokenReq extends PacketHandler {
// Checks if the player is banned // Checks if the player is banned
if (session.getAccount().isBanned()) { if (session.getAccount().isBanned()) {
session.setState(SessionState.ACCOUNT_BANNED);
session.send(new PacketGetPlayerTokenRsp(session, 21, "FORBID_CHEATING_PLUGINS", session.getAccount().getBanEndTime())); session.send(new PacketGetPlayerTokenRsp(session, 21, "FORBID_CHEATING_PLUGINS", session.getAccount().getBanEndTime()));
session.close();
return; return;
} }
@ -132,8 +133,7 @@ public class HandlerGetPlayerTokenReq extends PacketHandler {
session.send(new PacketGetPlayerTokenRsp(session, base64str, "bm90aGluZyBoZXJl")); session.send(new PacketGetPlayerTokenRsp(session, base64str, "bm90aGluZyBoZXJl"));
} }
} } else {
else {
// Send packet // Send packet
session.send(new PacketGetPlayerTokenRsp(session)); session.send(new PacketGetPlayerTokenRsp(session));
} }