SafeNet/MotoTRBO_SOC/Program.cs
2021-02-24 13:50:23 +02:00

252 lines
9.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Collections;
using System.Net.Sockets;
using Nini.Config;
using SafeNetLib;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;
using System.Collections.ObjectModel;
namespace MotoTRBO_SOC
{
public static class MotoTRBO_GW
{
public static DateTime lastEntry = DateTime.Now;
public static DebugMSG_Type debugMSG_type = DebugMSG_Type.DEV;
public static RouteManager routeManager;
public static Config cfg;
public static UdpClient smsUDPclient = null;
public static bool unitsLoaded = false;
public static bool addressLoaded = false;
public static LocationThread LocationConnection = null; // the threads for the 5 services we handle
public static DBconnThread DBConnection = null;
public static ReceiveSMSThread ReceiveSMSthr = null;
public static SendSMSThread SendSMSthr = null;
public static ARSThread ARSConnection = null;
public static Thread LocationThreadobj = null;
public static Thread ARSThreadobj = null;
public static Thread DBThreadobjARS = null;
public static Thread DBThreadobjGPS = null;
public static Thread DBThreadobjAux = null;
public static Thread DBThreadobjSMS = null;
public static Thread DBThreadobjSMS_conf = null;
public static Thread DBThreadobjAddr = null;
public static Thread DBThreadobjPOLL = null;
public static Thread DBThreadobjTallysman= null;
public static Thread ReceiveSMSThreadobj = null;
public static Thread SendSMSThreadobj = null;
public static Thread CompAddrThread = null;
public static EmailHandler emailH;
public static LOGS hLOG;
public static Boolean running = true;
public static Int64 lastDBEntry;
public static DateTime lastSMSThreadUpdate;
public static DateTime lastARSThreadUpdate;
public static DateTime lastPositionThreadUpdate;
public static Boolean isConsoleEnabled = false;
public static InterthreadMessageQueue<MotoTRBO_SOC.TallysmanReceiveThread.TallysmanEventArgs> tallysmanEventsQueue
= new InterthreadMessageQueue<MotoTRBO_SOC.TallysmanReceiveThread.TallysmanEventArgs>();
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.Title = "MotoTrbo_GWConsole";
setConsoleWindowVisibility(true, Console.Title);
if(!HandleCommandLineArgs(args))
return;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SingleInstanceController controller = new SingleInstanceController();
controller.Run(args);
//Application.Run(new FormMain());
}
public class SingleInstanceController : WindowsFormsApplicationBase
{
public static ReadOnlyCollection<string> CommandLineArgs = null;
public SingleInstanceController()
{
IsSingleInstance = true;
Startup += SingleInstanceController_Startup;
StartupNextInstance += this_StartupNextInstance;
}
void SingleInstanceController_Startup(object sender, StartupEventArgs e)
{
}
void this_StartupNextInstance(object sender, StartupNextInstanceEventArgs e)
{
FormMain form = MainForm as FormMain; //My derived form type
if (form.WindowState == FormWindowState.Minimized)
{
form.Show();
form.ShowInTaskbar = true;
form.WindowState = FormWindowState.Normal;
// bring to front
form.Activate();
}
}
protected override void OnCreateMainForm()
{
MainForm = new FormMain();
}
}
/// <summary>
/// Check to see which Command Line Args were passed to the application
/// </summary>
/// <param name="CommandLineArgs">String array containing all command line arguments</param>
/// <return>A value indicating if the application needs to be closed or not</return>
private static bool HandleCommandLineArgs(string[] CommandLineArgs)
{
if (CommandLineArgs.Length > 0)
{
if (CommandLineArgs[0] == "-v")
{
Utils.WriteLine("Version " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(),ConsoleColor.Yellow);
Console.ReadKey();
Utils.ConsWrite(DebugMSG_Type.always, "Application ended!!!!");
System.Windows.Forms.Application.Exit();
return false;
}
else if (CommandLineArgs[0] == "-h" || CommandLineArgs[0] == "help")
{
Console.WriteLine("\nOptions:\n");
Console.WriteLine("-d [to decrypt] [key]\t Decrypts a string using the specified key");
Console.WriteLine("-e [to encrypt] [key]\t Encrypts a string using the specified key");
Console.ReadKey();
System.Windows.Forms.Application.Exit();
return false;
}
else if (CommandLineArgs[0] == "-d" && CommandLineArgs[1] != null && CommandLineArgs[2] != null)
{
try
{
//Console.WriteLine("Decrypt required for " + args[1]);
Console.WriteLine("" + Encryption.Decrypt(CommandLineArgs[1], CommandLineArgs[2]));
}
catch (Exception ex)
{
}
finally
{
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
System.Windows.Forms.Application.Exit();
}
return false;
}
else if (CommandLineArgs[0] == "-e" && CommandLineArgs[1] != null && CommandLineArgs[2] != null)
{
try
{
//Console.WriteLine("Encrypt required for " + args[1]);
Console.WriteLine("" + Encryption.Encrypt(CommandLineArgs[1], CommandLineArgs[2]));
}
catch (Exception ex)
{
}
finally
{
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
System.Windows.Forms.Application.Exit();
}
return false;
}
// let the console to be visible
else if (CommandLineArgs[0] == "-c")
{
isConsoleEnabled = true;
setConsoleWindowVisibility(true, Console.Title);
LOGS.WRITE_LOGS = true;
}
// let the console to be visible
else if (CommandLineArgs[0] == "-l")
{
isConsoleEnabled = false;
setConsoleWindowVisibility(false, Console.Title);
LOGS.WRITE_LOGS = true;
}
// hide the console
else
{
setConsoleWindowVisibility(false, Console.Title);
}
}
else
setConsoleWindowVisibility(false, Console.Title);
return true;
}
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
public static void setConsoleWindowVisibility(bool visible, string title)
{
isConsoleEnabled = visible;
IntPtr hWnd = IntPtr.Zero;
while (hWnd == IntPtr.Zero)
{
hWnd = FindWindow(null, title);
if (hWnd != IntPtr.Zero)
{
if (!visible)
//Hide the window
ShowWindow(hWnd, 0); // 0 = SW_HIDE
else
//Show window again
ShowWindow(hWnd, 1); //1 = SW_SHOWNORMA
}
Thread.Sleep(100);
}
}
}
}