Skip to content

Commit

Permalink
Adaptions/improvements for the network analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
sven-n committed Oct 6, 2024
1 parent f704508 commit 5fe2564
Show file tree
Hide file tree
Showing 7 changed files with 747 additions and 408 deletions.
709 changes: 343 additions & 366 deletions src/Network/Analyzer/MainForm.Designer.cs

Large diffs are not rendered by default.

65 changes: 51 additions & 14 deletions src/Network/Analyzer/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public partial class MainForm : Form

private readonly Dictionary<ClientVersion, string> _clientVersions = new ()
{
{ new ClientVersion(106, 3, ClientLanguage.English), "Extended Open Source (2.04d)" },
{ new ClientVersion(106, 3, ClientLanguage.English), "Extended S6E3 (2.04d)" },
{ new ClientVersion(6, 3, ClientLanguage.English), "S6E3 (1.04d)" },
{ new ClientVersion(1, 0, ClientLanguage.Invariant), "Season 1 - 6" },
{ new ClientVersion(0, 97, ClientLanguage.Invariant), "0.97" },
Expand Down Expand Up @@ -59,15 +59,7 @@ public MainForm()
this._analyzer = new PacketAnalyzer();
this.Disposed += (_, _) => this._analyzer.Dispose();

this.clientVersionComboBox.SelectedIndexChanged += (_, _) =>
{
if (this._clientListener is { } listener)
{
listener.ClientVersion = this.SelectedClientVersion;
}
this._analyzer.ClientVersion = this.SelectedClientVersion;
};
this.clientVersionComboBox.SelectedIndexChanged += OnSelectedClientVersionChanged;
this.clientVersionComboBox.DataSource = new BindingSource(this._clientVersions, null);
this.clientVersionComboBox.DisplayMember = "Value";
this.clientVersionComboBox.ValueMember = "Key";
Expand Down Expand Up @@ -206,6 +198,10 @@ private void SetPacketDataSource()
if (this._unfilteredList is { } oldList)
{
oldList.ListChanged -= this.OnUnfilteredListChanged;
foreach (var packet in oldList)
{
packet.AnalyzingRequested -= this.OnPacketAnalyzingRequested;
}
}

this._unfilteredList = this.SelectedConnection?.PacketList;
Expand All @@ -215,21 +211,36 @@ private void SetPacketDataSource()
return;
}

foreach (var packet in this._unfilteredList)
{
packet.AnalyzingRequested += this.OnPacketAnalyzingRequested;
if (packet.AnalyzedByVersion != this.SelectedClientVersion)
{
packet.ClearMessage();
}
}

this._unfilteredList.ListChanged += this.OnUnfilteredListChanged;

if (this._filterExpression is null)
{
this.packetBindingSource.DataSource = this.SelectedConnection?.PacketList;
}
else
{
this.packetBindingSource.DataSource = this._unfilteredList.AsQueryable().Where(this._filterExpression).ToList();
this._unfilteredList.ListChanged += this.OnUnfilteredListChanged;
}
}

private void OnPacketAnalyzingRequested(object? sender, Packet.AnalyzingRequestedEventArgs e)
{
e.ClientVersion = this._analyzer.ClientVersion;
(e.Message, e.Definition) = this._analyzer.ExtractShortInformation(e.Packet);
}

private void OnUnfilteredListChanged(object? sender, ListChangedEventArgs e)
{
if (e.ListChangedType != ListChangedType.ItemAdded
|| this._filterMethod is not { } filter
|| this._unfilteredList is not { } sourceList
|| this._unfilteredList?.Count < e.NewIndex
|| e.NewIndex < 0)
Expand All @@ -238,13 +249,31 @@ private void OnUnfilteredListChanged(object? sender, ListChangedEventArgs e)
}

var newPacket = sourceList[e.NewIndex];

if (filter.DynamicInvoke(newPacket) is true)
newPacket.AnalyzingRequested += this.OnPacketAnalyzingRequested;

if (this._filterMethod is { } filter
&& filter.DynamicInvoke(newPacket) is true)
{
this.packetBindingSource.Add(newPacket);
}
}

private void OnSelectedClientVersionChanged(object? o, EventArgs eventArgs)
{
if (this._clientListener is { } listener)
{
listener.ClientVersion = this.SelectedClientVersion;
}

this._analyzer.ClientVersion = this.SelectedClientVersion;
if (this._unfilteredList is { } unfilteredList)
{
foreach (var packet in unfilteredList)
{
packet.ClearMessage();
}
}
}
private void OnPacketFilterStringChanged(object? sender, AdvancedDataGridView.FilterEventArgs e)
{
try
Expand All @@ -265,6 +294,10 @@ private void StartProxy(object sender, System.EventArgs e)
this._clientListener.Stop();
this._clientListener = null;
this.btnStartProxy.Text = "Start Proxy";
this.listenerPortNumericUpDown.Enabled = true;
this.targetPortNumericUpDown.Enabled = true;
this.targetHostTextBox.Enabled = true;
this.clientVersionComboBox.Enabled = true;
return;
}

Expand All @@ -283,6 +316,10 @@ private void StartProxy(object sender, System.EventArgs e)
this._clientListener.ClientConnected += this.ClientListenerOnClientConnected;
this._clientListener.Start();
this.btnStartProxy.Text = "Stop Proxy";
this.listenerPortNumericUpDown.Enabled = false;
this.targetPortNumericUpDown.Enabled = false;
this.targetHostTextBox.Enabled = false;
this.clientVersionComboBox.Enabled = false;
}

private void ClientListenerOnClientConnected(object? sender, ClientConnectedEventArgs e)
Expand Down
65 changes: 64 additions & 1 deletion src/Network/Analyzer/MainForm.resx
Original file line number Diff line number Diff line change
@@ -1,4 +1,64 @@
<root>
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
Expand Down Expand Up @@ -72,6 +132,9 @@
<metadata name="Direction.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="Message.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="PacketData.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
Expand Down
128 changes: 126 additions & 2 deletions src/Network/Analyzer/Packet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@

namespace MUnique.OpenMU.Network.Analyzer;

using System.ComponentModel;
using System.Runtime.CompilerServices;
using MUnique.OpenMU.Network.Packets;
using MUnique.OpenMU.Network.PlugIns;

/// <summary>
/// A captured data packet.
/// </summary>
public class Packet
public sealed class Packet : INotifyPropertyChanged
{
private readonly byte[] _innerData;

private string? _dataAsString;
private ushort? _displayCode;
private string? _message;
private ClientVersion _analyzedByVersion;
private PacketDefinition? _definition;

/// <summary>
/// Initializes a new instance of the <see cref="Packet"/> class.
/// Initializes a new instance of the <see cref="Packet" /> class.
/// </summary>
/// <param name="timestamp">The timestamp.</param>
/// <param name="data">The data.</param>
Expand All @@ -27,6 +36,14 @@ public Packet(TimeSpan timestamp, byte[] data, bool toServer)
this.Direction = this.ToServer ? "C->S" : "S->C";
}

/// <summary>
/// Occurs when analyzing the packet requested, e.g. when the description is needed.
/// </summary>
public event EventHandler<AnalyzingRequestedEventArgs>? AnalyzingRequested;

/// <inheritdoc />
public event PropertyChangedEventHandler? PropertyChanged;

/// <summary>
/// Gets the timestamp.
/// </summary>
Expand All @@ -52,11 +69,61 @@ public Packet(TimeSpan timestamp, byte[] data, bool toServer)
/// </summary>
public byte? SubCode => this._innerData.AsSpan().GetPacketSubType();

/// <summary>
/// Gets the display code of the packet, which specifies the kind of the message.
/// </summary>
public ushort DisplayCode
{
get
{
if (this._displayCode is not null)
{
return this._displayCode.Value;
}

if (this._definition?.SubCodeSpecified is not true)
{
return this.Code;
}

ushort result = this.Code;
if (this.SubCode is { } subCode)
{
result <<= 8;
result |= subCode;
}

this._displayCode = result;
return result;
}
}

/// <summary>
/// Gets the direction as string.
/// </summary>
public string Direction { get; }

/// <summary>
/// Gets the packet as text in a one-line string.
/// </summary>
public string Message
{
get
{
if (this._message is null && this.AnalyzingRequested is not null)
{
var request = new AnalyzingRequestedEventArgs(this);
this.AnalyzingRequested?.Invoke(this, request);
this._message = request.Message;
this._analyzedByVersion = request.ClientVersion;
this._definition = request.Definition;
this.RaisePropertyChanged(nameof(DisplayCode));
}

return this._message ?? string.Empty;
}
}

/// <summary>
/// Gets a value indicating whether the packet was sent to the server; Otherwise it was sent to the client.
/// </summary>
Expand All @@ -72,9 +139,66 @@ public Packet(TimeSpan timestamp, byte[] data, bool toServer)
/// </summary>
public string PacketData => this._dataAsString ??= this._innerData.AsString();

/// <summary>
/// Gets the protocol version which was applied when analyzing.
/// </summary>
public ClientVersion AnalyzedByVersion => _analyzedByVersion;

/// <inheritdoc />
public override string ToString()
{
return $"{this.Direction}: {this.PacketData}";
}

/// <summary>
/// Clears the message which was generated by the packet analyzer based on a selected version.
/// </summary>
public void ClearMessage()
{
this._analyzedByVersion = default;
this._definition = null;
this._displayCode = null;
this._message = null;
this.RaisePropertyChanged(nameof(Message));
}

private void RaisePropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

/// <summary>
/// Event args for <see cref="Packet.AnalyzingRequested"/>.
/// </summary>
public class AnalyzingRequestedEventArgs : EventArgs
{
/// <summary>
/// Initializes a new instance of the <see cref="AnalyzingRequestedEventArgs"/> class.
/// </summary>
/// <param name="data">The data.</param>
public AnalyzingRequestedEventArgs(Packet data)
{
Packet = data;
}

/// <summary>
/// Gets the packet which should be analyzed.
/// </summary>
public Packet Packet { get; }

/// <summary>
/// Gets or sets the definition which was found during the analyzing.
/// </summary>
public PacketDefinition? Definition { get; set; }

/// <summary>
/// Gets or sets the client version which was set during analyzing.
/// </summary>
public ClientVersion ClientVersion { get; set; }

/// <summary>
/// Gets or sets the message which describes the packet in a one-line string.
/// </summary>
public string Message { get; set; } = string.Empty;
}
}
Loading

0 comments on commit 5fe2564

Please sign in to comment.