添加证书更新相关业务功能
This commit is contained in:
parent
4cbee59acb
commit
a4d411bcb7
|
@ -7,16 +7,13 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="BouncyCastle.NetCore" Version="2.2.1" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
|
||||||
<PackageReference Include="System.Management" Version="8.0.0"/>
|
<PackageReference Include="System.Management" Version="8.0.0"/>
|
||||||
<PackageReference Include="Serilog" Version="4.0.0"/>
|
<PackageReference Include="Serilog" Version="4.0.0"/>
|
||||||
<PackageReference Include="Serilog.Sinks.File" Version="5.0.1-dev-00972"/>
|
<PackageReference Include="Serilog.Sinks.File" Version="5.0.1-dev-00972"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="Model\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Content Include="bin\Debug\net7.0\MasstransferCommon.deps.json" />
|
<Content Include="bin\Debug\net7.0\MasstransferCommon.deps.json" />
|
||||||
<Content Include="bin\Debug\net7.0\MasstransferCommon.dll" />
|
<Content Include="bin\Debug\net7.0\MasstransferCommon.dll" />
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
namespace MasstransferCommon.Model.Constant;
|
||||||
|
|
||||||
|
public class ProcessTopics
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 证书更新事件
|
||||||
|
/// </summary>
|
||||||
|
public const string LicenseUpdateEvent = "LicenseUpdateEvent";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 证书更新事件反馈
|
||||||
|
/// </summary>
|
||||||
|
public const string LicenseUpdateEventFeedback = "LicenseUpdateEventFeedback";
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
namespace MasstransferCommon.Model.Constant;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 这里保存所有的 Mqtt Topic
|
||||||
|
/// </summary>
|
||||||
|
public class Topics
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 更新证书事件
|
||||||
|
/// </summary>
|
||||||
|
public const string UpdateLicenseEvent = "UpdateLicenseEvent";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 更新证书事件反馈
|
||||||
|
/// </summary>
|
||||||
|
public const string UpdateLicenseEventFeedback = "UpdateLicenseEventFeedback";
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
using MasstransferCommon.Model.Constant;
|
||||||
|
using MasstransferCommunicate.Process.Service;
|
||||||
|
using MasstransferInfrastructure.Mqtt.Client;
|
||||||
|
|
||||||
|
namespace MasstransferExporter.License;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 证书业务
|
||||||
|
/// </summary>
|
||||||
|
public class LicenseService
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 启动监听证书更新事件
|
||||||
|
/// </summary>
|
||||||
|
public static async Task ListenLicenseUpdateEvent()
|
||||||
|
{
|
||||||
|
await MessageQueueHelper.Subscribe(Topics.UpdateLicenseEvent, HandleUpdateLicenseEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 处理接收到的证书更新事件
|
||||||
|
/// </summary>
|
||||||
|
private static async Task HandleUpdateLicenseEvent(string topic, string license)
|
||||||
|
{
|
||||||
|
ProcessCommunicator.Subscribe(ProcessTopics.LicenseUpdateEventFeedback, HandleUpdateLicenseEventFeedback);
|
||||||
|
|
||||||
|
await ProcessCommunicator.Send(ProcessTopics.LicenseUpdateEvent, license);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 处理证书更新反馈
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="topic"></param>
|
||||||
|
/// <param name="result"></param>
|
||||||
|
private static async Task HandleUpdateLicenseEventFeedback(string topic, string result)
|
||||||
|
{
|
||||||
|
await MessageQueueHelper.Publish(Topics.UpdateLicenseEventFeedback, result);
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,6 +8,9 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="BouncyCastle.NetCore" Version="2.2.1" />
|
||||||
|
<PackageReference Include="M2Mqtt" Version="4.3.0" />
|
||||||
|
<PackageReference Include="MQTTnet.Extensions.ManagedClient" Version="4.3.6.1152" />
|
||||||
<PackageReference Include="Serilog" Version="4.0.0" />
|
<PackageReference Include="Serilog" Version="4.0.0" />
|
||||||
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
|
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
|
||||||
<PackageReference Include="System.Diagnostics.Process" Version="4.3.0" />
|
<PackageReference Include="System.Diagnostics.Process" Version="4.3.0" />
|
||||||
|
|
|
@ -1,19 +1,8 @@
|
||||||
using MasstransferCommunicate.Process.Client;
|
class Program
|
||||||
using MasstransferCommunicate.Process.Service;
|
|
||||||
|
|
||||||
class Program
|
|
||||||
{
|
{
|
||||||
static async Task Main()
|
static void Main()
|
||||||
{
|
{
|
||||||
await ProcessCommunicator.Connect();
|
Console.WriteLine("按任意键退出");
|
||||||
|
Console.ReadKey();
|
||||||
Console.WriteLine("输入要发送的消息 (输入 'exit' 退出):");
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
string messageToSend = Console.ReadLine();
|
|
||||||
if (messageToSend == "exit") break;
|
|
||||||
await ProcessCommunicator.Send("ClientMessage", messageToSend);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -10,16 +10,16 @@ namespace MasstransferCommunicate.Process.Service;
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ProcessCommunicator
|
public class ProcessCommunicator
|
||||||
{
|
{
|
||||||
private static readonly Dictionary<string, List<Action<string>>> Subscribers = new();
|
private static readonly Dictionary<string, List<Delegate>> Subscribers = new();
|
||||||
|
|
||||||
private static ProcessHelper? _helper;
|
private static ProcessHelper? _helper;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 连接到服务端
|
/// 启动连接
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static async Task Connect()
|
public static async Task Connect()
|
||||||
{
|
{
|
||||||
_helper = await ProcessHelper.CreateClient("Masstransfer");
|
_helper = await ProcessHelper.CreateServer("Masstransfer");
|
||||||
_helper.MessageReceived += HandleMessageReceived;
|
_helper.MessageReceived += HandleMessageReceived;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ public class ProcessCommunicator
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="topic"></param>
|
/// <param name="topic"></param>
|
||||||
/// <param name="delegate"></param>
|
/// <param name="delegate"></param>
|
||||||
public static Task<bool> Subscribe<T>(string topic, Action<string> @delegate)
|
public static void Subscribe(string topic, Delegate @delegate)
|
||||||
{
|
{
|
||||||
if (!Subscribers.ContainsKey(topic))
|
if (!Subscribers.ContainsKey(topic))
|
||||||
{
|
{
|
||||||
|
@ -37,7 +37,6 @@ public class ProcessCommunicator
|
||||||
}
|
}
|
||||||
|
|
||||||
Subscribers[topic].Add(@delegate);
|
Subscribers[topic].Add(@delegate);
|
||||||
return Task.FromResult(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -59,23 +58,24 @@ public class ProcessCommunicator
|
||||||
{
|
{
|
||||||
if (message == null) return;
|
if (message == null) return;
|
||||||
|
|
||||||
Console.WriteLine($"收到来自服务端的消息: {message}");
|
|
||||||
|
|
||||||
var dictionary = JsonUtil.ToDictionary(message);
|
var dictionary = JsonUtil.ToDictionary(message);
|
||||||
if (dictionary == null) return;
|
if (dictionary == null) return;
|
||||||
|
|
||||||
var topic = dictionary["Topic"] as string;
|
var topic = dictionary["Topic"] as string;
|
||||||
var data = dictionary["Data"];
|
var data = dictionary["Data"];
|
||||||
|
|
||||||
|
|
||||||
if (!Subscribers.TryGetValue(topic, out var subscribers)) return;
|
if (!Subscribers.TryGetValue(topic, out var subscribers)) return;
|
||||||
|
|
||||||
foreach (var subscriber in subscribers)
|
foreach (var subscriber in subscribers)
|
||||||
{
|
{
|
||||||
try
|
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)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue