1
|
using System.Runtime.InteropServices;
|
2
|
using System.Security.Principal;
|
3
|
using LDClient.detection;
|
4
|
using LDClient.network;
|
5
|
using LDClient.utils;
|
6
|
using LDClient.utils.loggers;
|
7
|
|
8
|
using static System.Diagnostics.Process;
|
9
|
using static System.Reflection.Assembly;
|
10
|
|
11
|
namespace LDClient;
|
12
|
|
13
|
internal static class Program {
|
14
|
|
15
|
public static ConfigLoader Config { get; } = new();
|
16
|
public static ALogger DefaultLogger { get; } = ALogger.Current;
|
17
|
private static IApiClient? DefaultApiClient { get; set; }
|
18
|
private static readonly NetworkDetection NetDetect = new(Config.ApiPort, Config.DetectionPeriod);
|
19
|
private static readonly ProcessDetection ProcDetect = new(Config.T32ProcessName, Config.DetectionPeriod);
|
20
|
|
21
|
// Main Method
|
22
|
public static int Main() {
|
23
|
var exists = GetProcessesByName(Path.GetFileNameWithoutExtension(GetEntryAssembly()?.Location)).Length > 1;
|
24
|
if (exists) {
|
25
|
DefaultLogger.Error("Another instance of the application is already running");
|
26
|
return 1;
|
27
|
}
|
28
|
|
29
|
DefaultApiClient = new ApiClient(
|
30
|
Config.ApiBaseAddress,
|
31
|
Config.ApiPort,
|
32
|
Config.ApiUsbEndPoint,
|
33
|
Config.RetryPeriod, Config.MaxEntries,
|
34
|
Config.MaxRetries,
|
35
|
Config.CacheFileName
|
36
|
);
|
37
|
|
38
|
DefaultLogger.Debug("Main -> starting the ApiClient");
|
39
|
var apiClientThread = new Thread(DefaultApiClient.Run) {
|
40
|
IsBackground = true
|
41
|
};
|
42
|
apiClientThread.Start();
|
43
|
|
44
|
var admin = IsAdministrator();
|
45
|
DefaultLogger.Debug($"Is program executed with admin rights? {admin}");
|
46
|
|
47
|
var networkTread = new Thread(NetDetect.RunPeriodicDetection);
|
48
|
networkTread.Start();
|
49
|
|
50
|
if (admin) {
|
51
|
ProcDetect.RegisterProcessListeners();
|
52
|
} else {
|
53
|
var processThread = new Thread(ProcDetect.RunPeriodicDetection);
|
54
|
processThread.Start();
|
55
|
processThread.Join();
|
56
|
}
|
57
|
|
58
|
networkTread.Join();
|
59
|
|
60
|
DefaultLogger.Debug("Main -> stopping the ApiClient");
|
61
|
DefaultApiClient.Stop();
|
62
|
apiClientThread.Join();
|
63
|
DefaultLogger.Debug("Main -> finished");
|
64
|
|
65
|
return 0;
|
66
|
}
|
67
|
|
68
|
private static bool IsAdministrator() {
|
69
|
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
|
70
|
return false;
|
71
|
}
|
72
|
var identity = WindowsIdentity.GetCurrent();
|
73
|
var principal = new WindowsPrincipal(identity);
|
74
|
return principal.IsInRole(WindowsBuiltInRole.Administrator);
|
75
|
}
|
76
|
}
|