From 1e80f289727423d0d87ad95929663b5e79a430b3 Mon Sep 17 00:00:00 2001 From: XuShanQiXun <3401460572@qq.com> Date: Sun, 22 Jun 2025 00:28:57 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E8=B5=84=E6=BA=90=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E5=B9=B6=E5=A2=9E=E5=BC=BA=E5=AE=A2=E6=88=B7=E7=AB=AF?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在 `MainWindow.xaml` 中移除了 `TimeFormatConverter` 资源定义。 在 `Program.cs` 中添加了 `using System.Text;` 以支持字符串编码。 在 `OpenUser_db` 方法中移除了注释,保持逻辑不变。 在 `HandleClient` 方法中增加了对接收到的字节流的处理,检测 HTTP 请求并拒绝连接。 --- chatclient/MainWindow.xaml | 3 --- chatserver/Program.cs | 15 ++++++++++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/chatclient/MainWindow.xaml b/chatclient/MainWindow.xaml index 144a7c0..a81236f 100644 --- a/chatclient/MainWindow.xaml +++ b/chatclient/MainWindow.xaml @@ -10,9 +10,6 @@ mc:Ignorable="d" Title="ChatWindow" Height="450" Width="800" MinHeight="240" MinWidth="380" Style="{StaticResource MaterialDesignWindow}" Closed="MainWindow_Closed" Loaded="MainWindow_Loaded"> - - - diff --git a/chatserver/Program.cs b/chatserver/Program.cs index 6ed120c..f83f425 100644 --- a/chatserver/Program.cs +++ b/chatserver/Program.cs @@ -9,6 +9,7 @@ using System.Text.Json; using System.Reflection; using static log4net.Appender.FileAppender; using System.IO.Compression; +using System.Text; [assembly: XmlConfigurator(ConfigFile = "config/log4net.config", Watch = true)] namespace chatserver @@ -50,7 +51,7 @@ namespace chatserver public static void OpenUser_db() { log.Info("正在打开数据库连接..."); - User_db = new SQLiteConnection("Data Source=ServerUser.db;Version=3;"); //没有数据库则自动创建 + User_db = new SQLiteConnection("Data Source=ServerUser.db;Version=3;"); User_db.Open(); InitializeTable(); log.Info("数据库连接已打开"); @@ -59,6 +60,18 @@ namespace chatserver { const int prefixSize = sizeof(int); byte[] prefixBuffer = new byte[prefixSize]; + int received = socket.Receive(prefixBuffer, 0, 4, SocketFlags.Peek); + if (received == 4) + { + string httpStart = Encoding.ASCII.GetString(prefixBuffer); + if (httpStart.StartsWith("GET") || httpStart.StartsWith("POST") || + httpStart.StartsWith("HEAD") || httpStart.StartsWith("HTTP")) + { + log.Warn("检测到HTTP请求,拒绝连接"); + socket.Close(); + return; + } + } try { while (true)