@aomex/abuse 4.3.0 → 4.4.1-alpha.c1419fd5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/dist/index.d.ts +90 -24
- package/dist/index.js +154 -61
- package/dist/index.js.map +1 -1
- package/package.json +13 -6
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [4.4.0](https://github.com/aomex/aomex/compare/v4.3.0...v4.4.0) (2026-11-22)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @aomex/abuse
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
6
14
|
# [4.3.0](https://github.com/aomex/aomex/compare/v4.2.0...v4.3.0) (2026-11-21)
|
|
7
15
|
|
|
8
16
|
### Features
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { CacheAdapter } from "@aomex/cache";
|
|
2
2
|
import { DurationInput } from "@aomex/common";
|
|
3
3
|
import { HttpError, WebContext, WebMiddleware } from "@aomex/web";
|
|
4
|
+
import { SocketMiddleware, WebsocketContext } from "@aomex/websocket";
|
|
4
5
|
/** 用户主体标识。存储前统一 `String(id)` */
|
|
5
6
|
type AbuseId = string | number;
|
|
6
7
|
/** 路径匹配:精确路径、简单 glob,或正则(正则会匹配 pathname+search) */
|
|
7
8
|
type AbusePathMatcher = string | RegExp;
|
|
8
9
|
/**
|
|
9
|
-
*
|
|
10
|
+
* HTTP 路径蜜罐:命中后按 IP 计数,满额封 IP。仅 `.web()` 生效。
|
|
10
11
|
*/
|
|
11
12
|
type AbuseHoneypotStrategy = {
|
|
12
13
|
strategy: 'honeypot';
|
|
@@ -30,7 +31,7 @@ type AbuseHoneypotStrategy = {
|
|
|
30
31
|
blockFor: DurationInput;
|
|
31
32
|
};
|
|
32
33
|
/**
|
|
33
|
-
*
|
|
34
|
+
* 按 HTTP 响应状态码累计。满额后封 IP。仅 `.web()` 生效。
|
|
34
35
|
*/
|
|
35
36
|
type AbuseHttpErrorStrategy = {
|
|
36
37
|
strategy: 'http-error';
|
|
@@ -60,7 +61,28 @@ type AbuseHttpErrorStrategy = {
|
|
|
60
61
|
blockFor: DurationInput;
|
|
61
62
|
};
|
|
62
63
|
/**
|
|
63
|
-
*
|
|
64
|
+
* WebSocket 事件蜜罐:命中 `events` 后按 IP 计数,满额封 IP。仅 `.socket()` 生效。
|
|
65
|
+
*/
|
|
66
|
+
type AbuseSocketEventsStrategy = {
|
|
67
|
+
strategy: 'socket-events';
|
|
68
|
+
/**
|
|
69
|
+
* 蜜罐事件名(不能为空)。字符串支持 `*`;正则整串匹配 `ctx.event`
|
|
70
|
+
*/
|
|
71
|
+
events: readonly (string | RegExp)[];
|
|
72
|
+
/** 窗口内累计次数上限,须为正整数 */
|
|
73
|
+
limit: number;
|
|
74
|
+
/**
|
|
75
|
+
* 固定计数窗口(须为正数时长)。自该 IP 首次命中起算,窗口内累计次数;到期后计数清零重开
|
|
76
|
+
*/
|
|
77
|
+
window: DurationInput;
|
|
78
|
+
/**
|
|
79
|
+
* 撞线后封禁 IP 的时长(须为正数)
|
|
80
|
+
*/
|
|
81
|
+
blockFor: DurationInput;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* 业务上报的用户滥用。只吃返回值上的 `userAbuse()`(HTTP / WebSocket 均可调);
|
|
85
|
+
* 满额触发 `onBanUser`,不自动封 IP。
|
|
64
86
|
*/
|
|
65
87
|
type AbuseUserAbuseStrategy<Reason extends string | number = string | number> = {
|
|
66
88
|
strategy: 'user-abuse';
|
|
@@ -82,7 +104,8 @@ type AbuseUserAbuseStrategy<Reason extends string | number = string | number> =
|
|
|
82
104
|
banFor?: DurationInput;
|
|
83
105
|
};
|
|
84
106
|
/**
|
|
85
|
-
* 业务上报的 IP 滥用(如 logout token 未命中仍返回 204
|
|
107
|
+
* 业务上报的 IP 滥用(如 logout token 未命中仍返回 204)。
|
|
108
|
+
* 只吃 `ipAbuse()`(HTTP / WebSocket 均可调);满额封 IP。
|
|
86
109
|
*/
|
|
87
110
|
type AbuseIpAbuseStrategy<Reason extends string | number = string | number> = {
|
|
88
111
|
strategy: 'ip-abuse';
|
|
@@ -103,7 +126,7 @@ type AbuseIpAbuseStrategy<Reason extends string | number = string | number> = {
|
|
|
103
126
|
*/
|
|
104
127
|
blockFor: DurationInput;
|
|
105
128
|
};
|
|
106
|
-
type AbuseStrategy = AbuseHoneypotStrategy | AbuseHttpErrorStrategy | AbuseUserAbuseStrategy | AbuseIpAbuseStrategy;
|
|
129
|
+
type AbuseStrategy = AbuseHoneypotStrategy | AbuseHttpErrorStrategy | AbuseSocketEventsStrategy | AbuseUserAbuseStrategy | AbuseIpAbuseStrategy;
|
|
107
130
|
type InferUserAbuseReasons<S extends readonly AbuseStrategy[]> = Extract<S[number], {
|
|
108
131
|
strategy: 'user-abuse';
|
|
109
132
|
}> extends {
|
|
@@ -115,7 +138,7 @@ type InferIpAbuseReasons<S extends readonly AbuseStrategy[]> = Extract<S[number]
|
|
|
115
138
|
reason: infer R;
|
|
116
139
|
} ? R extends string | number ? R : never : never;
|
|
117
140
|
/** `onBlockIP` 的 reason */
|
|
118
|
-
type AbuseBlockStrategy = 'honeypot' | 'http-error' | 'ip-abuse' | 'manual';
|
|
141
|
+
type AbuseBlockStrategy = 'honeypot' | 'http-error' | 'socket-events' | 'ip-abuse' | 'manual';
|
|
119
142
|
type AbuseApi<UserAbuseReason extends string | number = string | number, IpAbuseReason extends string | number = string | number> = {
|
|
120
143
|
/**
|
|
121
144
|
* 上报一次用户滥用(按 reason 分桶计数)。满额只回调 `onBanUser`,需业务自行写库封用户
|
|
@@ -140,10 +163,54 @@ type AbuseApi<UserAbuseReason extends string | number = string | number, IpAbuse
|
|
|
140
163
|
*/
|
|
141
164
|
isIPBlocked(ip: string): Promise<boolean>;
|
|
142
165
|
};
|
|
166
|
+
/** `.web()` 可选配置 */
|
|
167
|
+
type AbuseWebOptions = {
|
|
168
|
+
/**
|
|
169
|
+
* 跳过探测与封禁检查(如健康检查、内网)
|
|
170
|
+
*/
|
|
171
|
+
bypass?: (ctx: WebContext) => boolean | Promise<boolean>;
|
|
172
|
+
/**
|
|
173
|
+
* IP 被封禁时抛出的错误。默认:`403 Forbidden`
|
|
174
|
+
*/
|
|
175
|
+
blockedError?: HttpError;
|
|
176
|
+
};
|
|
177
|
+
/** `.socket()` 可选配置 */
|
|
178
|
+
type AbuseSocketOptions = {
|
|
179
|
+
/**
|
|
180
|
+
* 跳过封禁检查(如内网、监控探针)
|
|
181
|
+
*/
|
|
182
|
+
bypass?: (ctx: WebsocketContext) => boolean | Promise<boolean>;
|
|
183
|
+
/**
|
|
184
|
+
* 被封禁时回推 `error` 帧的文案(随后以 1008 断开连接)。默认走 i18n
|
|
185
|
+
*/
|
|
186
|
+
errorMessage?: string;
|
|
187
|
+
};
|
|
143
188
|
/**
|
|
144
|
-
* `createAbuse()`
|
|
189
|
+
* `createAbuse()` 返回值:上报 API + `.web()` / `.socket()` 适配器。
|
|
190
|
+
* 不挂 `ctx`;HTTP / WebSocket 共用同一封禁态与 API。
|
|
145
191
|
*/
|
|
146
|
-
type
|
|
192
|
+
type AbuseService<UserAbuseReason extends string | number = string | number, IpAbuseReason extends string | number = string | number> = AbuseApi<UserAbuseReason, IpAbuseReason> & {
|
|
193
|
+
/**
|
|
194
|
+
* HTTP 中间件。挂在 `WebApp.mount`。
|
|
195
|
+
* 含蜜罐 / http-error 累计与 IP 封禁检查。
|
|
196
|
+
*
|
|
197
|
+
* ```ts
|
|
198
|
+
* mount: [abuse.web({ bypass: (ctx) => ctx.request.pathname === '/health' })]
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
web(options?: AbuseWebOptions): WebMiddleware;
|
|
202
|
+
/**
|
|
203
|
+
* WebSocket 入站中间件。挂在 `WebsocketApp.mount`。
|
|
204
|
+
* 调用前须 `import '@aomex/websocket'`(注册 `middleware.socket`)。
|
|
205
|
+
* IP 已封禁或 `socket-events` 满额时:回推 `error` 并以 1008 断开连接。
|
|
206
|
+
*
|
|
207
|
+
* ```ts
|
|
208
|
+
* import '@aomex/websocket';
|
|
209
|
+
* mount: [abuse.socket(), channels('./src/channels')]
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
212
|
+
socket(options?: AbuseSocketOptions): SocketMiddleware;
|
|
213
|
+
};
|
|
147
214
|
type AbuseOnBanUser<S extends readonly AbuseStrategy[]> = (id: AbuseId, reason: InferUserAbuseReasons<S>, detail: {
|
|
148
215
|
count: number;
|
|
149
216
|
limit: number;
|
|
@@ -154,18 +221,10 @@ type AbuseOptionsBase<S extends readonly AbuseStrategy[]> = {
|
|
|
154
221
|
* 缓存适配器(`@aomex/cache`,如 memory / redis)
|
|
155
222
|
*/
|
|
156
223
|
store: CacheAdapter;
|
|
157
|
-
/**
|
|
158
|
-
* 跳过探测与封禁检查(如健康检查、内网)
|
|
159
|
-
*/
|
|
160
|
-
bypass?: (ctx: WebContext) => boolean | Promise<boolean>;
|
|
161
224
|
/**
|
|
162
225
|
* IP 被封禁时回调(策略撞线、`ipAbuse` 满额或手动 `blockIP`)
|
|
163
226
|
*/
|
|
164
227
|
onBlockIP?: (ip: string, reason: AbuseBlockStrategy, detail: AbuseBlockDetail) => void | Promise<void>;
|
|
165
|
-
/**
|
|
166
|
-
* IP被封禁时抛出的错误。默认:`403 Forbidden`
|
|
167
|
-
*/
|
|
168
|
-
blockedError?: HttpError;
|
|
169
228
|
strategies: S;
|
|
170
229
|
};
|
|
171
230
|
/**
|
|
@@ -195,17 +254,24 @@ type AbuseBlockDetail = {
|
|
|
195
254
|
/**
|
|
196
255
|
* 蜜罐路径 / 错误累计 / 业务滥用计数。
|
|
197
256
|
*
|
|
198
|
-
* - `honeypot` / `http-error
|
|
257
|
+
* - `honeypot` / `http-error`:仅 `.web()` 按请求匹配计数,满额封 IP
|
|
258
|
+
* - `socket-events`:仅 `.socket()` 按事件名蜜罐计数,满额封 IP
|
|
199
259
|
* - `ip-abuse`:业务上报 IP 滥用(如 logout token 未命中),满额封 IP
|
|
200
260
|
* - `user-abuse`:业务上报,满额只触发 `onBanUser`(账号封禁由业务写库)
|
|
201
261
|
*
|
|
202
|
-
*
|
|
262
|
+
* 返回值是服务(API + `.web()` / `.socket()`),不挂到 `ctx`:
|
|
203
263
|
* ```ts
|
|
204
|
-
* const abuse = createAbuse({
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
264
|
+
* const abuse = createAbuse({
|
|
265
|
+
* store,
|
|
266
|
+
* strategies: [
|
|
267
|
+
* { strategy: 'socket-events', events: ['admin.destroy'], limit: 1, window: '1h', blockFor: '1d' },
|
|
268
|
+
* ],
|
|
269
|
+
* });
|
|
270
|
+
* app.mount([abuse.web()]);
|
|
271
|
+
* // 须先 import '@aomex/websocket',以注册 middleware.socket
|
|
272
|
+
* ws.mount([abuse.socket(), channels('./src/channels')]);
|
|
273
|
+
* await abuse.ipAbuse(ip, 'logout-miss');
|
|
208
274
|
* ```
|
|
209
275
|
*/
|
|
210
|
-
export declare const createAbuse: <const S extends readonly AbuseStrategy[]>(options: AbuseOptions<S>) =>
|
|
211
|
-
export type { AbuseApi, AbuseBlockDetail, AbuseBlockStrategy, AbuseHoneypotStrategy, AbuseHttpErrorStrategy, AbuseId, AbuseIpAbuseStrategy,
|
|
276
|
+
export declare const createAbuse: <const S extends readonly AbuseStrategy[]>(options: AbuseOptions<S>) => AbuseService<InferUserAbuseReasons<S>, InferIpAbuseReasons<S>>;
|
|
277
|
+
export type { AbuseApi, AbuseBlockDetail, AbuseBlockStrategy, AbuseHoneypotStrategy, AbuseHttpErrorStrategy, AbuseId, AbuseIpAbuseStrategy, AbuseOptions, AbusePathMatcher, AbuseService, AbuseSocketEventsStrategy, AbuseSocketOptions, AbuseStrategy, AbuseUserAbuseStrategy, AbuseWebOptions, InferIpAbuseReasons, InferUserAbuseReasons };
|
package/dist/index.js
CHANGED
|
@@ -13,7 +13,9 @@ const zh = I18n.define({
|
|
|
13
13
|
invalid_block_for: "blockFor 必须为正数时长,收到:{{blockFor}}",
|
|
14
14
|
invalid_ban_for: "banFor 必须为正数时长,收到:{{banFor}}",
|
|
15
15
|
empty_honeypot_paths: "honeypot 策略的 paths 不能为空",
|
|
16
|
-
empty_http_error_status_codes: "http-error 策略的 statusCodes 不能为空"
|
|
16
|
+
empty_http_error_status_codes: "http-error 策略的 statusCodes 不能为空",
|
|
17
|
+
empty_socket_events: "socket-events 策略的 events 不能为空",
|
|
18
|
+
blocked: "禁止访问"
|
|
17
19
|
});
|
|
18
20
|
// src/i18n/locales/en-us.ts
|
|
19
21
|
const en = I18n.satisfies(zh).define({
|
|
@@ -27,7 +29,9 @@ const en = I18n.satisfies(zh).define({
|
|
|
27
29
|
invalid_block_for: "blockFor must be a positive duration, got: {{blockFor}}",
|
|
28
30
|
invalid_ban_for: "banFor must be a positive duration, got: {{banFor}}",
|
|
29
31
|
empty_honeypot_paths: "honeypot strategy paths must not be empty",
|
|
30
|
-
empty_http_error_status_codes: "http-error strategy statusCodes must not be empty"
|
|
32
|
+
empty_http_error_status_codes: "http-error strategy statusCodes must not be empty",
|
|
33
|
+
empty_socket_events: "socket-events strategy events must not be empty",
|
|
34
|
+
blocked: "Access denied"
|
|
31
35
|
});
|
|
32
36
|
// src/i18n/index.ts
|
|
33
37
|
const i18n = createI18n({
|
|
@@ -62,6 +66,35 @@ function matchMethod(method, methods) {
|
|
|
62
66
|
const upper = method.toUpperCase();
|
|
63
67
|
return methods.some((item) => item.toUpperCase() === upper);
|
|
64
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* 事件名匹配:精确字符串、简单 glob(`*` → 任意字符)、或正则。
|
|
71
|
+
*/
|
|
72
|
+
function matchEvent(event, matcher) {
|
|
73
|
+
if (matcher instanceof RegExp) return matcher.test(event);
|
|
74
|
+
if (!matcher.includes("*")) return event === matcher;
|
|
75
|
+
let pattern = "";
|
|
76
|
+
for (let i = 0; i < matcher.length; i += 1) {
|
|
77
|
+
const char = matcher[i];
|
|
78
|
+
if (char === "*") {
|
|
79
|
+
pattern += ".*";
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (/[.+?^${}()|[\]\\]/.test(char)) {
|
|
83
|
+
pattern += `\\${char}`;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
pattern += char;
|
|
87
|
+
}
|
|
88
|
+
return new RegExp(`^${pattern}$`).test(event);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 任一事件匹配器命中即为 true。
|
|
92
|
+
* `events` 为 `undefined` 时视为不限;空数组不命中。
|
|
93
|
+
*/
|
|
94
|
+
function matchAnyEvent(event, events) {
|
|
95
|
+
if (events === void 0) return true;
|
|
96
|
+
return events.some((item) => matchEvent(event, item));
|
|
97
|
+
}
|
|
65
98
|
function globToRegExp(glob) {
|
|
66
99
|
let source = glob;
|
|
67
100
|
let optionalDeep = false;
|
|
@@ -101,6 +134,8 @@ const validateStrategies = (strategies) => {
|
|
|
101
134
|
if (item.paths.length === 0) throw new Error(i18n.t("empty_honeypot_paths"));
|
|
102
135
|
} else if (item.strategy === "http-error") {
|
|
103
136
|
if (item.statusCodes.length === 0) throw new Error(i18n.t("empty_http_error_status_codes"));
|
|
137
|
+
} else if (item.strategy === "socket-events") {
|
|
138
|
+
if (item.events.length === 0) throw new Error(i18n.t("empty_socket_events"));
|
|
104
139
|
}
|
|
105
140
|
if (item.strategy === "user-abuse") {
|
|
106
141
|
if (item.banFor !== void 0) {
|
|
@@ -126,16 +161,23 @@ const validateStrategies = (strategies) => {
|
|
|
126
161
|
/**
|
|
127
162
|
* 蜜罐路径 / 错误累计 / 业务滥用计数。
|
|
128
163
|
*
|
|
129
|
-
* - `honeypot` / `http-error
|
|
164
|
+
* - `honeypot` / `http-error`:仅 `.web()` 按请求匹配计数,满额封 IP
|
|
165
|
+
* - `socket-events`:仅 `.socket()` 按事件名蜜罐计数,满额封 IP
|
|
130
166
|
* - `ip-abuse`:业务上报 IP 滥用(如 logout token 未命中),满额封 IP
|
|
131
167
|
* - `user-abuse`:业务上报,满额只触发 `onBanUser`(账号封禁由业务写库)
|
|
132
168
|
*
|
|
133
|
-
*
|
|
169
|
+
* 返回值是服务(API + `.web()` / `.socket()`),不挂到 `ctx`:
|
|
134
170
|
* ```ts
|
|
135
|
-
* const abuse = createAbuse({
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
171
|
+
* const abuse = createAbuse({
|
|
172
|
+
* store,
|
|
173
|
+
* strategies: [
|
|
174
|
+
* { strategy: 'socket-events', events: ['admin.destroy'], limit: 1, window: '1h', blockFor: '1d' },
|
|
175
|
+
* ],
|
|
176
|
+
* });
|
|
177
|
+
* app.mount([abuse.web()]);
|
|
178
|
+
* // 须先 import '@aomex/websocket',以注册 middleware.socket
|
|
179
|
+
* ws.mount([abuse.socket(), channels('./src/channels')]);
|
|
180
|
+
* await abuse.ipAbuse(ip, 'logout-miss');
|
|
139
181
|
* ```
|
|
140
182
|
*/
|
|
141
183
|
const createAbuse = (options) => {
|
|
@@ -144,12 +186,6 @@ const createAbuse = (options) => {
|
|
|
144
186
|
const getBlockKey = (ip) => abuseKey.key("block", ip);
|
|
145
187
|
const onBanUser = options.onBanUser;
|
|
146
188
|
const onBlockIP = options.onBlockIP;
|
|
147
|
-
const bypass = options.bypass ?? (() => false);
|
|
148
|
-
const blockedError = options.blockedError;
|
|
149
|
-
const throwBlocked = (ctx) => {
|
|
150
|
-
if (blockedError) throw blockedError;
|
|
151
|
-
return ctx.throw(403);
|
|
152
|
-
};
|
|
153
189
|
const bumpCount = async (countKey, windowMs) => {
|
|
154
190
|
const count = await caching.increment(countKey);
|
|
155
191
|
if (count === 1 || await caching.ttl(countKey) === -1) await caching.expire(countKey, Math.max(windowMs, 1));
|
|
@@ -165,14 +201,20 @@ const createAbuse = (options) => {
|
|
|
165
201
|
};
|
|
166
202
|
const honeypotStrategies = options.strategies.filter((item) => item.strategy === "honeypot");
|
|
167
203
|
const httpErrorStrategies = options.strategies.filter((item) => item.strategy === "http-error");
|
|
204
|
+
const socketEventsStrategies = options.strategies.filter((item) => item.strategy === "socket-events");
|
|
168
205
|
const { userAbuseStrategies, ipAbuseStrategies } = validateStrategies(options.strategies);
|
|
169
206
|
if (userAbuseStrategies.size > 0 && !onBanUser) throw new Error(i18n.t("missing_on_ban_user"));
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
207
|
+
let ready;
|
|
208
|
+
const ensureReady = () => {
|
|
209
|
+
ready ??= registerLifecycle({
|
|
210
|
+
onInit: () => adapter.connect(),
|
|
211
|
+
onDestroy: () => adapter.disconnect()
|
|
212
|
+
});
|
|
213
|
+
return ready;
|
|
214
|
+
};
|
|
174
215
|
const api = {
|
|
175
216
|
async userAbuse(id, reason) {
|
|
217
|
+
await ensureReady();
|
|
176
218
|
const reasonKey = String(reason);
|
|
177
219
|
const strategy = userAbuseStrategies.get(reasonKey);
|
|
178
220
|
if (!strategy) throw new Error(i18n.t("unknown_user_abuse_reason", { reason: reasonKey }));
|
|
@@ -197,6 +239,7 @@ const createAbuse = (options) => {
|
|
|
197
239
|
}
|
|
198
240
|
},
|
|
199
241
|
async ipAbuse(ip, reason) {
|
|
242
|
+
await ensureReady();
|
|
200
243
|
const reasonKey = String(reason);
|
|
201
244
|
const strategy = ipAbuseStrategies.get(reasonKey);
|
|
202
245
|
if (!strategy) throw new Error(i18n.t("unknown_ip_abuse_reason", { reason: reasonKey }));
|
|
@@ -210,6 +253,7 @@ const createAbuse = (options) => {
|
|
|
210
253
|
});
|
|
211
254
|
},
|
|
212
255
|
async blockIP(ip, duration) {
|
|
256
|
+
await ensureReady();
|
|
213
257
|
const blockFor = parseDuration(duration);
|
|
214
258
|
if (blockFor <= 0) throw new Error(i18n.t("invalid_block_for", { blockFor: String(duration) }));
|
|
215
259
|
await ensureBlocked(ip, "manual", {
|
|
@@ -219,55 +263,104 @@ const createAbuse = (options) => {
|
|
|
219
263
|
});
|
|
220
264
|
},
|
|
221
265
|
async unblockIP(ip) {
|
|
266
|
+
await ensureReady();
|
|
222
267
|
await caching.delete(getBlockKey(ip));
|
|
223
268
|
},
|
|
224
269
|
async isIPBlocked(ip) {
|
|
270
|
+
await ensureReady();
|
|
225
271
|
return caching.exists(getBlockKey(ip));
|
|
226
272
|
}
|
|
227
273
|
};
|
|
228
|
-
const
|
|
229
|
-
|
|
230
|
-
const
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
if (
|
|
237
|
-
const
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
274
|
+
const web = (webOptions = {}) => {
|
|
275
|
+
const bypass = webOptions.bypass ?? (() => false);
|
|
276
|
+
const throwBlocked = (ctx) => {
|
|
277
|
+
if (webOptions.blockedError) throw webOptions.blockedError;
|
|
278
|
+
return ctx.throw(403);
|
|
279
|
+
};
|
|
280
|
+
return middleware.web(async (ctx, next) => {
|
|
281
|
+
await ensureReady();
|
|
282
|
+
if (await bypass(ctx)) return next();
|
|
283
|
+
const ip = ctx.request.ip;
|
|
284
|
+
if (await caching.exists(getBlockKey(ip))) return throwBlocked(ctx);
|
|
285
|
+
const request = ctx.request;
|
|
286
|
+
for (let i = 0; i < honeypotStrategies.length; i += 1) {
|
|
287
|
+
const strategy = honeypotStrategies[i];
|
|
288
|
+
if (!matchMethod(request.method, strategy.methods) || !matchAnyPath(request.pathname, request.search, strategy.paths)) continue;
|
|
289
|
+
const countKey = abuseKey.key("count", "honeypot", i, ip);
|
|
290
|
+
const count = await bumpCount(countKey, parseDuration(strategy.window));
|
|
291
|
+
if (count < strategy.limit) continue;
|
|
292
|
+
await ensureBlocked(ip, "honeypot", {
|
|
293
|
+
count,
|
|
294
|
+
limit: strategy.limit,
|
|
295
|
+
blockFor: parseDuration(strategy.blockFor)
|
|
296
|
+
});
|
|
297
|
+
return throwBlocked(ctx);
|
|
298
|
+
}
|
|
299
|
+
let statusCode;
|
|
300
|
+
let thrown;
|
|
301
|
+
try {
|
|
302
|
+
await next();
|
|
303
|
+
statusCode = ctx.response.statusCode;
|
|
304
|
+
} catch (e) {
|
|
305
|
+
thrown = HttpError.from(e);
|
|
306
|
+
statusCode = thrown.statusCode;
|
|
307
|
+
}
|
|
308
|
+
for (let i = 0; i < httpErrorStrategies.length; i += 1) {
|
|
309
|
+
const strategy = httpErrorStrategies[i];
|
|
310
|
+
if (!strategy.statusCodes.includes(statusCode) || !matchMethod(request.method, strategy.methods) || !matchAnyPath(request.pathname, request.search, strategy.paths)) continue;
|
|
311
|
+
const countKey = abuseKey.key("count", "http-error", i, ip);
|
|
312
|
+
const count = await bumpCount(countKey, parseDuration(strategy.window));
|
|
313
|
+
if (count < strategy.limit) continue;
|
|
314
|
+
await ensureBlocked(ip, "http-error", {
|
|
315
|
+
count,
|
|
316
|
+
limit: strategy.limit,
|
|
317
|
+
blockFor: parseDuration(strategy.blockFor)
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
if (thrown) throw thrown;
|
|
321
|
+
});
|
|
322
|
+
};
|
|
323
|
+
const socket = (socketOptions = {}) => {
|
|
324
|
+
const socketBypass = socketOptions.bypass ?? (() => false);
|
|
325
|
+
/** 回推 error 后断开:封禁不是软限流,连接不应继续占用 */
|
|
326
|
+
const rejectBlocked = async (ctx) => {
|
|
327
|
+
const message = socketOptions.errorMessage || i18n.t("blocked");
|
|
328
|
+
try {
|
|
329
|
+
await ctx.client.send("error", { message });
|
|
330
|
+
} finally {
|
|
331
|
+
await ctx.client.close(1008, message);
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
return middleware.socket(async (ctx, next) => {
|
|
335
|
+
await ensureReady();
|
|
336
|
+
if (await socketBypass(ctx)) return next();
|
|
337
|
+
const ip = ctx.client.request.ip;
|
|
338
|
+
if (await caching.exists(getBlockKey(ip))) {
|
|
339
|
+
await rejectBlocked(ctx);
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
for (let i = 0; i < socketEventsStrategies.length; i += 1) {
|
|
343
|
+
const strategy = socketEventsStrategies[i];
|
|
344
|
+
if (!matchAnyEvent(ctx.event, strategy.events)) continue;
|
|
345
|
+
const countKey = abuseKey.key("count", "socket-events", i, ip);
|
|
346
|
+
const count = await bumpCount(countKey, parseDuration(strategy.window));
|
|
347
|
+
if (count < strategy.limit) continue;
|
|
348
|
+
await ensureBlocked(ip, "socket-events", {
|
|
349
|
+
count,
|
|
350
|
+
limit: strategy.limit,
|
|
351
|
+
blockFor: parseDuration(strategy.blockFor)
|
|
352
|
+
});
|
|
353
|
+
await rejectBlocked(ctx);
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
return next();
|
|
357
|
+
});
|
|
358
|
+
};
|
|
359
|
+
return {
|
|
360
|
+
...api,
|
|
361
|
+
web,
|
|
362
|
+
socket
|
|
363
|
+
};
|
|
271
364
|
};
|
|
272
365
|
export { createAbuse };
|
|
273
366
|
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/i18n/locales/zh-cn.ts","../src/i18n/locales/en-us.ts","../src/i18n/index.ts","../src/match.ts","../src/abuse.ts"],"sourcesContent":["import { I18n } from '@aomex/common';\n\nexport const zh = I18n.define({\n unknown_user_abuse_reason: '未知的用户滥用原因:{{reason}}',\n unknown_ip_abuse_reason: '未知的 IP 滥用原因:{{reason}}',\n duplicate_user_abuse_reason: '重复的用户滥用原因:{{reason}}',\n duplicate_ip_abuse_reason: '重复的 IP 滥用原因:{{reason}}',\n missing_on_ban_user: '配置了 user-abuse 策略时必须提供 onBanUser',\n invalid_limit: 'limit 必须为正整数,收到:{{limit}}',\n invalid_window: 'window 必须为正数时长,收到:{{window}}',\n invalid_block_for: 'blockFor 必须为正数时长,收到:{{blockFor}}',\n invalid_ban_for: 'banFor 必须为正数时长,收到:{{banFor}}',\n empty_honeypot_paths: 'honeypot 策略的 paths 不能为空',\n empty_http_error_status_codes: 'http-error 策略的 statusCodes 不能为空',\n});\n","import { I18n } from '@aomex/common';\nimport { zh } from './zh-cn';\n\nexport const en = I18n.satisfies(zh).define({\n unknown_user_abuse_reason: 'Unknown user abuse reason: {{reason}}',\n unknown_ip_abuse_reason: 'Unknown IP abuse reason: {{reason}}',\n duplicate_user_abuse_reason: 'Duplicate user abuse reason: {{reason}}',\n duplicate_ip_abuse_reason: 'Duplicate IP abuse reason: {{reason}}',\n missing_on_ban_user: 'onBanUser is required when user-abuse strategies are configured',\n invalid_limit: 'limit must be a positive integer, got: {{limit}}',\n invalid_window: 'window must be a positive duration, got: {{window}}',\n invalid_block_for: 'blockFor must be a positive duration, got: {{blockFor}}',\n invalid_ban_for: 'banFor must be a positive duration, got: {{banFor}}',\n empty_honeypot_paths: 'honeypot strategy paths must not be empty',\n empty_http_error_status_codes: 'http-error strategy statusCodes must not be empty',\n});\n","import { createI18n } from '@aomex/common';\nimport { zh } from './locales/zh-cn';\nimport { en } from './locales/en-us';\n\nexport const i18n = createI18n({\n zh,\n en,\n load: (file) => import(new URL(`./locales/${file}.js`, import.meta.url).href),\n});\n","/**\n * 路径匹配:精确字符串、简单 glob(`*` / `**`)、或正则。\n * 正则会对 `pathname + search` 检测,便于捕获 query 中的探测特征。\n */\nexport function matchPath(\n pathname: string,\n search: string,\n matcher: string | RegExp,\n): boolean {\n if (matcher instanceof RegExp) {\n return matcher.test(pathname + search);\n }\n\n if (!matcher.includes('*')) {\n return pathname === matcher;\n }\n\n const re = globToRegExp(matcher);\n return re.test(pathname);\n}\n\n/**\n * 任一路径匹配器命中即为 true。\n * `paths` 为 `undefined` 时视为不限路径;空数组不命中。\n */\nexport function matchAnyPath(\n pathname: string,\n search: string,\n paths?: readonly (string | RegExp)[],\n): boolean {\n if (paths === undefined) return true;\n return paths.some((item) => matchPath(pathname, search, item));\n}\n\n/**\n * 匹配 HTTP 方法(忽略大小写)。\n * `methods` 为 `undefined` 或空数组时视为不限方法。\n */\nexport function matchMethod(method: string, methods?: readonly string[]): boolean {\n if (!methods?.length) return true;\n const upper = method.toUpperCase();\n return methods.some((item) => item.toUpperCase() === upper);\n}\n\nfunction globToRegExp(glob: string): RegExp {\n let source = glob;\n let optionalDeep = false;\n if (source.endsWith('/**')) {\n source = source.slice(0, -3);\n optionalDeep = true;\n }\n\n let pattern = '';\n for (let i = 0; i < source.length; i += 1) {\n const char = source[i]!;\n if (char === '*') {\n if (source[i + 1] === '*') {\n pattern += '.*';\n i += 1;\n } else {\n pattern += '[^/]*';\n }\n continue;\n }\n if (/[.+?^${}()|[\\]\\\\]/.test(char)) {\n pattern += `\\\\${char}`;\n continue;\n }\n pattern += char;\n }\n if (optionalDeep) pattern += '(?:/.*)?';\n return new RegExp(`^${pattern}$`);\n}\n","import { CacheKey, Caching } from '@aomex/cache';\nimport { middleware, parseDuration, registerLifecycle } from '@aomex/common';\nimport { HttpError, type WebContext } from '@aomex/web';\nimport { i18n } from './i18n';\nimport { matchAnyPath, matchMethod } from './match';\nimport type {\n AbuseApi,\n AbuseBlockStrategy,\n AbuseHoneypotStrategy,\n AbuseHttpErrorStrategy,\n AbuseIpAbuseStrategy,\n AbuseMiddleware,\n AbuseOptions,\n AbuseStrategy,\n AbuseUserAbuseStrategy,\n InferIpAbuseReasons,\n InferUserAbuseReasons,\n} from './types';\n\nconst abuseKey = new CacheKey().key('abuse');\n\n/** 校验策略并按 reason 分桶;非法配置创建期抛错 */\nconst validateStrategies = (strategies: readonly AbuseStrategy[]) => {\n const userAbuseStrategies = new Map<string, AbuseUserAbuseStrategy>();\n const ipAbuseStrategies = new Map<string, AbuseIpAbuseStrategy>();\n\n for (const item of strategies) {\n if (!Number.isInteger(item.limit) || item.limit <= 0) {\n throw new Error(i18n.t('invalid_limit', { limit: String(item.limit) }));\n }\n const windowMs = parseDuration(item.window);\n if (windowMs <= 0) {\n throw new Error(i18n.t('invalid_window', { window: String(item.window) }));\n }\n if (item.strategy === 'honeypot') {\n if (item.paths.length === 0) {\n throw new Error(i18n.t('empty_honeypot_paths'));\n }\n } else if (item.strategy === 'http-error') {\n if (item.statusCodes.length === 0) {\n throw new Error(i18n.t('empty_http_error_status_codes'));\n }\n }\n if (item.strategy === 'user-abuse') {\n if (item.banFor !== undefined) {\n const banForMs = parseDuration(item.banFor);\n if (banForMs <= 0) {\n throw new Error(i18n.t('invalid_ban_for', { banFor: String(item.banFor) }));\n }\n }\n const reasonKey = String(item.reason);\n if (userAbuseStrategies.has(reasonKey)) {\n throw new Error(i18n.t('duplicate_user_abuse_reason', { reason: reasonKey }));\n }\n userAbuseStrategies.set(reasonKey, item);\n } else {\n const blockForMs = parseDuration(item.blockFor);\n if (blockForMs <= 0) {\n throw new Error(i18n.t('invalid_block_for', { blockFor: String(item.blockFor) }));\n }\n if (item.strategy === 'ip-abuse') {\n const reasonKey = String(item.reason);\n if (ipAbuseStrategies.has(reasonKey)) {\n throw new Error(i18n.t('duplicate_ip_abuse_reason', { reason: reasonKey }));\n }\n ipAbuseStrategies.set(reasonKey, item);\n }\n }\n }\n\n return { userAbuseStrategies, ipAbuseStrategies };\n};\n\n/**\n * 蜜罐路径 / 错误累计 / 业务滥用计数。\n *\n * - `honeypot` / `http-error`:按请求匹配计数,满额封 IP\n * - `ip-abuse`:业务上报 IP 滥用(如 logout token 未命中),满额封 IP\n * - `user-abuse`:业务上报,满额只触发 `onBanUser`(账号封禁由业务写库)\n *\n * 返回值既是中间件,也带 API(不挂到 `ctx`):\n * ```ts\n * const abuse = createAbuse({ store, strategies });\n * app.mount([abuse]);\n * await abuse.ipAbuse(ip, 'logout-miss'); // 路由内上报\n * await abuse.userAbuse(userId, 'order-probe');\n * ```\n */\nexport const createAbuse = <const S extends readonly AbuseStrategy[]>(\n options: AbuseOptions<S>,\n): AbuseMiddleware<InferUserAbuseReasons<S>, InferIpAbuseReasons<S>> => {\n const adapter = options.store;\n const caching = new Caching(adapter);\n const getBlockKey = (ip: string) => abuseKey.key('block', ip);\n const onBanUser = options.onBanUser;\n const onBlockIP = options.onBlockIP;\n const bypass = options.bypass ?? (() => false);\n const blockedError = options.blockedError;\n const throwBlocked = (ctx: WebContext): never => {\n if (blockedError) throw blockedError;\n return ctx.throw(403);\n };\n\n const bumpCount = async (countKey: Caching.Key, windowMs: number): Promise<number> => {\n const count = await caching.increment(countKey);\n if (count === 1 || (await caching.ttl(countKey)) === -1) {\n await caching.expire(countKey, Math.max(windowMs, 1));\n }\n return count;\n };\n\n /**\n * 封 IP:`setNX` 原子抢占,已封禁则跳过钩子;抢到则写入并触发 `onBlockIP`\n */\n const ensureBlocked = async (\n ip: string,\n reason: AbuseBlockStrategy,\n detail: { count: number; limit: number; blockFor: number },\n ): Promise<boolean> => {\n const created = await caching.setNX(getBlockKey(ip), reason, detail.blockFor);\n if (!created) return false;\n await onBlockIP?.(ip, reason, detail);\n return true;\n };\n\n const honeypotStrategies = options.strategies.filter(\n (item): item is AbuseHoneypotStrategy => item.strategy === 'honeypot',\n );\n const httpErrorStrategies = options.strategies.filter(\n (item): item is AbuseHttpErrorStrategy => item.strategy === 'http-error',\n );\n const { userAbuseStrategies, ipAbuseStrategies } = validateStrategies(\n options.strategies,\n );\n\n if (userAbuseStrategies.size > 0 && !onBanUser) {\n throw new Error(i18n.t('missing_on_ban_user'));\n }\n\n void registerLifecycle({\n onInit: () => adapter.connect(),\n onDestroy: () => adapter.disconnect(),\n });\n\n const api: AbuseApi<InferUserAbuseReasons<S>, InferIpAbuseReasons<S>> = {\n async userAbuse(id, reason) {\n const reasonKey = String(reason);\n const strategy = userAbuseStrategies.get(reasonKey);\n if (!strategy) {\n throw new Error(i18n.t('unknown_user_abuse_reason', { reason: reasonKey }));\n }\n const idKey = String(id);\n const countKey = abuseKey.key('count', 'user-abuse', reasonKey, idKey);\n const windowMs = parseDuration(strategy.window);\n const count = await bumpCount(countKey, windowMs);\n if (count < strategy.limit) return;\n\n // 无 IP 封禁键:hook 标记 setNX,避免窗口内重复 / 并发触发 onBanUser\n const hookKey = abuseKey.key('hook', 'user-abuse', reasonKey, idKey);\n const banFor =\n strategy.banFor === undefined ? undefined : parseDuration(strategy.banFor);\n const countTtl = await caching.ttl(countKey);\n const marked = await caching.setNX(\n hookKey,\n '1',\n Math.max(countTtl > 0 ? countTtl : windowMs, 1),\n );\n if (!marked) return;\n try {\n await onBanUser!(id, reason, { count, limit: strategy.limit, banFor });\n } catch (e) {\n // 回调失败则释放 hook,允许窗口内重试\n await caching.delete(hookKey);\n throw e;\n }\n },\n async ipAbuse(ip, reason) {\n const reasonKey = String(reason);\n const strategy = ipAbuseStrategies.get(reasonKey);\n if (!strategy) {\n throw new Error(i18n.t('unknown_ip_abuse_reason', { reason: reasonKey }));\n }\n const countKey = abuseKey.key('count', 'ip-abuse', reasonKey, ip);\n const count = await bumpCount(countKey, parseDuration(strategy.window));\n if (count < strategy.limit) return;\n\n await ensureBlocked(ip, 'ip-abuse', {\n count,\n limit: strategy.limit,\n blockFor: parseDuration(strategy.blockFor),\n });\n },\n async blockIP(ip, duration) {\n const blockFor = parseDuration(duration);\n if (blockFor <= 0) {\n throw new Error(i18n.t('invalid_block_for', { blockFor: String(duration) }));\n }\n await ensureBlocked(ip, 'manual', {\n count: 1,\n limit: 1,\n blockFor,\n });\n },\n async unblockIP(ip) {\n await caching.delete(getBlockKey(ip));\n },\n async isIPBlocked(ip) {\n return caching.exists(getBlockKey(ip));\n },\n };\n\n const md = middleware.web(async (ctx, next) => {\n if (await bypass(ctx)) return next();\n\n const ip = ctx.request.ip;\n if (!ip) return next();\n\n if (await caching.exists(getBlockKey(ip))) {\n return throwBlocked(ctx);\n }\n\n const request = ctx.request;\n\n for (let i = 0; i < honeypotStrategies.length; i += 1) {\n const strategy = honeypotStrategies[i]!;\n if (\n !matchMethod(request.method, strategy.methods) ||\n !matchAnyPath(request.pathname, request.search, strategy.paths)\n ) {\n continue;\n }\n\n const countKey = abuseKey.key('count', 'honeypot', i, ip);\n const count = await bumpCount(countKey, parseDuration(strategy.window));\n if (count < strategy.limit) continue;\n\n await ensureBlocked(ip, 'honeypot', {\n count,\n limit: strategy.limit,\n blockFor: parseDuration(strategy.blockFor),\n });\n return throwBlocked(ctx);\n }\n\n let statusCode: number;\n let thrown: HttpError | undefined;\n try {\n await next();\n statusCode = ctx.response.statusCode;\n } catch (e) {\n thrown = HttpError.from(e);\n statusCode = thrown.statusCode;\n }\n\n for (let i = 0; i < httpErrorStrategies.length; i += 1) {\n const strategy = httpErrorStrategies[i]!;\n if (\n !strategy.statusCodes.includes(statusCode) ||\n !matchMethod(request.method, strategy.methods) ||\n !matchAnyPath(request.pathname, request.search, strategy.paths)\n ) {\n continue;\n }\n\n const countKey = abuseKey.key('count', 'http-error', i, ip);\n const count = await bumpCount(countKey, parseDuration(strategy.window));\n if (count < strategy.limit) continue;\n\n await ensureBlocked(ip, 'http-error', {\n count,\n limit: strategy.limit,\n blockFor: parseDuration(strategy.blockFor),\n });\n }\n\n if (thrown) throw thrown;\n });\n\n return Object.assign(md, api) as AbuseMiddleware<\n InferUserAbuseReasons<S>,\n InferIpAbuseReasons<S>\n >;\n};\n"],"mappings":";;;;AAEA,MAAa,KAAK,KAAK,OAAO;CAC5B,2BAA2B;CAC3B,yBAAyB;CACzB,6BAA6B;CAC7B,2BA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,YAAY,QAAQ,QAAQ,SAAS,OAAO,KAC7C,CAAC,aAAa,QAAQ,UAAU,QAAQ,QAAQ,SAAS,KAAK,GAE9D;GAGF,MAAM,WAAW,SAAS,IAAI,SAAS,YAAY,GAAG,EAAE;GACxD,MAAM,QAAQ,MAAM,UAAU,UAAU,cAAc,SAAS,MAAM,CAAC;GACtE,IAAI,QAAQ,SAAS,OAAO;GAE5B,MAAM,cAAc,IAAI,YAAY;IAClC;IACA,OAAO,SAAS;IAChB,UAAU,cAAc,SAAS,QAAQ;GAC3C,CAAC;GACD,OAAO,aAAa,GAAG;EACzB;EAEA,IAAI;EACJ,IAAI;EACJ,IAAI;GACF,MAAM,KAAK;GACX,aAAa,IAAI,SAAS;EAC5B,SAAS,GAAG;GACV,SAAS,UAAU,KAAK,CAAC;GACzB,aAAa,OAAO;EACtB;EAEA,KAAK,IAAI,IAAI,GAAG,IAAI,oBAAoB,QAAQ,KAAK,GAAG;GACtD,MAAM,WAAW,oBAAoB;GACrC,IACE,CAAC,SAAS,YAAY,SAAS,UAAU,KACzC,CAAC,YAAY,QAAQ,QAAQ,SAAS,OAAO,KAC7C,CAAC,aAAa,QAAQ,UAAU,QAAQ,QAAQ,SAAS,KAAK,GAE9D;GAGF,MAAM,WAAW,SAAS,IAAI,SAAS,cAAc,GAAG,EAAE;GAC1D,MAAM,QAAQ,MAAM,UAAU,UAAU,cAAc,SAAS,MAAM,CAAC;GACtE,IAAI,QAAQ,SAAS,OAAO;GAE5B,MAAM,cAAc,IAAI,cAAc;IACpC;IACA,OAAO,SAAS;IAChB,UAAU,cAAc,SAAS,QAAQ;GAC3C,CAAC;EACH;EAEA,IAAI,QAAQ,MAAM;CACpB,CAAC;CAED,OAAO,OAAO,OAAO,IAAI,GAAG;AAI9B"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/i18n/locales/zh-cn.ts","../src/i18n/locales/en-us.ts","../src/i18n/index.ts","../src/match.ts","../src/abuse.ts"],"sourcesContent":["import { I18n } from '@aomex/common';\n\nexport const zh = I18n.define({\n unknown_user_abuse_reason: '未知的用户滥用原因:{{reason}}',\n unknown_ip_abuse_reason: '未知的 IP 滥用原因:{{reason}}',\n duplicate_user_abuse_reason: '重复的用户滥用原因:{{reason}}',\n duplicate_ip_abuse_reason: '重复的 IP 滥用原因:{{reason}}',\n missing_on_ban_user: '配置了 user-abuse 策略时必须提供 onBanUser',\n invalid_limit: 'limit 必须为正整数,收到:{{limit}}',\n invalid_window: 'window 必须为正数时长,收到:{{window}}',\n invalid_block_for: 'blockFor 必须为正数时长,收到:{{blockFor}}',\n invalid_ban_for: 'banFor 必须为正数时长,收到:{{banFor}}',\n empty_honeypot_paths: 'honeypot 策略的 paths 不能为空',\n empty_http_error_status_codes: 'http-error 策略的 statusCodes 不能为空',\n empty_socket_events: 'socket-events 策略的 events 不能为空',\n blocked: '禁止访问',\n});\n","import { I18n } from '@aomex/common';\nimport { zh } from './zh-cn';\n\nexport const en = I18n.satisfies(zh).define({\n unknown_user_abuse_reason: 'Unknown user abuse reason: {{reason}}',\n unknown_ip_abuse_reason: 'Unknown IP abuse reason: {{reason}}',\n duplicate_user_abuse_reason: 'Duplicate user abuse reason: {{reason}}',\n duplicate_ip_abuse_reason: 'Duplicate IP abuse reason: {{reason}}',\n missing_on_ban_user: 'onBanUser is required when user-abuse strategies are configured',\n invalid_limit: 'limit must be a positive integer, got: {{limit}}',\n invalid_window: 'window must be a positive duration, got: {{window}}',\n invalid_block_for: 'blockFor must be a positive duration, got: {{blockFor}}',\n invalid_ban_for: 'banFor must be a positive duration, got: {{banFor}}',\n empty_honeypot_paths: 'honeypot strategy paths must not be empty',\n empty_http_error_status_codes: 'http-error strategy statusCodes must not be empty',\n empty_socket_events: 'socket-events strategy events must not be empty',\n blocked: 'Access denied',\n});\n","import { createI18n } from '@aomex/common';\nimport { zh } from './locales/zh-cn';\nimport { en } from './locales/en-us';\n\nexport const i18n = createI18n({\n zh,\n en,\n load: (file) => import(new URL(`./locales/${file}.js`, import.meta.url).href),\n});\n","/**\n * 路径匹配:精确字符串、简单 glob(`*` / `**`)、或正则。\n * 正则会对 `pathname + search` 检测,便于捕获 query 中的探测特征。\n */\nexport function matchPath(\n pathname: string,\n search: string,\n matcher: string | RegExp,\n): boolean {\n if (matcher instanceof RegExp) {\n return matcher.test(pathname + search);\n }\n\n if (!matcher.includes('*')) {\n return pathname === matcher;\n }\n\n const re = globToRegExp(matcher);\n return re.test(pathname);\n}\n\n/**\n * 任一路径匹配器命中即为 true。\n * `paths` 为 `undefined` 时视为不限路径;空数组不命中。\n */\nexport function matchAnyPath(\n pathname: string,\n search: string,\n paths?: readonly (string | RegExp)[],\n): boolean {\n if (paths === undefined) return true;\n return paths.some((item) => matchPath(pathname, search, item));\n}\n\n/**\n * 匹配 HTTP 方法(忽略大小写)。\n * `methods` 为 `undefined` 或空数组时视为不限方法。\n */\nexport function matchMethod(method: string, methods?: readonly string[]): boolean {\n if (!methods?.length) return true;\n const upper = method.toUpperCase();\n return methods.some((item) => item.toUpperCase() === upper);\n}\n\n/**\n * 事件名匹配:精确字符串、简单 glob(`*` → 任意字符)、或正则。\n */\nexport function matchEvent(event: string, matcher: string | RegExp): boolean {\n if (matcher instanceof RegExp) {\n return matcher.test(event);\n }\n\n if (!matcher.includes('*')) {\n return event === matcher;\n }\n\n let pattern = '';\n for (let i = 0; i < matcher.length; i += 1) {\n const char = matcher[i]!;\n if (char === '*') {\n pattern += '.*';\n continue;\n }\n if (/[.+?^${}()|[\\]\\\\]/.test(char)) {\n pattern += `\\\\${char}`;\n continue;\n }\n pattern += char;\n }\n return new RegExp(`^${pattern}$`).test(event);\n}\n\n/**\n * 任一事件匹配器命中即为 true。\n * `events` 为 `undefined` 时视为不限;空数组不命中。\n */\nexport function matchAnyEvent(\n event: string,\n events?: readonly (string | RegExp)[],\n): boolean {\n if (events === undefined) return true;\n return events.some((item) => matchEvent(event, item));\n}\n\nfunction globToRegExp(glob: string): RegExp {\n let source = glob;\n let optionalDeep = false;\n if (source.endsWith('/**')) {\n source = source.slice(0, -3);\n optionalDeep = true;\n }\n\n let pattern = '';\n for (let i = 0; i < source.length; i += 1) {\n const char = source[i]!;\n if (char === '*') {\n if (source[i + 1] === '*') {\n pattern += '.*';\n i += 1;\n } else {\n pattern += '[^/]*';\n }\n continue;\n }\n if (/[.+?^${}()|[\\]\\\\]/.test(char)) {\n pattern += `\\\\${char}`;\n continue;\n }\n pattern += char;\n }\n if (optionalDeep) pattern += '(?:/.*)?';\n return new RegExp(`^${pattern}$`);\n}\n","import { CacheKey, Caching } from '@aomex/cache';\nimport { middleware, parseDuration, registerLifecycle } from '@aomex/common';\nimport { HttpError, type WebContext } from '@aomex/web';\nimport type { SocketMiddleware, WebsocketContext } from '@aomex/websocket';\nimport { i18n } from './i18n';\nimport { matchAnyEvent, matchAnyPath, matchMethod } from './match';\nimport type {\n AbuseApi,\n AbuseBlockStrategy,\n AbuseHoneypotStrategy,\n AbuseHttpErrorStrategy,\n AbuseIpAbuseStrategy,\n AbuseOptions,\n AbuseService,\n AbuseSocketEventsStrategy,\n AbuseSocketOptions,\n AbuseStrategy,\n AbuseWebOptions,\n AbuseUserAbuseStrategy,\n InferIpAbuseReasons,\n InferUserAbuseReasons,\n} from './types';\n\nconst abuseKey = new CacheKey().key('abuse');\n\n/** 校验策略并按 reason 分桶;非法配置创建期抛错 */\nconst validateStrategies = (strategies: readonly AbuseStrategy[]) => {\n const userAbuseStrategies = new Map<string, AbuseUserAbuseStrategy>();\n const ipAbuseStrategies = new Map<string, AbuseIpAbuseStrategy>();\n\n for (const item of strategies) {\n if (!Number.isInteger(item.limit) || item.limit <= 0) {\n throw new Error(i18n.t('invalid_limit', { limit: String(item.limit) }));\n }\n const windowMs = parseDuration(item.window);\n if (windowMs <= 0) {\n throw new Error(i18n.t('invalid_window', { window: String(item.window) }));\n }\n if (item.strategy === 'honeypot') {\n if (item.paths.length === 0) {\n throw new Error(i18n.t('empty_honeypot_paths'));\n }\n } else if (item.strategy === 'http-error') {\n if (item.statusCodes.length === 0) {\n throw new Error(i18n.t('empty_http_error_status_codes'));\n }\n } else if (item.strategy === 'socket-events') {\n if (item.events.length === 0) {\n throw new Error(i18n.t('empty_socket_events'));\n }\n }\n if (item.strategy === 'user-abuse') {\n if (item.banFor !== undefined) {\n const banForMs = parseDuration(item.banFor);\n if (banForMs <= 0) {\n throw new Error(i18n.t('invalid_ban_for', { banFor: String(item.banFor) }));\n }\n }\n const reasonKey = String(item.reason);\n if (userAbuseStrategies.has(reasonKey)) {\n throw new Error(i18n.t('duplicate_user_abuse_reason', { reason: reasonKey }));\n }\n userAbuseStrategies.set(reasonKey, item);\n } else {\n const blockForMs = parseDuration(item.blockFor);\n if (blockForMs <= 0) {\n throw new Error(i18n.t('invalid_block_for', { blockFor: String(item.blockFor) }));\n }\n if (item.strategy === 'ip-abuse') {\n const reasonKey = String(item.reason);\n if (ipAbuseStrategies.has(reasonKey)) {\n throw new Error(i18n.t('duplicate_ip_abuse_reason', { reason: reasonKey }));\n }\n ipAbuseStrategies.set(reasonKey, item);\n }\n }\n }\n\n return { userAbuseStrategies, ipAbuseStrategies };\n};\n\n/**\n * 蜜罐路径 / 错误累计 / 业务滥用计数。\n *\n * - `honeypot` / `http-error`:仅 `.web()` 按请求匹配计数,满额封 IP\n * - `socket-events`:仅 `.socket()` 按事件名蜜罐计数,满额封 IP\n * - `ip-abuse`:业务上报 IP 滥用(如 logout token 未命中),满额封 IP\n * - `user-abuse`:业务上报,满额只触发 `onBanUser`(账号封禁由业务写库)\n *\n * 返回值是服务(API + `.web()` / `.socket()`),不挂到 `ctx`:\n * ```ts\n * const abuse = createAbuse({\n * store,\n * strategies: [\n * { strategy: 'socket-events', events: ['admin.destroy'], limit: 1, window: '1h', blockFor: '1d' },\n * ],\n * });\n * app.mount([abuse.web()]);\n * // 须先 import '@aomex/websocket',以注册 middleware.socket\n * ws.mount([abuse.socket(), channels('./src/channels')]);\n * await abuse.ipAbuse(ip, 'logout-miss');\n * ```\n */\nexport const createAbuse = <const S extends readonly AbuseStrategy[]>(\n options: AbuseOptions<S>,\n): AbuseService<InferUserAbuseReasons<S>, InferIpAbuseReasons<S>> => {\n const adapter = options.store;\n const caching = new Caching(adapter);\n const getBlockKey = (ip: string) => abuseKey.key('block', ip);\n const onBanUser = options.onBanUser;\n const onBlockIP = options.onBlockIP;\n\n const bumpCount = async (countKey: Caching.Key, windowMs: number): Promise<number> => {\n const count = await caching.increment(countKey);\n if (count === 1 || (await caching.ttl(countKey)) === -1) {\n await caching.expire(countKey, Math.max(windowMs, 1));\n }\n return count;\n };\n\n /**\n * 封 IP:`setNX` 原子抢占,已封禁则跳过钩子;抢到则写入并触发 `onBlockIP`\n */\n const ensureBlocked = async (\n ip: string,\n reason: AbuseBlockStrategy,\n detail: { count: number; limit: number; blockFor: number },\n ): Promise<boolean> => {\n const created = await caching.setNX(getBlockKey(ip), reason, detail.blockFor);\n if (!created) return false;\n await onBlockIP?.(ip, reason, detail);\n return true;\n };\n\n const honeypotStrategies = options.strategies.filter(\n (item): item is AbuseHoneypotStrategy => item.strategy === 'honeypot',\n );\n const httpErrorStrategies = options.strategies.filter(\n (item): item is AbuseHttpErrorStrategy => item.strategy === 'http-error',\n );\n const socketEventsStrategies = options.strategies.filter(\n (item): item is AbuseSocketEventsStrategy => item.strategy === 'socket-events',\n );\n const { userAbuseStrategies, ipAbuseStrategies } = validateStrategies(\n options.strategies,\n );\n\n if (userAbuseStrategies.size > 0 && !onBanUser) {\n throw new Error(i18n.t('missing_on_ban_user'));\n }\n\n let ready: Promise<void> | undefined;\n const ensureReady = () => {\n ready ??= registerLifecycle({\n onInit: () => adapter.connect(),\n onDestroy: () => adapter.disconnect(),\n });\n return ready;\n };\n\n const api: AbuseApi<InferUserAbuseReasons<S>, InferIpAbuseReasons<S>> = {\n async userAbuse(id, reason) {\n await ensureReady();\n const reasonKey = String(reason);\n const strategy = userAbuseStrategies.get(reasonKey);\n if (!strategy) {\n throw new Error(i18n.t('unknown_user_abuse_reason', { reason: reasonKey }));\n }\n const idKey = String(id);\n const countKey = abuseKey.key('count', 'user-abuse', reasonKey, idKey);\n const windowMs = parseDuration(strategy.window);\n const count = await bumpCount(countKey, windowMs);\n if (count < strategy.limit) return;\n\n // 无 IP 封禁键:hook 标记 setNX,避免窗口内重复 / 并发触发 onBanUser\n const hookKey = abuseKey.key('hook', 'user-abuse', reasonKey, idKey);\n const banFor =\n strategy.banFor === undefined ? undefined : parseDuration(strategy.banFor);\n const countTtl = await caching.ttl(countKey);\n const marked = await caching.setNX(\n hookKey,\n '1',\n Math.max(countTtl > 0 ? countTtl : windowMs, 1),\n );\n if (!marked) return;\n try {\n await onBanUser!(id, reason, { count, limit: strategy.limit, banFor });\n } catch (e) {\n // 回调失败则释放 hook,允许窗口内重试\n await caching.delete(hookKey);\n throw e;\n }\n },\n async ipAbuse(ip, reason) {\n await ensureReady();\n const reasonKey = String(reason);\n const strategy = ipAbuseStrategies.get(reasonKey);\n if (!strategy) {\n throw new Error(i18n.t('unknown_ip_abuse_reason', { reason: reasonKey }));\n }\n const countKey = abuseKey.key('count', 'ip-abuse', reasonKey, ip);\n const count = await bumpCount(countKey, parseDuration(strategy.window));\n if (count < strategy.limit) return;\n\n await ensureBlocked(ip, 'ip-abuse', {\n count,\n limit: strategy.limit,\n blockFor: parseDuration(strategy.blockFor),\n });\n },\n async blockIP(ip, duration) {\n await ensureReady();\n const blockFor = parseDuration(duration);\n if (blockFor <= 0) {\n throw new Error(i18n.t('invalid_block_for', { blockFor: String(duration) }));\n }\n await ensureBlocked(ip, 'manual', {\n count: 1,\n limit: 1,\n blockFor,\n });\n },\n async unblockIP(ip) {\n await ensureReady();\n await caching.delete(getBlockKey(ip));\n },\n async isIPBlocked(ip) {\n await ensureReady();\n return caching.exists(getBlockKey(ip));\n },\n };\n\n const web = (webOptions: AbuseWebOptions = {}) => {\n const bypass = webOptions.bypass ?? (() => false);\n const throwBlocked = (ctx: WebContext): never => {\n if (webOptions.blockedError) throw webOptions.blockedError;\n return ctx.throw(403);\n };\n\n return middleware.web(async (ctx, next) => {\n await ensureReady();\n if (await bypass(ctx)) return next();\n\n const ip = ctx.request.ip;\n\n if (await caching.exists(getBlockKey(ip))) {\n return throwBlocked(ctx);\n }\n\n const request = ctx.request;\n\n for (let i = 0; i < honeypotStrategies.length; i += 1) {\n const strategy = honeypotStrategies[i]!;\n if (\n !matchMethod(request.method, strategy.methods) ||\n !matchAnyPath(request.pathname, request.search, strategy.paths)\n ) {\n continue;\n }\n\n const countKey = abuseKey.key('count', 'honeypot', i, ip);\n const count = await bumpCount(countKey, parseDuration(strategy.window));\n if (count < strategy.limit) continue;\n\n await ensureBlocked(ip, 'honeypot', {\n count,\n limit: strategy.limit,\n blockFor: parseDuration(strategy.blockFor),\n });\n return throwBlocked(ctx);\n }\n\n let statusCode: number;\n let thrown: HttpError | undefined;\n try {\n await next();\n statusCode = ctx.response.statusCode;\n } catch (e) {\n thrown = HttpError.from(e);\n statusCode = thrown.statusCode;\n }\n\n for (let i = 0; i < httpErrorStrategies.length; i += 1) {\n const strategy = httpErrorStrategies[i]!;\n if (\n !strategy.statusCodes.includes(statusCode) ||\n !matchMethod(request.method, strategy.methods) ||\n !matchAnyPath(request.pathname, request.search, strategy.paths)\n ) {\n continue;\n }\n\n const countKey = abuseKey.key('count', 'http-error', i, ip);\n const count = await bumpCount(countKey, parseDuration(strategy.window));\n if (count < strategy.limit) continue;\n\n await ensureBlocked(ip, 'http-error', {\n count,\n limit: strategy.limit,\n blockFor: parseDuration(strategy.blockFor),\n });\n }\n\n if (thrown) throw thrown;\n });\n };\n\n const socket = (socketOptions: AbuseSocketOptions = {}): SocketMiddleware => {\n // 调用方须先 import '@aomex/websocket',以注册 middleware.socket\n const socketBypass = socketOptions.bypass ?? (() => false);\n /** 回推 error 后断开:封禁不是软限流,连接不应继续占用 */\n const rejectBlocked = async (ctx: WebsocketContext) => {\n const message = socketOptions.errorMessage || i18n.t('blocked');\n try {\n await ctx.client.send('error', { message });\n } finally {\n // 1008 Policy Violation;reason 与 error 帧一致,传输层会截断超长 reason\n await ctx.client.close(1008, message);\n }\n };\n\n return middleware.socket(async (ctx: WebsocketContext, next) => {\n await ensureReady();\n if (await socketBypass(ctx)) return next();\n\n const ip = ctx.client.request.ip;\n\n if (await caching.exists(getBlockKey(ip))) {\n await rejectBlocked(ctx);\n return;\n }\n\n for (let i = 0; i < socketEventsStrategies.length; i += 1) {\n const strategy = socketEventsStrategies[i]!;\n if (!matchAnyEvent(ctx.event, strategy.events)) continue;\n\n const countKey = abuseKey.key('count', 'socket-events', i, ip);\n const count = await bumpCount(countKey, parseDuration(strategy.window));\n if (count < strategy.limit) continue;\n\n await ensureBlocked(ip, 'socket-events', {\n count,\n limit: strategy.limit,\n blockFor: parseDuration(strategy.blockFor),\n });\n await rejectBlocked(ctx);\n return;\n }\n\n return next();\n });\n };\n\n return {\n ...api,\n web,\n socket,\n } as AbuseService<InferUserAbuseReasons<S>, InferIpAbuseReasons<S>>;\n};\n"],"mappings":";;;;AAEA,MAAa,KAAK,KAAK,OAAO;CAC5B,2BAA2B;CAC3B,yBAAyB;CACzB,6BAA6B;CAC7B,2BAA2B;CAC3B,qBAAqB;CACrB,eAAe;CACf,gBAAgB;CAChB,mBAAmB;CACnB,iBAAiB;CACjB,sBAAsB;CACtB,+BAA+B;CAC/B,qBAAqB;CACrB,SAAS;AACX,CAAC;;ACbD,MAAa,KAAK,KAAK,UAAU,EAAE,CAAC,CAAC,OAAO;CAC1C,2BAA2B;CAC3B,yBAAyB;CACzB,6BAA6B;CAC7B,2BAA2B;CAC3B,qBAAqB;CACrB,eAAe;CACf,gBAAgB;CAChB,mBAAmB;CACnB,iBAAiB;CACjB,sBAAsB;CACtB,+BAA+B;CAC/B,qBAAqB;CACrB,SAAS;AACX,CAAC;;ACbD,MAAa,OAAO,WAAW;CAC7B;CACA;CACA,OAAO,SAAS,OAAO,IAAI,IAAI,aAAa,KAAK,MAAM,YAAY,GAAG,CAAC,CAAC;AAC1E,CAAC;;;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AEA,OAAO,KAAK;EACd,CAAC;CACH;CAEA,OAAO;EACL,GAAG;EACH;EACA;CACF;AACF"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aomex/abuse",
|
|
3
|
-
"version": "4.
|
|
4
|
-
"description": "滥用计数与封禁(蜜罐路径 / HTTP 错误 / IP·用户滥用)",
|
|
3
|
+
"version": "4.4.1-alpha.c1419fd5.0",
|
|
4
|
+
"description": "滥用计数与封禁(蜜罐路径 / HTTP 错误 / 事件蜜罐 / IP·用户滥用)",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git@github.com:aomex/aomex.git"
|
|
@@ -30,12 +30,19 @@
|
|
|
30
30
|
"peerDependencies": {
|
|
31
31
|
"@aomex/cache": "^4.0.0",
|
|
32
32
|
"@aomex/common": "^4.0.0",
|
|
33
|
-
"@aomex/web": "^4.0.0"
|
|
33
|
+
"@aomex/web": "^4.0.0",
|
|
34
|
+
"@aomex/websocket": "^4.0.0"
|
|
35
|
+
},
|
|
36
|
+
"peerDependenciesMeta": {
|
|
37
|
+
"@aomex/websocket": {
|
|
38
|
+
"optional": true
|
|
39
|
+
}
|
|
34
40
|
},
|
|
35
41
|
"devDependencies": {
|
|
36
|
-
"@aomex/cache": "4.
|
|
37
|
-
"@aomex/common": "4.
|
|
38
|
-
"@aomex/web": "4.
|
|
42
|
+
"@aomex/cache": "4.4.1-alpha.c1419fd5.0",
|
|
43
|
+
"@aomex/common": "4.4.1-alpha.c1419fd5.0",
|
|
44
|
+
"@aomex/web": "4.4.1-alpha.c1419fd5.0",
|
|
45
|
+
"@aomex/websocket": "4.4.1-alpha.c1419fd5.0"
|
|
39
46
|
},
|
|
40
47
|
"scripts": {}
|
|
41
48
|
}
|