Projekt

Obecné

Profil

Stáhnout (2.04 KB) Statistiky
| Větev: | Tag: | Revize:
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
namespace LDClient;
9

    
10
internal class Program {
11

    
12
    public static ConfigLoader Config { get; set; } = new();
13
    public static ALogger DefaultLogger { get; } = ALogger.Current;
14

    
15
    public static IApiClient DefaultApiClient { get; set; } = new ApiClient(Config.ApiBaseAddress,
16
        Config.ApiPort, Config.ApiUsbEndPoint, Config.RetryPeriod, Config.MaxEntries,
17
        Config.MaxRetries, Config.CacheFileName);
18

    
19

    
20
    private static NetworkDetection _netDetect = new(Config.ApiPort, Config.DetectionPeriod);
21
    private static ProcessDetection _procDetect = new(Config.T32ProcessName, Config.DetectionPeriod);
22

    
23

    
24
    // Main Method
25
    public static async Task Main() {
26
        var apiClientThread = new Thread(DefaultApiClient.Run) {
27
            IsBackground = true
28
        };
29
        DefaultLogger.Debug("Main -> starting the ApiClient");
30
        apiClientThread.Start();
31

    
32
        var admin = IsAdministrator();
33
        DefaultLogger.Debug($"Is program executed with admin rights? {admin}");
34

    
35
        var networkTread = new Thread(_netDetect.RunPeriodicDetection);
36
        networkTread.Start();
37
        if (admin) {
38
            _procDetect.RegisterProcessListeners();
39

    
40
        } else {
41
            var processThread = new Thread(_procDetect.RunPeriodicDetection);
42
            processThread.Start();
43
            processThread.Join();
44
        }
45

    
46
        networkTread.Join();
47

    
48
        DefaultLogger.Debug("Main -> stopping the ApiClient");
49
        DefaultApiClient.Stop();
50
        apiClientThread.Join();
51
        DefaultLogger.Debug("Main -> finished");
52
    }
53

    
54
    public static bool IsAdministrator() {
55
        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
56
            var identity = WindowsIdentity.GetCurrent();
57
            var principal = new WindowsPrincipal(identity);
58
            return principal.IsInRole(WindowsBuiltInRole.Administrator);
59

    
60
        } else {
61
            return false;
62
        }
63
    }
64
}
(2-2/3)