mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-29 08:39:59 +08:00
* 添加测试插件 * ✨ feat(auth): 添加缓存机制以优化用户和插件数据查询性能 * 🗑️ chore(help): 删除笨蛋检测插件代码 * ✨ feat(bot): 添加对扩展插件的加载支持 * ✨ feat(auth): 优化权限检查和缓存机制,增加用户和插件数据的并行查询 * ✨ feat(cache): 引入运行时缓存机制,优化用户和插件的ban记录管理 * ✨ feat(auth): 更新is_ban函数文档,添加参数和返回值说明 * ``` feat(auth): 使用内存缓存优化权限验证性能 - 移除数据库查询超时控制,改用 LevelUserMemoryCache、BotMemoryCache 和 PluginLimitMemoryCache 进行缓存查询 - 优化 auth_admin、auth_bot、auth_limit 权限验证逻辑,提升响应速度 - 添加 background 参数支持异步发送权限不足提示消息 - 移除 asyncio 依赖,简化代码结构 fix(auth): 修复限制通知频率控制问题 - 实现限制通知冷却机制,避免重复发送相同限制消息 - 添加 AUTH_LIMIT_NOTICE_CD 配置项,默认值为 2 秒 - 使用 FreqLimiter 控制限制通知发送频率 refactor(models): 增强模型数据变更时的缓存同步 - 在 BotConsole、GroupConsole、LevelUser、PluginLimit 模型的 create、update_or_create、save、delete 方法中自动更新对应缓存 - 确保数据库和内存缓存数据一致性 docs(ban_console): 修正文档注释并优化日志信息 - 修正 BanConsole 类中方法的文档字符串,使用标准参数和返回值格式 - 优化调试日志信息,使描述更加清晰准确 ``` * ✨ feat(auth): 更新Limit类以支持PluginLimitSnapshot,优化限制信息处理 * ✨ feat(bot_manage): 优化Bot控制台初始化逻辑,处理IntegrityError异常 * ✨ feat(mmm1): 新增消息推送功能,支持私聊和群聊事件处理 * ✨ feat(group_member_update): 优化群组成员更新逻辑,增加活动跟踪和消息记录功能 * ✨ feat(mmm1): 删除冗余的消息推送功能代码 * ✨ feat(auth): 优化权限检查逻辑,增加模块阻止功能和缓存处理 * ✨ feat(auth): 优化权限检查逻辑,增加快速ban检测和前置检查功能 * ✨ feat(chat_history): 增强消息处理规则,添加时间间隔限制以防止重复消息 ✨ feat(data_source): 引入异步获取群成员信息的功能,优化用户信息更新逻辑 * ✨ feat(send_queue): 添加异步发送队列以优化API调用和速率限制 * ✨ feat(auth): 添加缓存就绪检查以优化权限处理逻辑 * ✨ feat(plugins): 移除不必要的插件加载以简化插件管理 * ✨ feat(group_console): 优化群组获取逻辑,添加缓存检查以提升性能 ✨ 只接收缓存完成之后时间的消息 * ✨ feat(auth): 添加异步任务管理和超载检测,优化权限处理逻辑 ✨ feat(chat_history): 修改规则函数为异步,提升消息处理效率 ✨ feat(group_handle): 增加安全获取群组信息的异步方法,添加超时处理 ✨ feat(record_request): 引入安全获取群组信息的异步方法,优化群邀请处理 ✨ feat(ban_memory_cache): 增强禁言内存缓存,添加负缓存机制 ✨ feat(message_load): 新增消息负载检测功能,优化任务调度 ✨ feat(scheduler): 在调度器中集成消息压力检测,优化任务执行 ✨ feat(send_queue): 引入异步任务管理,优化发送队列处理 * ✨ feat(auth): 添加对 LevelUserSnapshot 和 BotSnapshot 的支持,优化权限检查逻辑 ✨ 格式化 * ✨ feat(db_context): 增强 get_or_create 方法,处理并发创建冲突并回退查询已存在记录 * ✨ feat(bot_manage): 增强 init_bot_console 方法,处理并发创建冲突并回退查询已存在的 bot 数据 * 🚨 auto fix by pre-commit hooks * ✨ feat(runtime_cache): 优化消息处理逻辑,支持 bytes 和 bytearray 类型的联合判断 * ✨ style(runtime_cache): 格式化代码,优化多行表达式的可读性 * 🚨 auto fix by pre-commit hooks * ✨ refactor: 优化类型注解,改进群组成员更新逻辑和平台处理 ✨ 格式化 * 🚨 auto fix by pre-commit hooks * ✨ refactor: 优化代码格式,增强可读性并修复类型注解 * 🚨 auto fix by pre-commit hooks * ✨ refactor: 更新文档注释,增强is_ban函数的可读性 * ✨ refactor: 优化代码结构,移除冗余函数,增强可读性并改进任务调度逻辑 * ✨ refactor: 调整定时任务时间,优化渲染服务的初始化逻辑,增强代码可读性 --------- Co-authored-by: ATTomatoo <1126160939@qq.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
350 lines
12 KiB
Python
350 lines
12 KiB
Python
import asyncio
|
|
import time
|
|
from typing import Any, ClassVar
|
|
|
|
import nonebot
|
|
from nonebot_plugin_uninfo import Uninfo
|
|
from pydantic import BaseModel
|
|
|
|
from zhenxun.configs.config import Config
|
|
from zhenxun.models.plugin_info import PluginInfo
|
|
from zhenxun.models.plugin_limit import PluginLimit
|
|
from zhenxun.services.cache.runtime_cache import (
|
|
PluginLimitMemoryCache,
|
|
PluginLimitSnapshot,
|
|
)
|
|
from zhenxun.services.db_context import DB_TIMEOUT_SECONDS
|
|
from zhenxun.services.log import logger
|
|
from zhenxun.utils.enum import LimitWatchType, PluginLimitType
|
|
from zhenxun.utils.limiters import CountLimiter, FreqLimiter, UserBlockLimiter
|
|
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
|
|
from zhenxun.utils.message import MessageUtils
|
|
from zhenxun.utils.time_utils import TimeUtils
|
|
from zhenxun.utils.utils import get_entity_ids
|
|
|
|
from .config import LOGGER_COMMAND, WARNING_THRESHOLD
|
|
from .exception import SkipPluginException
|
|
|
|
driver = nonebot.get_driver()
|
|
|
|
Config.add_plugin_config(
|
|
"hook",
|
|
"AUTH_LIMIT_NOTICE_CD",
|
|
2,
|
|
help="auth limit notice cooldown seconds",
|
|
)
|
|
_LIMIT_NOTICE_CD = int(Config.get_config("hook", "AUTH_LIMIT_NOTICE_CD", 2) or 2)
|
|
_LIMIT_NOTICE_LIMITER = FreqLimiter(_LIMIT_NOTICE_CD)
|
|
_LIMIT_NOTICE_TASKS: set[asyncio.Task] = set()
|
|
|
|
|
|
@PriorityLifecycle.on_startup(priority=5)
|
|
async def _():
|
|
"""初始化限制"""
|
|
await LimitManager.init_limit()
|
|
|
|
|
|
class Limit(BaseModel):
|
|
limit: PluginLimit | PluginLimitSnapshot
|
|
limiter: FreqLimiter | UserBlockLimiter | CountLimiter
|
|
|
|
class Config:
|
|
arbitrary_types_allowed = True
|
|
|
|
|
|
def _limit_notice_key(
|
|
limit: PluginLimit | PluginLimitSnapshot,
|
|
user_id: str,
|
|
group_id: str | None,
|
|
channel_id: str | None,
|
|
) -> str:
|
|
key = user_id
|
|
if group_id and limit.watch_type == LimitWatchType.GROUP:
|
|
key = channel_id or group_id
|
|
return f"{limit.module}:{limit.limit_type}:{key}"
|
|
|
|
|
|
def _send_limit_notice(message: str, format_kwargs: dict[str, Any], key: str) -> None:
|
|
if not _LIMIT_NOTICE_LIMITER.check(key):
|
|
return
|
|
_LIMIT_NOTICE_LIMITER.start_cd(key)
|
|
|
|
async def _send():
|
|
try:
|
|
await MessageUtils.build_message(message, format_args=format_kwargs).send()
|
|
except Exception as exc:
|
|
logger.error("limit notice send failed", LOGGER_COMMAND, e=exc)
|
|
|
|
task = asyncio.create_task(_send())
|
|
_LIMIT_NOTICE_TASKS.add(task)
|
|
task.add_done_callback(_LIMIT_NOTICE_TASKS.discard)
|
|
|
|
|
|
class LimitManager:
|
|
add_module: ClassVar[list] = []
|
|
last_update_time: ClassVar[float] = 0
|
|
update_interval: ClassVar[float] = 6000 # 1小时更新一次
|
|
is_updating: ClassVar[bool] = False # 防止并发更新
|
|
|
|
cd_limit: ClassVar[dict[str, Limit]] = {}
|
|
block_limit: ClassVar[dict[str, Limit]] = {}
|
|
count_limit: ClassVar[dict[str, Limit]] = {}
|
|
|
|
# 模块限制缓存,避免频繁查询数据库
|
|
module_limit_cache: ClassVar[
|
|
dict[str, tuple[float, list[PluginLimitSnapshot], bool]]
|
|
] = {}
|
|
module_cache_ttl: ClassVar[float] = 60 # 模块缓存有效期(秒)
|
|
module_cache_error_ttl: ClassVar[float] = 5 # 超时缓存有效期(秒)
|
|
|
|
@classmethod
|
|
async def init_limit(cls):
|
|
"""初始化限制"""
|
|
cls.last_update_time = time.time()
|
|
try:
|
|
await asyncio.wait_for(cls.update_limits(), timeout=DB_TIMEOUT_SECONDS * 2)
|
|
except asyncio.TimeoutError:
|
|
logger.error("初始化限制超时", LOGGER_COMMAND)
|
|
|
|
@classmethod
|
|
async def update_limits(cls):
|
|
"""更新限制信息"""
|
|
# 防止并发更新
|
|
if cls.is_updating:
|
|
return
|
|
|
|
cls.is_updating = True
|
|
try:
|
|
start_time = time.time()
|
|
await PluginLimitMemoryCache.ensure_loaded()
|
|
limit_list = PluginLimitMemoryCache.get_all_limits()
|
|
|
|
# 清空旧数据
|
|
cls.add_module = []
|
|
cls.cd_limit = {}
|
|
cls.block_limit = {}
|
|
cls.count_limit = {}
|
|
# 添加新数据
|
|
for limit in limit_list:
|
|
cls.add_limit(limit)
|
|
|
|
cls.last_update_time = time.time()
|
|
elapsed = time.time() - start_time
|
|
if elapsed > WARNING_THRESHOLD: # 记录耗时超过500ms的更新
|
|
logger.warning(f"更新限制信息耗时: {elapsed:.3f}s", LOGGER_COMMAND)
|
|
finally:
|
|
cls.is_updating = False
|
|
|
|
@classmethod
|
|
def add_limit(cls, limit: PluginLimit | PluginLimitSnapshot):
|
|
"""添加限制
|
|
|
|
参数:
|
|
limit: PluginLimit
|
|
"""
|
|
if limit.module not in cls.add_module:
|
|
cls.add_module.append(limit.module)
|
|
if limit.limit_type == PluginLimitType.BLOCK:
|
|
cls.block_limit[limit.module] = Limit(
|
|
limit=limit, limiter=UserBlockLimiter()
|
|
)
|
|
elif limit.limit_type == PluginLimitType.CD:
|
|
cd_value = int(limit.cd or 0)
|
|
cls.cd_limit[limit.module] = Limit(
|
|
limit=limit, limiter=FreqLimiter(cd_value)
|
|
)
|
|
elif limit.limit_type == PluginLimitType.COUNT:
|
|
max_count = int(limit.max_count or 0)
|
|
if max_count <= 0:
|
|
return
|
|
cls.count_limit[limit.module] = Limit(
|
|
limit=limit, limiter=CountLimiter(max_count)
|
|
)
|
|
|
|
@classmethod
|
|
def unblock(
|
|
cls, module: str, user_id: str, group_id: str | None, channel_id: str | None
|
|
):
|
|
"""解除插件block
|
|
|
|
参数:
|
|
module: 模块名
|
|
user_id: 用户id
|
|
group_id: 群组id
|
|
channel_id: 频道id
|
|
"""
|
|
if limit_model := cls.block_limit.get(module):
|
|
limit = limit_model.limit
|
|
limiter: UserBlockLimiter = limit_model.limiter # type: ignore
|
|
key_type = user_id
|
|
if group_id and limit.watch_type == LimitWatchType.GROUP:
|
|
key_type = channel_id or group_id
|
|
logger.debug(
|
|
f"解除对象: {key_type} 的block限制",
|
|
LOGGER_COMMAND,
|
|
session=user_id,
|
|
group_id=group_id,
|
|
)
|
|
limiter.set_false(key_type)
|
|
|
|
@classmethod
|
|
async def get_module_limits(cls, module: str) -> list[PluginLimitSnapshot]:
|
|
"""获取模块的限制信息,使用缓存减少数据库查询
|
|
|
|
参数:
|
|
module: 模块名
|
|
|
|
返回:
|
|
list[PluginLimit]: 限制列表
|
|
"""
|
|
current_time = time.time()
|
|
|
|
# 检查缓存
|
|
if module in cls.module_limit_cache:
|
|
cache_time, limits, is_error = cls.module_limit_cache[module]
|
|
ttl = cls.module_cache_error_ttl if is_error else cls.module_cache_ttl
|
|
if current_time - cache_time < ttl:
|
|
return limits
|
|
|
|
# 缓存不存在或已过期,从内存缓存获取
|
|
try:
|
|
await PluginLimitMemoryCache.ensure_loaded()
|
|
limits = await PluginLimitMemoryCache.get_limits(module)
|
|
cls.module_limit_cache[module] = (current_time, limits, False)
|
|
return limits
|
|
except Exception as exc:
|
|
logger.error(f"get module limits failed: {module}", LOGGER_COMMAND, e=exc)
|
|
cls.module_limit_cache[module] = (current_time, [], True)
|
|
return []
|
|
|
|
@classmethod
|
|
async def check(
|
|
cls,
|
|
module: str,
|
|
user_id: str,
|
|
group_id: str | None,
|
|
channel_id: str | None,
|
|
):
|
|
"""检测限制
|
|
|
|
参数:
|
|
module: 模块名
|
|
user_id: 用户id
|
|
group_id: 群组id
|
|
channel_id: 频道id
|
|
|
|
异常:
|
|
IgnoredException: IgnoredException
|
|
"""
|
|
start_time = time.time()
|
|
|
|
# 定期更新全局限制信息
|
|
if (
|
|
time.time() - cls.last_update_time > cls.update_interval
|
|
and not cls.is_updating
|
|
):
|
|
# 使用异步任务更新,避免阻塞当前请求
|
|
asyncio.create_task(cls.update_limits()) # noqa: RUF006
|
|
|
|
# 如果模块不在已加载列表中,只加载该模块的限制
|
|
if module not in cls.add_module:
|
|
limits = await cls.get_module_limits(module)
|
|
for limit in limits:
|
|
cls.add_limit(limit)
|
|
|
|
# 检查各种限制
|
|
try:
|
|
if limit_model := cls.cd_limit.get(module):
|
|
await cls.__check(limit_model, user_id, group_id, channel_id)
|
|
if limit_model := cls.block_limit.get(module):
|
|
await cls.__check(limit_model, user_id, group_id, channel_id)
|
|
if limit_model := cls.count_limit.get(module):
|
|
await cls.__check(limit_model, user_id, group_id, channel_id)
|
|
finally:
|
|
# 记录总执行时间
|
|
elapsed = time.time() - start_time
|
|
if elapsed > WARNING_THRESHOLD: # 记录耗时超过500ms的检查
|
|
logger.warning(
|
|
f"限制检查耗时: {elapsed:.3f}s, 模块: {module}",
|
|
LOGGER_COMMAND,
|
|
session=user_id,
|
|
group_id=group_id,
|
|
)
|
|
|
|
@classmethod
|
|
async def __check(
|
|
cls,
|
|
limit_model: Limit | None,
|
|
user_id: str,
|
|
group_id: str | None,
|
|
channel_id: str | None,
|
|
):
|
|
"""检测限制
|
|
|
|
参数:
|
|
limit_model: Limit
|
|
user_id: 用户id
|
|
group_id: 群组id
|
|
channel_id: 频道id
|
|
|
|
异常:
|
|
IgnoredException: IgnoredException
|
|
"""
|
|
if not limit_model:
|
|
return
|
|
limit = limit_model.limit
|
|
limiter = limit_model.limiter
|
|
is_limit = (
|
|
LimitWatchType.ALL
|
|
or (group_id and limit.watch_type == LimitWatchType.GROUP)
|
|
or (not group_id and limit.watch_type == LimitWatchType.USER)
|
|
)
|
|
key_type = user_id
|
|
if group_id and limit.watch_type == LimitWatchType.GROUP:
|
|
key_type = channel_id or group_id
|
|
if is_limit and not limiter.check(key_type):
|
|
if limit.result:
|
|
format_kwargs = {}
|
|
if isinstance(limiter, FreqLimiter):
|
|
left_time = limiter.left_time(key_type)
|
|
cd_str = TimeUtils.format_duration(left_time)
|
|
format_kwargs = {"cd": cd_str}
|
|
notice_key = _limit_notice_key(limit, user_id, group_id, channel_id)
|
|
_send_limit_notice(limit.result, format_kwargs, notice_key)
|
|
raise SkipPluginException(
|
|
f"{limit.module}({limit.limit_type}) 正在限制中..."
|
|
)
|
|
else:
|
|
logger.debug(
|
|
f"开始进行限制 {limit.module}({limit.limit_type})...",
|
|
LOGGER_COMMAND,
|
|
session=user_id,
|
|
group_id=group_id,
|
|
)
|
|
if isinstance(limiter, FreqLimiter):
|
|
limiter.start_cd(key_type)
|
|
if isinstance(limiter, UserBlockLimiter):
|
|
limiter.set_true(key_type)
|
|
if isinstance(limiter, CountLimiter):
|
|
limiter.increase(key_type)
|
|
|
|
|
|
async def auth_limit(plugin: PluginInfo, session: Uninfo):
|
|
"""插件限制
|
|
|
|
参数:
|
|
plugin: PluginInfo
|
|
session: Uninfo
|
|
"""
|
|
entity = get_entity_ids(session)
|
|
try:
|
|
await asyncio.wait_for(
|
|
LimitManager.check(
|
|
plugin.module, entity.user_id, entity.group_id, entity.channel_id
|
|
),
|
|
timeout=DB_TIMEOUT_SECONDS * 2, # 给予更长的超时时间
|
|
)
|
|
except asyncio.TimeoutError:
|
|
logger.error(f"检查插件限制超时: {plugin.module}", LOGGER_COMMAND)
|
|
# 超时时不抛出异常,允许继续执行
|