private static void OnPacketAction(object sender, SnifferEventArgs e) { SWPacket p = e.Packet; _lastSockId = e.SocketId; PacketLogConsole(p, e.Outgoing); PacketLogDisk(p, e.Outgoing); }
private static void PacketLogConsole(SWPacket p, bool outgoing) { if (p.ID == 0x0106 || p.PacketType() == PacketType.ClientCharacterUpdateSpecialOptionList) { return; //keepalive-ish stuff } if ((p.ID & 0xFF00) == 0x0500) { return; //movement } Console.WriteLine($"{(outgoing ? "[OUT]" : "[IN] ")} {p}"); }
private static void PacketLogDisk(SWPacket p, bool outgoing) { if (p.ID == 0x0106 || p.PacketType() == PacketType.ClientCharacterUpdateSpecialOptionList) { return; //keepalive-ish stuff } if ((p.ID & 0xFF00) == 0x0500) { return; //movement } if (!Directory.Exists(_dirLog)) { Directory.CreateDirectory(_dirLog); } string fileName = $"{(int)(DateTime.Now - _startTime).TotalMilliseconds:D7}ms_{(outgoing ? "Out" : "In_")}_{p.IDString()}_len{p.Data.Length}.bin"; File.WriteAllBytes(Path.Combine(_dirLog, fileName), p.Data); }
/// <summary> Reads all packets from the message and invokes the event handlers. </summary> protected override void HandlePacket(PipeMessage msg, bool outgoing) { Debug.Assert(msg.HasData); var data = msg.Data; int packetStart = 0; while (packetStart < data.Length) { //read cleartext packet header Debug.Assert(BitConverter.ToInt16(data, packetStart + 0) == 0x0002, "Unknown xor offset"); short packetLen = BitConverter.ToInt16(data, packetStart + 2); //extract packet and parse it byte[] slice = new byte[packetLen]; //TODO: C# 7 slicing Array.Copy(data, packetStart, slice, 0, packetLen); SWPacket p = SWPacket.Parse(slice); PacketAction?.Invoke(this, new SnifferEventArgs(p, outgoing, msg.Header.SocketId)); //update packet start packetStart += packetLen; } }
public static PacketType PacketType(this SWPacket p) => (PacketType)p.ID;
public static string IDString(this SWPacket p) => Enum.GetName(typeof(PacketType), (PacketType)p.ID) ?? p.ID.ToString("X4");
public SnifferEventArgs(SWPacket packet, bool outgoing, int socketId) { SocketId = socketId; Packet = packet; Outgoing = outgoing; }