diff --git a/MasstransferCommon/MasstransferCommon.csproj b/MasstransferCommon/MasstransferCommon.csproj index a046f9d..05a69b5 100644 --- a/MasstransferCommon/MasstransferCommon.csproj +++ b/MasstransferCommon/MasstransferCommon.csproj @@ -7,16 +7,13 @@ + - - - - diff --git a/MasstransferCommon/Model/Constant/ProcessTopics.cs b/MasstransferCommon/Model/Constant/ProcessTopics.cs new file mode 100644 index 0000000..6f07f4e --- /dev/null +++ b/MasstransferCommon/Model/Constant/ProcessTopics.cs @@ -0,0 +1,14 @@ +namespace MasstransferCommon.Model.Constant; + +public class ProcessTopics +{ + /// + /// 证书更新事件 + /// + public const string LicenseUpdateEvent = "LicenseUpdateEvent"; + + /// + /// 证书更新事件反馈 + /// + public const string LicenseUpdateEventFeedback = "LicenseUpdateEventFeedback"; +} \ No newline at end of file diff --git a/MasstransferCommon/Model/Constant/Topics.cs b/MasstransferCommon/Model/Constant/Topics.cs new file mode 100644 index 0000000..335ef0f --- /dev/null +++ b/MasstransferCommon/Model/Constant/Topics.cs @@ -0,0 +1,17 @@ +namespace MasstransferCommon.Model.Constant; + +/// +/// 这里保存所有的 Mqtt Topic +/// +public class Topics +{ + /// + /// 更新证书事件 + /// + public const string UpdateLicenseEvent = "UpdateLicenseEvent"; + + /// + /// 更新证书事件反馈 + /// + public const string UpdateLicenseEventFeedback = "UpdateLicenseEventFeedback"; +} \ No newline at end of file diff --git a/MasstransferCommon/Utils/CertUtil.cs b/MasstransferCommon/Utils/CertUtil.cs new file mode 100644 index 0000000..d62a278 --- /dev/null +++ b/MasstransferCommon/Utils/CertUtil.cs @@ -0,0 +1,29 @@ +using System.Security.Cryptography; +using System.Security.Cryptography.X509Certificates; + +namespace MasstransferCommon.Utils; + +public class CertUtil +{ + public static void ConvertCertToPfx(string certPath, string keyPath, string pfxPath, string password) + { + // 加载证书文件(.cer 或 .crt) + var cert = new X509Certificate2(certPath); + + // 加载私钥文件(.key) + var keyContent = File.ReadAllText(keyPath); + + // 从PEM格式的私钥中提取RSA密钥 + var privateKey = RSA.Create(); + privateKey.ImportFromPem(keyContent.ToCharArray()); + + // 创建带有私钥的X509Certificate2对象 + var certWithPrivateKey = cert.CopyWithPrivateKey(privateKey); + + // 导出为PFX格式 + var pfxData = certWithPrivateKey.Export(X509ContentType.Pfx, password); + + // 保存PFX文件 + File.WriteAllBytes(pfxPath, pfxData); + } +} \ No newline at end of file diff --git a/MasstransferExporter/License/LicenseService.cs b/MasstransferExporter/License/LicenseService.cs new file mode 100644 index 0000000..0a882e5 --- /dev/null +++ b/MasstransferExporter/License/LicenseService.cs @@ -0,0 +1,40 @@ +using MasstransferCommon.Model.Constant; +using MasstransferCommunicate.Process.Service; +using MasstransferInfrastructure.Mqtt.Client; + +namespace MasstransferExporter.License; + +/// +/// 证书业务 +/// +public class LicenseService +{ + /// + /// 启动监听证书更新事件 + /// + public static async Task ListenLicenseUpdateEvent() + { + await MessageQueueHelper.Subscribe(Topics.UpdateLicenseEvent, HandleUpdateLicenseEvent); + } + + + /// + /// 处理接收到的证书更新事件 + /// + private static async Task HandleUpdateLicenseEvent(string topic, string license) + { + ProcessCommunicator.Subscribe(ProcessTopics.LicenseUpdateEventFeedback, HandleUpdateLicenseEventFeedback); + + await ProcessCommunicator.Send(ProcessTopics.LicenseUpdateEvent, license); + } + + /// + /// 处理证书更新反馈 + /// + /// + /// + private static async Task HandleUpdateLicenseEventFeedback(string topic, string result) + { + await MessageQueueHelper.Publish(Topics.UpdateLicenseEventFeedback, result); + } +} \ No newline at end of file diff --git a/MasstransferExporter/MasstransferExporter.csproj b/MasstransferExporter/MasstransferExporter.csproj index ff8ac27..500d4d7 100644 --- a/MasstransferExporter/MasstransferExporter.csproj +++ b/MasstransferExporter/MasstransferExporter.csproj @@ -8,6 +8,9 @@ + + + diff --git a/MasstransferExporter/Program.cs b/MasstransferExporter/Program.cs index bd0d441..4503ab9 100644 --- a/MasstransferExporter/Program.cs +++ b/MasstransferExporter/Program.cs @@ -1,19 +1,8 @@ -using MasstransferCommunicate.Process.Client; -using MasstransferCommunicate.Process.Service; - -class Program +class Program { - static async Task Main() + static void Main() { - await ProcessCommunicator.Connect(); - - Console.WriteLine("输入要发送的消息 (输入 'exit' 退出):"); - - while (true) - { - string messageToSend = Console.ReadLine(); - if (messageToSend == "exit") break; - await ProcessCommunicator.Send("ClientMessage", messageToSend); - } + Console.WriteLine("按任意键退出"); + Console.ReadKey(); } } \ No newline at end of file diff --git a/MasstransferExporter/Stat/HeartbeatExporter.cs b/MasstransferExporter/StatExporter/HeartbeatExporter.cs similarity index 100% rename from MasstransferExporter/Stat/HeartbeatExporter.cs rename to MasstransferExporter/StatExporter/HeartbeatExporter.cs diff --git a/MasstransferExporter/Stat/Model/CpuLoad.cs b/MasstransferExporter/StatExporter/Model/CpuLoad.cs similarity index 100% rename from MasstransferExporter/Stat/Model/CpuLoad.cs rename to MasstransferExporter/StatExporter/Model/CpuLoad.cs diff --git a/MasstransferExporter/Stat/Model/DiskStat.cs b/MasstransferExporter/StatExporter/Model/DiskStat.cs similarity index 100% rename from MasstransferExporter/Stat/Model/DiskStat.cs rename to MasstransferExporter/StatExporter/Model/DiskStat.cs diff --git a/MasstransferExporter/Stat/Model/MemoryStat.cs b/MasstransferExporter/StatExporter/Model/MemoryStat.cs similarity index 100% rename from MasstransferExporter/Stat/Model/MemoryStat.cs rename to MasstransferExporter/StatExporter/Model/MemoryStat.cs diff --git a/MasstransferExporter/Stat/Model/NetworkStat.cs b/MasstransferExporter/StatExporter/Model/NetworkStat.cs similarity index 100% rename from MasstransferExporter/Stat/Model/NetworkStat.cs rename to MasstransferExporter/StatExporter/Model/NetworkStat.cs diff --git a/MasstransferExporter/Stat/Model/OSInfo.cs b/MasstransferExporter/StatExporter/Model/OSInfo.cs similarity index 100% rename from MasstransferExporter/Stat/Model/OSInfo.cs rename to MasstransferExporter/StatExporter/Model/OSInfo.cs diff --git a/MasstransferExporter/Stat/Model/SystemStat.cs b/MasstransferExporter/StatExporter/Model/SystemStat.cs similarity index 100% rename from MasstransferExporter/Stat/Model/SystemStat.cs rename to MasstransferExporter/StatExporter/Model/SystemStat.cs diff --git a/MasstransferExporter/Stat/SystemStatExporter.cs b/MasstransferExporter/StatExporter/SystemStatExporter.cs similarity index 100% rename from MasstransferExporter/Stat/SystemStatExporter.cs rename to MasstransferExporter/StatExporter/SystemStatExporter.cs diff --git a/MasstransferInfrastructure/Mqtt/Client/MessageQueueHelper.cs b/MasstransferInfrastructure/Mqtt/Client/MessageQueueHelper.cs index f645ff5..fb07a17 100644 --- a/MasstransferInfrastructure/Mqtt/Client/MessageQueueHelper.cs +++ b/MasstransferInfrastructure/Mqtt/Client/MessageQueueHelper.cs @@ -87,7 +87,7 @@ public class MessageQueueHelper var methodInfo = subscriber.Method; var parameters = methodInfo.GetParameters(); if (parameters.Length != 2) continue; - var type = parameters[1].ParameterType; + var type = parameters[1].ParameterType; // 通知订阅者 subscriber.DynamicInvoke(topic, JsonUtil.FromJson(type, message)); } diff --git a/MasstransferInfrastructure/Process/Service/ProcessCommunicator.cs b/MasstransferInfrastructure/Process/Service/ProcessCommunicator.cs index 8d0580b..5bb369b 100644 --- a/MasstransferInfrastructure/Process/Service/ProcessCommunicator.cs +++ b/MasstransferInfrastructure/Process/Service/ProcessCommunicator.cs @@ -10,16 +10,16 @@ namespace MasstransferCommunicate.Process.Service; /// public class ProcessCommunicator { - private static readonly Dictionary>> Subscribers = new(); + private static readonly Dictionary> Subscribers = new(); private static ProcessHelper? _helper; /// - /// 连接到服务端 + /// 启动连接 /// public static async Task Connect() { - _helper = await ProcessHelper.CreateClient("Masstransfer"); + _helper = await ProcessHelper.CreateServer("Masstransfer"); _helper.MessageReceived += HandleMessageReceived; } @@ -29,7 +29,7 @@ public class ProcessCommunicator /// /// /// - public static Task Subscribe(string topic, Action @delegate) + public static void Subscribe(string topic, Delegate @delegate) { if (!Subscribers.ContainsKey(topic)) { @@ -37,7 +37,6 @@ public class ProcessCommunicator } Subscribers[topic].Add(@delegate); - return Task.FromResult(true); } /// @@ -59,23 +58,24 @@ public class ProcessCommunicator { if (message == null) return; - Console.WriteLine($"收到来自服务端的消息: {message}"); - var dictionary = JsonUtil.ToDictionary(message); if (dictionary == null) return; var topic = dictionary["Topic"] as string; var data = dictionary["Data"]; - if (!Subscribers.TryGetValue(topic, out var subscribers)) return; foreach (var subscriber in subscribers) { try { + var methodInfo = subscriber.Method; + var parameters = methodInfo.GetParameters(); + if (parameters.Length != 2) continue; + var type = parameters[1].ParameterType; // 通知订阅者 - subscriber(JsonUtil.ToJson(data)); + subscriber.DynamicInvoke(topic, JsonUtil.FromJson(type, message)); } catch (Exception exception) {