From 416145a90ee27418a741cb574320919b64ee256d Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Mon, 28 Apr 2025 11:53:16 +0800 Subject: [PATCH 1/4] =?UTF-8?q?update:=E4=BF=AE=E5=A4=8Dwebsocket=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E8=A2=AB=E6=B5=8F=E8=A7=88=E5=99=A8=E8=AE=BF=E9=97=AE?= =?UTF-8?q?=E5=87=BA=E9=94=99=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/core/websocket_server.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/main/xiaozhi-server/core/websocket_server.py b/main/xiaozhi-server/core/websocket_server.py index 038379d2..fae94046 100644 --- a/main/xiaozhi-server/core/websocket_server.py +++ b/main/xiaozhi-server/core/websocket_server.py @@ -2,7 +2,7 @@ import asyncio import websockets from config.logger import setup_logging from core.connection import ConnectionHandler -from core.utils.util import get_local_ip, initialize_modules +from core.utils.util import initialize_modules TAG = __name__ @@ -27,7 +27,9 @@ class WebSocketServer: host = server_config.get("ip", "0.0.0.0") port = int(server_config.get("port", 8000)) - async with websockets.serve(self._handle_connection, host, port): + async with websockets.serve( + self._handle_connection, host, port, process_request=self._http_response + ): await asyncio.Future() async def _handle_connection(self, websocket): @@ -47,3 +49,12 @@ class WebSocketServer: await handler.handle_connection(websocket) finally: self.active_connections.discard(handler) + + async def _http_response(self, websocket, request_headers): + # 检查是否为 WebSocket 升级请求 + if request_headers.headers.get("connection", "").lower() == "upgrade": + # 如果是 WebSocket 请求,返回 None 允许握手继续 + return None + else: + # 如果是普通 HTTP 请求,返回 "server is running" + return websocket.respond(200, "Server is running\n") From 90a31debd1e4d07feeecbfb2da1e4c892c12d1e7 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Mon, 28 Apr 2025 11:56:17 +0800 Subject: [PATCH 2/4] =?UTF-8?q?update:=E4=BF=AE=E6=94=B9=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/xiaozhi/common/constant/Constant.java | 2 +- main/xiaozhi-server/config/logger.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java b/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java index cee9727e..07d746b8 100644 --- a/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java +++ b/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java @@ -177,5 +177,5 @@ public interface Constant { /** * 版本号 */ - public static final String VERSION = "0.3.12"; + public static final String VERSION = "0.3.13"; } \ No newline at end of file diff --git a/main/xiaozhi-server/config/logger.py b/main/xiaozhi-server/config/logger.py index f42ca6c0..9eed8540 100644 --- a/main/xiaozhi-server/config/logger.py +++ b/main/xiaozhi-server/config/logger.py @@ -3,7 +3,7 @@ import sys from loguru import logger from config.config_loader import load_config -SERVER_VERSION = "0.3.12" +SERVER_VERSION = "0.3.13" def get_module_abbreviation(module_name, module_dict): From 1f5a78f4580345e3b0c56ac3b1a254ebb2abdb72 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Mon, 28 Apr 2025 16:30:31 +0800 Subject: [PATCH 3/4] =?UTF-8?q?update:=E8=8E=B7=E5=8F=96=E5=B7=AE=E5=BC=82?= =?UTF-8?q?=E5=8C=96=E9=85=8D=E7=BD=AE=E6=97=B6=EF=BC=8C=E4=B8=8D=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E6=95=8F=E6=84=9F=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/core/connection.py | 39 +++++++++- .../core/handle/sendAudioHandle.py | 71 ++++++++++--------- 2 files changed, 72 insertions(+), 38 deletions(-) diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index 3cf6b018..fe8d1ef3 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -241,7 +241,7 @@ class ConnectionHandler: ) private_config["delete_audio"] = bool(self.config.get("delete_audio", True)) self.logger.bind(tag=TAG).info( - f"{time.time() - begin_time} 秒,获取差异化配置成功: {private_config}" + f"{time.time() - begin_time} 秒,获取差异化配置成功: {json.dumps(filter_sensitive_info(private_config), ensure_ascii=False)}" ) except DeviceNotFoundException as e: self.need_bind = True @@ -448,7 +448,7 @@ class ConnectionHandler: pos = current_text.rfind(punct) prev_char = current_text[pos - 1] if pos - 1 >= 0 else "" # 如果.前面是数字统一判断为小数 - if prev_char.isdigit() and punct == '.': + if prev_char.isdigit() and punct == ".": number_flag = False if pos > last_punct_pos and number_flag: last_punct_pos = pos @@ -579,7 +579,7 @@ class ConnectionHandler: pos = current_text.rfind(punct) prev_char = current_text[pos - 1] if pos - 1 >= 0 else "" # 如果.前面是数字统一判断为小数 - if prev_char.isdigit() and punct == '.': + if prev_char.isdigit() and punct == ".": number_flag = False if pos > last_punct_pos and number_flag: last_punct_pos = pos @@ -945,3 +945,36 @@ class ConnectionHandler: break except Exception as e: self.logger.bind(tag=TAG).error(f"超时检查任务出错: {e}") + + +def filter_sensitive_info(config: dict) -> dict: + """ + 过滤配置中的敏感信息 + Args: + config: 原始配置字典 + Returns: + 过滤后的配置字典 + """ + sensitive_keys = [ + "api_key", + "personal_access_token", + "access_token", + "token", + "access_key_secret", + "secret_key", + ] + + def _filter_dict(d: dict) -> dict: + filtered = {} + for k, v in d.items(): + if any(sensitive in k.lower() for sensitive in sensitive_keys): + filtered[k] = "***" + elif isinstance(v, dict): + filtered[k] = _filter_dict(v) + elif isinstance(v, list): + filtered[k] = [_filter_dict(i) if isinstance(i, dict) else i for i in v] + else: + filtered[k] = v + return filtered + + return _filter_dict(copy.deepcopy(config)) diff --git a/main/xiaozhi-server/core/handle/sendAudioHandle.py b/main/xiaozhi-server/core/handle/sendAudioHandle.py index ec29a9fd..3387a133 100644 --- a/main/xiaozhi-server/core/handle/sendAudioHandle.py +++ b/main/xiaozhi-server/core/handle/sendAudioHandle.py @@ -2,51 +2,52 @@ from config.logger import setup_logging import json import asyncio import time -from core.utils.util import ( - get_string_no_punctuation_or_emoji, - analyze_emotion -) +from core.utils.util import get_string_no_punctuation_or_emoji, analyze_emotion TAG = __name__ logger = setup_logging() emoji_map = { - 'neutral': '😶', - 'happy': '🙂', - 'laughing': '😆', - 'funny': '😂', - 'sad': '😔', - 'angry': '😠', - 'crying': '😭', - 'loving': '😍', - 'embarrassed': '😳', - 'surprised': '😲', - 'shocked': '😱', - 'thinking': '🤔', - 'winking': '😉', - 'cool': '😎', - 'relaxed': '😌', - 'delicious': '🤤', - 'kissy': '😘', - 'confident': '😏', - 'sleepy': '😴', - 'silly': '😜', - 'confused': '🙄' + "neutral": "😶", + "happy": "🙂", + "laughing": "😆", + "funny": "😂", + "sad": "😔", + "angry": "😠", + "crying": "😭", + "loving": "😍", + "embarrassed": "😳", + "surprised": "😲", + "shocked": "😱", + "thinking": "🤔", + "winking": "😉", + "cool": "😎", + "relaxed": "😌", + "delicious": "🤤", + "kissy": "😘", + "confident": "😏", + "sleepy": "😴", + "silly": "😜", + "confused": "🙄", } + async def sendAudioMessage(conn, audios, text, text_index=0): # 发送句子开始消息 - emotion = analyze_emotion(text) - emoji = emoji_map.get(emotion, '🙂') # 默认使用笑脸 - await conn.websocket.send(json.dumps( - { - "type": "llm", - "text": emoji, - "emotion": emotion, - "session_id": conn.session_id, - } + if text is not None: + emotion = analyze_emotion(text) + emoji = emoji_map.get(emotion, "🙂") # 默认使用笑脸 + await conn.websocket.send( + json.dumps( + { + "type": "llm", + "text": emoji, + "emotion": emotion, + "session_id": conn.session_id, + } + ) ) - ) + if text_index == conn.tts_first_text_index: logger.bind(tag=TAG).info(f"发送第一段语音: {text}") await send_tts_message(conn, "sentence_start", text) From e5525934744b3041dc491abcaafbd65c2525d9cf Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Mon, 28 Apr 2025 16:44:36 +0800 Subject: [PATCH 4/4] =?UTF-8?q?update:=E5=8F=96=E6=B6=88=E5=AF=B9=E4=B8=A5?= =?UTF-8?q?=E6=A0=BCmac=E5=9C=B0=E5=9D=80=E7=9A=84=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../device/controller/OTAController.java | 4 +-- .../modules/device/utils/NetworkUtil.java | 34 ------------------- 2 files changed, 1 insertion(+), 37 deletions(-) delete mode 100644 main/manager-api/src/main/java/xiaozhi/modules/device/utils/NetworkUtil.java diff --git a/main/manager-api/src/main/java/xiaozhi/modules/device/controller/OTAController.java b/main/manager-api/src/main/java/xiaozhi/modules/device/controller/OTAController.java index 6333c627..fc0a61c1 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/device/controller/OTAController.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/device/controller/OTAController.java @@ -28,7 +28,6 @@ import xiaozhi.modules.device.dto.DeviceReportReqDTO; import xiaozhi.modules.device.dto.DeviceReportRespDTO; import xiaozhi.modules.device.entity.DeviceEntity; import xiaozhi.modules.device.service.DeviceService; -import xiaozhi.modules.device.utils.NetworkUtil; import xiaozhi.modules.sys.service.SysParamsService; @Tag(name = "设备管理", description = "OTA 相关接口") @@ -53,9 +52,8 @@ public class OTAController { clientId = deviceId; } String macAddress = deviceReportReqDTO.getMacAddress(); - boolean macAddressValid = NetworkUtil.isMacAddressValid(macAddress); // 设备Id和Mac地址应是一致的, 并且必须需要application字段 - if (!deviceId.equals(macAddress) || !macAddressValid || deviceReportReqDTO.getApplication() == null) { + if (!deviceId.equals(macAddress) || deviceReportReqDTO.getApplication() == null) { return createResponse(DeviceReportRespDTO.createError("Invalid OTA request")); } return createResponse(deviceService.checkDeviceActive(macAddress, clientId, deviceReportReqDTO)); diff --git a/main/manager-api/src/main/java/xiaozhi/modules/device/utils/NetworkUtil.java b/main/manager-api/src/main/java/xiaozhi/modules/device/utils/NetworkUtil.java deleted file mode 100644 index ab269f7b..00000000 --- a/main/manager-api/src/main/java/xiaozhi/modules/device/utils/NetworkUtil.java +++ /dev/null @@ -1,34 +0,0 @@ -package xiaozhi.modules.device.utils; - -import java.util.regex.Pattern; - -import org.apache.commons.lang3.StringUtils; - -/** - * 网络工具类 - */ -public class NetworkUtil { - /** - * MAC地址正则表达式 - */ - private static final Pattern macPattern = Pattern.compile("^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"); - - /** - * 判断MAC地址是否合法 - */ - public static boolean isMacAddressValid(String mac) { - if (StringUtils.isBlank(mac)) { - return false; - } - // 正则校验格式 - if (!macPattern.matcher(mac).matches()) { - return false; - } - // 校验MAC地址是否为单播地址 - String normalized = mac.toLowerCase(); - String[] parts = normalized.split("[:-]"); - int firstByte = Integer.parseInt(parts[0], 16); - return (firstByte & 1) == 0; // 最低位为0表示单播地址,合法 - } - -}