Projekt

Obecné

Profil

Stáhnout (2.51 KB) Statistiky
| Větev: | Tag: | Revize:
1 f5a08dd5 Pultak
using System.Runtime.InteropServices;
2
using System.Security.Principal;
3
using LDClient.detection;
4 aaad9ad4 Pultak
using LDClient.network;
5 4b0f0b36 Pultak
using LDClient.utils;
6 74bd1e40 Pultak
using LDClient.utils.loggers;
7 c318a92c Pultak
8 30c849e4 silhavyj
using static System.Diagnostics.Process;
9
using static System.Reflection.Assembly;
10
11 f5a08dd5 Pultak
namespace LDClient;
12 c318a92c Pultak
13 30c849e4 silhavyj
internal static class Program {
14 c318a92c Pultak
15 30c849e4 silhavyj
    public static ConfigLoader Config { get; } = new();
16 4b0f0b36 Pultak
    public static ALogger DefaultLogger { get; } = ALogger.Current;
17 30c849e4 silhavyj
    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 4b0f0b36 Pultak
    // Main Method
22 30c849e4 silhavyj
    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 aaad9ad4 Pultak
        var apiClientThread = new Thread(DefaultApiClient.Run) {
40
            IsBackground = true
41
        };
42
        apiClientThread.Start();
43
44 f5a08dd5 Pultak
        var admin = IsAdministrator();
45
        DefaultLogger.Debug($"Is program executed with admin rights? {admin}");
46 aaad9ad4 Pultak
47 30c849e4 silhavyj
        var networkTread = new Thread(NetDetect.RunPeriodicDetection);
48 f5a08dd5 Pultak
        networkTread.Start();
49 30c849e4 silhavyj
        
50 f5a08dd5 Pultak
        if (admin) {
51 30c849e4 silhavyj
            ProcDetect.RegisterProcessListeners();
52 f5a08dd5 Pultak
        } else {
53 30c849e4 silhavyj
            var processThread = new Thread(ProcDetect.RunPeriodicDetection);
54 f5a08dd5 Pultak
            processThread.Start();
55
            processThread.Join();
56
        }
57
58
        networkTread.Join();
59 aaad9ad4 Pultak
60
        DefaultLogger.Debug("Main -> stopping the ApiClient");
61
        DefaultApiClient.Stop();
62
        apiClientThread.Join();
63
        DefaultLogger.Debug("Main -> finished");
64 30c849e4 silhavyj
        
65
        return 0;
66 aaad9ad4 Pultak
    }
67 f5a08dd5 Pultak
68 30c849e4 silhavyj
    private static bool IsAdministrator() {
69
        if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
70 f5a08dd5 Pultak
            return false;
71
        }
72 30c849e4 silhavyj
        var identity = WindowsIdentity.GetCurrent();
73
        var principal = new WindowsPrincipal(identity);
74
        return principal.IsInRole(WindowsBuiltInRole.Administrator);
75 c318a92c Pultak
    }
76
}