-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
487 lines (422 loc) · 17.8 KB
/
Program.cs
File metadata and controls
487 lines (422 loc) · 17.8 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
class Program
{
string framework = RuntimeInformation.FrameworkDescription;
static string feedbackText = "";
static volatile bool feedbackActive = false;
static volatile bool countdownCancelled = false;
static char pendingAction = '\0';
static double sharedFrame = 0;
static readonly object frameLock = new();
static readonly string[] BannerTemplate =
{
@"",
@"",
@"",
@" // ████████╗ ██████╗ ██████╗ ██╗ ██████╗ ██████╗ ██╗ ██╗ ╔═════════════╗ \\",
@" // ╚══██╔══╝ ██╔═══██╗ ██╔═══██╗ ██║ ██╔══██╗ ██╔═══██╗ ╚██╗██╔╝ ║ Version ║ \\",
@" // ██║ ██║ ██║ ██║ ██║ ██║ ██████╔╝ ██║ ██║ ╚███╔╝ ║{VERSION}║ \\",
@" // ██║ ██║ ██║ ██║ ██║ ██║ ██╔══██╗ ██║ ██║ ██╔██╗ ║ ║ \\",
@" // ██║ ╚██████╔╝ ╚██████╔╝ ███████╗ ██████╔╝ ╚██████╔╝ ██╔╝ ██╗ ║{STATUS}║ \\",
@" // ╚═╝ ╚═════╝ ╚═════╝ ╚══════╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════════════╝ \\"
};
static readonly string[] MenuLines =
{
@"",
@"",
@"",
@"",
@"",
@"",
@"",
@"",
@" ╔═» [ Update ] [U]",
@" ╠═» [ PW Gen ] [P]",
@" #═╦══════════════════════════════════╦═════════════════════════════════════════════════════╬═» [ Exit ] [E]",
@" ╠═══════» [ Cleaner ] [1] ╠═══════» [ Task Manager ] [6] ╠═» [ Credits ] [C]",
@" ╠═══════» [ Kill VMware ] [2] ╠═══════» [ Registry Editor ] [7] ╚═» [ Information ] [I]",
@" ╠═══════» [ Kill VirtualBox ] [3] ╠═══════» [ Control Panel ] [8]",
@" ╠═══════» [ Settings ] [4] ╠═══════» [ Command Prompt ] [9]",
@" ╠═══════» [ Utilities ] [5] ╚═══════» [ System Info ] [0]",
@" ║"
};
static readonly int[] Palette = { 240, 242, 245, 247, 249, 251, 253 };
static string localVersion = "Unknown";
static string statusText = "Checking...";
static readonly string versionFile = Path.Combine(AppContext.BaseDirectory, "Updater\\Version.txt");
static int feedbackLine;
static readonly object consoleLock = new();
static readonly Dictionary<char, (string name, Action handler)> menuActions = new();
static async Task Main()
{
EnableAnsiSupport();
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.InputEncoding = System.Text.Encoding.UTF8;
Console.Title = "Toolbox - Ghost";
try
{
#pragma warning disable CA1416
Console.SetWindowSize(120, 30);
Console.SetBufferSize(120, 30);
#pragma warning restore CA1416
}
catch { }
try
{
Console.CursorVisible = false;
}
catch { }
feedbackLine = BannerTemplate.Length + MenuLines.Length;
InitializeMenuActions();
DrawMenuAndBanner();
using CancellationTokenSource cts = new();
var token = cts.Token;
Task bannerTask = AnimateBannerGradient(token);
Task menuTask = AnimateMenuGradient(token);
Task versionTask = LiveVersionChecker(token);
Task inputTask = WaitForKeyWithFeedback(token);
Task promptTask = AnimatePrompt(token);
await Task.WhenAll(bannerTask, menuTask, versionTask, inputTask, promptTask);
}
static void EnableAnsiSupport()
{
try
{
var checkAnsi = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/c reg query HKCU\\Console /v VirtualTerminalLevel 2>nul | findstr /r /c:\"0x1\" >nul",
UseShellExecute = false,
CreateNoWindow = true
};
var checkProcess = Process.Start(checkAnsi);
checkProcess?.WaitForExit();
if (checkProcess?.ExitCode != 0)
{
var setAnsi = new ProcessStartInfo
{
FileName = "reg",
Arguments = "add HKCU\\Console /v VirtualTerminalLevel /t REG_DWORD /d 1 /f",
UseShellExecute = false,
CreateNoWindow = true
};
Process.Start(setAnsi)?.WaitForExit();
Process.Start(new ProcessStartInfo
{
FileName = Environment.ProcessPath,
UseShellExecute = true,
CreateNoWindow = true,
});
Environment.Exit(0);
}
}
catch
{
}
}
static void InitializeMenuActions()
{
menuActions.Add('1', ("Cleaner", () => RunBatchScript("Files\\Cleaner.bat")));
menuActions.Add('2', ("Kill VMware", () => RunBatchScript("Files\\VMware.bat")));
menuActions.Add('3', ("Kill VirtualBox", () => RunBatchScript("Files\\VirtualBox.bat")));
menuActions.Add('4', ("Settings", () => RunBatchScript("Files\\Settings.bat")));
menuActions.Add('5', ("Utilities", () => RunBatchScript("Files\\Utilities.bat")));
menuActions.Add('6', ("Task Manager", () => RunBatchScript("Files\\TaskManager.bat")));
menuActions.Add('7', ("Registry Editor", () => RunBatchScript("Files\\RegistryEditor.bat")));
menuActions.Add('8', ("Control Panel", () => RunBatchScript("Files\\ControlPanel.bat")));
menuActions.Add('9', ("Command Prompt", () => RunBatchScript("Files\\CommandPrompt.bat")));
menuActions.Add('0', ("System Info", () => RunBatchScript("Files\\SystemInfo.bat")));
menuActions.Add('C', ("Credits", () => RunBatchScript("Files\\ToolboxCredits.bat")));
menuActions.Add('I', ("Information", () => RunBatchScript("Files\\Information.bat")));
menuActions.Add('P', ("PW Gen", () => RunBatchScript("Files\\PasswordGen.bat")));
menuActions.Add('U', ("Update", () => RunBatchScript("Updater\\Update-Toolbox.bat")));
}
static void DrawMenuAndBanner()
{
lock (consoleLock)
{
int bannerHeight = BannerTemplate.Length;
for (int y = 0; y < bannerHeight; y++)
{
string line = BannerTemplate[y]
.Replace("{VERSION}", CenterText(localVersion, 13))
.Replace("{STATUS}", CenterText(statusText, 13));
Console.SetCursorPosition(0, y);
Console.Write(line.PadRight(Console.WindowWidth));
}
for (int i = 0; i < MenuLines.Length; i++)
{
Console.SetCursorPosition(0, bannerHeight + i);
Console.Write(MenuLines[i].PadRight(Console.WindowWidth));
}
}
}
static async Task AnimateBannerGradient(CancellationToken token)
{
int bannerHeight = BannerTemplate.Length;
while (!token.IsCancellationRequested)
{
double currentFrame;
lock (frameLock)
{
currentFrame = sharedFrame;
}
for (int y = 0; y < bannerHeight; y++)
{
string line = BannerTemplate[y]
.Replace("{VERSION}", CenterText(localVersion, 13))
.Replace("{STATUS}", CenterText(statusText, 13));
lock (consoleLock)
{
Console.SetCursorPosition(0, y);
var output = new System.Text.StringBuilder(line.Length * 30);
for (int x = 0; x < line.Length; x++)
{
double p = (y - currentFrame + x * 0.25) % Palette.Length;
if (p < 0) p += Palette.Length;
int i1 = (int)Math.Floor(p);
int i2 = (i1 + 1) % Palette.Length;
double t = p - i1;
int c = (int)Math.Round(Palette[i1] * (1 - t) + Palette[i2] * t);
output.Append($"\x1b[38;5;{c}m{line[x]}\x1b[0m");
}
output.Append(new string(' ', Console.WindowWidth - line.Length));
Console.Write(output.ToString());
}
}
lock (frameLock)
{
sharedFrame = (sharedFrame + 0.19) % Palette.Length;
}
await Task.Delay(1, token);
}
}
static async Task AnimateMenuGradient(CancellationToken token)
{
int bannerHeight = BannerTemplate.Length;
int menuHeight = MenuLines.Length;
while (!token.IsCancellationRequested)
{
double currentFrame;
lock (frameLock)
{
currentFrame = sharedFrame;
}
for (int i = 0; i < menuHeight; i++)
{
string line = MenuLines[i];
int y = bannerHeight + i;
lock (consoleLock)
{
Console.SetCursorPosition(0, y);
var output = new System.Text.StringBuilder(line.Length * 30);
for (int x = 0; x < line.Length; x++)
{
double p = ((bannerHeight + i) - currentFrame + x * 0.20) % Palette.Length;
if (p < 0) p += Palette.Length;
int i1 = (int)Math.Floor(p);
int i2 = (i1 + 1) % Palette.Length;
double t = p - i1;
int c = (int)Math.Round(Palette[i1] * (1 - t) + Palette[i2] * t);
output.Append($"\x1b[38;5;{c}m{line[x]}\x1b[0m");
}
output.Append(new string(' ', Console.WindowWidth - line.Length));
Console.Write(output.ToString());
}
}
await Task.Yield();
}
}
static async Task AnimatePrompt(CancellationToken token)
{
const string idleText = " ╚═══════> ";
while (!token.IsCancellationRequested)
{
string textToDraw = feedbackActive ? feedbackText : idleText;
lock (consoleLock)
{
Console.SetCursorPosition(0, feedbackLine);
Console.Write(textToDraw.PadRight(Console.WindowWidth));
}
await Task.Delay(50, token);
}
}
static async Task WaitForKeyWithFeedback(CancellationToken token)
{
const string prompt = " ╚═══════> ";
while (!token.IsCancellationRequested)
{
if (!Console.KeyAvailable)
{
await Task.Delay(10, token);
continue;
}
char key = char.ToUpperInvariant(Console.ReadKey(true).KeyChar);
feedbackActive = true;
countdownCancelled = false;
pendingAction = key;
for (int i = 3; i > 0 && !countdownCancelled; i--)
{
feedbackText = $"{prompt}{key} Running In {i}... (Press E To Cancel)";
var startTime = DateTime.UtcNow;
var targetTime = startTime.AddMilliseconds(1000);
while (DateTime.UtcNow < targetTime && !countdownCancelled)
{
if (Console.KeyAvailable)
{
char cancelKey = char.ToUpperInvariant(Console.ReadKey(true).KeyChar);
if (cancelKey == 'E')
{
countdownCancelled = true;
break;
}
}
var remaining = (targetTime - DateTime.UtcNow).TotalMilliseconds;
if (remaining > 50)
{
await Task.Delay(50, token);
}
else if (remaining > 0)
{
await Task.Delay((int)remaining, token);
}
}
}
if (countdownCancelled)
{
feedbackText = $"{prompt}{key} Cancelled.";
await Task.Delay(1000, token);
feedbackActive = false;
pendingAction = '\0';
continue;
}
if (pendingAction == 'E')
{
feedbackText = $"{prompt}{key} Exiting...";
await Task.Delay(500, token);
Environment.Exit(0);
return;
}
else if (menuActions.ContainsKey(pendingAction))
{
var action = menuActions[pendingAction];
feedbackText = $"{prompt}{key} Running {action.name}...";
await Task.Delay(500, token);
try
{
action.handler();
feedbackText = $"{prompt}{key} {action.name} Running.";
}
catch (Exception ex)
{
feedbackText = $"{prompt}{key} Error: {ex.Message}";
}
await Task.Delay(2000, token);
}
else
{
feedbackText = $"{prompt}{key} Invalid Option.";
await Task.Delay(1000, token);
}
feedbackActive = false;
pendingAction = '\0';
}
}
static async Task LiveVersionChecker(CancellationToken token)
{
await Task.Delay(1000, token);
while (!token.IsCancellationRequested)
{
try
{
localVersion = File.Exists(versionFile) ? File.ReadAllText(versionFile).Trim() : "Unknown";
string? latest = await GetLatestRelease("SimonGhost1012", "Toolbox");
if (string.IsNullOrEmpty(latest))
{
statusText = CenterText("Unknown", 13);
}
else
{
string normalizedLocal = NormalizeVersion(localVersion);
string normalizedLatest = NormalizeVersion(latest);
statusText = normalizedLatest == normalizedLocal
? CenterText("Up-To-Date!", 13)
: CenterText("Update!", 13);
}
}
catch
{
statusText = CenterText("Unknown", 13);
}
await Task.Delay(30000, token);
}
}
static string NormalizeVersion(string version)
{
if (string.IsNullOrEmpty(version) || version == "Unknown") return version;
return version.Trim().TrimStart('v', 'V');
}
static async Task<string?> GetLatestRelease(string owner, string repo)
{
try
{
using HttpClient client = new();
client.DefaultRequestHeaders.UserAgent.ParseAdd("Toolbox");
var response = await client.GetAsync($"https://api.github.com/repos/{owner}/{repo}/releases/latest");
if (!response.IsSuccessStatusCode) return null;
using JsonDocument doc = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
return doc.RootElement.TryGetProperty("tag_name", out var tag) ? tag.GetString() : null;
}
catch { return null; }
}
static void HandleKeyAction(char key)
{
if (key == 'E')
Environment.Exit(0);
}
static string CenterText(string text, int width)
{
if (text.Length >= width) return text[..width];
int left = (width - text.Length) / 2;
return new string(' ', left) + text + new string(' ', width - text.Length - left);
}
static void RunBatchScript(string scriptPath)
{
try
{
Process.Start(new ProcessStartInfo
{
FileName = scriptPath,
UseShellExecute = true
});
}
catch
{
throw new Exception($"Could Not Run {scriptPath}");
}
}
static void CheckForUpdates()
{
try
{
Process.Start(new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/c echo Checking For Updates... & timeout /t 3 & echo Update Check Complete.",
UseShellExecute = true
});
}
catch
{
throw new Exception("Could Not Check For Updates");
}
}
}