""" 防封模块 — 内容防封守卫 1. 敏感词过滤(黑名单 + 正则模式匹配) 2. 内容差异化(变量替换 + 零宽字符 + 同义词 + 表情随机) 3. 消息唯一性保障 """ import random import re import time import logging from typing import List, Optional logger = logging.getLogger(__name__) SENSITIVE_WORDS: List[str] = [ "加我微信", "微信号", "扫码", "二维码", "转账", "红包", "免费领", "赚钱", "兼职", "日赚", "月入", "暴利", "刷单", "代理", "优惠券", "点击链接", "限时", "秒杀", "抢购", "加群", "进群", "私聊我", "代购", "佣金", "保证赚", "零风险", "稳赚不赔", "日入过万", "躺赚", "加Telegram", "加TG", "加WhatsApp", "加WX", "V信", "威信", "薇信", "WX号", "银行卡", "信用卡套现", "贷款", "网贷", "色情", "赌博", "博彩", "棋牌", ] SENSITIVE_PATTERNS: List[re.Pattern] = [ re.compile(r"https?://\S+"), re.compile(r"www\.\S+"), re.compile(r"[a-zA-Z0-9]{6,}\.(?:com|cn|net|org|xyz|top|cc|vip)\b"), re.compile(r"(? str: result = text for word, replacement in SAFE_REPLACEMENTS.items(): result = result.replace(word, replacement) for word in SENSITIVE_WORDS: if word in result and word not in SAFE_REPLACEMENTS: result = result.replace(word, "*" * len(word)) for pat in SENSITIVE_PATTERNS: result = pat.sub("[已过滤]", result) return result def has_sensitive_words(text: str) -> List[str]: found = [] for word in SENSITIVE_WORDS: if word in text: found.append(word) for pat in SENSITIVE_PATTERNS: matches = pat.findall(text) if matches: found.extend(matches[:3]) return found def _get_greeting() -> str: hour = time.localtime().tm_hour for hr_range, greetings in GREETINGS_BY_HOUR.items(): if hour in hr_range: return random.choice(greetings) return "您好" def diversify_content( text: str, variables: Optional[dict] = None, add_invisible: bool = True, add_emoji: bool = False, ) -> str: """ 内容差异化处理,确保每条消息唯一。 Args: text: 原始消息文本 variables: 自定义变量 {"{昵称}": "小明"} 等 add_invisible: 是否插入零宽字符增加唯一性 add_emoji: 是否在末尾随机添加表情 """ result = text result = result.replace("{时间}", _get_greeting()) result = result.replace("{日期}", time.strftime("%m月%d日")) if variables: for k, v in variables.items(): result = result.replace(k, str(v)) result = filter_sensitive(result) if add_invisible and len(result) > 2 and not result.startswith("CMD:") and not result.startswith("{") and not result.startswith("[") and not result.startswith("http"): num_inserts = random.randint(1, min(3, len(result) // 4)) for _ in range(num_inserts): pos = random.randint(1, len(result) - 1) char = random.choice(INVISIBLE_CHARS) result = result[:pos] + char + result[pos:] if add_emoji and random.random() < 0.4: result = result.rstrip() + random.choice(EMOJI_POOL) return result def batch_diversify(texts: List[str], **kwargs) -> List[str]: return [diversify_content(t, **kwargs) for t in texts]