diff --git a/zhenxun/builtin_plugins/hooks/auth/auth_admin.py b/zhenxun/builtin_plugins/hooks/auth/auth_admin.py index 19059f98..a2930f5b 100644 --- a/zhenxun/builtin_plugins/hooks/auth/auth_admin.py +++ b/zhenxun/builtin_plugins/hooks/auth/auth_admin.py @@ -6,13 +6,13 @@ from nonebot_plugin_uninfo import Uninfo from zhenxun.models.level_user import LevelUser from zhenxun.models.plugin_info import PluginInfo +from zhenxun.services.auth_snapshot.exception import SkipPluginException from zhenxun.services.data_access import DataAccess from zhenxun.services.db_context import DB_TIMEOUT_SECONDS from zhenxun.services.log import logger from zhenxun.utils.utils import get_entity_ids from .config import LOGGER_COMMAND, WARNING_THRESHOLD -from .exception import SkipPluginException from .utils import send_message diff --git a/zhenxun/builtin_plugins/hooks/auth/auth_ban.py b/zhenxun/builtin_plugins/hooks/auth/auth_ban.py index efb3027b..8b18b23a 100644 --- a/zhenxun/builtin_plugins/hooks/auth/auth_ban.py +++ b/zhenxun/builtin_plugins/hooks/auth/auth_ban.py @@ -9,13 +9,13 @@ from nonebot_plugin_uninfo import Uninfo from zhenxun.configs.config import Config from zhenxun.models.ban_console import BanConsole from zhenxun.models.plugin_info import PluginInfo +from zhenxun.services.auth_snapshot.exception import SkipPluginException from zhenxun.services.db_context import DB_TIMEOUT_SECONDS from zhenxun.services.log import logger from zhenxun.utils.enum import PluginType from zhenxun.utils.utils import EntityIDs, get_entity_ids from .config import LOGGER_COMMAND, WARNING_THRESHOLD -from .exception import SkipPluginException from .utils import freq, send_message Config.add_plugin_config( diff --git a/zhenxun/builtin_plugins/hooks/auth/auth_bot.py b/zhenxun/builtin_plugins/hooks/auth/auth_bot.py index ab902991..5e29a986 100644 --- a/zhenxun/builtin_plugins/hooks/auth/auth_bot.py +++ b/zhenxun/builtin_plugins/hooks/auth/auth_bot.py @@ -3,13 +3,13 @@ import time from zhenxun.models.bot_console import BotConsole from zhenxun.models.plugin_info import PluginInfo +from zhenxun.services.auth_snapshot.exception import SkipPluginException from zhenxun.services.data_access import DataAccess from zhenxun.services.db_context import DB_TIMEOUT_SECONDS from zhenxun.services.log import logger from zhenxun.utils.common_utils import CommonUtils from .config import LOGGER_COMMAND, WARNING_THRESHOLD -from .exception import SkipPluginException async def auth_bot(plugin: PluginInfo, bot_id: str): diff --git a/zhenxun/builtin_plugins/hooks/auth/auth_cost.py b/zhenxun/builtin_plugins/hooks/auth/auth_cost.py index 53da21a9..59ef862b 100644 --- a/zhenxun/builtin_plugins/hooks/auth/auth_cost.py +++ b/zhenxun/builtin_plugins/hooks/auth/auth_cost.py @@ -4,10 +4,10 @@ from nonebot_plugin_uninfo import Uninfo from zhenxun.models.plugin_info import PluginInfo from zhenxun.models.user_console import UserConsole +from zhenxun.services.auth_snapshot.exception import SkipPluginException from zhenxun.services.log import logger from .config import LOGGER_COMMAND, WARNING_THRESHOLD -from .exception import SkipPluginException from .utils import send_message diff --git a/zhenxun/builtin_plugins/hooks/auth/auth_group.py b/zhenxun/builtin_plugins/hooks/auth/auth_group.py index 20114bef..bb6a4b63 100644 --- a/zhenxun/builtin_plugins/hooks/auth/auth_group.py +++ b/zhenxun/builtin_plugins/hooks/auth/auth_group.py @@ -4,10 +4,10 @@ from nonebot_plugin_alconna import UniMsg from zhenxun.models.group_console import GroupConsole from zhenxun.models.plugin_info import PluginInfo +from zhenxun.services.auth_snapshot.exception import SkipPluginException from zhenxun.services.log import logger from .config import LOGGER_COMMAND, WARNING_THRESHOLD, SwitchEnum -from .exception import SkipPluginException async def auth_group( diff --git a/zhenxun/builtin_plugins/hooks/auth/auth_limit.py b/zhenxun/builtin_plugins/hooks/auth/auth_limit.py index 80650472..1fb9a7a6 100644 --- a/zhenxun/builtin_plugins/hooks/auth/auth_limit.py +++ b/zhenxun/builtin_plugins/hooks/auth/auth_limit.py @@ -8,6 +8,7 @@ from pydantic import BaseModel from zhenxun.models.plugin_info import PluginInfo from zhenxun.models.plugin_limit import PluginLimit +from zhenxun.services.auth_snapshot.exception import SkipPluginException from zhenxun.services.db_context import DB_TIMEOUT_SECONDS from zhenxun.services.log import logger from zhenxun.utils.enum import LimitWatchType, PluginLimitType @@ -18,7 +19,6 @@ 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() diff --git a/zhenxun/builtin_plugins/hooks/auth/auth_plugin.py b/zhenxun/builtin_plugins/hooks/auth/auth_plugin.py index ddab3161..e0c496cc 100644 --- a/zhenxun/builtin_plugins/hooks/auth/auth_plugin.py +++ b/zhenxun/builtin_plugins/hooks/auth/auth_plugin.py @@ -6,13 +6,16 @@ from nonebot_plugin_uninfo import Uninfo from zhenxun.models.group_console import GroupConsole from zhenxun.models.plugin_info import PluginInfo +from zhenxun.services.auth_snapshot.exception import ( + IsSuperuserException, + SkipPluginException, +) from zhenxun.services.db_context import DB_TIMEOUT_SECONDS from zhenxun.services.log import logger from zhenxun.utils.common_utils import CommonUtils from zhenxun.utils.enum import BlockType from .config import LOGGER_COMMAND, WARNING_THRESHOLD -from .exception import IsSuperuserException, SkipPluginException from .utils import freq, is_poke, send_message diff --git a/zhenxun/builtin_plugins/hooks/auth/bot_filter.py b/zhenxun/builtin_plugins/hooks/auth/bot_filter.py index 04e47372..88982d98 100644 --- a/zhenxun/builtin_plugins/hooks/auth/bot_filter.py +++ b/zhenxun/builtin_plugins/hooks/auth/bot_filter.py @@ -2,8 +2,7 @@ import nonebot from nonebot_plugin_uninfo import Uninfo from zhenxun.configs.config import Config - -from .exception import SkipPluginException +from zhenxun.services.auth_snapshot.exception import SkipPluginException Config.add_plugin_config( "hook", diff --git a/zhenxun/builtin_plugins/hooks/auth_hook.py b/zhenxun/builtin_plugins/hooks/auth_hook.py index ded8f9aa..2cf86fac 100644 --- a/zhenxun/builtin_plugins/hooks/auth_hook.py +++ b/zhenxun/builtin_plugins/hooks/auth_hook.py @@ -1,16 +1,21 @@ import time from nonebot.adapters import Bot, Event +from nonebot.exception import IgnoredException from nonebot.matcher import Matcher from nonebot.message import run_postprocessor, run_preprocessor from nonebot_plugin_alconna import UniMsg from nonebot_plugin_uninfo import Uninfo from zhenxun.services.auth_snapshot.checker import optimized_auth_checker +from zhenxun.services.auth_snapshot.exception import ( + PermissionExemption, + SkipPluginException, +) from zhenxun.services.log import logger +from .auth.auth_limit import LimitManager from .auth.config import LOGGER_COMMAND -from .auth_checker import LimitManager # # 权限检测 @@ -24,7 +29,19 @@ async def _(matcher: Matcher, event: Event, bot: Bot, session: Uninfo, message: # session, # message, # ) - await optimized_auth_checker.check(matcher, event, bot, session, message) + try: + await optimized_auth_checker.check(matcher, event, bot, session, message) + except SkipPluginException as e: + logger.info(str(e), LOGGER_COMMAND, session=session) + raise IgnoredException(str(e)) + except PermissionExemption as e: + logger.info( + str(e) or "超级用户跳过权限检测...", LOGGER_COMMAND, session=session + ) + raise IgnoredException(str(e)) + except Exception as e: + logger.error(f"权限检测异常: {e}", LOGGER_COMMAND, session=session, e=e) + raise SkipPluginException("权限检测异常") from e logger.debug(f"权限检测耗时:{time.time() - start_time}秒", LOGGER_COMMAND) diff --git a/zhenxun/builtin_plugins/init/__init_cache.py b/zhenxun/builtin_plugins/init/__init_cache.py index 0d1eed5d..a61c332d 100644 --- a/zhenxun/builtin_plugins/init/__init_cache.py +++ b/zhenxun/builtin_plugins/init/__init_cache.py @@ -11,6 +11,7 @@ from zhenxun.models.group_plugin_setting import GroupPluginSetting from zhenxun.models.level_user import LevelUser from zhenxun.models.plugin_info import PluginInfo from zhenxun.models.user_console import UserConsole +from zhenxun.services.auth_snapshot import AuthSnapshot, PluginSnapshot from zhenxun.services.cache import CacheRegistry, cache_config from zhenxun.services.cache.config import CacheMode from zhenxun.services.log import logger @@ -34,6 +35,9 @@ def register_cache_types(): ) CacheRegistry.register(CacheType.BAN, BanConsole, key_format="{user_id}_{group_id}") CacheRegistry.register(CacheType.TEMP, None, 3600) + CacheRegistry.register(CacheType.AUTH_SNAPSHOT, AuthSnapshot) + CacheRegistry.register(CacheType.PLUGIN_SNAPSHOT, PluginSnapshot) + if cache_config.cache_mode == CacheMode.NONE: logger.info("缓存功能已禁用,将直接从数据库获取数据") else: diff --git a/zhenxun/services/auth_snapshot/builder.py b/zhenxun/services/auth_snapshot/builder.py index 6b8ce8b4..59ac3ed8 100644 --- a/zhenxun/services/auth_snapshot/builder.py +++ b/zhenxun/services/auth_snapshot/builder.py @@ -271,7 +271,7 @@ class SnapshotBuilder: queries.append(f""" SELECT 'level_global' as query_type, NULL as gold, user_level, NULL as ban_time, NULL as duration - FROM level_user WHERE user_id = {ph()} AND group_id IS NULL + FROM level_users WHERE user_id = {ph()} AND group_id IS NULL """) params.append(user_id) @@ -280,7 +280,7 @@ class SnapshotBuilder: queries.append(f""" SELECT 'level_group' as query_type, NULL as gold, user_level, NULL as ban_time, NULL as duration - FROM level_user + FROM level_users WHERE user_id = {ph()} AND group_id = {ph()} """) params.extend([user_id, group_id]) diff --git a/zhenxun/services/auth_snapshot/checker.py b/zhenxun/services/auth_snapshot/checker.py index b55390bd..22985338 100644 --- a/zhenxun/services/auth_snapshot/checker.py +++ b/zhenxun/services/auth_snapshot/checker.py @@ -8,7 +8,6 @@ import asyncio import time from nonebot.adapters import Bot, Event -from nonebot.exception import IgnoredException from nonebot.matcher import Matcher from nonebot_plugin_alconna import UniMsg from nonebot_plugin_uninfo import Uninfo @@ -19,10 +18,11 @@ from zhenxun.utils.enum import BlockType, GoldHandle from zhenxun.utils.platform import PlatformUtils from zhenxun.utils.utils import get_entity_ids +from .exception import IsSuperuserException, SkipPluginException from .models import AuthSnapshot, PluginSnapshot from .service import AuthSnapshotService, PluginSnapshotService -LOG_COMMAND = "auth_checker_v2" +LOG_COMMAND = "AuthSnapshotChecker" WARNING_THRESHOLD = 0.5 # 警告阈值(秒) @@ -78,7 +78,7 @@ class OptimizedAuthChecker: if not module: result.fail("Matcher插件名称不存在...") - raise IgnoredException(result.skip_reason) + raise SkipPluginException(result.skip_reason) # 2. 获取权限快照(第一次查询) snapshot_start = time.time() @@ -96,12 +96,12 @@ class OptimizedAuthChecker: if not plugin_snapshot: result.fail(f"插件:{module} 数据不存在...") - raise IgnoredException(result.skip_reason) + raise SkipPluginException(result.skip_reason) # 4. 检查是否为隐藏插件 if plugin_snapshot.is_hidden(): result.fail(f"插件: {plugin_snapshot.name}:{module} 为HIDDEN...") - raise IgnoredException(result.skip_reason) + return # 5. 检查超级用户 is_superuser = session.user.id in bot.config.superusers @@ -122,7 +122,7 @@ class OptimizedAuthChecker: # 7. 处理检查结果 if not result.passed: logger.info(result.skip_reason, LOG_COMMAND, session=session) - raise IgnoredException(result.skip_reason) + raise SkipPluginException(result.skip_reason) # 8. 超级用户跳过后续限制 if is_superuser: @@ -162,12 +162,13 @@ class OptimizedAuthChecker: logger.error( f"扣除金币超时,模块: {module}", LOG_COMMAND, session=session ) - - except IgnoredException: + except IsSuperuserException: + raise + except SkipPluginException: raise except Exception as e: logger.error(f"权限检查异常: {e}", LOG_COMMAND, session=session, e=e) - raise IgnoredException("权限检查异常") from e + raise SkipPluginException("权限检查异常") from e finally: # 记录总执行时间 total_time = time.time() - start_time diff --git a/zhenxun/builtin_plugins/hooks/auth/exception.py b/zhenxun/services/auth_snapshot/exception.py similarity index 100% rename from zhenxun/builtin_plugins/hooks/auth/exception.py rename to zhenxun/services/auth_snapshot/exception.py diff --git a/zhenxun/services/auth_snapshot/service.py b/zhenxun/services/auth_snapshot/service.py index b8db6ffe..0d4fb3a2 100644 --- a/zhenxun/services/auth_snapshot/service.py +++ b/zhenxun/services/auth_snapshot/service.py @@ -108,7 +108,7 @@ class AuthSnapshotService: # 2. 尝试从Redis获取 if not force_refresh and cache_config.cache_mode != CacheMode.NONE: try: - cached = await CacheRoot.get(CacheType.TEMP, cache_key) + cached = await CacheRoot.get(CacheType.AUTH_SNAPSHOT, cache_key) if cached and isinstance(cached, dict): snapshot = AuthSnapshot.model_validate(cached) if not snapshot.is_expired(AUTH_REDIS_TTL): @@ -201,7 +201,7 @@ class AuthSnapshotService: """异步存入Redis""" try: await CacheRoot.set( - CacheType.TEMP, + CacheType.AUTH_SNAPSHOT, cache_key, snapshot.model_dump(), expire=AUTH_REDIS_TTL, @@ -329,7 +329,7 @@ class PluginSnapshotService: # 2. 尝试从Redis获取 if not force_refresh and cache_config.cache_mode != CacheMode.NONE: try: - cached = await CacheRoot.get(CacheType.PLUGINS, cache_key) + cached = await CacheRoot.get(CacheType.PLUGIN_SNAPSHOT, cache_key) if cached and isinstance(cached, dict): snapshot = PluginSnapshot.model_validate(cached) if not snapshot.is_expired(PLUGIN_REDIS_TTL): @@ -395,7 +395,7 @@ class PluginSnapshotService: """异步存入Redis""" try: await CacheRoot.set( - CacheType.PLUGINS, + CacheType.PLUGIN_SNAPSHOT, cache_key, snapshot.model_dump(), expire=PLUGIN_REDIS_TTL, @@ -420,7 +420,7 @@ class PluginSnapshotService: # 清理Redis缓存 if cache_config.cache_mode != CacheMode.NONE: try: - await CacheRoot.delete(CacheType.PLUGINS, cache_key) + await CacheRoot.delete(CacheType.PLUGIN_SNAPSHOT, cache_key) except Exception: pass