-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCONClient.Packets.cs
215 lines (186 loc) · 7.35 KB
/
SCONClient.Packets.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
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Aragas.Network;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;
using PokeD.Core.Data.SCON;
using PokeD.Core.Packets.SCON.Authorization;
using PokeD.Core.Packets.SCON.Chat;
using PokeD.Core.Packets.SCON.Logs;
using PokeD.Core.Packets.SCON.Status;
using PokeD.SCON.Exceptions;
using PokeD.SCON.UILibrary.GridModels;
namespace PokeD.SCON
{
public partial class SCONClient
{
AuthorizationStatus AuthorizationStatus { get; set; }
bool Authorized { get; set; }
private void HandleAuthorizationResponse(AuthorizationResponsePacket packet)
{
if (Authorized)
return;
AuthorizationStatus = packet.AuthorizationStatus;
if(!AuthorizationStatus.HasFlag(AuthorizationStatus.EncryprionEnabled))
SendPacket(new AuthorizationPasswordPacket { PasswordHash = Password.Hash });
}
private void HandleEncryptionRequest(EncryptionRequestPacket packet)
{
if(Authorized)
return;
if (AuthorizationStatus.HasFlag(AuthorizationStatus.EncryprionEnabled))
{
var generator = new CipherKeyGenerator();
generator.Init(new KeyGenerationParameters(new SecureRandom(), 16 * 8));
var sharedKey = generator.GenerateKey();
var pkcs = new PKCS1Signer(packet.PublicKey);
var signedSecret = pkcs.SignData(sharedKey);
var signedVerify = pkcs.SignData(packet.VerificationToken);
SendPacket(new EncryptionResponsePacket { SharedSecret = signedSecret, VerificationToken = signedVerify });
Stream.InitializeEncryption(sharedKey);
SendPacket(new AuthorizationPasswordPacket { PasswordHash = Password.Hash });
}
else
throw new SCONException("Encryption was not enabled!");
}
private void HandleAuthorizationComplete(AuthorizationCompletePacket packet)
{
Authorized = true;
}
private void HandleAuthorizationDisconnect(AuthorizationDisconnectPacket packet)
{
Authorized = false;
}
private void HandlePlayerInfoListResponse(PlayerInfoListResponsePacket packet)
{
if (Authorized)
{
BasicUIVM.PlayersGridDataList.Clear();
for (var i = 0; i < packet.PlayerInfos.Length; i++)
{
BasicUIVM.PlayersGridDataList.Add(new PlayersDataGridModel
{
Number = i,
Name = packet.PlayerInfos[i].Name,
IP = packet.PlayerInfos[i].IP,
Ping = packet.PlayerInfos[i].Ping,
Position = packet.PlayerInfos[i].Position,
LevelFile = packet.PlayerInfos[i].LevelFile,
PlayTime = packet.PlayerInfos[i].PlayTime
});
}
}
else
DisplayMessage("You are not authorized.");
}
private void HandleChatMessage(ChatMessagePacket packet)
{
if (Authorized)
{
var builder = new StringBuilder();
foreach (var line in BasicUIVM.ConsoleOutput.Split(new [] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
builder.AppendLine(line);
builder.AppendLine($"{packet.Player}: {packet.Message}");
var reversed = builder.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Reverse();
builder.Clear();
foreach (var line in reversed)
builder.AppendLine(line);
BasicUIVM.ConsoleOutput = builder.ToString();
}
else
DisplayMessage("You are not authorized.");
}
private void HandleLogListResponse(LogListResponsePacket packet)
{
if (Authorized)
{
BasicUIVM.LogsGridDataList.Clear();
for (var i = 0; i < packet.Logs.Length; i++)
{
BasicUIVM.LogsGridDataList.Add(new LogsDataGridModel
{
Number = i,
LogFilename = packet.Logs[i].LogFileName
});
}
}
else
DisplayMessage("You are not authorized.");
}
private void HandleLogFileResponse(LogFileResponsePacket packet)
{
if (Authorized)
BasicUIVM.DisplayLog(packet.LogFilename, packet.LogFile);
else
DisplayMessage("You are not authorized.");
}
private void HandleCrashLogListResponse(CrashLogListResponsePacket packet)
{
if (Authorized)
{
BasicUIVM.CrashLogsGridDataList.Clear();
for (var i = 0; i < packet.CrashLogs.Length; i++)
{
BasicUIVM.CrashLogsGridDataList.Add(new LogsDataGridModel
{
Number = i,
LogFilename = packet.CrashLogs[i].LogFileName
});
}
}
else
DisplayMessage("You are not authorized.");
}
private void HandleCrashLogFileResponse(CrashLogFileResponsePacket packet)
{
if (Authorized)
{
BasicUIVM.DisplayLog(packet.CrashLogFilename, packet.CrashLogFile);
}
else
DisplayMessage("You are not authorized.");
}
private void HandlePlayerDatabaseListResponse(PlayerDatabaseListResponsePacket packet)
{
if (Authorized)
{
BasicUIVM.PlayersDatabaseGridDataList.Clear();
for (var i = 0; i < packet.PlayerDatabases.Length; i++)
{
BasicUIVM.PlayersDatabaseGridDataList.Add(new PlayersDatabaseDataGridModel
{
Number = i,
Name = packet.PlayerDatabases[i].Name,
LastIP = packet.PlayerDatabases[i].LastIP,
LastSeen = packet.PlayerDatabases[i].LastSeen,
});
}
}
else
DisplayMessage("You are not authorized.");
}
private void HandleBanListResponse(BanListResponsePacket packet)
{
if (Authorized)
{
BasicUIVM.BansGridDataList.Clear();
for (var i = 0; i < packet.Bans.Length; i++)
{
BasicUIVM.BansGridDataList.Add(new BansDataGridModel
{
Number = i,
Name = packet.Bans[i].Name,
IP = packet.Bans[i].IP,
BanTime = packet.Bans[i].BanTime,
UnBanTime = packet.Bans[i].UnBanTime,
Reason = packet.Bans[i].Reason,
});
}
}
else
DisplayMessage("You are not authorized.");
}
}
}