-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathApp.xaml.cs
320 lines (270 loc) · 8.49 KB
/
App.xaml.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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Net.Sockets;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Threading;
using System.Dynamic;
using System.Xml;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Input;
using System.IO;
using Microsoft.Data.Sqlite;
using Microsoft.Win32;
using AsyncFriendlyStackTrace;
namespace DangerZoneHackerTracker
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application, IDisposable
{
Settings Settings;
public RemoteConsole Console;
CheaterSet Cheaters;
EventableSortedSet<User> users;
public EventableSortedSet<User> Users { get => users; }
EventableSortedSet<User> TempUsers;
readonly Regex SteamIDRegex = new Regex(string.Format(@"# *\d+ *(?<Index>\d+) *{0}(?<Name>.*){0} *(?<SteamID>STEAM_\d:\d:\d+)", "\""));
readonly Regex MapNameRegex = new Regex(@"map\s+: (\w+)");
readonly Regex HostNameRegex = new Regex(@" *hostname *: *(.+)");
public delegate void ClientEventCallback(User user);
public delegate void StringChangedCallback(string oldValue, string newValue);
public ThreadSafeEvent<ClientEventCallback> ClientConnected = new();
public ThreadSafeEvent<ClientEventCallback> ClientDisconnected = new();
public ThreadSafeEvent<StringChangedCallback> MapChanged = new();
public ThreadSafeEvent<StringChangedCallback> HostChanged = new();
public string MapName { get; set; }
public string HostName { get; set; }
private const string DatabaseFilename = "Cheaters.sq3";
private static string DatabasePath
{
get
{
var basePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var folder = Path.Combine(basePath, "HackerTracker");
return Path.Combine(folder, DatabaseFilename);
}
}
public static new App Current => Application.Current as App;
protected override void OnStartup(StartupEventArgs e)
{
//Debugger.Launch();
AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;
base.OnStartup(e);
users = new EventableSortedSet<User>(new UserComparer());
TempUsers = new EventableSortedSet<User>(new UserComparer());
Settings = Settings.Init();
Cheaters = CheaterSet.Init();
ImportOldDatabase();
this.LoadCompleted += App_LoadCompleted;
this.Activated += App_Activated;
ClientConnected.AddEvent(OnClientConnected);
ClientDisconnected.AddEvent(OnClientDisconnected);
MapChanged.AddEvent(OnMapChanged);
}
private void OnMapChanged(string oldValue, string newValue)
{
Users.Clear();
}
private void CurrentDomain_FirstChanceException(object sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
{
//if(e.Exception is not IOException)
//{
// Logger.Log(e.Exception.ToString());
//}
}
private void App_Activated(object sender, EventArgs e)
{
// remove the event listener right away, this will be running on two threads
this.Activated -= App_Activated;
// Add the code that we want to run when the Console connection completes a thread safe event listener.
ThreadSafeEvent<Action<bool>> callback = new();
callback.AddEvent((bool didConnect) =>
{
Console.LineRead += Console_LineRead;
if (!didConnect)
{
var wind = new MissingParamsWindow()
{
ShowActivated = true
};
wind.Show();
}
});
// Now we connect to the console on a separate thread.
// If csgo isn't running right, then this will take a few seconds and will block the ui thread while it tries to connect.
new Thread(() =>
{
Console = new RemoteConsole();
bool didConnect = Console.AwaitConnection();
callback.Invoke(didConnect);
}).Start();
}
private void App_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
}
private void MainWindow_Activated(object sender, EventArgs e)
{
}
private void ImportOldDatabase()
{
static T TryGetValue<T>(SqliteDataReader reader, string value)
{
try
{
return (T)reader[value];
}
catch
{
return default;
}
}
if (!Settings["HasImportedDB"])
{
if (File.Exists(DatabasePath))
{
using var connection = new SqliteConnection($"Data Source={DatabasePath}");
connection.Open();
#region Import Settings
using var settingsCommand = connection.CreateCommand();
settingsCommand.CommandText = "SELECT * FROM Settings";
using var reader2 = settingsCommand.ExecuteReader();
if (reader2.Read())
{
Settings["Volume"] = TryGetValue<double>(reader2, "Volume");
Settings["UpdateRate"] = Math.Max(TryGetValue<double>(reader2, "UpdateRate"), 0.1);
Settings["UserNameOverride"] = TryGetValue<string>(reader2, "UserNameOverride");
}
if (string.IsNullOrEmpty(Settings["UserNameOverride"]))
{
using var subkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Valve\Steam");
Settings["UserNameOverride"] = subkey.GetValue("LastGameNameUsed") as string;
}
#endregion
#region Import Cheaters
using var cheaterCommand = connection.CreateCommand();
cheaterCommand.CommandText = "SELECT * FROM Cheaters";
using var reader = cheaterCommand.ExecuteReader();
while (reader.Read())
{
var steamid3 = $"[U:1:{TryGetValue<long>(reader, "AccountID")}]";
var steam = new SteamID();
steam.SetFromSteam3String(steamid3);
var cheater = new Cheater()
{
CheatList = TryGetValue<string>(reader, "CheatList"),
AccountID = steam.ConvertToUInt64(),
LastKnownName = TryGetValue<string>(reader, "LastKnownName"),
Notes = TryGetValue<string>(reader, "Notes") ?? "",
Submitter = Settings["UserNameOverride"],
ThreatLevel = (int)TryGetValue<long>(reader, "ThreatLevel")
};
Cheaters.Add(cheater);
}
#endregion
}
Settings["HasImportedDB"] = true;
}
}
private void OnClientDisconnected(User user)
{
Debug.WriteLine($"'{user.Name}' has disconnected from the server");
Users.Remove(user);
}
private void OnClientConnected(User user)
{
Debug.WriteLine($"'{user.Name}' has joined the server");
user.Cheater = Cheaters.FirstOrDefault(t => t.AccountID == user.AccountID);
Users.Add(user);
if (user.IsCheater)
{
ToastManager.ShowToastAsync(
title: $"Hacker {user.Name} Found In Game",
message: $"Threat Level: {user.Cheater.ThreatLevel}\n" +
$"Known Cheats: {user.Cheater.CheatList}\n" +
$"Previous Name: {user.Cheater.LastKnownName}",
duration: TimeSpan.FromSeconds(10.0));
user.Cheater.LastKnownName = user.Name;
if (user.Cheater.ThreatLevel > 3)
{
SoundManager.PlayEmbeddedSound("haaaaxedit.mp3", Settings["Volume"]);
}
Cheaters.Save();
}
}
private void Console_LineRead(string line)
{
if (line.StartsWith("hostname"))
{
var hostMatch = HostNameRegex.Match(line);
if (HostName != hostMatch.Groups[1].Value)
{
HostChanged.Invoke(HostName, hostMatch.Groups[1].Value);
HostName = hostMatch.Groups[1].Value;
}
return;
}
if (line == "Not connected to server")
{
// remove the users from the list preemptively but not from the grid until we have a new set.
Users.Clear();
return;
}
if (line == "#end")
{
var disconnectedUsers = users.Except(TempUsers, new UserComparer()).ToArray();
foreach (var user in disconnectedUsers)
{
ClientDisconnected?.Invoke(user);
}
var connectedUsers = TempUsers.Except(users, new UserComparer()).ToArray();
foreach (var user in connectedUsers)
{
ClientConnected?.Invoke(user);
}
TempUsers.Clear();
return;
}
var match = SteamIDRegex.Match(line);
if (match.Success)
{
User user = new User()
{
Index = Convert.ToInt32(match.Groups["Index"].Value),
Name = match.Groups["Name"].Value,
SteamID = new SteamID(match.Groups["SteamID"].Value)
};
TempUsers.Add(user);
return;
}
match = MapNameRegex.Match(line);
if (match.Success)
{
if (MapName != match.Groups[1].Value)
{
MapChanged.Invoke(MapName, match.Groups[1].Value);
MapName = match.Groups[1].Value;
}
return;
}
}
protected override void OnExit(ExitEventArgs e)
{
this.Dispose();
base.OnExit(e);
}
public void Dispose()
{
Console?.Dispose();
Console = null;
}
}
}