-
Notifications
You must be signed in to change notification settings - Fork 0
/
SguildConnection.cs
271 lines (260 loc) · 10.9 KB
/
SguildConnection.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
namespace PT_Sguil
{
using System;
using System.ComponentModel;
using System.IO;
using System.Net.Security;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Windows.Forms;
internal static class SguildConnection
{
private static TcpClient _client;
private static string _dataBuffer;
private static byte[] _readBuffer = new byte[ConfigurationSupport.bufferSize];
internal static SslStream _sslStreamReader;
private static StreamWriter _sslStreamWriter;
private static string errorMsg;
internal static bool isConnected;
internal delegate void IncomingPcapFileProgressEventHandler(object sender, EventArgs e, int pcapPercentage);
internal static event IncomingPcapFileProgressEventHandler IncomingPcapFileProgress;
internal static void CloseConnection()
{
isConnected = false;
if (_sslStreamReader != null)
{
_sslStreamReader.Dispose();
}
if (_sslStreamWriter != null)
{
_sslStreamWriter.Dispose();
}
if (_client != null)
{
_client.Close();
}
}
private static void OnReceivedData(IAsyncResult ar)
{
try
{
if (_sslStreamReader.CanRead)
{
int currentBytesRead = _sslStreamReader.EndRead(ar);
if (currentBytesRead > 0)
{
ReadServerMessage(currentBytesRead);
}
}
}
catch (Exception exception)
{
MessageBox.Show(exception.ToString());
}
}
internal static string OpenConnection()
{
errorMsg = string.Empty;
_dataBuffer = string.Empty;
int result = 0;
int.TryParse(ConfigurationSupport.currentPort, out result);
try
{
_client = new TcpClient(ConfigurationSupport.currentHost, result);
string str = QuickRead(null);
if (str != ConfigurationSupport.version)
{
errorMsg = errorMsg + "Mismatched versions." + Environment.NewLine;
errorMsg = errorMsg + string.Format("SERVER: ({0})" + Environment.NewLine, str);
errorMsg = errorMsg + string.Format("CLIENT: ({0})" + Environment.NewLine, ConfigurationSupport.version);
CloseConnection();
}
if (_client.Connected)
{
StreamWriter writer = new StreamWriter(_client.GetStream());
writer.WriteLine(string.Format("VersionInfo {{{0}}}", ConfigurationSupport.version));
writer.Flush();
_sslStreamReader = new SslStream(_client.GetStream(), false, new RemoteCertificateValidationCallback(CertificateValidationCallBack));
try
{
_sslStreamReader.AuthenticateAsClient(ConfigurationSupport.currentHost, null, SslProtocols.Ssl3, false);
}
catch (AuthenticationException exception)
{
errorMsg = errorMsg + "SSL Authentication Error." + Environment.NewLine;
errorMsg = errorMsg + exception.ToString();
}
_sslStreamWriter = new StreamWriter(_sslStreamReader);
_sslStreamWriter.AutoFlush = true;
_sslStreamWriter.WriteLine(string.Format("ValidateUser {0} {1}", ConfigurationSupport.currentUsername, ConfigurationSupport.currentPassword));
string str2 = QuickRead(_sslStreamReader);
if (str2 == "UserID INVALID")
{
CloseConnection();
errorMsg = "Invalid USERNAME and/or PASSWORD";
}
else
{
isConnected = true;
ConfigurationSupport.userID = str2.Split(new char[0])[1];
}
}
}
catch (Exception ex)
{
isConnected = false;
errorMsg = string.Format("Could not connect to {0}:{1}", ConfigurationSupport.currentHost, ConfigurationSupport.currentPort);
}
if (isConnected)
{
_readBuffer = new byte[0x100];
_sslStreamReader.BeginRead(_readBuffer, 0, _readBuffer.Length, new AsyncCallback(SguildConnection.OnReceivedData), _client.GetStream());
}
return errorMsg;
}
private static string QuickRead(SslStream sslStream)
{
_readBuffer = new byte[ConfigurationSupport.bufferSize];
int offset = 0;
int count = 0;
StringBuilder builder = new StringBuilder();
if (sslStream != null)
{
do
{
count = sslStream.Read(_readBuffer, 0, _readBuffer.Length);
System.Text.Decoder decoder = Encoding.ASCII.GetDecoder();
char[] chars = new char[decoder.GetCharCount(_readBuffer, 0, count)];
decoder.GetChars(_readBuffer, 0, count, chars, 0);
builder.Append(chars);
if (builder.ToString().IndexOf("\n") != -1)
{
break;
}
}
while (count != 0);
}
else
{
do
{
int num3 = _client.GetStream().Read(_readBuffer, offset, _readBuffer.Length - offset);
offset += num3;
}
while (_client.GetStream().DataAvailable);
builder.Append(Encoding.ASCII.GetString(_readBuffer, 0, offset));
}
return builder.ToString().TrimEnd(new char[] { '\r', '\n' });
}
private static void ReadServerMessage(int currentBytesRead)
{
string msg = Encoding.ASCII.GetString(_readBuffer);
if (msg.Length > currentBytesRead)
{
msg = msg.Substring(0, currentBytesRead);
}
if (!msg.EndsWith("\n"))
{
_dataBuffer = _dataBuffer + msg;
_readBuffer = new byte[ConfigurationSupport.bufferSize];
_sslStreamReader.BeginRead(_readBuffer, 0, _readBuffer.Length, new AsyncCallback(SguildConnection.OnReceivedData), _sslStreamReader);
}
else
{
SynchronizationContext synchronizationContext;
if (_dataBuffer.Length > 0)
{
msg = _dataBuffer + msg;
_dataBuffer = string.Empty;
}
msg = msg.TrimEnd(new char[] { '\r', '\n' });
_readBuffer = new byte[ConfigurationSupport.bufferSize];
if (msg.StartsWith("WiresharkDataPcap"))
{
string strData = msg.Replace("WiresharkDataPcap ", string.Empty);
synchronizationContext = AsyncOperationManager.SynchronizationContext;
Program.MainForm.IncomingPcapFile(strData);
AsyncOperationManager.SynchronizationContext = synchronizationContext;
_readBuffer = new byte[ConfigurationSupport.bufferSize];
_sslStreamReader.BeginRead(_readBuffer, 0, _readBuffer.Length, new AsyncCallback(SguildConnection.OnReceivedData), _sslStreamReader);
}
else
{
_sslStreamReader.BeginRead(_readBuffer, 0, _readBuffer.Length, new AsyncCallback(SguildConnection.OnReceivedData), _sslStreamReader);
synchronizationContext = AsyncOperationManager.SynchronizationContext;
SguildCommands.ServerCommandRcvd(msg);
AsyncOperationManager.SynchronizationContext = synchronizationContext;
}
}
}
internal static void ReceivePcapFile(object _pcapInfo)
{
int num2;
string[] strArray = (string[]) _pcapInfo;
_readBuffer = new byte[ConfigurationSupport.bufferSize];
int num = 0;
string str = strArray[0];
int.TryParse(strArray[1], out num2);
int num3 = num2;
int num4 = 0;
int count = 0;
int pcapPercentage = 0;
FileStream stream = File.Open(string.Format(@"{0}\{1}", ConfigurationSupport.wiresharkStorageDir, str), FileMode.Create);
try
{
do
{
if (num3 > _readBuffer.Length)
{
count = _readBuffer.Length;
}
else
{
count = num3;
}
num = _sslStreamReader.Read(_readBuffer, 0, _readBuffer.Length);
stream.Write(_readBuffer, 0, count);
num4 += count;
num3 -= num;
double num7 = num4;
double num8 = num2;
double d = (num7 / num8) * 100.0;
pcapPercentage = (int) Math.Truncate(d);
IncomingPcapFileProgress(null, EventArgs.Empty, pcapPercentage);
}
while (num3 > 0);
stream.Flush();
}
catch (UnauthorizedAccessException exception)
{
MessageBox.Show(exception.ToString());
}
catch (IOException exception2)
{
MessageBox.Show(exception2.ToString());
}
finally
{
if (stream != null)
{
stream.Dispose();
}
}
}
internal static void SendToSguild(string msg)
{
if (isConnected)
{
_sslStreamWriter.WriteLine(msg);
}
}
private static bool CertificateValidationCallBack(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return ((sslPolicyErrors == SslPolicyErrors.None) || true);
}
}
}