-
Notifications
You must be signed in to change notification settings - Fork 2
/
NetLobby.cs
337 lines (303 loc) · 13.8 KB
/
NetLobby.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lidgren.Network;
using Lidgren.Network.Authentication;
namespace Lidgren.Network.Lobby
{
public static class NetLobby
{
/// <summary>
/// Keysize of SRP
/// </summary>
public static Int32 KeySize = 1024;
/// <summary>
/// The LogonManager handler
/// </summary>
public static ILogonManager LogonManager;
/// <summary>
/// Delegate that runs when a handshake is finished
/// </summary>
/// <param name="reason"></param>
public delegate void HandshakeFinishedEvent(String reason);
/// <summary>
/// On handshake denied (username/password failure)
/// </summary>
public static event HandshakeFinishedEvent OnDenied = delegate { };
/// <summary>
/// On handshake succeeded
/// </summary>
public static event HandshakeFinishedEvent OnSucces = delegate { };
/// <summary>
/// On handshake expired (timeout/old session)
/// </summary>
public static event HandshakeFinishedEvent OnExpired = delegate { };
/// <summary>
/// On handshake error
/// </summary>
public static event HandshakeFinishedEvent OnError = delegate { };
/// <summary>
/// Authenticates the connection
/// </summary>
/// <param name="connection">Connection to authenticate</param>
/// <param name="username">Username</param>
/// <param name="password">Password</param>
/// <param name="data">Additional data to send</param>
public static void Authenticate(NetConnection connection, String username, String password, Byte[] data = null)
{
var handshake = new Handshake(true, KeySize);
var request = handshake.GenerateSRPRequest(username, password, data ?? new Byte[0]);
var result = Create(connection, Handshake.Contents.Username, request.ByteSize);
handshake.WriteSRPRequest(result);
connection.SendMessage(result, NetDeliveryMethod.ReliableUnordered, 0);
connection.Tag = handshake;
#if DEBUG
Console.WriteLine(">> Autenticating with user:{0} and pass:{1} <<", username, password);
#endif
}
/// <summary>
/// Receives authentication
/// </summary>
/// <param name="message">Message with authentication</param>
internal static void ReceiveAuthenticate(NetIncomingMessage message)
{
try
{
var handshake = new Handshake(false, KeySize, LogonManager);
message.SenderConnection.Tag = handshake;
var response = Handshake.HandshakeFromActive(message);
var result = Create(message.SenderConnection, Handshake.Contents.Password, response.ByteSize);
handshake.WriteSRPResponse(result);
message.SenderConnection.SendMessage(result, NetDeliveryMethod.ReliableUnordered, 0);
#if DEBUG
Console.WriteLine(">> Authentication received with userdata: {0} <<", handshake.Username ?? handshake.UserData);
#endif
}
catch (Lidgren.Network.Authentication.NetSRP.HandShakeException ex)
{
ExceptionHandle(message, ex.Message);
return;
}
}
/// <summary>
/// Receives authentication response
/// </summary>
/// <param name="message"></param>
internal static void ReceiveResponse(NetIncomingMessage message)
{
try
{
var verification = Handshake.HandshakeFromPassive(message);
}
catch (Lidgren.Network.Authentication.NetSRP.HandShakeException ex)
{
ExceptionHandle(message, ex.Message);
return;
}
var result = Create(message.SenderConnection, Handshake.Contents.Verification, 21);
(message.SenderConnection.Tag as Handshake).WriteSRPVerification(result);
message.SenderConnection.SendMessage(result, NetDeliveryMethod.ReliableUnordered, 0);
}
/// <summary>
/// Receives active party verification
/// </summary>
/// <param name="message"></param>
internal static void ReceiveActiveVerification(NetIncomingMessage message)
{
try
{
var verification = Handshake.FinishHandshakeFromActive(message);
}
catch (Lidgren.Network.Authentication.NetSRP.HandShakeException ex)
{
ExceptionHandle(message, ex.Message);
return;
}
var result = Create(message.SenderConnection, Handshake.Contents.Verification, 21);
(message.SenderConnection.Tag as Handshake).WriteSRPVerification(result);
message.SenderConnection.SendMessage(result, NetDeliveryMethod.ReliableUnordered, 0);
// Finished!
(message.SenderConnection.Tag as Handshake).MarkHandshakeAsSucceeded();
OnSucces.Invoke("Authentication completed!");
}
/// <summary>
/// Recieves passive verification
/// </summary>
/// <param name="message"></param>
internal static void ReceivePassiveVerification(NetIncomingMessage message)
{
try
{
var result = Handshake.FinishHandshakeFromPassive(message);
// Finished!
}
catch (Lidgren.Network.Authentication.NetSRP.HandShakeException ex)
{
ExceptionHandle(message, ex.Message);
return;
}
(message.SenderConnection.Tag as Handshake).MarkHandshakeAsSucceeded();
OnSucces.Invoke("Authentication completed!");
}
/// <summary>
/// Receives expired
/// </summary>
internal static void ReceiveFromExpired(NetIncomingMessage message)
{
var result = Create(message.SenderConnection, Handshake.Contents.Expired);
message.SenderConnection.SendMessage(result, NetDeliveryMethod.ReliableUnordered, 0);
}
/// <summary>
/// Handles exceptions
/// </summary>
/// <param name="message"></param>
internal static void ExceptionHandle(NetIncomingMessage message, String reason)
{
Handshake.Contents contents;
switch ((message.SenderConnection.Tag as Handshake).HandshakeState)
{
case Handshake.State.Failed:
contents = Handshake.Contents.Error;
OnError.Invoke(reason);
break;
case Handshake.State.Expired:
contents = Handshake.Contents.Expired;
OnExpired.Invoke(reason);
break;
case Handshake.State.Denied:
contents = Handshake.Contents.Denied;
OnDenied.Invoke(reason);
break;
default:
contents = Handshake.Contents.Error;
OnError.Invoke(reason);
break;
}
var result = Create(message.SenderConnection, contents);
result.Write(reason);
message.SenderConnection.SendMessage(result, NetDeliveryMethod.ReliableUnordered, 0);
}
/// <summary>
/// Creates a new message
/// </summary>
/// <param name="connection"></param>
/// <param name="reason"></param>
/// <param name="size"></param>
/// <returns></returns>
private static NetOutgoingMessage Create(NetConnection connection, Handshake.Contents reason, Int32 size = 4)
{
var message = connection.Peer.CreateMessage(size);
message.Write((Byte)reason);
return message;
}
/// <summary>
/// Handles incoming messages
/// </summary>
/// <param name="message"></param>
public static Handshake.Contents IncomingMessage(NetIncomingMessage message)
{
switch (message.MessageType)
{
case NetIncomingMessageType.Data:
var reasonByte = message.ReadByte();
var reason = (Handshake.Contents)reasonByte;
#if DEBUG
Console.WriteLine(">> Got handshake {0} <<", reason);
#endif
var handshake = message.SenderConnection.Tag as Handshake;
switch (reason)
{
case Handshake.Contents.Succes:
OnSucces.Invoke("Authentication complete!");
return reason;
case Handshake.Contents.Error:
OnError.Invoke(message.ReadString());
return reason;
case Handshake.Contents.Denied:
OnDenied.Invoke(message.ReadString());
return reason;
}
if (handshake == null) // Server
{
switch (reason)
{
case Handshake.Contents.Username:
ReceiveAuthenticate(message);
break;
default:
// Can't happen!
throw new Lidgren.Network.Authentication.NetSRP.HandShakeException("Handshake not initialized when receiving " + reason.ToString() + " from client");
}
return reason;
}
switch (handshake.HandshakeState)
{
case Handshake.State.Expired:
case Handshake.State.Denied:
case Handshake.State.NotInitialized: // Server
switch (reason)
{
case Handshake.Contents.Username:
ReceiveAuthenticate(message);
break;
default:
// Can't happen!
throw new Lidgren.Network.Authentication.NetSRP.HandShakeException("Handshake not initialized when receiving " + reason.ToString() + " from client");
}
break;
case Handshake.State.Succeeded:
return Handshake.Contents.Succes;
case Handshake.State.Requesting: // Client
switch (reason)
{
case Handshake.Contents.Password:
ReceiveResponse(message);
break;
case Handshake.Contents.Expired:
Authenticate(message.SenderConnection, handshake.Username, handshake.UserData);
break;
default:
// Can't happen!
throw new Lidgren.Network.Authentication.NetSRP.HandShakeException("Expected response but received: " + reason.ToString() + " from server");
}
break;
case Handshake.State.Responding: // Server
switch (reason)
{
case Handshake.Contents.Verification:
ReceiveActiveVerification(message);
break;
case Handshake.Contents.Expired:
ReceiveFromExpired(message);
break;
default:
// Can't happen!
throw new Lidgren.Network.Authentication.NetSRP.HandShakeException("Expected verification but received: " + reason.ToString() + " from client ");
}
break;
case Handshake.State.Verificating: // Client
switch (reason)
{
case Handshake.Contents.Expired:
Authenticate(message.SenderConnection, handshake.Username, handshake.UserData);
break;
case Handshake.Contents.Verification:
ReceivePassiveVerification(message);
break;
default:
// Can't happen!
throw new Lidgren.Network.Authentication.NetSRP.HandShakeException("Expected completion but received: " + reason.ToString() + " from server ");
}
break;
default:
throw new Lidgren.Network.Authentication.NetSRP.HandShakeException("Expected nothing but received: " + reason.ToString());
}
if (handshake.HandshakeState == Handshake.State.Succeeded)
return Handshake.Contents.Succes;
return reason;
}
return Handshake.Contents.None;
}
}
}