Skip to content

fix: 修复 BlazeSocket 连接关闭时的 SSL 错误 #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion utils/bf1/bf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,10 @@ async def get_player_list(
try:
response = await blaze_socket.send(packet)
except TimeoutError:
await blaze_socket.close()
try:
await blaze_socket.close()
except Exception as e:
logger.error(f"关闭连接时出错: {e}")
logger.error("Blaze后端超时!")
return "Blaze后端超时!"
if origin:
Expand Down
23 changes: 19 additions & 4 deletions utils/bf1/blaze/BlazeClient.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import random

from utils.bf1.blaze.BlazeSocket import BlazeSocket, BlazeServerREQ
from utils.bf1.blaze.BlazeSocket import BlazeServerREQ, BlazeSocket


class BlazeClient:
Expand Down Expand Up @@ -38,7 +38,12 @@ async def get_socket_for_pid(self, pid=None) -> BlazeSocket | None:
client = self.clients_by_pid[pid]
if client.connect:
return client
await client.close()
try:
await client.close()
except Exception as e:
from loguru import logger

logger.error(f"关闭客户端连接时出错: {e}")
del self.clients_by_pid[pid]

new_client = BlazeClient()
Expand All @@ -51,14 +56,24 @@ async def get_socket_for_pid(self, pid=None) -> BlazeSocket | None:
return None

async def close_all(self):
from loguru import logger

for client in self.clients_by_pid.values():
await client.close()
try:
await client.close()
except Exception as e:
logger.error(f"关闭客户端连接时出错: {e}")
self.clients_by_pid.clear()

async def remove_client(self, pid: str):
if pid in self.clients_by_pid:
client = self.clients_by_pid[pid]
await client.close()
try:
await client.close()
except Exception as e:
from loguru import logger

logger.error(f"关闭客户端连接时出错: {e}")
del self.clients_by_pid[pid]


Expand Down
11 changes: 9 additions & 2 deletions utils/bf1/blaze/BlazeSocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,15 @@ async def connect_to_server(self):
async def close(self):
# 关闭连接
self.connect = False
self.writer.close()
await self.writer.wait_closed()
if self.writer:
self.writer.close()
try:
await self.writer.wait_closed()
except ssl.SSLError as e:
# 处理SSL错误:APPLICATION_DATA_AFTER_CLOSE_NOTIFY
logger.warning(f"SSL关闭连接时出现错误 (可忽略): {e}")
except Exception as e:
logger.error(f"关闭连接时出现错误: {e}")
logger.success(f"已断开与Blaze服务器 {self.host}:{self.port} 的连接")

async def keepalive(self):
Expand Down