移除资源定义并增强客户端处理逻辑

在 `MainWindow.xaml` 中移除了 `TimeFormatConverter` 资源定义。
在 `Program.cs` 中添加了 `using System.Text;` 以支持字符串编码。
在 `OpenUser_db` 方法中移除了注释,保持逻辑不变。
在 `HandleClient` 方法中增加了对接收到的字节流的处理,检测 HTTP 请求并拒绝连接。
This commit is contained in:
绪山七寻 2025-06-22 00:28:57 +08:00
parent 5f042446b4
commit 1e80f28972
2 changed files with 14 additions and 4 deletions

View File

@ -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">
<Window.Resources>
<data:TimeFormatConverter x:Key="TimeConverter"/>
</Window.Resources>
<Grid>
<materialDesign:Card>
<TabControl x:Name="TabControl" VerticalContentAlignment="Bottom" materialDesign:ColorZoneAssist.Mode="PrimaryMid" Style="{StaticResource MaterialDesignNavigationRailTabControl}">

View File

@ -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)