ForcePilot/backend/package/yuxi/channel/extensions/matrix/security.py
Kris 4e9c6dd8ab feat(channel): 添加 Matrix 渠道扩展
新增 Matrix 渠道扩展,支持在 Yuxi 平台中集成 Matrix 去中心化通讯协议。

包含以下功能模块:
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- outbound: 外发消息管理
- streaming: 流式消息处理
- pairing: 用户配对与绑定
- security: 安全校验
- crypto: 端到端加密
- dedupe: 消息去重
- monitor: 渠道状态监控
- status: 会话状态管理
- session: 会话管理
- room_resolver: 房间解析
- dm_tracker: 私聊追踪
- rate_limiter: 速率限制
- actions: 动作处理
- constants: 常量定义
- utils: 工具函数
- types: 类型定义
2026-05-21 11:18:13 +08:00

84 lines
3.0 KiB
Python

from __future__ import annotations
import logging
from yuxi.channel.sdk import (
AllowlistHitReason,
)
from .config import _apply_env_overrides, _dict_to_account
from .utils import is_room_id, is_matrix_user_id
logger = logging.getLogger(__name__)
async def check_allowlist(peer_id: str, channel_type: str, config: dict = None, account_id: str = None) -> dict:
from yuxi.channel.sdk import get_allowlist_checker
config = config or {}
checker = await get_allowlist_checker()
aid = account_id or "default"
account_data = config.get("accounts", {}).get(aid, {})
account = _dict_to_account(account_data)
account = _apply_env_overrides(account)
if is_room_id(peer_id):
if account.group_policy == "disabled":
return {"allowed": False, "reason": AllowlistHitReason.GROUP_DISABLED.value}
if account.group_policy == "open":
return {"allowed": True, "reason": AllowlistHitReason.GROUP_OPEN.value}
if not account.group_allow_from:
return {"allowed": True, "reason": AllowlistHitReason.ALLOW_FROM_EMPTY.value}
result = checker.check_group(peer_id)
return {"allowed": result.allowed, "reason": result.reason.value, "matched_entry": result.matched_entry}
if is_matrix_user_id(peer_id):
if account.dm_policy == "disabled":
return {"allowed": False, "reason": AllowlistHitReason.DM_DISABLED.value}
if account.dm_policy == "open":
return {"allowed": True, "reason": AllowlistHitReason.DM_OPEN.value}
if account.dm_policy == "pairing":
result = checker.check_dm(peer_id)
return {
"allowed": result.allowed,
"reason": result.reason.value,
"matched_entry": result.matched_entry,
"pairing_required": result.pairing_required,
}
result = checker.check_dm(peer_id)
return {"allowed": result.allowed, "reason": result.reason.value, "matched_entry": result.matched_entry}
return {"allowed": False, "reason": "unknown_peer_type"}
def resolve_dm_policy(config: dict = None, account_id: str = None) -> dict:
config = config or {}
aid = account_id or "default"
account_data = config.get("accounts", {}).get(aid, {})
account = _dict_to_account(account_data)
account = _apply_env_overrides(account)
return {"dm_policy": account.dm_policy}
def apply_config_fixes(config: dict) -> dict:
return config
def collect_warnings(config, account_id=None, account=None) -> list[str]:
warnings = []
if account:
acct = _dict_to_account(account) if isinstance(account, dict) else account
acct = _apply_env_overrides(acct)
if acct.dm_policy == "open":
warnings.append("DM policy is set to 'open' — anyone can DM the bot")
if acct.group_policy == "open":
warnings.append("Group policy is set to 'open' — anyone can trigger the bot")
return warnings
def collect_audit_findings(config, account_id=None, account=None) -> list[dict]:
return []