-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobals.cs
More file actions
306 lines (294 loc) · 10.7 KB
/
globals.cs
File metadata and controls
306 lines (294 loc) · 10.7 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
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization.Metadata;
using System.Text.Json;
using System.Timers;
namespace SimplePasswordManager
{
internal class Globals
{
// Save file variables
public static bool isFirstLoad = true;
public static bool debugMode = false;
public static string version = "1.0.0";
// Filesystem structure
public static string rootFolder;
public static string tempFolder;
public static string configFolder;
public static string configFile;
public static string resourcesFolder;
public static string passwordFolder;
public static string passwordFile;
public static string recoveryCodeFile;
public static string loginsFolder;
public static List<string> loginFiles;
// Password & recovery code
public static string password;
public static string recoveryCode;
/// <summary>
/// Clears the console after an optional delay.
/// </summary>
/// <param name="t">Delay in seconds before clearing the console.</param>
public static void ClearConsole(int t = 0)
{
try
{
Console.Clear();
Console.Beep();
if (t > 0)
{
Thread.Sleep(t * 1000);
}
if (debugMode)
{
Console.WriteLine($"[DEBUG] Console cleared with {t} second delay.");
}
}
catch (Exception ex)
{
if (debugMode)
{
Console.WriteLine($"[DEBUG] Error clearing console: {ex.Message}");
}
}
}
/// <summary>
/// Reads a password from the console, displaying asterisks for each character.
/// Returns the password string.
/// </summary>
/// <returns>The entered password.</returns>
public static string ReadPassword()
{
StringBuilder password = new StringBuilder();
try
{
while (true)
{
ConsoleKeyInfo key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Enter)
{
if (debugMode)
{
Console.WriteLine("[DEBUG] Password input completed.");
}
Console.WriteLine();
break;
}
else if (key.Key == ConsoleKey.Backspace)
{
if (password.Length > 0)
{
password.Length--;
Console.Write("\b \b");
if (debugMode)
{
Console.WriteLine("[DEBUG] Backspace in password input.");
}
}
}
else
{
password.Append(key.KeyChar);
Console.Write("*");
if (debugMode)
{
Console.WriteLine($"[DEBUG] Password char added.");
}
}
}
return password.ToString();
}
catch (Exception ex)
{
if (debugMode)
{
Console.WriteLine($"[DEBUG] Error reading password: {ex.Message}");
}
return string.Empty;
}
}
/// <summary>
/// Reads user input from the console, displaying each character as typed.
/// Returns the input string or null if Escape is pressed.
/// </summary>
/// <returns>The input string, or null if Escape is pressed.</returns>
public static string ReadInput()
{
StringBuilder input = new StringBuilder();
try
{
while (true)
{
ConsoleKeyInfo key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Enter)
{
Console.WriteLine();
if (debugMode)
{
Console.WriteLine("[DEBUG] Input completed.");
}
return input.ToString();
}
else if (key.Key == ConsoleKey.Escape)
{
if (debugMode)
{
Console.WriteLine("[DEBUG] Input cancelled with Escape.");
}
return null;
}
else if (key.Key == ConsoleKey.Backspace)
{
if (input.Length > 0)
{
input.Length--;
Console.Write("\b \b");
if (debugMode)
{
Console.WriteLine("[DEBUG] Backspace in input.");
}
}
}
else
{
input.Append(key.KeyChar);
Console.Write(key.KeyChar);
if (debugMode)
{
Console.WriteLine($"[DEBUG] Added char: {key.KeyChar}");
}
}
}
}
catch (Exception ex)
{
if (debugMode)
{
Console.WriteLine($"[DEBUG] Error reading input: {ex.Message}");
}
return string.Empty;
}
}
/// <summary>
/// Executes a shell command to copy text to the clipboard.
/// </summary>
/// <param name="command">The clipboard command (e.g., clip, pbcopy, xclip).</param>
/// <param name="input">The text to pipe into the command.</param>
/// <exception cref="ExternalException">Thrown if the command execution fails.</exception>
public static void ExecuteCommand(string command, string input)
{
try
{
using (var process = new Process())
{
process.StartInfo = new ProcessStartInfo
{
FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : "/bin/bash",
Arguments = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? $"/c echo {input} | {command}" : $"-c \"echo -n {input} | {command}\"",
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
process.Start();
process.WaitForExit();
if (debugMode)
{
Console.WriteLine($"[DEBUG] Executed command: {command}");
}
}
}
catch (Exception ex)
{
if (debugMode)
{
Console.WriteLine($"[DEBUG] Error executing command '{command}': {ex.Message}");
}
throw new ExternalException($"Failed to execute command: {ex.Message}", ex);
}
}
/// <summary>
/// Creates a key-based menu and returns the pressed key.
/// </summary>
/// <param name="messages">Menu options to display (e.g., "[S]ettings").</param>
/// <returns>The ConsoleKeyInfo of the pressed key.</returns>
public static ConsoleKeyInfo MenuBuilder(params string[] messages)
{
try
{
foreach (string message in messages)
{
Console.WriteLine(message);
}
Console.Write(": ");
ConsoleKeyInfo key = Console.ReadKey();
if (debugMode)
{
Console.WriteLine($"[DEBUG] Menu selection: {key.KeyChar}");
}
Console.WriteLine();
return key;
}
catch (Exception ex)
{
if (debugMode)
{
Console.WriteLine($"[DEBUG] Error in MenuBuilder: {ex.Message}");
}
return new ConsoleKeyInfo();
}
}
/// <summary>
/// Saves the current configuration to the config.json file.
/// </summary>
/// <exception cref="IOException">Thrown if writing to the config file fails.</exception>
public static void SaveConfig()
{
try
{
JsonObject j = new JsonObject
{
["config"] = new JsonObject
{
["first_load"] = isFirstLoad,
["debug_mode"] = debugMode
},
["version"] = version,
};
var options = new JsonSerializerOptions
{
WriteIndented = true,
TypeInfoResolver = new DefaultJsonTypeInfoResolver()
};
string jsonContent = j.ToJsonString(options);
File.WriteAllText(configFile, jsonContent);
if (debugMode)
{
Console.WriteLine($"[DEBUG] Config saved to {configFile}:");
Console.WriteLine(jsonContent);
}
}
catch (IOException ex)
{
Console.WriteLine($"Error saving config: {ex.Message}");
if (debugMode)
{
Console.WriteLine($"[DEBUG] IOException in SaveConfig: {ex}");
}
throw;
}
catch (Exception ex)
{
Console.WriteLine($"Error saving config: {ex.Message}");
if (debugMode)
{
Console.WriteLine($"[DEBUG] Exception in SaveConfig: {ex}");
}
throw;
}
}
}
}