mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-25 16:43:55 +08:00
refactor(connection): 合并旧逻辑新的业务逻辑代码
This commit is contained in:
@@ -1,13 +1,42 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import websockets
|
||||
from typing import Dict, Any
|
||||
from config.logger import setup_logging
|
||||
from core.services.connection_service import ConnectionService
|
||||
from core.transport.websocket_transport import WebSocketTransport
|
||||
from config.config_loader import get_config_from_api
|
||||
from config.config_loader import get_config_from_api_async
|
||||
from core.utils.util import check_vad_update, check_asr_update
|
||||
from core.auth import AuthMiddleware, AuthenticationError
|
||||
|
||||
|
||||
class SuppressInvalidHandshakeFilter(logging.Filter):
|
||||
"""过滤掉无效握手错误日志(如HTTPS访问WS端口)"""
|
||||
|
||||
def filter(self, record):
|
||||
msg = record.getMessage()
|
||||
suppress_keywords = [
|
||||
"opening handshake failed",
|
||||
"did not receive a valid HTTP request",
|
||||
"connection closed while reading HTTP request",
|
||||
"line without CRLF",
|
||||
]
|
||||
return not any(keyword in msg for keyword in suppress_keywords)
|
||||
|
||||
|
||||
def _setup_websockets_logger():
|
||||
"""配置 websockets 相关的所有 logger,过滤无效握手错误"""
|
||||
filter_instance = SuppressInvalidHandshakeFilter()
|
||||
for logger_name in ["websockets", "websockets.server", "websockets.client"]:
|
||||
ws_logger = logging.getLogger(logger_name)
|
||||
ws_logger.addFilter(filter_instance)
|
||||
|
||||
|
||||
_setup_websockets_logger()
|
||||
|
||||
|
||||
logger = setup_logging()
|
||||
TAG = __name__
|
||||
|
||||
|
||||
class NewWebSocketServer:
|
||||
@@ -26,6 +55,9 @@ class NewWebSocketServer:
|
||||
|
||||
# 活跃连接管理
|
||||
self.active_connections = set()
|
||||
|
||||
# 认证中间件
|
||||
self.auth_middleware = AuthMiddleware(config)
|
||||
|
||||
async def start(self):
|
||||
"""启动WebSocket服务器"""
|
||||
@@ -33,7 +65,7 @@ class NewWebSocketServer:
|
||||
host = server_config.get("ip", "0.0.0.0")
|
||||
port = int(server_config.get("port", 8000))
|
||||
|
||||
logger.info(f"启动新架构WebSocket服务器: {host}:{port}")
|
||||
logger.bind(tag=TAG).info(f"启动新架构WebSocket服务器: {host}:{port}")
|
||||
|
||||
async with websockets.serve(
|
||||
self._handle_connection,
|
||||
@@ -41,13 +73,29 @@ class NewWebSocketServer:
|
||||
port,
|
||||
process_request=self._http_response
|
||||
):
|
||||
logger.info("WebSocket服务器启动成功")
|
||||
logger.bind(tag=TAG).info("WebSocket服务器启动成功")
|
||||
await asyncio.Future() # 保持服务器运行
|
||||
|
||||
async def _handle_connection(self, websocket):
|
||||
"""处理新连接 - 使用新架构"""
|
||||
# 提取连接头信息
|
||||
headers = self._extract_headers(websocket)
|
||||
device_id = headers.get('device-id')
|
||||
|
||||
# 如果没有 device-id,提示并关闭连接
|
||||
if not device_id:
|
||||
await websocket.send("端口正常,如需测试连接,请使用test_page.html")
|
||||
await websocket.close()
|
||||
return
|
||||
|
||||
# 连接时认证
|
||||
try:
|
||||
await self._handle_auth(headers)
|
||||
except AuthenticationError as e:
|
||||
logger.bind(tag=TAG).warning(f"认证失败: {e}")
|
||||
await websocket.send("认证失败")
|
||||
await websocket.close()
|
||||
return
|
||||
|
||||
# 创建WebSocket传输层
|
||||
transport = WebSocketTransport(websocket)
|
||||
@@ -56,15 +104,17 @@ class NewWebSocketServer:
|
||||
self.active_connections.add(transport)
|
||||
|
||||
try:
|
||||
logger.info(f"新连接建立: {headers.get('device-id', 'unknown')} from {headers.get('x-real-ip', 'unknown')}")
|
||||
logger.bind(tag=TAG).info(
|
||||
f"新连接建立: {device_id} from {headers.get('x-real-ip', 'unknown')}"
|
||||
)
|
||||
|
||||
# 使用ConnectionService处理连接
|
||||
await self.connection_service.handle_connection(transport, headers)
|
||||
|
||||
except websockets.exceptions.ConnectionClosed:
|
||||
logger.info("WebSocket连接正常关闭")
|
||||
logger.bind(tag=TAG).info("WebSocket连接正常关闭")
|
||||
except Exception as e:
|
||||
logger.error(f"处理WebSocket连接时出错: {e}", exc_info=True)
|
||||
logger.bind(tag=TAG).error(f"处理WebSocket连接时出错: {e}", exc_info=True)
|
||||
finally:
|
||||
# 确保从活动连接集合中移除
|
||||
self.active_connections.discard(transport)
|
||||
@@ -76,32 +126,73 @@ class NewWebSocketServer:
|
||||
elif hasattr(websocket, "state") and websocket.state.name != "CLOSED":
|
||||
await websocket.close()
|
||||
except Exception as close_error:
|
||||
logger.error(f"强制关闭WebSocket连接时出错: {close_error}")
|
||||
logger.bind(tag=TAG).error(f"强制关闭WebSocket连接时出错: {close_error}")
|
||||
|
||||
async def _handle_auth(self, headers: Dict[str, str]):
|
||||
"""
|
||||
连接时认证
|
||||
|
||||
Args:
|
||||
headers: HTTP 请求头
|
||||
|
||||
Raises:
|
||||
AuthenticationError: 认证失败时抛出
|
||||
"""
|
||||
await self.auth_middleware.authenticate_async(headers)
|
||||
|
||||
def _extract_headers(self, websocket) -> Dict[str, str]:
|
||||
"""从WebSocket请求中提取头信息"""
|
||||
"""
|
||||
从WebSocket请求中提取头信息
|
||||
|
||||
支持从以下来源提取信息:
|
||||
1. HTTP 请求头
|
||||
2. URL 查询参数(device-id, client-id, authorization)
|
||||
3. 路径参数(如 ?from=mqtt_gateway)
|
||||
"""
|
||||
headers = {}
|
||||
|
||||
# 提取请求头
|
||||
if hasattr(websocket, 'request_headers'):
|
||||
# 1. 提取 HTTP 请求头
|
||||
if hasattr(websocket, 'request') and hasattr(websocket.request, 'headers'):
|
||||
for name, value in websocket.request.headers.items():
|
||||
headers[name.lower()] = value
|
||||
elif hasattr(websocket, 'request_headers'):
|
||||
for name, value in websocket.request_headers.items():
|
||||
headers[name.lower()] = value
|
||||
|
||||
# 提取路径参数(如果有的话)
|
||||
if hasattr(websocket, 'path'):
|
||||
# 可以从路径中提取device-id等参数
|
||||
# 例如: /ws?device-id=xxx&client-id=yyy
|
||||
# 2. 提取路径参数(如果有的话)
|
||||
request_path = None
|
||||
if hasattr(websocket, 'request') and hasattr(websocket.request, 'path'):
|
||||
request_path = websocket.request.path
|
||||
elif hasattr(websocket, 'path'):
|
||||
request_path = websocket.path
|
||||
|
||||
if request_path:
|
||||
from urllib.parse import urlparse, parse_qs
|
||||
parsed = urlparse(websocket.path)
|
||||
parsed = urlparse(request_path)
|
||||
query_params = parse_qs(parsed.query)
|
||||
|
||||
# 处理关键参数:device-id, client-id, authorization
|
||||
key_params = ['device-id', 'client-id', 'authorization']
|
||||
for key in key_params:
|
||||
if key in query_params and query_params[key]:
|
||||
# URL 参数优先级低于 header
|
||||
if key not in headers or not headers[key]:
|
||||
headers[key] = query_params[key][0]
|
||||
|
||||
# 处理其他参数
|
||||
for key, values in query_params.items():
|
||||
if values:
|
||||
if values and key not in headers:
|
||||
headers[key] = values[0]
|
||||
|
||||
# 检查是否来自 MQTT 网关
|
||||
if request_path.endswith("?from=mqtt_gateway") or "from=mqtt_gateway" in request_path:
|
||||
headers['from_mqtt_gateway'] = 'true'
|
||||
|
||||
# 提取远程地址
|
||||
# 3. 提取远程地址
|
||||
if hasattr(websocket, 'remote_address'):
|
||||
headers['x-real-ip'] = websocket.remote_address[0]
|
||||
# 如果 headers 中没有 x-real-ip,使用 remote_address
|
||||
if 'x-real-ip' not in headers:
|
||||
headers['x-real-ip'] = websocket.remote_address[0]
|
||||
|
||||
return headers
|
||||
|
||||
@@ -124,53 +215,84 @@ class NewWebSocketServer:
|
||||
"""
|
||||
try:
|
||||
async with self.config_lock:
|
||||
logger.info("开始更新服务器配置")
|
||||
logger.bind(tag=TAG).info("开始更新服务器配置")
|
||||
|
||||
# 重新获取配置
|
||||
new_config = get_config_from_api(self.config)
|
||||
# 异步获取新配置
|
||||
new_config = await get_config_from_api_async(self.config)
|
||||
if new_config is None:
|
||||
logger.error("获取新配置失败")
|
||||
logger.bind(tag=TAG).error("获取新配置失败")
|
||||
return False
|
||||
|
||||
logger.info("获取新配置成功")
|
||||
logger.bind(tag=TAG).info("获取新配置成功")
|
||||
|
||||
# 检查配置变化
|
||||
config_changed = self._check_config_changes(self.config, new_config)
|
||||
# 检查 VAD 和 ASR 类型是否需要更新
|
||||
update_vad = check_vad_update(self.config, new_config)
|
||||
update_asr = check_asr_update(self.config, new_config)
|
||||
logger.bind(tag=TAG).info(
|
||||
f"检查VAD和ASR类型是否需要更新: VAD={update_vad}, ASR={update_asr}"
|
||||
)
|
||||
|
||||
# 检查配置是否有重大变化
|
||||
changed_configs = self._get_changed_configs(self.config, new_config)
|
||||
|
||||
# 保存旧配置引用
|
||||
old_config = self.config
|
||||
|
||||
# 更新配置
|
||||
self.config = new_config
|
||||
|
||||
# 重新创建连接服务(如果配置有重大变化)
|
||||
if config_changed:
|
||||
logger.info("配置有重大变化,重新创建连接服务")
|
||||
# 根据变化类型进行更新
|
||||
if changed_configs:
|
||||
logger.bind(tag=TAG).info(f"配置项变化: {', '.join(changed_configs)}")
|
||||
|
||||
# 重新创建连接服务,使用新配置
|
||||
# 注意:已建立的连接会继续使用旧配置,只有新连接使用新配置
|
||||
self.connection_service = ConnectionService(new_config)
|
||||
|
||||
# 如果 ASR 配置变化且使用共享 ASR 管理器,需要特殊处理
|
||||
if update_asr and '_shared_asr_manager' in old_config:
|
||||
logger.bind(tag=TAG).warning(
|
||||
"ASR 配置已变化,但共享 ASR 管理器需要重启服务才能更新"
|
||||
)
|
||||
else:
|
||||
# 即使没有重大变化,也更新 ConnectionService 的配置引用
|
||||
self.connection_service.config = new_config
|
||||
|
||||
logger.info("配置更新任务执行完毕")
|
||||
# 更新认证中间件
|
||||
self.auth_middleware = AuthMiddleware(new_config)
|
||||
|
||||
logger.bind(tag=TAG).info("配置更新任务执行完毕")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"更新服务器配置失败: {str(e)}", exc_info=True)
|
||||
logger.bind(tag=TAG).error(f"更新服务器配置失败: {str(e)}", exc_info=True)
|
||||
return False
|
||||
|
||||
def _check_config_changes(self, old_config: Dict[str, Any], new_config: Dict[str, Any]) -> bool:
|
||||
"""检查配置是否有重大变化"""
|
||||
# 检查关键配置项是否变化
|
||||
def _get_changed_configs(self, old_config: Dict[str, Any], new_config: Dict[str, Any]) -> list:
|
||||
"""
|
||||
获取变化的配置项列表
|
||||
|
||||
Returns:
|
||||
list: 变化的配置项名称列表
|
||||
"""
|
||||
changed = []
|
||||
key_configs = [
|
||||
"selected_module",
|
||||
"vad",
|
||||
"asr",
|
||||
"llm",
|
||||
"tts",
|
||||
"memory",
|
||||
"intent"
|
||||
"VAD",
|
||||
"ASR",
|
||||
"LLM",
|
||||
"TTS",
|
||||
"Memory",
|
||||
"Intent"
|
||||
]
|
||||
|
||||
for key in key_configs:
|
||||
if old_config.get(key) != new_config.get(key):
|
||||
logger.info(f"配置项 {key} 发生变化")
|
||||
return True
|
||||
old_value = old_config.get(key)
|
||||
new_value = new_config.get(key)
|
||||
if old_value != new_value:
|
||||
changed.append(key)
|
||||
|
||||
return False
|
||||
return changed
|
||||
|
||||
def get_active_connections_count(self) -> int:
|
||||
"""获取活跃连接数"""
|
||||
|
||||
Reference in New Issue
Block a user