mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
update:增加单台设备每天最多聊天字数,防止被ddos
This commit is contained in:
parent
ac8017872a
commit
be90eae1ec
6 changed files with 89 additions and 5 deletions
|
|
@ -91,6 +91,7 @@ public class ConfigServiceImpl implements ConfigService {
|
||||||
}
|
}
|
||||||
throw new RenException(ErrorCode.OTA_DEVICE_NOT_FOUND, "not found device");
|
throw new RenException(ErrorCode.OTA_DEVICE_NOT_FOUND, "not found device");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取智能体信息
|
// 获取智能体信息
|
||||||
AgentEntity agent = agentService.getAgentById(device.getAgentId());
|
AgentEntity agent = agentService.getAgentById(device.getAgentId());
|
||||||
if (agent == null) {
|
if (agent == null) {
|
||||||
|
|
@ -104,7 +105,9 @@ public class ConfigServiceImpl implements ConfigService {
|
||||||
}
|
}
|
||||||
// 构建返回数据
|
// 构建返回数据
|
||||||
Map<String, Object> result = new HashMap<>();
|
Map<String, Object> result = new HashMap<>();
|
||||||
|
// 获取单台设备每天最多输出字数
|
||||||
|
String deviceMaxOutputSize = sysParamsService.getValue("device_max_output_size", true);
|
||||||
|
result.put("device_max_output_size", deviceMaxOutputSize);
|
||||||
// 如果客户端已实例化模型,则不返回
|
// 如果客户端已实例化模型,则不返回
|
||||||
String alreadySelectedVadModelId = (String) selectedModule.get("VAD");
|
String alreadySelectedVadModelId = (String) selectedModule.get("VAD");
|
||||||
if (alreadySelectedVadModelId != null && alreadySelectedVadModelId.equals(agent.getVadModelId())) {
|
if (alreadySelectedVadModelId != null && alreadySelectedVadModelId.equals(agent.getVadModelId())) {
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,7 @@
|
||||||
|
-- 调整意图识别配置
|
||||||
delete from `ai_model_config` where id = 'Intent_function_call';
|
delete from `ai_model_config` where id = 'Intent_function_call';
|
||||||
INSERT INTO `ai_model_config` VALUES ('Intent_function_call', 'Intent', 'function_call', '函数调用意图识别', 0, 1, '{\"type\": \"function_call\", \"functions\": \"change_role;get_weather;get_news;play_music\"}', NULL, NULL, 3, NULL, NULL, NULL, NULL);
|
INSERT INTO `ai_model_config` VALUES ('Intent_function_call', 'Intent', 'function_call', '函数调用意图识别', 0, 1, '{\"type\": \"function_call\", \"functions\": \"change_role;get_weather;get_news;play_music\"}', NULL, NULL, 3, NULL, NULL, NULL, NULL);
|
||||||
|
|
||||||
|
-- 增加单台设备每天最多聊天句数
|
||||||
|
delete from `sys_params` where id = 105;
|
||||||
|
INSERT INTO `sys_params` (id, param_code, param_value, value_type, param_type, remark) VALUES (105, 'device_max_output_size', '0', 'number', 1, '单台设备每天最多输出字数,0表示不限制');
|
||||||
|
|
@ -59,9 +59,9 @@ databaseChangeLog:
|
||||||
encoding: utf8
|
encoding: utf8
|
||||||
path: classpath:db/changelog/202504151206.sql
|
path: classpath:db/changelog/202504151206.sql
|
||||||
- changeSet:
|
- changeSet:
|
||||||
id: 202504181534
|
id: 202504181536
|
||||||
author: John
|
author: John
|
||||||
changes:
|
changes:
|
||||||
- sqlFile:
|
- sqlFile:
|
||||||
encoding: utf8
|
encoding: utf8
|
||||||
path: classpath:db/changelog/202504181534.sql
|
path: classpath:db/changelog/202504181536.sql
|
||||||
|
|
@ -29,6 +29,7 @@ from core.auth import AuthMiddleware, AuthenticationError
|
||||||
from core.mcp.manager import MCPManager
|
from core.mcp.manager import MCPManager
|
||||||
from config.config_loader import get_private_config_from_api
|
from config.config_loader import get_private_config_from_api
|
||||||
from config.manage_api_client import DeviceNotFoundException, DeviceBindException
|
from config.manage_api_client import DeviceNotFoundException, DeviceBindException
|
||||||
|
from core.utils.output_counter import add_device_output
|
||||||
|
|
||||||
TAG = __name__
|
TAG = __name__
|
||||||
|
|
||||||
|
|
@ -57,6 +58,7 @@ class ConnectionHandler:
|
||||||
self.session_id = None
|
self.session_id = None
|
||||||
self.prompt = None
|
self.prompt = None
|
||||||
self.welcome_msg = None
|
self.welcome_msg = None
|
||||||
|
self.max_output_size = 0
|
||||||
|
|
||||||
# 客户端状态相关
|
# 客户端状态相关
|
||||||
self.client_abort = False
|
self.client_abort = False
|
||||||
|
|
@ -315,7 +317,6 @@ class ConnectionHandler:
|
||||||
self.config["selected_module"]["LLM"] = private_config["selected_module"][
|
self.config["selected_module"]["LLM"] = private_config["selected_module"][
|
||||||
"LLM"
|
"LLM"
|
||||||
]
|
]
|
||||||
|
|
||||||
if private_config.get("Memory", None) is not None:
|
if private_config.get("Memory", None) is not None:
|
||||||
init_memory = True
|
init_memory = True
|
||||||
self.config["Memory"] = private_config["Memory"]
|
self.config["Memory"] = private_config["Memory"]
|
||||||
|
|
@ -328,6 +329,8 @@ class ConnectionHandler:
|
||||||
self.config["selected_module"]["Intent"] = private_config[
|
self.config["selected_module"]["Intent"] = private_config[
|
||||||
"selected_module"
|
"selected_module"
|
||||||
]["Intent"]
|
]["Intent"]
|
||||||
|
if private_config.get("device_max_output_size", None) is not None:
|
||||||
|
self.max_output_size = int(private_config["device_max_output_size"])
|
||||||
try:
|
try:
|
||||||
modules = initialize_modules(
|
modules = initialize_modules(
|
||||||
self.logger,
|
self.logger,
|
||||||
|
|
@ -845,6 +848,8 @@ class ConnectionHandler:
|
||||||
self.logger.bind(tag=TAG).error(f"tts转换失败,{text}")
|
self.logger.bind(tag=TAG).error(f"tts转换失败,{text}")
|
||||||
return None, text, text_index
|
return None, text, text_index
|
||||||
self.logger.bind(tag=TAG).debug(f"TTS 文件生成完毕: {tts_file}")
|
self.logger.bind(tag=TAG).debug(f"TTS 文件生成完毕: {tts_file}")
|
||||||
|
if self.max_output_size > 0:
|
||||||
|
add_device_output(self.headers.get("device-id"), len(text))
|
||||||
return tts_file, text, text_index
|
return tts_file, text, text_index
|
||||||
|
|
||||||
def clearSpeakStatus(self):
|
def clearSpeakStatus(self):
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
from config.logger import setup_logging
|
from config.logger import setup_logging
|
||||||
import time
|
import time
|
||||||
import asyncio
|
|
||||||
from core.utils.util import remove_punctuation_and_length
|
from core.utils.util import remove_punctuation_and_length
|
||||||
from core.handle.sendAudioHandle import send_stt_message
|
from core.handle.sendAudioHandle import send_stt_message
|
||||||
from core.handle.intentHandler import handle_user_intent
|
from core.handle.intentHandler import handle_user_intent
|
||||||
|
from core.utils.output_counter import check_device_output_limit
|
||||||
|
|
||||||
TAG = __name__
|
TAG = __name__
|
||||||
logger = setup_logging()
|
logger = setup_logging()
|
||||||
|
|
@ -51,6 +51,15 @@ async def startToChat(conn, text):
|
||||||
if conn.need_bind:
|
if conn.need_bind:
|
||||||
await check_bind_device(conn)
|
await check_bind_device(conn)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# 如果当日的输出字数大于限定的字数
|
||||||
|
if conn.max_output_size > 0:
|
||||||
|
if check_device_output_limit(
|
||||||
|
conn.headers.get("device-id"), conn.max_output_size
|
||||||
|
):
|
||||||
|
await max_out_size(conn)
|
||||||
|
return
|
||||||
|
|
||||||
# 首先进行意图分析
|
# 首先进行意图分析
|
||||||
intent_handled = await handle_user_intent(conn, text)
|
intent_handled = await handle_user_intent(conn, text)
|
||||||
|
|
||||||
|
|
@ -89,6 +98,18 @@ async def no_voice_close_connect(conn):
|
||||||
await startToChat(conn, prompt)
|
await startToChat(conn, prompt)
|
||||||
|
|
||||||
|
|
||||||
|
async def max_out_size(conn):
|
||||||
|
text = "不好意思,我现在有点事情要忙,明天这个时候我们再聊,约好了哦!明天不见不散,拜拜!"
|
||||||
|
await send_stt_message(conn, text)
|
||||||
|
conn.tts_first_text_index = 0
|
||||||
|
conn.tts_last_text_index = 0
|
||||||
|
conn.llm_finish_task = True
|
||||||
|
file_path = "config/assets/max_output_size.wav"
|
||||||
|
opus_packets, _ = conn.tts.audio_to_opus_data(file_path)
|
||||||
|
conn.audio_play_queue.put((opus_packets, text, 0))
|
||||||
|
conn.close_after_chat = True
|
||||||
|
|
||||||
|
|
||||||
async def check_bind_device(conn):
|
async def check_bind_device(conn):
|
||||||
if conn.bind_code:
|
if conn.bind_code:
|
||||||
# 确保bind_code是6位数字
|
# 确保bind_code是6位数字
|
||||||
|
|
|
||||||
50
main/xiaozhi-server/core/utils/output_counter.py
Normal file
50
main/xiaozhi-server/core/utils/output_counter.py
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
import datetime
|
||||||
|
from typing import Dict, Tuple
|
||||||
|
|
||||||
|
# 全局字典,用于存储每个设备的每日输出字数
|
||||||
|
_device_daily_output: Dict[Tuple[str, datetime.date], int] = {}
|
||||||
|
# 记录最后一次检查的日期
|
||||||
|
_last_check_date: datetime.date = None
|
||||||
|
|
||||||
|
|
||||||
|
def reset_device_output():
|
||||||
|
"""
|
||||||
|
重置所有设备的每日输出字数
|
||||||
|
每天0点调用此函数
|
||||||
|
"""
|
||||||
|
_device_daily_output.clear()
|
||||||
|
|
||||||
|
|
||||||
|
def get_device_output(device_id: str) -> int:
|
||||||
|
"""
|
||||||
|
获取设备当日的输出字数
|
||||||
|
"""
|
||||||
|
current_date = datetime.datetime.now().date()
|
||||||
|
return _device_daily_output.get((device_id, current_date), 0)
|
||||||
|
|
||||||
|
|
||||||
|
def add_device_output(device_id: str, char_count: int):
|
||||||
|
"""
|
||||||
|
增加设备的输出字数
|
||||||
|
"""
|
||||||
|
current_date = datetime.datetime.now().date()
|
||||||
|
global _last_check_date
|
||||||
|
|
||||||
|
# 如果是第一次调用或者日期发生变化,清空计数器
|
||||||
|
if _last_check_date is None or _last_check_date != current_date:
|
||||||
|
_device_daily_output.clear()
|
||||||
|
_last_check_date = current_date
|
||||||
|
|
||||||
|
current_count = _device_daily_output.get((device_id, current_date), 0)
|
||||||
|
_device_daily_output[(device_id, current_date)] = current_count + char_count
|
||||||
|
|
||||||
|
|
||||||
|
def check_device_output_limit(device_id: str, max_output_size: int) -> bool:
|
||||||
|
"""
|
||||||
|
检查设备是否超过输出限制
|
||||||
|
:return: True 如果超过限制,False 如果未超过
|
||||||
|
"""
|
||||||
|
if not device_id:
|
||||||
|
return False
|
||||||
|
current_output = get_device_output(device_id)
|
||||||
|
return current_output >= max_output_size
|
||||||
Loading…
Reference in a new issue