-
Notifications
You must be signed in to change notification settings - Fork 1
/
MessageViewer.Loading.cs
254 lines (213 loc) · 9.68 KB
/
MessageViewer.Loading.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#pragma warning disable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using Mooege.Net.GS.Message.Definitions.Player;
namespace GameMessageViewer
{
public partial class MessageViewer
{
private byte[] String_To_Bytes(string strInput)
{
// i variable used to hold position in string
int i = 0;
// x variable used to hold byte array element position
int x = 0;
// allocate byte array based on half of string length
byte[] bytes = new byte[(strInput.Length) / 2];
// loop through the string - 2 bytes at a time converting
// it to decimal equivalent and store in byte array
while (strInput.Length > i + 1)
{
long lngDecimal = Convert.ToInt32(strInput.Substring(i, 2), 16);
bytes[x] = Convert.ToByte(lngDecimal);
i = i + 2;
++x;
}
// return the finished byte array of decimal values
return bytes;
}
private void LoadWiresharkHex(string text)
{
if (text.Contains(" "))
{
String[] rows = text.Split('\n');
String currentBuffer = "";
text = "";
for (int i = 0; i < rows.Length; i++)
{
if (i > 0 && (rows[i].StartsWith(" ") ^ rows[i - 1].StartsWith(" ")))
{
Buffer buffer = new Buffer(String_To_Bytes(currentBuffer));
BufferNode newNode = new BufferNode(buffer, actors, questTree, "1");
newNode.Start = text.Length;
newNode.BackColor = rows[i].StartsWith(" ") ? newNode.BackColor = Color.LightCoral : Color.LightBlue;
tree.Nodes.Add(newNode);
text += currentBuffer;
currentBuffer = "";
}
currentBuffer += (rows[i].StartsWith(" ") ? rows[i].Substring(14, 3 * 16) : rows[i].Substring(10, 3 * 16)).Trim().Replace(" ", "");
}
}
else
{
Buffer buffer = new Buffer(String_To_Bytes(text));
BufferNode newNode = new BufferNode(buffer, actors, questTree, "1");
newNode.Parse();
tree.Nodes.Add(newNode);
}
input.Text = text;
ApplyFilter();
}
/// <summary>
/// Pcap loading extracts the largest session from a pcap, transforms it to dump format (in a memory stream)
/// which can be read by LoadDump afterwards
/// </summary>
/// <param name="path"></param>
private void LoadPcap(string path)
{
try
{
// This ugly thing returns a list of MemoryStreams. One for each session in the cap
var streams = pCapReader.ReconSingleFileSharpPcap(path);
// Take the largest session (yeah if its no working for you, this may cause it)
var ordered = streams.OrderBy(stream => stream.Length);
ordered.Last().Seek(0, SeekOrigin.Begin);
LoadDump(new StreamReader(ordered.Last()).ReadToEnd());
}
catch (SharpPcap.PcapException) {
MessageBox.Show("The file could not be read. Only lipcap cap files can be loaded. If you want to load a NetMon cap the README tells you how to", "Loading error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void LoadPreparsed(string text)
{
String[] rows = text.Split('\n');
String currentBuffer = "";
text = "";
string currentDirection = "";
progressBar.Maximum = rows.Length;
progressBar.Value = 0;
progressBar.Visible = true;
for (int i = 0; i < rows.Length; i++)
{
progressBar.Value = i;
Application.DoEvents();
if (this.Visible == false)
break;
}
}
private void LoadDump(string text)
{
String[] rows = text.Split('\n');
String currentBuffer = "";
text = "";
string currentDirection = "";
progressBar.Maximum = rows.Length;
progressBar.Value = 0;
progressBar.Visible = true;
allNodes = new List<BufferNode>();
Color[][] trafficColors = new Color[][]
{ new Color[] { Color.LightCoral , Color.LightBlue },
new Color[] { Color.Tomato, Color.Blue },
new Color[] { Color.Red, Color.BlueViolet },
new Color[] { Color.PaleVioletRed, Color.CadetBlue }
};
List<string> clients = new List<string>();
Dictionary<string, Color[]> colors = new Dictionary<string,Color[]>();
// to read mooege dumps, some leading info must be removed
// the amount of chars is fixed so its calculated once
int removeChars = rows[0].IndexOf("Inc:");
if(removeChars < 0)
removeChars = rows[0].IndexOf("Out:");
string clientHash = "";
for (int i = 0; i < rows.Length; i++)
{
if (rows[i].Length > removeChars)
{
// Skip anything til the Inc/Out part (for mooege dumps), note client hash
rows[i] = rows[i].Substring(removeChars);
clientHash = rows[i].Substring(4, 8);
if (clients.Contains(clientHash) == false)
{
clients.Add(clientHash);
colors[clientHash] = trafficColors[clients.Count - 1];
}
progressBar.Value = i;
//causes bugs
//Application.DoEvents();
//if (this.Visible == false)
// break;
if (rows[i].Length > 3)
{
// Everytime the direction of data changes, the buffer is parsed and emptied
// this is for pcap dumps where the data stream is sent in smaller packets
// in mooege, data is dumped in whole
if (i > 0 && rows[i].Substring(0, 1) != currentDirection)
{
Buffer buffer = new Buffer(String_To_Bytes(currentBuffer));
BufferNode newNode = new BufferNode(buffer, actors, questTree, clientHash);
newNode.Start = text.Length;
newNode.BackColor = currentDirection == "I" ? colors[clientHash][0] : colors[clientHash][1];
allNodes.Add(newNode);
//tree.Nodes.Add(newNode);
newNode.ApplyFilter(filterWindow.Filter);
text += currentBuffer;
currentBuffer = "";
currentDirection = rows[i].Substring(0, 1);
}
if (currentDirection == "") currentDirection = rows[i].Substring(0, 1);
currentBuffer += (rows[i].Substring(13)).Replace("\r", "");
}
}
}
if (currentBuffer.Length > 10)
{
Buffer buffer = new Buffer(String_To_Bytes(currentBuffer));
BufferNode newNode = new BufferNode(buffer, actors, questTree, clientHash);
newNode.Start = text.Length;
newNode.BackColor = currentDirection == "I" ? colors[clientHash][0] : colors[clientHash][1];
allNodes.Add(newNode);
newNode.ApplyFilter(filterWindow.Filter);
text += currentBuffer;
}
// Create a filter menu entry for every client in the stream.
filterPlayersToolStripMenuItem.DropDownItems.Clear();
foreach (string client in clients)
{
ToolStripMenuItem m = new ToolStripMenuItem(client);
m.Tag = client;
m.Checked = true;
m.Click += new EventHandler(FilterPlayerClick);
// find toon name for client hash
foreach(BufferNode bn in allNodes)
if(bn.clientHash.Equals(m.Tag.ToString()))
foreach(MessageNode mn in bn.Nodes)
if(mn.gameMessage is NewPlayerMessage)
{
m.Text = (mn.gameMessage as NewPlayerMessage).ToonName;
goto hell;
}
hell:
filterPlayersToolStripMenuItem.DropDownItems.Add(m);
}
tree.Nodes.AddRange(allNodes.ToArray());
input.Text = text;
progressBar.Visible = false;
}
void FilterPlayerClick(object sender, EventArgs e)
{
Dictionary<string, bool> filter = new Dictionary<string, bool>();
((ToolStripMenuItem)sender).Checked = !((ToolStripMenuItem)sender).Checked;
foreach (ToolStripMenuItem m in filterPlayersToolStripMenuItem.DropDownItems)
filter.Add(m.Tag.ToString(), m.Checked);
tree.Nodes.Clear();
foreach (BufferNode bn in allNodes)
if (filter[bn.clientHash])
tree.Nodes.Add(bn);
}
}
}