Revize f281acac
Přidáno uživatelem Jakub Šilhavý před asi 2 roky(ů)
ld_client/LDClient/Program.cs | ||
---|---|---|
1 |
using System.Runtime.InteropServices; |
|
2 |
using System.Security.Principal; |
|
3 | 1 |
using LDClient.detection; |
4 | 2 |
using LDClient.network; |
5 | 3 |
using LDClient.utils; |
... | ... | |
15 | 13 |
public static ConfigLoader Config { get; } = new(); |
16 | 14 |
public static ALogger DefaultLogger { get; } = ALogger.Current; |
17 | 15 |
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 | 16 |
|
21 |
// Main Method |
|
17 |
private static readonly InfoFetcher InfoFetcher = new( |
|
18 |
5, |
|
19 |
1000, |
|
20 |
"output.txt" |
|
21 |
); |
|
22 |
|
|
22 | 23 |
public static int Main() { |
23 | 24 |
var exists = GetProcessesByName(Path.GetFileNameWithoutExtension(GetEntryAssembly()?.Location)).Length > 1; |
24 | 25 |
if (exists) { |
... | ... | |
35 | 36 |
Config.CacheFileName |
36 | 37 |
); |
37 | 38 |
|
39 |
IProcessDetection processProcessDetection = new ProcessProcessDetection( |
|
40 |
Config.T32ProcessName, |
|
41 |
Config.DetectionPeriod, |
|
42 |
InfoFetcher, |
|
43 |
DefaultApiClient |
|
44 |
); |
|
45 |
|
|
38 | 46 |
DefaultLogger.Debug("Main -> starting the ApiClient"); |
39 | 47 |
var apiClientThread = new Thread(DefaultApiClient.Run) { |
40 | 48 |
IsBackground = true |
41 | 49 |
}; |
42 | 50 |
apiClientThread.Start(); |
43 | 51 |
|
44 |
var admin = IsAdministrator(); |
|
45 |
DefaultLogger.Debug($"Is program executed with admin rights? {admin}"); |
|
52 |
var processThread = new Thread(processProcessDetection.RunPeriodicDetection) { |
|
53 |
IsBackground = true |
|
54 |
}; |
|
55 |
processThread.Start(); |
|
46 | 56 |
|
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(); |
|
57 |
while (true) { |
|
58 |
Thread.Sleep(10 * 1000); |
|
56 | 59 |
} |
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 | 60 |
return 0; |
66 | 61 |
} |
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 | 62 |
} |
Také k dispozici: Unified diff
re #9566 Added InfoFetcher.cs, modified some other files (a bit of refactoring)