fix: Adds packet logging (#836)

### Enabling Packet Logging
`[packetlogging on` and target the user.
Supports command modifiers such as:
`[online packetlogging on where accesslevel = player`

Logs are saved in `path/<ip address>/packets.log`. It is a simple append-format log with no date splitting.
This commit is contained in:
Kamron Batman 2021-10-31 10:22:22 -07:00 committed by GitHub
parent 85d699fcd7
commit 2890f5c3ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 186 additions and 193 deletions

View file

@ -73,6 +73,7 @@ namespace Server.Network
internal ParserState _parserState = ParserState.AwaitingNextPacket;
internal ProtocolState _protocolState = ProtocolState.AwaitingSeed;
internal GCHandle _handle;
private bool _packetLogging;
internal enum ParserState
{
@ -97,6 +98,13 @@ namespace Server.Network
Error
}
private static string _packetLoggingPath;
public static void Configure()
{
_packetLoggingPath = ServerConfiguration.GetSetting("netstate.packetLoggingPath", Path.Combine(Core.BaseDirectory, "Packets"));
}
public static void Initialize()
{
Timer.DelayCall(TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1.5), CheckAllAlive);
@ -142,6 +150,21 @@ namespace Server.Network
CreatedCallback?.Invoke(this);
}
// Only use this for debugging. This will make your server very slow!
public bool PacketLogging
{
get => _packetLogging;
set
{
_packetLogging = value;
if (_packetLogging)
{
StartPacketLog();
}
}
}
public DateTime ConnectedOn { get; }
public TimeSpan ConnectedFor => Core.Now - ConnectedOn;
@ -486,6 +509,11 @@ namespace Server.Network
buffer.CopyFrom(span);
}
if (PacketLogging)
{
LogPacket(span, ReadOnlySpan<byte>.Empty, span.Length, false);
}
SendPipe.Writer.Advance((uint)length);
if (!_flushQueued)
@ -506,6 +534,48 @@ namespace Server.Network
}
}
private void StartPacketLog()
{
try
{
var logDir = Path.Combine(_packetLoggingPath, _toString);
PathUtility.EnsureDirectory(logDir);
var logPath = Path.Combine(logDir, "packets.log");
using var op = new StreamWriter(logPath, true);
op.WriteLine(">>>>>>>>>> Logging started {0:yyyy/MM/dd HH:mm::ss} <<<<<<<<<<", Core.Now);
op.WriteLine();
op.WriteLine();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
private void LogPacket(ReadOnlySpan<byte> first, ReadOnlySpan<byte> second, int totalLength, bool incoming)
{
try
{
var logDir = Path.Combine(_packetLoggingPath, _toString);
PathUtility.EnsureDirectory(logDir);
var logPath = Path.Combine(logDir, "packets.log");
const string incomingStr = "Client -> Server";
const string outgoingStr = "Server -> Client";
using var sw = new StreamWriter(logPath, true);
sw.WriteLine($"{Core.Now:HH:mm:ss.ffff}: {(incoming ? incomingStr : outgoingStr)} 0x{first[0]:X2} (Length: {totalLength})");
sw.FormatBuffer(first, second, totalLength);
sw.WriteLine();
sw.WriteLine();
}
catch
{
// ignored
}
}
public void HandleReceive()
{
if (!_running)
@ -760,6 +830,11 @@ namespace Server.Network
UpdatePacketCount(packetId);
if (PacketLogging)
{
LogPacket(packetReader.First, packetReader.Second, packetLength, true);
}
handler.OnReceive(this, packetReader, ref packetLength);
prof?.Finish(packetLength);