feat: 增强功能批量入库 - 双色彩引擎/抖动/杂色清理/品牌色板/PDF导出/编辑工具/分享/i18n 等验收代码
This commit is contained in:
1 parent
02762400fb
commit
f846fabfc6
77 files changed
+11372
-226
No files matched your search
@@ -0,0 +1,114 @@
|
||||
import type Database from "better-sqlite3";
|
||||
|
||||
/**
|
||||
* 兑换码防刷检测(防滥用)。
|
||||
*
|
||||
* 判定规则(命中任一即视为异常):
|
||||
* 1. 同一兑换码 24 小时内被超过 5 个不同 IP 兑换 → 疑似共享/泄露码(薅羊毛);
|
||||
* 2. 同一 IP 10 分钟内对无效兑换码累计超过 50 次尝试 → 疑似暴力枚举。
|
||||
*
|
||||
* IP 追踪依赖 security_events 表(见 src/db/schema.ts):
|
||||
* redemption_logs 无 IP 列,兑换成功/失败事件统一写入 security_events 携带来源 IP。
|
||||
*/
|
||||
|
||||
/** 安全事件类型(对应 security_events.event_type 列的 CHECK 约束) */
|
||||
export type SecurityEventType = "redeem_ok" | "redeem_fail" | "code_disabled";
|
||||
|
||||
/** 安全事件动作(对应 security_events.action) */
|
||||
export type SecurityAction = "redeem" | "consume" | "disable";
|
||||
|
||||
/** 疑似异常时返回给客户端的提示 */
|
||||
export const ABUSE_MESSAGE = "检测到异常操作,请联系客服";
|
||||
|
||||
// ---- 防刷阈值 ----
|
||||
/** 24 小时内同一兑换码来自不同 IP 的数量上限 */
|
||||
const MAX_DISTINCT_IPS_PER_CODE = 5;
|
||||
/** 10 分钟内同一 IP 尝试无效兑换码的次数上限 */
|
||||
const MAX_FAILED_ATTEMPTS_PER_IP = 50;
|
||||
/** 不同 IP 统计窗口 */
|
||||
const DISTINCT_IP_WINDOW_HOURS = 24;
|
||||
/** 无效尝试统计窗口(分钟) */
|
||||
const FAIL_WINDOW_MINUTES = 10;
|
||||
|
||||
/**
|
||||
* 记录一条安全事件(IP 追踪)。
|
||||
* @param ip 来源 IP,可空(如禁用事件无来源)
|
||||
* @param code 涉及/尝试的兑换码(未规范化的原值由调用方负责 normalize)
|
||||
*/
|
||||
export function logSecurityEvent(
|
||||
db: Database.Database,
|
||||
ip: string | null,
|
||||
code: string,
|
||||
action: SecurityAction,
|
||||
eventType: SecurityEventType,
|
||||
): void {
|
||||
db.prepare("INSERT INTO security_events (ip, code, action, event_type) VALUES (?, ?, ?, ?)").run(
|
||||
ip,
|
||||
code,
|
||||
action,
|
||||
eventType,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测兑换码是否存在滥用行为。
|
||||
*
|
||||
* @param db 数据库实例
|
||||
* @param code 兑换码(已规范化)
|
||||
* @param ip 当前请求来源 IP(可选)。提供时同时校验"同一 IP 无效尝试"规则;
|
||||
* 不提供时退化为按该兑换码分组统计。
|
||||
* @returns 判定为滥用返回 true
|
||||
*/
|
||||
export function detectAbuse(db: Database.Database, code: string, ip?: string): boolean {
|
||||
// 规则 1:同一兑换码 24h 内被超过 5 个不同 IP 兑换
|
||||
const distinct = db
|
||||
.prepare(
|
||||
`SELECT COUNT(DISTINCT ip) AS c FROM security_events
|
||||
WHERE code = ? AND event_type = 'redeem_ok'
|
||||
AND created_at >= datetime('now', ?)`,
|
||||
)
|
||||
.get(code, `-${DISTINCT_IP_WINDOW_HOURS} hours`) as { c: number };
|
||||
if (distinct.c > MAX_DISTINCT_IPS_PER_CODE) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 规则 2:同一 IP 10 分钟内无效兑换码尝试超过 50 次
|
||||
if (ip !== undefined && ip !== "") {
|
||||
const failed = db
|
||||
.prepare(
|
||||
`SELECT COUNT(*) AS c FROM security_events
|
||||
WHERE ip = ? AND event_type = 'redeem_fail'
|
||||
AND created_at >= datetime('now', ?)`,
|
||||
)
|
||||
.get(ip, `-${FAIL_WINDOW_MINUTES} minutes`) as { c: number };
|
||||
if (failed.c > MAX_FAILED_ATTEMPTS_PER_IP) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 未提供 IP 时:按该兑换码分组的失败尝试是否有人超限
|
||||
const badIp = db
|
||||
.prepare(
|
||||
`SELECT ip FROM security_events
|
||||
WHERE code = ? AND event_type = 'redeem_fail'
|
||||
AND created_at >= datetime('now', ?)
|
||||
GROUP BY ip HAVING COUNT(*) > ? LIMIT 1`,
|
||||
)
|
||||
.get(code, `-${FAIL_WINDOW_MINUTES} minutes`, MAX_FAILED_ATTEMPTS_PER_IP);
|
||||
return badIp !== undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* 确认滥用后将兑换码禁用并记录安全事件。
|
||||
*
|
||||
* @param code 兑换码(已规范化);不存在时静默(UPDATE 影响 0 行)
|
||||
* @returns 是否实际更新了兑换码状态(false 表示兑换码不存在或已禁用)
|
||||
*/
|
||||
export function markSuspicious(db: Database.Database, code: string): boolean {
|
||||
const result = db
|
||||
.prepare("UPDATE codes SET status = 'disabled' WHERE code = ? AND status != 'disabled'")
|
||||
.run(code);
|
||||
logSecurityEvent(db, null, code, "disable", "code_disabled");
|
||||
return result.changes > 0;
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
import type { NextFunction, Request, RequestHandler, Response } from "express";
|
||||
|
||||
/**
|
||||
* 内存滑动窗口限流器(防刷)。
|
||||
*
|
||||
* 按 (动作名, IP) 维度维护每个窗口内的请求时间戳:
|
||||
* - 窗口内请求数达到 max 时返回 429;
|
||||
* - 时间戳滑出窗口后自动放行(滑动窗口,无需定时器);
|
||||
* - 周期性清理空桶,防止内存无限增长。
|
||||
*
|
||||
* 内存实现适合单进程部署;多进程/多实例部署时应换成 Redis 等共享存储。
|
||||
*/
|
||||
|
||||
/** 限流配置 */
|
||||
export interface RateLimitConfig {
|
||||
/** 窗口大小(毫秒) */
|
||||
windowMs: number;
|
||||
/** 窗口内每个 IP 允许的最大请求数 */
|
||||
max: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 各动作的限流配置(单位:次/小时/IP)。
|
||||
* 常量导出供路由与测试共用。
|
||||
*/
|
||||
export const RATE_LIMITS = {
|
||||
/** 兑换码兑换:10 次/小时/IP */
|
||||
redeem: { windowMs: 60 * 60 * 1000, max: 10 },
|
||||
/** 兑换码核销(下载消耗):30 次/小时/IP */
|
||||
consume: { windowMs: 60 * 60 * 1000, max: 30 },
|
||||
/** 创建支付订单:5 次/小时/IP */
|
||||
orders: { windowMs: 60 * 60 * 1000, max: 5 },
|
||||
/** AI 生成:10 次/小时/IP */
|
||||
chibiGenerate: { windowMs: 60 * 60 * 1000, max: 10 },
|
||||
} as const;
|
||||
|
||||
/** 429 响应文案 */
|
||||
export const RATE_LIMIT_MESSAGE = "操作过于频繁,请稍后再试";
|
||||
|
||||
/**
|
||||
* 限流中间件工厂。
|
||||
* 用法:createRateLimiter(RATE_LIMITS.redeem)("redeem")
|
||||
* 返回的中间件以 action 名称 + req.ip 为键独立计数。
|
||||
*/
|
||||
export function createRateLimiter(config: RateLimitConfig): (action: string) => RequestHandler {
|
||||
/** key = `${action}:${ip}` → 窗口内请求时间戳(升序) */
|
||||
const buckets = new Map<string, number[]>();
|
||||
let lastCleanup = Date.now();
|
||||
|
||||
const pruneExpired = (now: number): void => {
|
||||
for (const [key, timestamps] of buckets) {
|
||||
while (timestamps.length > 0 && timestamps[0]! <= now - config.windowMs) {
|
||||
timestamps.shift();
|
||||
}
|
||||
if (timestamps.length === 0) {
|
||||
buckets.delete(key);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return (action: string): RequestHandler => {
|
||||
return (req: Request, res: Response, next: NextFunction): void => {
|
||||
const now = Date.now();
|
||||
|
||||
// 周期性清理过期桶(每个窗口周期一次,摊销成本)
|
||||
if (now - lastCleanup > config.windowMs) {
|
||||
pruneExpired(now);
|
||||
lastCleanup = now;
|
||||
}
|
||||
|
||||
const ip = req.ip ?? "unknown";
|
||||
const key = `${action}:${ip}`;
|
||||
|
||||
// 剔除已滑出窗口的时间戳
|
||||
const timestamps = buckets.get(key) ?? [];
|
||||
while (timestamps.length > 0 && timestamps[0]! <= now - config.windowMs) {
|
||||
timestamps.shift();
|
||||
}
|
||||
|
||||
if (timestamps.length >= config.max) {
|
||||
res.status(429).json({ error: RATE_LIMIT_MESSAGE });
|
||||
return;
|
||||
}
|
||||
|
||||
timestamps.push(now);
|
||||
buckets.set(key, timestamps);
|
||||
next();
|
||||
};
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user