-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
653 lines (577 loc) · 23.8 KB
/
Copy pathProgram.cs
File metadata and controls
653 lines (577 loc) · 23.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
// Copyright (c) 2026 llz121517
// Licensed under the Apache License, Version 2.0
using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
namespace AstralStatusReporter
{
// Win32 API 导入
public static class Win32
{
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder sb, int max);
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
}
// 配置类
public class Config
{
public string ApiEndpoint { get; set; } = "http://localhost:8000/api/v1/report";
public int SleepInterval { get; set; } = 30;
public string AutorunName { get; set; } = "AstralStatusReporter";
public string EnvAesKeyName { get; set; } = "ASTRAL_AES_KEY";
}
// 进程信息类
public class ProcessInfo
{
public string ProcessName { get; set; }
public string ProcessRealName { get; set; }
public string Description { get; set; }
public string WindowClass { get; set; }
public string Error { get; set; }
}
// 上报数据类
public class ReportPayload
{
public string hostname { get; set; }
public long timestamp { get; set; }
public int interval { get; set; }
public string status { get; set; } = "online";
public string windowTitle { get; set; }
public string windowClass { get; set; }
public string processName { get; set; }
public string processRealName { get; set; }
public string description { get; set; }
public string error { get; set; }
}
// 主程序
public class Program
{
private static Config _config;
private static string _scriptDir;
private static string _aesKey;
private static bool _autorunWarnReported = false;
private static string _initialError = null;
private static bool _encryptFailedOnce = false;
private static HttpClient _httpClient;
[STAThread]
public static void Main(string[] args)
{
// 判断运行模式:开发模式显示控制台,生产模式隐藏
bool isDevelopment = Debugger.IsAttached ||
args.Contains("--debug") ||
args.Contains("-d");
if (!isDevelopment)
{
HideConsoleWindow();
}
// 强制 TLS 1.2
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Console.WriteLine("\n=== Astral 状态上报客户端 启动 ===");
// 初始化路径
_scriptDir = AppDomain.CurrentDomain.BaseDirectory;
// 配置加载错误将被捕获并记录
try
{
LoadConfig();
}
catch (Exception ex)
{
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] WARN 配置加载异常: {ex.Message}");
_config = new Config(); // 使用默认配置
}
// AES密钥加载错误将被捕获并记录
try
{
LoadAesKey();
}
catch (Exception ex)
{
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] WARN AES密钥加载异常: {ex.Message}");
_aesKey = null;
}
// 自启动设置错误被捕获并存储
try
{
_initialError = SetAutorun();
}
catch (Exception ex)
{
_initialError = $"自启动设置异常: {ex.Message}";
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] ERROR {_initialError}");
}
// 初始化HTTP客户端
_httpClient = new HttpClient();
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] INFO 当前上报间隔时间: {_config.SleepInterval} 秒");
try
{
// 无限循环上报
while (true)
{
string windowTitle = null;
string windowClass = null;
string runtimeError = null;
// 窗口信息获取错误被捕获并记录
try
{
var windowInfo = GetActiveWindowInfo();
windowTitle = windowInfo.Item1;
windowClass = windowInfo.Item2;
}
catch (Exception ex)
{
runtimeError = $"窗口信息获取失败: {ex.Message}";
}
// 进程信息获取错误被捕获并记录
ProcessInfo procInfo;
try
{
procInfo = GetFocusProcessInfo();
if (!string.IsNullOrEmpty(procInfo.Error))
{
runtimeError = AppendError(runtimeError, procInfo.Error);
}
}
catch (Exception ex)
{
procInfo = new ProcessInfo();
runtimeError = AppendError(runtimeError, $"进程信息异常: {ex.Message}");
}
// 确保所有非致命错误都被包含
string reportError = null;
if (!string.IsNullOrEmpty(_initialError) && !_autorunWarnReported)
{
reportError = _initialError;
_autorunWarnReported = true;
}
else
{
reportError = runtimeError;
}
// 构建JSON payload
var payload = new ReportPayload
{
hostname = Environment.MachineName,
timestamp = DateTimeOffset.Now.ToUnixTimeSeconds(),
interval = _config.SleepInterval,
status = "online",
windowTitle = windowTitle,
windowClass = windowClass,
processName = procInfo.ProcessName,
processRealName = procInfo.ProcessRealName,
description = procInfo.Description,
error = reportError // 所有非致命错误上报字段
};
string jsonBody = Newtonsoft.Json.JsonConvert.SerializeObject(payload);
// 加密失败 - 无法通信,只打印控制台
string encryptedBody = ProtectJsonAes(jsonBody);
if (string.IsNullOrEmpty(encryptedBody))
{
if (!_encryptFailedOnce)
{
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] FAIL 首次加密失败,后续将不再重复告警");
_encryptFailedOnce = true;
}
System.Threading.Thread.Sleep(_config.SleepInterval * 1000);
continue; // 跳过本次上报
}
// 网络请求失败 - 无法通信,只打印控制台
try
{
var content = new StringContent(encryptedBody, Encoding.UTF8, "text/plain");
var response = _httpClient.PostAsync(_config.ApiEndpoint, content).Result;
response.EnsureSuccessStatusCode();
string displayTitle = !string.IsNullOrEmpty(windowTitle) ? windowTitle : "NULL";
string displayClass = !string.IsNullOrEmpty(windowClass) ? windowClass : "NULL";
string displayErr = !string.IsNullOrEmpty(reportError) ? reportError : "None";
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Okay! 窗口: {displayTitle} | 类名: {displayClass} | 错误: {displayErr}");
string displayProcName = !string.IsNullOrEmpty(procInfo.ProcessName) ? procInfo.ProcessName : "NULL";
string displayProcReal = !string.IsNullOrEmpty(procInfo.ProcessRealName) ? procInfo.ProcessRealName : "NULL";
string displayDesc = !string.IsNullOrEmpty(procInfo.Description) ? procInfo.Description : "NULL";
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Okay! 进程: {displayProcName} | {displayProcReal} | {displayDesc}");
Console.WriteLine();
}
catch (Exception ex)
{
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] FAIL 网络请求失败: {ex.Message}");
// 继续下一次循环
}
System.Threading.Thread.Sleep(_config.SleepInterval * 1000);
}
}
finally
{
// 释放 HttpClient 资源
_httpClient?.Dispose();
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] INFO 程序退出,资源已释放");
}
}
// 追加错误信息
private static string AppendError(string existing, string newError)
{
if (string.IsNullOrEmpty(existing))
return newError;
return $"{existing}; {newError}";
}
// 隐藏控制台窗口
[DllImport("kernel32.dll")]
private static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private const int SW_HIDE = 0;
private const int SW_SHOW = 5;
private static void HideConsoleWindow()
{
var handle = GetConsoleWindow();
if (handle != IntPtr.Zero)
{
ShowWindow(handle, SW_HIDE);
}
}
// 获取当前活动窗口标题和类名
private static Tuple<string, string> GetActiveWindowInfo()
{
IntPtr hwnd = Win32.GetForegroundWindow();
if (hwnd == IntPtr.Zero)
{
return Tuple.Create<string, string>(null, null);
}
string title = null;
string className = null;
try
{
// 分别捕获标题和类名的错误
var titleSb = new StringBuilder(256);
int titleLen = Win32.GetWindowText(hwnd, titleSb, titleSb.Capacity);
if (titleLen > 0)
{
title = titleSb.ToString();
}
}
catch (Exception ex)
{
throw new Exception($"窗口标题获取失败: {ex.Message}");
}
try
{
var classSb = new StringBuilder(256);
int classLen = Win32.GetClassName(hwnd, classSb, classSb.Capacity);
if (classLen > 0)
{
className = classSb.ToString();
}
}
catch (Exception ex)
{
throw new Exception($"窗口类名获取失败: {ex.Message}");
}
return Tuple.Create(title, className);
}
// 获取当前进程信息
private static ProcessInfo GetFocusProcessInfo()
{
IntPtr hwnd = Win32.GetForegroundWindow();
if (hwnd == IntPtr.Zero)
{
return new ProcessInfo
{
ProcessName = null,
ProcessRealName = null,
Description = null,
WindowClass = null,
Error = null
};
}
uint focusPid = 0;
Win32.GetWindowThreadProcessId(hwnd, out focusPid);
if (focusPid == 0)
{
return new ProcessInfo
{
ProcessName = null,
ProcessRealName = null,
Description = null,
WindowClass = null,
Error = null
};
}
try
{
using (var proc = Process.GetProcessById((int)focusPid))
{
string name = proc.ProcessName;
string realName = proc.ProcessName;
string desc = null;
string winClass = null;
string error = null;
// MainModule 可能为 null
if (proc.MainModule != null)
{
try
{
if (proc.MainModule.FileVersionInfo != null &&
!string.IsNullOrWhiteSpace(proc.MainModule.FileVersionInfo.FileDescription))
{
desc = proc.MainModule.FileVersionInfo.FileDescription;
}
}
catch (Exception ex)
{
error = AppendError(error, $"文件描述获取失败: {ex.Message}");
}
}
else
{
error = AppendError(error, "MainModule 为空");
}
// 分别捕获窗口类名的错误
try
{
var classSb = new StringBuilder(256);
int classLen = Win32.GetClassName(hwnd, classSb, classSb.Capacity);
if (classLen > 0)
{
winClass = classSb.ToString();
}
}
catch (Exception ex)
{
error = AppendError(error, $"窗口类名获取失败: {ex.Message}");
}
return new ProcessInfo
{
ProcessName = name,
ProcessRealName = realName,
Description = desc,
WindowClass = winClass,
Error = error
};
}
}
catch (ArgumentException)
{
// 进程不存在
return new ProcessInfo { Error = "进程不存在" };
}
catch (InvalidOperationException)
{
// 进程已退出
return new ProcessInfo { Error = "进程已退出" };
}
catch (Win32Exception ex)
{
// 访问权限问题
return new ProcessInfo { Error = $"进程访问失败: {ex.Message}" };
}
catch (Exception ex)
{
// 其他异常
return new ProcessInfo { Error = $"进程信息获取失败: {ex.Message}" };
}
}
// 加载INI配置
private static void LoadConfig()
{
_config = new Config();
string iniPath = Path.Combine(_scriptDir, "config.ini");
if (!File.Exists(iniPath))
{
GenerateDefaultConfig(iniPath);
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] INFO 配置文件不存在,已生成默认配置: {iniPath}");
return;
}
try
{
var lines = File.ReadAllLines(iniPath);
foreach (var line in lines)
{
var trimmed = line.Trim();
if (string.IsNullOrEmpty(trimmed) || trimmed.StartsWith("#") || trimmed.StartsWith(";"))
continue;
var parts = trimmed.Split(new[] { '=' }, 2);
if (parts.Length != 2)
continue;
var key = parts[0].Trim();
var value = parts[1].Trim();
switch (key.ToLower())
{
case "apiendpoint":
_config.ApiEndpoint = value;
break;
case "sleepinterval":
if (int.TryParse(value, out int interval))
_config.SleepInterval = interval;
break;
case "autorunname":
_config.AutorunName = value;
break;
case "envaeskeyname":
_config.EnvAesKeyName = value;
break;
}
}
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] INFO 配置加载完成");
}
catch (Exception ex)
{
throw new Exception($"配置文件读取失败: {ex.Message}");
}
}
// 生成默认配置文件
private static void GenerateDefaultConfig(string path)
{
var sb = new StringBuilder();
sb.AppendLine("# AstralStatusReporter 配置文件");
sb.AppendLine("# 后端接收接口地址");
sb.AppendLine($"apiEndpoint={_config.ApiEndpoint}");
sb.AppendLine();
sb.AppendLine("# 上报间隔(秒)");
sb.AppendLine($"sleepInterval={_config.SleepInterval}");
sb.AppendLine();
sb.AppendLine("# 注册表自启名称");
sb.AppendLine($"autorunName={_config.AutorunName}");
sb.AppendLine();
sb.AppendLine("# AES 密钥环境变量名称");
sb.AppendLine($"envAesKeyName={_config.EnvAesKeyName}");
sb.AppendLine();
sb.AppendLine("# 注意:AES密钥不在此文件中配置,请使用.env文件或系统环境变量");
File.WriteAllText(path, sb.ToString(), Encoding.UTF8);
}
// 加载.env文件
private static Dictionary<string, string> LoadEnvFile()
{
var envVars = new Dictionary<string, string>();
string envPath = Path.Combine(_scriptDir, ".env");
if (!File.Exists(envPath))
{
return envVars;
}
try
{
var lines = File.ReadAllLines(envPath);
foreach (var line in lines)
{
var trimmed = line.Trim();
if (string.IsNullOrEmpty(trimmed) || trimmed.StartsWith("#") || trimmed.StartsWith(";"))
continue;
var parts = trimmed.Split(new[] { '=' }, 2);
if (parts.Length != 2)
continue;
var key = parts[0].Trim();
var value = parts[1].Trim();
envVars[key] = value;
}
}
catch (Exception ex)
{
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] WARN .env文件读取失败: {ex.Message}");
}
return envVars;
}
// 加载AES密钥
private static void LoadAesKey()
{
// 优先从.env文件加载
var envVars = LoadEnvFile();
if (envVars.ContainsKey(_config.EnvAesKeyName))
{
_aesKey = envVars[_config.EnvAesKeyName];
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] INFO AES 密钥已从 .env 加载");
return;
}
// 其次从系统环境变量加载
_aesKey = Environment.GetEnvironmentVariable(_config.EnvAesKeyName, EnvironmentVariableTarget.User);
if (!string.IsNullOrEmpty(_aesKey))
{
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] INFO AES 密钥已从系统环境变量加载");
return;
}
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] WARN 未找到 AES 密钥");
}
// 设置自启
private static string SetAutorun()
{
try
{
string registryPath = @"Software\Microsoft\Windows\CurrentVersion\Run";
string exePath = Environment.ProcessPath;
string expectedCommand = $"\"{exePath}\"";
using (var key = Registry.CurrentUser.OpenSubKey(registryPath, true))
{
if (key == null)
{
return "无法打开注册表";
}
var currentValue = key.GetValue(_config.AutorunName) as string;
if (string.IsNullOrEmpty(currentValue))
{
key.SetValue(_config.AutorunName, expectedCommand);
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] INFO 自启项已安装");
return null;
}
if (currentValue != expectedCommand)
{
key.SetValue(_config.AutorunName, expectedCommand);
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] WARN 自启命令不匹配,已自动修复");
return "自启项参数异常,已重建";
}
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] INFO 自启配置正常");
return null;
}
}
catch (Exception ex)
{
return $"自启设置失败: {ex.Message}";
}
}
// AES加密
private static string ProtectJsonAes(string plainText)
{
try
{
if (string.IsNullOrEmpty(_aesKey))
{
throw new Exception("AES 密钥未加载");
}
byte[] keyBytes = Convert.FromBase64String(_aesKey);
if (keyBytes.Length != 16)
{
throw new Exception($"AES 密钥必须为 128 位(16 字节),当前解码长度为 {keyBytes.Length} 字节");
}
using (var aes = Aes.Create())
{
aes.Key = keyBytes;
aes.GenerateIV();
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
using (var encryptor = aes.CreateEncryptor())
{
byte[] data = Encoding.UTF8.GetBytes(plainText);
byte[] encrypted = encryptor.TransformFinalBlock(data, 0, data.Length);
byte[] result = new byte[aes.IV.Length + encrypted.Length];
Array.Copy(aes.IV, result, aes.IV.Length);
Array.Copy(encrypted, 0, result, aes.IV.Length, encrypted.Length);
return Convert.ToBase64String(result);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] AES 加密失败: {ex.Message}");
return null;
}
}
}
}