Skip to content

Commit

Permalink
Fix socket, add converters base.
Browse files Browse the repository at this point in the history
  • Loading branch information
AtefR committed Oct 10, 2020
1 parent 890b6bb commit da8d6f6
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 50 deletions.
28 changes: 28 additions & 0 deletions Sniffer/Models/ScrollingListView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;

namespace Sniffer.Models
{
public class ScrollingListView : ListView
{
public static readonly DependencyProperty FilterPredicateProperty = DependencyProperty.Register("AutoScroll",
typeof(bool), typeof(ListView), new PropertyMetadata(null));
public bool AutoScroll
{
get { return (bool)GetValue(FilterPredicateProperty); }
set { SetValue(FilterPredicateProperty, value); }
}

protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
{
if (!AutoScroll || e.NewItems == null) return;
var newItemCount = e.NewItems.Count;

if (newItemCount > 0)
ScrollIntoView(e.NewItems[newItemCount - 1]);

base.OnItemsChanged(e);
}
}
}
109 changes: 69 additions & 40 deletions Sniffer/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,24 @@
using OXGaming.TibiaAPI.Constants;
using OXGaming.TibiaAPI.Utilities;
using Sniffer.Models;
using Sniffer.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;

namespace Sniffer.ViewModels
{
public class MainViewModel : PropertyChangedBase
{
[DllImport("Kernel32")]
public static extern void AllocConsole();

[DllImport("Kernel32")]
public static extern void FreeConsole();

private MainWindow _window;
#region TibiaApi
private Client _client;

#endregion

private ObservableCollection<Packet> _packets;
public ObservableCollection<Packet> Packets
{
Expand Down Expand Up @@ -75,42 +70,54 @@ public string FilterOpCode
get { return _filterOpCode.ToString(); }
set
{
int opCode;
if(int.TryParse(value, out opCode))
if (int.TryParse(value, out int opCode))
{
_filterOpCode = opCode;
OnPropertyChanged();
}
}
}

public MainViewModel()
private bool _autoScroll;
public bool AutoScroll
{
if (!DesignerProperties.GetIsInDesignMode(new DependencyObject()))
get { return _autoScroll; }
set
{
try
{

AllocConsole();

using (_client = new Client(string.Empty))
{
_client.Logger.Level = Logger.LogLevel.Error;
_client.Logger.Output = Logger.LogOutput.Console;

_client.Connection.OnReceivedClientMessage += Proxy_OnReceivedClientMessage;
_client.Connection.OnReceivedServerMessage += Proxy_OnReceivedServerMessage;
_autoScroll = value;
OnPropertyChanged();
}
}

_client.Connection.IsClientPacketParsingEnabled = false;
_client.Connection.IsServerPacketParsingEnabled = false;
_client.StartConnection(httpPort: 7171, loginWebService: string.Empty);

}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
public MainViewModel()
{
Packets = new ObservableCollection<Packet>();
FilteredPackets = new ObservableCollection<Packet>();

AddToPackets(PacketType.Client, new byte[] { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 });
AddToPackets(PacketType.Client, new byte[] { 0x01, 0x02, 0x03, 0x04 });
AddToPackets(PacketType.Client, new byte[] { 0x01, 0x02, 0x03, 0x04 });
AddToPackets(PacketType.Server, new byte[] { 0x01, 0x02, 0x03, 0x04 });
AddToPackets(PacketType.Client, new byte[] { 0x01, 0x02, 0x03, 0x04 });
AddToPackets(PacketType.Server, new byte[] { 0x01, 0x02, 0x03, 0x04 });
AddToPackets(PacketType.Client, new byte[] { 0x01, 0x02, 0x03, 0x04 });
AddToPackets(PacketType.Server, new byte[] { 0x01, 0x02, 0x03, 0x04 });

//if (!DesignerProperties.GetIsInDesignMode(new DependencyObject()))
//{
// _client = new Client(string.Empty);

// _client.Logger.Level = Logger.LogLevel.Error;
// _client.Logger.Output = Logger.LogOutput.Console;

// _client.Connection.OnReceivedClientMessage += Proxy_OnReceivedClientMessage;
// _client.Connection.OnReceivedServerMessage += Proxy_OnReceivedServerMessage;

// _client.Connection.IsClientPacketParsingEnabled = false;
// _client.Connection.IsServerPacketParsingEnabled = false;
// _client.StartConnection(httpPort: 7171, loginWebService: string.Empty);
//}
}

private void Proxy_OnReceivedClientMessage(byte[] data)
Expand All @@ -126,18 +133,27 @@ private void Proxy_OnReceivedServerMessage(byte[] data)
public void AddToPackets(PacketType type, byte[] data)
{
var packet = new Packet(type, data);
Packets.Add(packet);
Application.Current.Dispatcher.Invoke(delegate
{
Packets.Add(packet);
});

var filterType = (PacketType)(SelectedPacketTypeIndex - 1);
if (filterType == type)
if (filterType == type || SelectedPacketTypeIndex == 0)
if (_filterOpCode > 0)
{
if (_filterOpCode == packet.OpCode)
FilteredPackets.Add(packet);
Application.Current.Dispatcher.Invoke(delegate
{
FilteredPackets.Add(packet);
});
}
else
{
FilteredPackets.Add(packet);
Application.Current.Dispatcher.Invoke(delegate
{
FilteredPackets.Add(packet);
});
}
}

Expand All @@ -149,8 +165,6 @@ private void Filter()

//Filter Type
IEnumerable<Packet> filteredList = null;
if (FilteredPackets == null)
FilteredPackets = new ObservableCollection<Packet>();
FilteredPackets.Clear();
if (SelectedPacketTypeIndex == 1)
{
Expand All @@ -173,5 +187,20 @@ private void Filter()

FilteredPackets = new ObservableCollection<Packet>(filteredList);
}

public ICommand ClearCommand => new Command(_ => Clear());
private void Clear()
{
Packets.Clear();
FilteredPackets.Clear();
}
public ICommand RemoveCommand => new Command(_ => Remove());
private void Remove()
{
if(Packets.Contains(SelectedPacket))
Packets.Remove(SelectedPacket);
if (FilteredPackets.Contains(SelectedPacket))
FilteredPackets.Remove(SelectedPacket);
}
}
}
67 changes: 57 additions & 10 deletions Sniffer/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
xmlns:local="clr-namespace:Sniffer"
xmlns:ViewModels="clr-namespace:Sniffer.ViewModels"
xmlns:hv="clr-namespace:HexView.Wpf;assembly=HexView.Wpf"
xmlns:models="clr-namespace:Sniffer.Models"
x:Class="Sniffer.Views.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="700" Width="600">
Title="Sniffer" Height="700" Width="600">
<Window.DataContext>
<ViewModels:MainViewModel/>
</Window.DataContext>
Expand All @@ -17,31 +18,77 @@
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
<RowDefinition Height="3" />
<RowDefinition Height="200"/>
<RowDefinition Height="200" MinHeight="170" />
</Grid.RowDefinitions>
<StackPanel Orientation = "Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<StackPanel Orientation="Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Label Content="Filter:" Margin="1" />
<ComboBox Width="70" Margin="5" SelectedIndex="{Binding SelectedPacketTypeIndex}">
<ComboBox Width="70" Margin="4" SelectedIndex="{Binding SelectedPacketTypeIndex}">
<ComboBoxItem Content="Both"/>
<ComboBoxItem Content="Client"/>
<ComboBoxItem Content="Server"/>
</ComboBox>
<TextBox Width="100" Text="{Binding FilterOpCode}" Margin="5" TextAlignment="Center" />
<Button Width="70" Margin="5" Content="Search" Command="{Binding FilterCommand}"/>
<CheckBox Content="AutoScroll" Margin="7" IsChecked="{Binding AutoScroll}" />
</StackPanel>
<ListView Grid.Row="1" Name="PacketsList" ItemsSource="{Binding FilteredPackets}" SelectedItem="{Binding SelectedPacket}" SelectionMode="Single">
<models:ScrollingListView Grid.Row="1" AutoScroll="True" x:Name="PacketsList" ItemsSource="{Binding FilteredPackets}" SelectedItem="{Binding SelectedPacket}" SelectionMode="Single">
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Header="_Remove" Command="{Binding RemoveCommand}"/>
<MenuItem Header="_Clear" Command="{Binding ClearCommand}"/>
</ContextMenu>
</ListView.ContextMenu>
<ListView.View>
<GridView>
<GridViewColumn Header="Type" Width="80" DisplayMemberBinding="{Binding Type}" />
<GridViewColumn Header="OpCode" Width="80" DisplayMemberBinding="{Binding OpCode}" />
<GridViewColumn Header="Data" Width="400" DisplayMemberBinding="{Binding}" />
</GridView>
</ListView.View>
</ListView>
</models:ScrollingListView>
<GridSplitter ResizeDirection="Rows" HorizontalAlignment="Stretch" Grid.Row="2"/>
<hv:HexViewer Grid.Row="3" x:Name="hexViewer"
DataSource="{Binding ElementName=PacketsList,
Path=SelectedItem.Binary, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Columns="{Binding RelativeSource={RelativeSource Self}, Path=MaxVisibleColumns}"/>
<Grid Grid.Row="3">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border BorderThickness="0 0 1 0" BorderBrush="Black" Margin="0 5 0 14">
<Grid MaxHeight="170" VerticalAlignment="Top" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1.5*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Label Content="Short: " />
<Label Foreground="#FF0066CC" Grid.Column="2" Content="{Binding ElementName=hexViewer, Path=SelectionStart, StringFormat={}{0:X8}}"/>

<Label Content="Int: " Grid.Row="1"/>
<Label Content="0" Foreground="#FF0066CC" Grid.Row="1" Grid.Column="2"/>

<Label Content="Long: " Grid.Row="2"/>
<Label Content="" Foreground="#FF0066CC" Grid.Row="2" Grid.Column="2"/>

<Label Content="UShort: " Grid.Row="3"/>
<Label Content="0" Foreground="#FF0066CC" Grid.Row="3" Grid.Column="2"/>

<Label Content="UInt: " Grid.Row="4"/>
<Label Content="0" Foreground="#FF0066CC" Grid.Row="4" Grid.Column="2"/>

<Label Content="ULong: " Grid.Row="5"/>
<Label Content="0" Foreground="#FF0066CC" Grid.Row="5" Grid.Column="2"/>
</Grid>
</Border>
<hv:HexViewer Grid.Column="1" x:Name="hexViewer"
DataSource="{Binding ElementName=PacketsList,
Path=SelectedItem.Binary, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Columns="{Binding RelativeSource={RelativeSource Self}, Path=MaxVisibleColumns}" ShowAddress="False"/>
</Grid>
</Grid>
</Window>

0 comments on commit da8d6f6

Please sign in to comment.