✨ feat(hook): 增强认证钩子和运行时缓存管理 (#2106)

* ✨ feat(hook): 增强认证钩子和运行时缓存管理
```
♻ refactor(hook): 移除未使用的配置项并优化缓存设置

移除 AUTH_HOOKS_CONCURRENCY_LIMIT 配置项,该配置项不再使用

✨feat(auth_ban): 简化缓存配置并添加实体参数支持

将 BAN_CACHE_TTL 相关配置从动态配置改为常量定义,
移除复杂的 TTL 值转换逻辑,并为 auth_ban 函数添加可选的
entity 参数以支持外部传入实体信息

♻ refactor(auth_limit): 移除未使用的配置依赖

移除 AUTH_LIMIT_NOTICE_CD 配置项,直接使用常量值 2
作为限制通知冷却时间

📦 依赖更新: update playwright dependency to version 1.57.0 in pyproject.toml and requirements.txt
```

* ✨ feat(auth_checker): 增强插件模块处理和预过滤逻辑,支持用户插件兼容性

* ✨ feat(http_utils): 添加内容缓存机制以优化HTTP响应处理

* ✨ feat(cache): 添加群组插件设置视图缓存类型并更新相关逻辑

* ✨ feat(renderer): 优化渲染引擎,增加内存缓存管理和HTML文档处理逻辑

* ✨ feat(sign_in): 添加好感度排行和好感度总排行命令

* ✨ feat(renderer): 增强浏览器实例管理和模板预处理,支持历史 include 语法兼容

* ✨ feat(renderer): 优化 Playwright 环境检查逻辑,增加结果缓存以提高性能

* ✨ feat(renderer): 增强模板文件渲染策略,优化资产加载路径处理

* 🚨 auto fix by pre-commit hooks

* ✨ feat(log): 增加日志内容安全序列化,避免超长 base64 等污染日志
✨ feat(log_sanitizer): 添加对嵌入超长 base64/data URI 的清理功能
✨ feat(auth_checker): 添加 Alconna 快捷方式缓存检查,优化路由匹配逻辑

* feat(renderer): 添加渲染结果内存缓存功能以优化性能
feat(help): 实现帮助菜单图像缓存机制
feat(sign_in): 更新HTML卡片生成以支持动画禁用和剪裁
feat(superuser): 在启动时预热超级用户帮助缓存
feat(theme): 优化主题管理器的资源解析缓存

* 🚨 auto fix by pre-commit hooks

* feat(renderer): 增加全页面视口最大宽度限制并优化内容尺寸计算

---------

Co-authored-by: ATTomatoo <1126160939@qq.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: HibiKier <45528451+HibiKier@users.noreply.github.com>
This commit is contained in:
ManyManyTomato
2026-03-03 15:54:47 +08:00
committed by GitHub
co-authored by ATTomatoo pre-commit-ci[bot] HibiKier
parent 51f4773e14
commit ce94f63d9a
30 changed files with 2392 additions and 638 deletions
+25 -1
View File
@@ -17,6 +17,7 @@ from zhenxun.services import (
generate,
)
from zhenxun.services.log import logger
from zhenxun.services.renderer.result_cache import RenderResultMemoryCache
from zhenxun.ui.models import PluginMenuCategory, PluginMenuData
from zhenxun.utils.common_utils import format_usage_for_markdown
from zhenxun.utils.enum import BlockType, PluginType
@@ -28,6 +29,11 @@ random_bk_path = IMAGE_PATH / "background" / "help" / "simple_help"
background = IMAGE_PATH / "background" / "0.png"
driver = nonebot.get_driver()
_HELP_MENU_IMAGE_CACHE = RenderResultMemoryCache(
ttl_seconds=300,
max_items=64,
max_total_bytes=64 * 1024 * 1024,
)
def _create_plugin_menu_item(
@@ -121,7 +127,25 @@ async def create_help_img(
categories=categories_objects,
)
return await ui.render(menu_data)
cache_payload = {
"self_id": session.self_id,
"group_id": group_id,
"is_detail": is_detail,
"theme": Config.get_config("UI", "THEME", "default"),
"menu_data": menu_data,
}
cache_key = RenderResultMemoryCache.build_key(cache_payload)
if cached_image := await _HELP_MENU_IMAGE_CACHE.get(cache_key):
return cached_image
image_bytes = await ui.render(
menu_data,
clip_selector=".wrapper",
clip_padding=20,
disable_animations=True,
)
await _HELP_MENU_IMAGE_CACHE.set(cache_key, image_bytes)
return image_bytes
async def get_user_allow_help(user_id: str) -> list[PluginType]:
@@ -58,14 +58,5 @@ Config.add_plugin_config(
type=bool,
)
Config.add_plugin_config(
"hook",
"AUTH_HOOKS_CONCURRENCY_LIMIT",
5,
help="同步进入权限钩子最大并发数",
default_value=5,
type=int,
)
nonebot.load_plugins(str(Path(__file__).parent.resolve()))
+12 -46
View File
@@ -26,52 +26,13 @@ Config.add_plugin_config(
"才不会给你发消息.",
help="对被ban用户发送的消息",
)
Config.add_plugin_config(
"hook",
"BAN_CACHE_TTL",
2,
help="ban cache ttl seconds",
)
Config.add_plugin_config(
"hook",
"BAN_CACHE_TTL_POSITIVE",
30,
help="ban cache ttl seconds for banned users",
)
Config.add_plugin_config(
"hook",
"BAN_CACHE_TTL_NEGATIVE",
5,
help="ban cache ttl seconds for non-banned users",
)
def _coerce_ttl(value, default):
try:
value_int = int(value)
except (TypeError, ValueError):
return default
return value_int if value_int >= 0 else default
_ban_cache_ttl_value = Config.get_config("hook", "BAN_CACHE_TTL", 2)
try:
_ban_cache_ttl_value = int(_ban_cache_ttl_value)
except (TypeError, ValueError):
_ban_cache_ttl_value = 2
_ban_cache_ttl_positive = _coerce_ttl(
Config.get_config("hook", "BAN_CACHE_TTL_POSITIVE", _ban_cache_ttl_value),
_ban_cache_ttl_value,
)
_ban_cache_ttl_negative = _coerce_ttl(
Config.get_config("hook", "BAN_CACHE_TTL_NEGATIVE", _ban_cache_ttl_value),
_ban_cache_ttl_value,
)
BAN_CACHE_TTL = 2
BAN_CACHE_TTL_POSITIVE = 30
BAN_CACHE_TTL_NEGATIVE = 5
BAN_CACHE = (
CacheDict("AUTH_BAN_CACHE", expire=0)
if max(_ban_cache_ttl_positive, _ban_cache_ttl_negative) > 0
if max(BAN_CACHE_TTL_POSITIVE, BAN_CACHE_TTL_NEGATIVE) > 0
else None
)
@@ -92,7 +53,7 @@ def _ban_cache_get(key: str) -> int | None:
def _ban_cache_set(key: str, value: int) -> None:
if not BAN_CACHE:
return
ttl = _ban_cache_ttl_positive if value else _ban_cache_ttl_negative
ttl = BAN_CACHE_TTL_POSITIVE if value else BAN_CACHE_TTL_NEGATIVE
if ttl <= 0:
return
BAN_CACHE.set(key, value, expire=ttl)
@@ -255,7 +216,11 @@ async def user_handle(plugin: PluginInfo, entity: EntityIDs, session: Uninfo) ->
async def auth_ban(
matcher: Matcher, bot: Bot, session: Uninfo, plugin: PluginInfo
matcher: Matcher,
bot: Bot,
session: Uninfo,
plugin: PluginInfo,
entity: EntityIDs | None = None,
) -> None:
"""权限检查 - ban 检查
@@ -270,7 +235,8 @@ async def auth_ban(
return
if not matcher.plugin_name:
return
entity = get_entity_ids(session)
if entity is None:
entity = get_entity_ids(session)
if entity.user_id in bot.config.superusers:
return
if entity.group_id:
@@ -6,7 +6,6 @@ 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 (
@@ -27,13 +26,7 @@ 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_CD = 2
_LIMIT_NOTICE_LIMITER = FreqLimiter(_LIMIT_NOTICE_CD)
_LIMIT_NOTICE_TASKS: set[asyncio.Task] = set()
File diff suppressed because it is too large Load Diff
+123 -10
View File
@@ -1,4 +1,5 @@
import asyncio
import contextlib
import time
from nonebot import get_driver
@@ -6,33 +7,39 @@ from nonebot.adapters import Bot, Event
from nonebot.exception import IgnoredException
from nonebot.matcher import Matcher
from nonebot.message import event_preprocessor, run_postprocessor, run_preprocessor
from nonebot.typing import T_State
from nonebot_plugin_alconna import UniMsg
from nonebot_plugin_uninfo import Uninfo
from zhenxun.services.cache.runtime_cache import is_cache_ready
from zhenxun.services.log import logger
from zhenxun.services.message_load import is_overloaded
from zhenxun.services.runtime_bootstrap import register_runtime_bootstrap
from zhenxun.utils.utils import get_entity_ids
from .auth.config import LOGGER_COMMAND
from .auth_checker import (
LimitManager,
_get_event_cache,
_get_route_context,
auth,
route_precheck,
start_auth_runtime_tasks,
stop_auth_runtime_tasks,
)
_SKIP_AUTH_PLUGINS = {"chat_history", "chat_message"}
_BOT_CONNECT_TS: float | None = None
_AUTH_QUEUE_MAXSIZE = 200
_AUTH_QUEUE: asyncio.Queue[tuple[Matcher, Event, Bot, Uninfo, UniMsg]] = asyncio.Queue(
maxsize=_AUTH_QUEUE_MAXSIZE
_AUTH_QUEUE: asyncio.Queue[tuple[Matcher, Event, Bot, Uninfo, UniMsg | None]] = (
asyncio.Queue(maxsize=_AUTH_QUEUE_MAXSIZE)
)
_AUTH_QUEUE_STARTED = False
_AUTH_WORKERS: list[asyncio.Task] = []
_LAST_DROP_LOG = 0.0
driver = get_driver()
register_runtime_bootstrap(driver)
@driver.on_bot_connect
@@ -63,6 +70,17 @@ async def _auth_worker(worker_id: int) -> None:
_AUTH_QUEUE.task_done()
def _extract_plain_text(message: UniMsg | None, event: Event) -> str:
if message is not None:
with contextlib.suppress(Exception):
return message.extract_plain_text()
with contextlib.suppress(Exception):
plain = event.get_plaintext()
if plain:
return plain.strip()
return ""
@driver.on_startup
async def _start_auth_queue():
global _AUTH_QUEUE_STARTED
@@ -72,6 +90,26 @@ async def _start_auth_queue():
worker_count = max(1, min(6, _AUTH_QUEUE_MAXSIZE // 50))
for idx in range(worker_count):
_AUTH_WORKERS.append(asyncio.create_task(_auth_worker(idx)))
await start_auth_runtime_tasks()
@driver.on_shutdown
async def _stop_auth_queue():
global _AUTH_QUEUE_STARTED
_AUTH_QUEUE_STARTED = False
workers = _AUTH_WORKERS.copy()
_AUTH_WORKERS.clear()
for task in workers:
task.cancel()
if workers:
await asyncio.gather(*workers, return_exceptions=True)
while not _AUTH_QUEUE.empty():
with contextlib.suppress(Exception):
_AUTH_QUEUE.get_nowait()
_AUTH_QUEUE.task_done()
await stop_auth_runtime_tasks()
def _skip_auth_for_plugin(matcher: Matcher) -> bool:
@@ -84,6 +122,35 @@ def _skip_auth_for_plugin(matcher: Matcher) -> bool:
return "chat_history" in module_name
def _resolve_actor_user_id(event: Event, fallback_user_id: str) -> str:
"""优先使用事件发起者ID,避免 notice 场景 session.user 指向 bot 自身。"""
event_user_id = getattr(event, "user_id", None)
if event_user_id is None:
return fallback_user_id
event_user_id = str(event_user_id)
return event_user_id or fallback_user_id
def _resolve_event_group_id(event: Event, fallback_group_id: str | None) -> str | None:
"""notice 场景 session.group 可能缺失,回退到事件上的 group_id。"""
event_group_id = getattr(event, "group_id", None)
if event_group_id is None:
return fallback_group_id
resolved = str(event_group_id)
return resolved or fallback_group_id
def _resolve_event_channel_id(
event: Event, fallback_channel_id: str | None
) -> str | None:
"""频道场景回退到事件上的 channel_id。"""
event_channel_id = getattr(event, "channel_id", None)
if event_channel_id is None:
return fallback_channel_id
resolved = str(event_channel_id)
return resolved or fallback_channel_id
@event_preprocessor
async def _drop_message_before_cache_ready(event: Event):
if event.get_type() != "message":
@@ -98,15 +165,56 @@ async def _drop_message_before_cache_ready(event: Event):
@run_preprocessor
async def _auth_preprocessor(
matcher: Matcher, event: Event, bot: Bot, session: Uninfo, message: UniMsg
matcher: Matcher,
event: Event,
bot: Bot,
session: Uninfo,
state: T_State,
message: UniMsg | None = None,
):
if event.get_type() == "message" and not is_cache_ready():
raise IgnoredException("cache not ready ignore")
start_time = time.time()
entity = get_entity_ids(session)
_get_event_cache(event, session, entity)
entity = state.get("_zx_entity")
if entity is None:
entity = get_entity_ids(session)
entity.user_id = _resolve_actor_user_id(event, entity.user_id)
entity.group_id = _resolve_event_group_id(event, entity.group_id)
entity.channel_id = _resolve_event_channel_id(event, entity.channel_id)
state["_zx_entity"] = entity
if await route_precheck(matcher, event, session, message):
event_cache = state.get("_zx_event_cache")
if event_cache is None:
event_cache = _get_event_cache(event, session, entity)
state["_zx_event_cache"] = event_cache
text = state.get("_zx_plain_text")
if text is None:
text = _extract_plain_text(message, event)
state["_zx_plain_text"] = text
if event_cache is not None:
event_cache["plain_text"] = text
route_modules = state.get("_zx_route_modules")
if route_modules is None:
route_modules = await _get_route_context(text, event_cache)
state["_zx_route_modules"] = route_modules
is_superuser = state.get("_zx_is_superuser")
if is_superuser is None:
is_superuser = entity.user_id in bot.config.superusers
state["_zx_is_superuser"] = is_superuser
if await route_precheck(
matcher,
event,
session,
message,
entity=entity,
event_cache=event_cache,
text=text,
route_modules=route_modules,
):
return
if _skip_auth_for_plugin(matcher):
return
@@ -119,6 +227,11 @@ async def _auth_preprocessor(
session,
message,
skip_ban=False,
entity=entity,
event_cache=event_cache,
text=text,
route_modules=route_modules,
is_superuser=is_superuser,
)
except IgnoredException:
raise
@@ -137,10 +250,10 @@ async def _auth_preprocessor(
@run_postprocessor
async def _unblock_after_matcher(matcher: Matcher, session: Uninfo):
user_id = session.user.id
group_id = None
channel_id = None
async def _unblock_after_matcher(matcher: Matcher, session: Uninfo, event: Event):
user_id = _resolve_actor_user_id(event, session.user.id)
group_id = _resolve_event_group_id(event, None)
channel_id = _resolve_event_channel_id(event, None)
if session.group:
if session.group.parent:
group_id = session.group.parent.id
@@ -29,6 +29,7 @@ def register_cache_types():
GroupPluginSetting,
key_format="{group_id}_{plugin_name}_{key}",
)
CacheRegistry.register(CacheType.GROUP_PLUGIN_SETTINGS_VIEW, dict)
CacheRegistry.register(
CacheType.LEVEL, LevelUser, key_format="{user_id}_{group_id}"
)
@@ -44,6 +44,8 @@ __plugin_meta__ = PluginMetadata(
commands=[
Command(command="签到"),
Command(command="我的签到"),
Command(command="好感度排行"),
Command(command="好感度总排行"),
Command(command="签到排行"),
Command(command="签到总排行"),
],
+51 -4
View File
@@ -10,6 +10,7 @@ from nonebot_plugin_uninfo import Uninfo
from zhenxun import ui
from zhenxun.configs.config import BotConfig, Config
from zhenxun.configs.path_config import THEMES_PATH
from zhenxun.models.sign_user import SignUser
from zhenxun.services import avatar_service
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
@@ -48,6 +49,39 @@ LG_MESSAGE = [
]
def _get_sign_template_files() -> list[Path]:
theme_name = str(Config.get_config("UI", "THEME", "default") or "default")
files: list[Path] = []
theme_candidates = [theme_name]
if theme_name != "default":
theme_candidates.append("default")
for candidate in theme_candidates:
base = THEMES_PATH / candidate / "pages" / "builtin" / "sign"
for file_name in ("main.html", "style.css", "manifest.json"):
file_path = base / file_name
if file_path.exists():
files.append(file_path)
return files
def _is_sign_card_cache_stale(card_file: Path) -> bool:
if not card_file.exists():
return False
try:
card_mtime = card_file.stat().st_mtime
except OSError:
return True
for template_file in _get_sign_template_files():
try:
if template_file.stat().st_mtime > card_mtime:
return True
except OSError:
continue
return False
@PriorityLifecycle.on_startup(priority=5)
async def init_image():
SIGN_TODAY_CARD_PATH.mkdir(exist_ok=True, parents=True)
@@ -86,13 +120,17 @@ async def get_card(
card_file = SIGN_TODAY_CARD_PATH / file_name
if card_file.exists():
return card_file
if not _is_sign_card_cache_stale(card_file):
return card_file
card_file.unlink(missing_ok=True)
if add_impression == -1:
view_name = f"{user_id}_view_{date}.png"
view_card_file = SIGN_TODAY_CARD_PATH / view_name
if view_card_file.exists():
return view_card_file
if not _is_sign_card_cache_stale(view_card_file):
return view_card_file
view_card_file.unlink(missing_ok=True)
is_card_view = True
return await _generate_html_card(
@@ -171,7 +209,9 @@ async def _generate_html_card(
card_file = SIGN_TODAY_CARD_PATH / file_name
if card_file.exists():
return card_file
if not _is_sign_card_cache_stale(card_file):
return card_file
card_file.unlink(missing_ok=True)
impression = float(user.impression)
user_console = await user.user_console
@@ -283,7 +323,14 @@ async def _generate_html_card(
"total_gold": total_gold,
}
image_bytes = await ui.render_template("pages/builtin/sign", data=card_data)
image_bytes = await ui.render_template(
"pages/builtin/sign",
data=card_data,
clip_selector=".wrapper",
clip_padding=8,
disable_animations=True,
screenshot_scale="css",
)
async with aiofiles.open(card_file, "wb") as f:
await f.write(image_bytes)
@@ -8,6 +8,7 @@ from zhenxun.services.help_service import create_plugin_help_image
from zhenxun.services.log import logger
from zhenxun.utils.enum import PluginType
from zhenxun.utils.exception import EmptyError
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
from zhenxun.utils.message import MessageUtils
__plugin_meta__ = PluginMetadata(
@@ -32,6 +33,14 @@ async def build_html_help() -> bytes:
)
@PriorityLifecycle.on_startup(priority=15)
async def _prewarm_super_help_cache() -> None:
try:
await build_html_help()
except Exception as e:
logger.warning("预热超级用户帮助缓存失败", "超级用户帮助", e=e)
_matcher = on_alconna(
Alconna("超级用户帮助"),
permission=SUPERUSER,
@@ -93,7 +93,7 @@ async def _(bot_id: str | None = None) -> Result[QueryCount]:
@router.get(
"get_fg_count",
"/get_fg_count",
dependencies=[authentication()],
response_model=Result[dict[str, int]],
response_class=JSONResponse,