From 8b726b78b69003c861d9be11e7129afee964a698 Mon Sep 17 00:00:00 2001 From: root <295172551@qq.com> Date: Thu, 26 Jun 2025 01:48:18 +0800 Subject: [PATCH] 11 --- LTEMvcApp/Controllers/MessageController.cs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/LTEMvcApp/Controllers/MessageController.cs b/LTEMvcApp/Controllers/MessageController.cs index a1cdb15..605b80b 100644 --- a/LTEMvcApp/Controllers/MessageController.cs +++ b/LTEMvcApp/Controllers/MessageController.cs @@ -8,6 +8,8 @@ using System.Threading; using System.IO; using System.Text; using System.Linq; +using System.Security.Cryptography; +using System.Text; namespace LTEMvcApp.Controllers { @@ -44,6 +46,19 @@ namespace LTEMvcApp.Controllers return address; } + /// + /// 工具方法:获取address的唯一hash + /// + private static string GetAddressHash(string address) + { + using (var sha1 = SHA1.Create()) + { + var bytes = Encoding.UTF8.GetBytes(address); + var hashBytes = sha1.ComputeHash(bytes); + return BitConverter.ToString(hashBytes).Replace("-", "").Substring(0, 8); + } + } + /// /// SSE推送客户端消息流 /// @@ -75,10 +90,11 @@ namespace LTEMvcApp.Controllers int lastReceivedCount = 0; var cancellationToken = HttpContext.RequestAborted; - // 使用安全文件名 + // 使用安全文件名+唯一hash var safeAddress = SanitizeFileName(address); - var sentLogFilePath = Path.Combine(_logsDirectory, $"{safeAddress}_sent_messages.log"); - var receivedLogFilePath = Path.Combine(_logsDirectory, $"{safeAddress}_received_messages.log"); + var hash = GetAddressHash(address); + var sentLogFilePath = Path.Combine(_logsDirectory, $"{safeAddress}_{hash}_sent_messages.log"); + var receivedLogFilePath = Path.Combine(_logsDirectory, $"{safeAddress}_{hash}_received_messages.log"); while (!cancellationToken.IsCancellationRequested) {