-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
219 lines (197 loc) · 8.11 KB
/
MainWindow.xaml.cs
File metadata and controls
219 lines (197 loc) · 8.11 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
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using SecureAppVault.Models;
using SecureAppVault.Services;
using SecureAppVault.ViewModels;
using MessageBox = System.Windows.MessageBox;
using MessageBoxOptions = System.Windows.MessageBoxOptions;
using NotifyIcon = System.Windows.Forms.NotifyIcon;
using OpenFileDialog = Microsoft.Win32.OpenFileDialog;
namespace SecureAppVault
{
public partial class MainWindow : Window
{
private readonly WhitelistService _whitelistService;
private readonly TwoFactorAuthService _twoFactorAuthService;
private readonly DispatcherTimer _monitorTimer;
private NotifyIcon _notifyIcon = null!;
public MainWindow()
{
InitializeComponent();
_monitorTimer = new DispatcherTimer();
var encryptionKey = "xbpz6ZVGvrsF3F6BZY9UeNQWg1BKheOO";
DataContext = new MainViewModel(encryptionKey);
_whitelistService = new WhitelistService(encryptionKey);
_twoFactorAuthService = new TwoFactorAuthService();
if (!_twoFactorAuthService.ConfigExists())
{
ShowConfigCode();
}
StartMonitoring();
Closing += OnWindowClosing; // Add event handler for window closing
StateChanged += OnStateChanged; // Add event handler for state change
InitializeNotifyIcon();
}
private void InitializeNotifyIcon()
{
_notifyIcon = new NotifyIcon
{
Icon = new Icon("Resources/Icon.ico"),
Visible = true,
Text = "SecureAppVault"
};
_notifyIcon.DoubleClick += (s, e) => RestoreWindow();
}
private void RestoreWindow()
{
Show();
WindowState = WindowState.Normal;
_notifyIcon.Visible = false;
}
private void OnStateChanged(object? sender, EventArgs e)
{
if (WindowState == WindowState.Minimized)
{
Hide();
_notifyIcon.Visible = true;
}
}
private void OnWindowClosing(object? sender, CancelEventArgs e)
{
e.Cancel = true; // Cancel the closing event
Hide();
_notifyIcon.Visible = true;
}
private void StartMonitoring() {
_monitorTimer.Interval = TimeSpan.FromSeconds(1); // Check every X seconds
_monitorTimer.Tick += MonitorApplications;
_monitorTimer.Start();
}
private void MonitorApplications(object? sender, EventArgs e)
{
var viewModel = (MainViewModel)DataContext;
var appNamesToTerminate = new HashSet<string>();
foreach (var app in viewModel.WhitelistedApps)
{
if (!app.IsRunningFromVault)
{
appNamesToTerminate.Add(Path.GetFileNameWithoutExtension(app.Path));
}
}
var runningProcesses = Process.GetProcesses();
foreach (var process in runningProcesses)
{
if (appNamesToTerminate.Contains(process.ProcessName))
{
try
{
process.Kill();
Debug.WriteLine($"Terminated process {process.ProcessName} with ID {process.Id}");
MessageBox.Show($"Terminated process {process.ProcessName} with ID {process.Id}\nRun through the vault", "Process Terminated", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly);
}
catch (Exception ex)
{
Debug.WriteLine($"Failed to terminate process {process.ProcessName} with ID {process.Id}: {ex.Message}");
}
}
}
}
private void AddAppButton_Click(object sender, RoutedEventArgs e) {
var openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true) {
var appName = Path.GetFileNameWithoutExtension(openFileDialog.FileName);
var appPath = openFileDialog.FileName;
var newApp = new WhitelistedApp(appName, appPath);
var viewModel = (MainViewModel)DataContext;
viewModel.WhitelistedApps.Add(newApp);
_whitelistService.SaveWhitelist(viewModel.WhitelistedApps.ToList());
}
}
private void RemoveAppButton_Click(object sender, RoutedEventArgs e) {
var viewModel = (MainViewModel)DataContext;
var selectedApp = (WhitelistedApp)AppListBox.SelectedItem;
if (selectedApp != null) {
var inputBox = new InputBox("Enter 2FA Code:");
if (inputBox.ShowDialog() == true) {
var code = inputBox.InputText;
if (_twoFactorAuthService.VerifyCode(code)) {
viewModel.WhitelistedApps.Remove(selectedApp);
_whitelistService.SaveWhitelist(viewModel.WhitelistedApps.ToList());
} else {
MessageBox.Show("Invalid 2FA Code.");
}
}
}
}
private void RunAppButton_Click(object sender, RoutedEventArgs e)
{
var selectedApp = (WhitelistedApp)AppListBox.SelectedItem;
if (selectedApp != null)
{
var appPath = selectedApp.Path;
if (File.Exists(appPath))
{
try
{
selectedApp.IsRunningFromVault = true;
var processStartInfo = new ProcessStartInfo(appPath)
{
UseShellExecute = true
};
var process = Process.Start(processStartInfo);
if (process != null)
{
process.EnableRaisingEvents = true;
process.Exited += (s, args) => selectedApp.IsRunningFromVault = false;
}
}
catch (Win32Exception ex)
{
MessageBox.Show($"Error starting application: {ex.Message}");
}
}
else
{
MessageBox.Show("The specified application file does not exist.");
}
}
}
private void UnlockVaultButton_Click(object sender, RoutedEventArgs e) {
var viewModel = (MainViewModel)DataContext;
if (viewModel.IsUnlocked) {
viewModel.IsUnlocked = false;
} else {
var inputBox = new InputBox("Enter 2FA Code:");
if (inputBox.ShowDialog() == true) {
var code = inputBox.InputText;
if (_twoFactorAuthService.VerifyCode(code)) {
viewModel.IsUnlocked = true;
} else {
MessageBox.Show("Invalid 2FA Code.");
}
}
}
}
private void ShowConfigCode() {
var qrCodeBytes = _twoFactorAuthService.GenerateQrCode("SecureAppVault");
var qrCodeImage = new BitmapImage();
using (var ms = new MemoryStream(qrCodeBytes)) {
qrCodeImage.BeginInit();
qrCodeImage.CacheOption = BitmapCacheOption.OnLoad;
qrCodeImage.StreamSource = ms;
qrCodeImage.EndInit();
}
var qrCodeWindow = new Window {
Title = "QR Code",
Content = new System.Windows.Controls.Image { Source = qrCodeImage }, // Use the correct Image class
Width = 300,
Height = 300
};
qrCodeWindow.ShowDialog();
}
}
}