Commit c79ca302 authored by akatatsu27's avatar akatatsu27 Committed by GitHub
Browse files

Added more server debug options (#1444)

Original commits:

* Added more server debug options

* made server debug code prettier

* fixed initialization bug

* Enables logging of packets contained in UnionCmdNotify, when debug level is WHITELIST or BLACKLIST
parent 9033c966
...@@ -4,6 +4,8 @@ import ch.qos.logback.classic.Level; ...@@ -4,6 +4,8 @@ import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.Logger;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import emu.grasscutter.Grasscutter.ServerDebugMode;
import emu.grasscutter.auth.AuthenticationSystem; import emu.grasscutter.auth.AuthenticationSystem;
import emu.grasscutter.auth.DefaultAuthentication; import emu.grasscutter.auth.DefaultAuthentication;
import emu.grasscutter.command.CommandMap; import emu.grasscutter.command.CommandMap;
...@@ -412,6 +414,6 @@ public final class Grasscutter { ...@@ -412,6 +414,6 @@ public final class Grasscutter {
} }
public enum ServerDebugMode { public enum ServerDebugMode {
ALL, MISSING, NONE ALL, MISSING, WHITELIST, BLACKLIST, NONE
} }
} }
...@@ -26,10 +26,10 @@ public class GameSession implements GameSessionManager.KcpChannel { ...@@ -26,10 +26,10 @@ public class GameSession implements GameSessionManager.KcpChannel {
private Account account; private Account account;
private Player player; private Player player;
private boolean useSecretKey; private boolean useSecretKey;
private SessionState state; private SessionState state;
private int clientTime; private int clientTime;
private long lastPingTime; private long lastPingTime;
private int lastClientSeq = 10; private int lastClientSeq = 10;
...@@ -39,11 +39,11 @@ public class GameSession implements GameSessionManager.KcpChannel { ...@@ -39,11 +39,11 @@ public class GameSession implements GameSessionManager.KcpChannel {
this.state = SessionState.WAITING_FOR_TOKEN; this.state = SessionState.WAITING_FOR_TOKEN;
this.lastPingTime = System.currentTimeMillis(); this.lastPingTime = System.currentTimeMillis();
} }
public GameServer getServer() { public GameServer getServer() {
return server; return server;
} }
public InetSocketAddress getAddress() { public InetSocketAddress getAddress() {
try{ try{
return tunnel.getAddress(); return tunnel.getAddress();
...@@ -55,7 +55,7 @@ public class GameSession implements GameSessionManager.KcpChannel { ...@@ -55,7 +55,7 @@ public class GameSession implements GameSessionManager.KcpChannel {
public boolean useSecretKey() { public boolean useSecretKey() {
return useSecretKey; return useSecretKey;
} }
public Account getAccount() { public Account getAccount() {
return account; return account;
} }
...@@ -63,7 +63,7 @@ public class GameSession implements GameSessionManager.KcpChannel { ...@@ -63,7 +63,7 @@ public class GameSession implements GameSessionManager.KcpChannel {
public void setAccount(Account account) { public void setAccount(Account account) {
this.account = account; this.account = account;
} }
public String getAccountId() { public String getAccountId() {
return this.getAccount().getId(); return this.getAccount().getId();
} }
...@@ -93,7 +93,7 @@ public class GameSession implements GameSessionManager.KcpChannel { ...@@ -93,7 +93,7 @@ public class GameSession implements GameSessionManager.KcpChannel {
public void setUseSecretKey(boolean useSecretKey) { public void setUseSecretKey(boolean useSecretKey) {
this.useSecretKey = useSecretKey; this.useSecretKey = useSecretKey;
} }
public int getClientTime() { public int getClientTime() {
return this.clientTime; return this.clientTime;
} }
...@@ -101,12 +101,12 @@ public class GameSession implements GameSessionManager.KcpChannel { ...@@ -101,12 +101,12 @@ public class GameSession implements GameSessionManager.KcpChannel {
public long getLastPingTime() { public long getLastPingTime() {
return lastPingTime; return lastPingTime;
} }
public void updateLastPingTime(int clientTime) { public void updateLastPingTime(int clientTime) {
this.clientTime = clientTime; this.clientTime = clientTime;
this.lastPingTime = System.currentTimeMillis(); this.lastPingTime = System.currentTimeMillis();
} }
public int getNextClientSequence() { public int getNextClientSequence() {
return ++lastClientSeq; return ++lastClientSeq;
} }
...@@ -114,17 +114,21 @@ public class GameSession implements GameSessionManager.KcpChannel { ...@@ -114,17 +114,21 @@ public class GameSession implements GameSessionManager.KcpChannel {
public void replayPacket(int opcode, String name) { public void replayPacket(int opcode, String name) {
String filePath = PACKET(name); String filePath = PACKET(name);
File p = new File(filePath); File p = new File(filePath);
if (!p.exists()) return; if (!p.exists()) return;
byte[] packet = FileUtils.read(p); byte[] packet = FileUtils.read(p);
BasePacket basePacket = new BasePacket(opcode); BasePacket basePacket = new BasePacket(opcode);
basePacket.setData(packet); basePacket.setData(packet);
send(basePacket); send(basePacket);
} }
public void logPacket( String sendOrRecv, int opcode, byte[] payload) {
Grasscutter.getLogger().info(sendOrRecv + ": " + PacketOpcodesUtil.getOpcodeName(opcode) + " (" + opcode + ")");
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) {
...@@ -137,26 +141,34 @@ public class GameSession implements GameSessionManager.KcpChannel { ...@@ -137,26 +141,34 @@ public class GameSession implements GameSessionManager.KcpChannel {
if(PacketOpcodes.BANNED_PACKETS.contains(packet.getOpcode())) { if(PacketOpcodes.BANNED_PACKETS.contains(packet.getOpcode())) {
return; return;
} }
// Header // Header
if (packet.shouldBuildHeader()) { if (packet.shouldBuildHeader()) {
packet.buildHeader(this.getNextClientSequence()); packet.buildHeader(this.getNextClientSequence());
} }
// Log // Log
if (SERVER.debugLevel == ServerDebugMode.ALL) { if (SERVER.debugLevel == ServerDebugMode.ALL) {
if (!loopPacket.contains(packet.getOpcode())) { if (!loopPacket.contains(packet.getOpcode())) {
Grasscutter.getLogger().info("SEND: " + PacketOpcodesUtil.getOpcodeName(packet.getOpcode()) + " (" + packet.getOpcode() + ")"); logPacket("SEND",packet.getOpcode(), packet.getData());
System.out.println(Utils.bytesToHex(packet.getData()));
} }
} }
if (SERVER.debugLevel == ServerDebugMode.WHITELIST && SERVER.DebugWhitelist.contains(packet.getOpcode())) {
logPacket("SEND",packet.getOpcode(), packet.getData());
}
if (SERVER.debugLevel == ServerDebugMode.BLACKLIST && !(SERVER.DebugBlacklist.contains(packet.getOpcode()))) {
logPacket("SEND",packet.getOpcode(), packet.getData());
}
// 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());
} }
} }
private static final Set<Integer> loopPacket = Set.of( private static final Set<Integer> loopPacket = Set.of(
PacketOpcodes.PingReq, PacketOpcodes.PingReq,
PacketOpcodes.PingRsp, PacketOpcodes.PingRsp,
...@@ -216,10 +228,18 @@ public class GameSession implements GameSessionManager.KcpChannel { ...@@ -216,10 +228,18 @@ public class GameSession implements GameSessionManager.KcpChannel {
// Log packet // Log packet
if (allDebug) { if (allDebug) {
if (!loopPacket.contains(opcode)) { if (!loopPacket.contains(opcode)) {
Grasscutter.getLogger().info("RECV: " + PacketOpcodesUtil.getOpcodeName(opcode) + " (" + opcode + ")"); logPacket("RECV",opcode, payload);
System.out.println(Utils.bytesToHex(payload));
} }
} }
if (SERVER.debugLevel == ServerDebugMode.WHITELIST && SERVER.DebugWhitelist.contains(opcode)) {
logPacket("RECV",opcode, payload);
}
if (SERVER.debugLevel == ServerDebugMode.BLACKLIST && !(SERVER.DebugBlacklist.contains(opcode))) {
logPacket("RECV",opcode, payload);
}
// Handle // Handle
getServer().getPacketHandler().handle(this, opcode, header, payload); getServer().getPacketHandler().handle(this, opcode, header, payload);
} }
......
package emu.grasscutter.server.packet.recv; package emu.grasscutter.server.packet.recv;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.net.packet.Opcodes; import emu.grasscutter.net.packet.Opcodes;
import emu.grasscutter.net.packet.PacketOpcodes; import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.UnionCmdNotifyOuterClass.UnionCmdNotify; import emu.grasscutter.net.proto.UnionCmdNotifyOuterClass.UnionCmdNotify;
import emu.grasscutter.net.proto.UnionCmdOuterClass.UnionCmd; import emu.grasscutter.net.proto.UnionCmdOuterClass.UnionCmd;
import emu.grasscutter.net.packet.PacketHandler; import emu.grasscutter.net.packet.PacketHandler;
import emu.grasscutter.server.game.GameSession; import emu.grasscutter.server.game.GameSession;
import emu.grasscutter.Grasscutter.ServerDebugMode;
import static emu.grasscutter.Configuration.SERVER;
@Opcodes(PacketOpcodes.UnionCmdNotify) @Opcodes(PacketOpcodes.UnionCmdNotify)
public class HandlerUnionCmdNotify extends PacketHandler { public class HandlerUnionCmdNotify extends PacketHandler {
...@@ -13,13 +17,21 @@ public class HandlerUnionCmdNotify extends PacketHandler { ...@@ -13,13 +17,21 @@ public class HandlerUnionCmdNotify extends PacketHandler {
public void handle(GameSession session, byte[] header, byte[] payload) throws Exception { public void handle(GameSession session, byte[] header, byte[] payload) throws Exception {
UnionCmdNotify req = UnionCmdNotify.parseFrom(payload); UnionCmdNotify req = UnionCmdNotify.parseFrom(payload);
for (UnionCmd cmd : req.getCmdListList()) { for (UnionCmd cmd : req.getCmdListList()) {
session.getServer().getPacketHandler().handle(session, cmd.getMessageId(), EMPTY_BYTE_ARRAY, cmd.getBody().toByteArray()); int cmdOpcode = cmd.getMessageId();
byte[] cmdPayload = cmd.getBody().toByteArray();
if(Grasscutter.config.server.debugLevel == ServerDebugMode.WHITELIST && SERVER.DebugWhitelist.contains(cmd.getMessageId())) {
session.logPacket("RECV in Union", cmdOpcode, cmdPayload);
} else if (Grasscutter.config.server.debugLevel == ServerDebugMode.BLACKLIST && !SERVER.DebugBlacklist.contains(cmd.getMessageId())) {
session.logPacket("RECV in Union", cmdOpcode, cmdPayload);
}
//debugLevel ALL ignores UnionCmdNotify, so we will also ignore the contained opcodes
session.getServer().getPacketHandler().handle(session, cmd.getMessageId(), EMPTY_BYTE_ARRAY, cmd.getBody().toByteArray());
} }
// Update // Update
session.getPlayer().getCombatInvokeHandler().update(session.getPlayer()); session.getPlayer().getCombatInvokeHandler().update(session.getPlayer());
session.getPlayer().getAbilityInvokeHandler().update(session.getPlayer()); session.getPlayer().getAbilityInvokeHandler().update(session.getPlayer());
// Handle attack results last // Handle attack results last
while (!session.getPlayer().getAttackResults().isEmpty()) { while (!session.getPlayer().getAttackResults().isEmpty()) {
session.getPlayer().getScene().handleAttack(session.getPlayer().getAttackResults().poll()); session.getPlayer().getScene().handleAttack(session.getPlayer().getAttackResults().poll());
......
...@@ -5,6 +5,7 @@ import emu.grasscutter.Grasscutter; ...@@ -5,6 +5,7 @@ import emu.grasscutter.Grasscutter;
import emu.grasscutter.Grasscutter.ServerDebugMode; import emu.grasscutter.Grasscutter.ServerDebugMode;
import emu.grasscutter.Grasscutter.ServerRunMode; import emu.grasscutter.Grasscutter.ServerRunMode;
import java.util.Set;
import java.io.FileReader; import java.io.FileReader;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.Arrays; import java.util.Arrays;
...@@ -21,7 +22,7 @@ public class ConfigContainer { ...@@ -21,7 +22,7 @@ public class ConfigContainer {
} }
/** /**
* Attempts to update the server's existing configuration to the latest * Attempts to update the server's existing configuration to the latest
*/ */
public static void updateConfig() { public static void updateConfig() {
try { // Check if the server is using a legacy config. try { // Check if the server is using a legacy config.
...@@ -58,7 +59,7 @@ public class ConfigContainer { ...@@ -58,7 +59,7 @@ public class ConfigContainer {
Grasscutter.getLogger().warn("Failed to inject the updated ", exception); Grasscutter.getLogger().warn("Failed to inject the updated ", exception);
} }
} }
public Structure folderStructure = new Structure(); public Structure folderStructure = new Structure();
public Database databaseInfo = new Database(); public Database databaseInfo = new Database();
public Language language = new Language(); public Language language = new Language();
...@@ -73,7 +74,7 @@ public class ConfigContainer { ...@@ -73,7 +74,7 @@ public class ConfigContainer {
public static class Database { public static class Database {
public DataStore server = new DataStore(); public DataStore server = new DataStore();
public DataStore game = new DataStore(); public DataStore game = new DataStore();
public static class DataStore { public static class DataStore {
public String connectionUri = "mongodb://localhost:27017"; public String connectionUri = "mongodb://localhost:27017";
public String collection = "grasscutter"; public String collection = "grasscutter";
...@@ -93,11 +94,13 @@ public class ConfigContainer { ...@@ -93,11 +94,13 @@ public class ConfigContainer {
public static class Server { public static class Server {
public ServerDebugMode debugLevel = ServerDebugMode.NONE; public ServerDebugMode debugLevel = ServerDebugMode.NONE;
public Set<Integer> DebugWhitelist = Set.of();
public Set<Integer> DebugBlacklist = Set.of();
public ServerRunMode runMode = ServerRunMode.HYBRID; public ServerRunMode runMode = ServerRunMode.HYBRID;
public HTTP http = new HTTP(); public HTTP http = new HTTP();
public Game game = new Game(); public Game game = new Game();
public Dispatch dispatch = new Dispatch(); public Dispatch dispatch = new Dispatch();
} }
...@@ -114,7 +117,7 @@ public class ConfigContainer { ...@@ -114,7 +117,7 @@ public class ConfigContainer {
} }
/* Server options. */ /* Server options. */
public static class HTTP { public static class HTTP {
public String bindAddress = "0.0.0.0"; public String bindAddress = "0.0.0.0";
/* This is the address used in URLs. */ /* This is the address used in URLs. */
...@@ -123,7 +126,7 @@ public class ConfigContainer { ...@@ -123,7 +126,7 @@ public class ConfigContainer {
public int bindPort = 443; public int bindPort = 443;
/* This is the port used in URLs. */ /* This is the port used in URLs. */
public int accessPort = 0; public int accessPort = 0;
public Encryption encryption = new Encryption(); public Encryption encryption = new Encryption();
public Policies policies = new Policies(); public Policies policies = new Policies();
public Files files = new Files(); public Files files = new Files();
...@@ -240,7 +243,7 @@ public class ConfigContainer { ...@@ -240,7 +243,7 @@ public class ConfigContainer {
public String nickName = "Server"; public String nickName = "Server";
public String signature = "Welcome to Grasscutter!"; public String signature = "Welcome to Grasscutter!";
} }
public static class Files { public static class Files {
public String indexFile = "./index.html"; public String indexFile = "./index.html";
public String errorFile = "./404.html"; public String errorFile = "./404.html";
...@@ -250,7 +253,7 @@ public class ConfigContainer { ...@@ -250,7 +253,7 @@ public class ConfigContainer {
public static class Region { public static class Region {
public Region() { } public Region() { }
public Region( public Region(
String name, String title, String name, String title,
String address, int port String address, int port
...@@ -260,7 +263,7 @@ public class ConfigContainer { ...@@ -260,7 +263,7 @@ public class ConfigContainer {
this.Ip = address; this.Ip = address;
this.Port = port; this.Port = port;
} }
public String Name = "os_usa"; public String Name = "os_usa";
public String Title = "Grasscutter"; public String Title = "Grasscutter";
public String Ip = "127.0.0.1"; public String Ip = "127.0.0.1";
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment