@f2a/network 0.1.2 → 0.1.3
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/package.json +8 -1
- package/.github/workflows/ci.yml +0 -113
- package/.github/workflows/publish.yml +0 -60
- package/MONOREPO.md +0 -58
- package/SKILL.md +0 -137
- package/dist/adapters/openclaw.d.ts +0 -103
- package/dist/adapters/openclaw.d.ts.map +0 -1
- package/dist/adapters/openclaw.js +0 -297
- package/dist/adapters/openclaw.js.map +0 -1
- package/dist/core/connection-manager.d.ts +0 -80
- package/dist/core/connection-manager.d.ts.map +0 -1
- package/dist/core/connection-manager.js +0 -235
- package/dist/core/connection-manager.js.map +0 -1
- package/dist/core/connection-manager.test.d.ts +0 -2
- package/dist/core/connection-manager.test.d.ts.map +0 -1
- package/dist/core/connection-manager.test.js +0 -52
- package/dist/core/connection-manager.test.js.map +0 -1
- package/dist/core/identity.d.ts +0 -47
- package/dist/core/identity.d.ts.map +0 -1
- package/dist/core/identity.js +0 -130
- package/dist/core/identity.js.map +0 -1
- package/dist/core/identity.test.d.ts +0 -2
- package/dist/core/identity.test.d.ts.map +0 -1
- package/dist/core/identity.test.js +0 -43
- package/dist/core/identity.test.js.map +0 -1
- package/dist/core/serverless.d.ts +0 -155
- package/dist/core/serverless.d.ts.map +0 -1
- package/dist/core/serverless.js +0 -615
- package/dist/core/serverless.js.map +0 -1
- package/dist/daemon/webhook.test.d.ts +0 -2
- package/dist/daemon/webhook.test.d.ts.map +0 -1
- package/dist/daemon/webhook.test.js +0 -24
- package/dist/daemon/webhook.test.js.map +0 -1
- package/dist/protocol/messages.d.ts +0 -739
- package/dist/protocol/messages.d.ts.map +0 -1
- package/dist/protocol/messages.js +0 -188
- package/dist/protocol/messages.js.map +0 -1
- package/dist/protocol/messages.test.d.ts +0 -2
- package/dist/protocol/messages.test.d.ts.map +0 -1
- package/dist/protocol/messages.test.js +0 -55
- package/dist/protocol/messages.test.js.map +0 -1
- package/docs/F2A-PROTOCOL.md +0 -61
- package/docs/MOBILE_BOOTSTRAP_DESIGN.md +0 -126
- package/docs/a2a-lessons.md +0 -316
- package/docs/middleware-guide.md +0 -448
- package/docs/readme-update-checklist.md +0 -90
- package/docs/reputation-guide.md +0 -396
- package/docs/rfcs/001-reputation-system.md +0 -712
- package/docs/security-design.md +0 -247
- package/install.sh +0 -231
- package/packages/openclaw-adapter/README.md +0 -510
- package/packages/openclaw-adapter/openclaw.plugin.json +0 -106
- package/packages/openclaw-adapter/package.json +0 -40
- package/packages/openclaw-adapter/src/announcement-queue.test.ts +0 -449
- package/packages/openclaw-adapter/src/announcement-queue.ts +0 -403
- package/packages/openclaw-adapter/src/capability-detector.test.ts +0 -99
- package/packages/openclaw-adapter/src/capability-detector.ts +0 -183
- package/packages/openclaw-adapter/src/claim-handlers.test.ts +0 -974
- package/packages/openclaw-adapter/src/claim-handlers.ts +0 -482
- package/packages/openclaw-adapter/src/connector.business.test.ts +0 -583
- package/packages/openclaw-adapter/src/connector.ts +0 -795
- package/packages/openclaw-adapter/src/index.test.ts +0 -82
- package/packages/openclaw-adapter/src/index.ts +0 -18
- package/packages/openclaw-adapter/src/integration.e2e.test.ts +0 -829
- package/packages/openclaw-adapter/src/logger.ts +0 -51
- package/packages/openclaw-adapter/src/network-client.test.ts +0 -266
- package/packages/openclaw-adapter/src/network-client.ts +0 -251
- package/packages/openclaw-adapter/src/network-recovery.test.ts +0 -465
- package/packages/openclaw-adapter/src/node-manager.test.ts +0 -136
- package/packages/openclaw-adapter/src/node-manager.ts +0 -429
- package/packages/openclaw-adapter/src/plugin.test.ts +0 -439
- package/packages/openclaw-adapter/src/plugin.ts +0 -104
- package/packages/openclaw-adapter/src/reputation.test.ts +0 -221
- package/packages/openclaw-adapter/src/reputation.ts +0 -368
- package/packages/openclaw-adapter/src/task-guard.test.ts +0 -502
- package/packages/openclaw-adapter/src/task-guard.ts +0 -860
- package/packages/openclaw-adapter/src/task-queue.concurrency.test.ts +0 -462
- package/packages/openclaw-adapter/src/task-queue.edge-cases.test.ts +0 -284
- package/packages/openclaw-adapter/src/task-queue.persistence.test.ts +0 -408
- package/packages/openclaw-adapter/src/task-queue.ts +0 -668
- package/packages/openclaw-adapter/src/tool-handlers.test.ts +0 -906
- package/packages/openclaw-adapter/src/tool-handlers.ts +0 -574
- package/packages/openclaw-adapter/src/types.ts +0 -361
- package/packages/openclaw-adapter/src/webhook-pusher.test.ts +0 -188
- package/packages/openclaw-adapter/src/webhook-pusher.ts +0 -220
- package/packages/openclaw-adapter/src/webhook-server.test.ts +0 -580
- package/packages/openclaw-adapter/src/webhook-server.ts +0 -202
- package/packages/openclaw-adapter/tsconfig.json +0 -20
- package/src/cli/commands.test.ts +0 -157
- package/src/cli/commands.ts +0 -129
- package/src/cli/index.test.ts +0 -77
- package/src/cli/index.ts +0 -234
- package/src/core/autonomous-economy.test.ts +0 -291
- package/src/core/autonomous-economy.ts +0 -428
- package/src/core/e2ee-crypto.test.ts +0 -125
- package/src/core/e2ee-crypto.ts +0 -246
- package/src/core/f2a.test.ts +0 -269
- package/src/core/f2a.ts +0 -618
- package/src/core/p2p-network.test.ts +0 -199
- package/src/core/p2p-network.ts +0 -1432
- package/src/core/reputation-security.test.ts +0 -403
- package/src/core/reputation-security.ts +0 -562
- package/src/core/reputation.test.ts +0 -260
- package/src/core/reputation.ts +0 -576
- package/src/core/review-committee.test.ts +0 -380
- package/src/core/review-committee.ts +0 -401
- package/src/core/token-manager.test.ts +0 -133
- package/src/core/token-manager.ts +0 -140
- package/src/daemon/control-server.test.ts +0 -216
- package/src/daemon/control-server.ts +0 -292
- package/src/daemon/index.test.ts +0 -85
- package/src/daemon/index.ts +0 -89
- package/src/daemon/main.ts +0 -44
- package/src/daemon/start.ts +0 -29
- package/src/daemon/webhook.test.ts +0 -68
- package/src/daemon/webhook.ts +0 -105
- package/src/index.test.ts +0 -436
- package/src/index.ts +0 -72
- package/src/types/index.test.ts +0 -87
- package/src/types/index.ts +0 -341
- package/src/types/result.ts +0 -68
- package/src/utils/benchmark.ts +0 -237
- package/src/utils/logger.ts +0 -331
- package/src/utils/middleware.ts +0 -229
- package/src/utils/rate-limiter.ts +0 -207
- package/src/utils/signature.ts +0 -136
- package/src/utils/validation.ts +0 -186
- package/tests/docker/Dockerfile.node +0 -23
- package/tests/docker/Dockerfile.runner +0 -18
- package/tests/docker/docker-compose.test.yml +0 -73
- package/tests/integration/message-passing.test.ts +0 -109
- package/tests/integration/multi-node.test.ts +0 -92
- package/tests/integration/p2p-connection.test.ts +0 -83
- package/tests/integration/test-config.ts +0 -32
- package/tsconfig.json +0 -21
- package/vitest.config.ts +0 -26
package/src/utils/middleware.ts
DELETED
|
@@ -1,229 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* F2A 中间件系统
|
|
3
|
-
* 支持消息拦截、过滤和转换
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { F2AMessage, AgentInfo } from '../types/index.js';
|
|
7
|
-
import { Logger } from './logger.js';
|
|
8
|
-
|
|
9
|
-
export interface MiddlewareContext {
|
|
10
|
-
/** 消息 */
|
|
11
|
-
message: F2AMessage;
|
|
12
|
-
/** 发送方 Peer ID */
|
|
13
|
-
peerId: string;
|
|
14
|
-
/** 发送方 Agent 信息 */
|
|
15
|
-
agentInfo?: AgentInfo;
|
|
16
|
-
/** 中间件元数据 */
|
|
17
|
-
metadata: Map<string, unknown>;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export type MiddlewareResult =
|
|
21
|
-
| { action: 'continue'; context: MiddlewareContext }
|
|
22
|
-
| { action: 'drop'; reason: string }
|
|
23
|
-
| { action: 'modify'; context: MiddlewareContext };
|
|
24
|
-
|
|
25
|
-
export interface Middleware {
|
|
26
|
-
/** 中间件名称 */
|
|
27
|
-
name: string;
|
|
28
|
-
/** 执行优先级(数字越小优先级越高) */
|
|
29
|
-
priority?: number;
|
|
30
|
-
/**
|
|
31
|
-
* 中间件类型
|
|
32
|
-
* - 'essential': 核心中间件,异常时中断链
|
|
33
|
-
* - 'optional': 可选中间件,异常时继续处理
|
|
34
|
-
*/
|
|
35
|
-
type?: 'essential' | 'optional';
|
|
36
|
-
/** 处理函数 */
|
|
37
|
-
process(context: MiddlewareContext): Promise<MiddlewareResult> | MiddlewareResult;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* 中间件管理器
|
|
42
|
-
*/
|
|
43
|
-
export class MiddlewareManager {
|
|
44
|
-
private middlewares: Middleware[] = [];
|
|
45
|
-
private logger: Logger;
|
|
46
|
-
|
|
47
|
-
constructor() {
|
|
48
|
-
this.logger = new Logger({ component: 'MiddlewareManager' });
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* 注册中间件
|
|
53
|
-
*/
|
|
54
|
-
use(middleware: Middleware): void {
|
|
55
|
-
this.middlewares.push(middleware);
|
|
56
|
-
// 按优先级排序
|
|
57
|
-
this.middlewares.sort((a, b) => (a.priority || 0) - (b.priority || 0));
|
|
58
|
-
this.logger.info('Registered middleware', { name: middleware.name });
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* 移除中间件
|
|
63
|
-
*/
|
|
64
|
-
remove(name: string): boolean {
|
|
65
|
-
const index = this.middlewares.findIndex(m => m.name === name);
|
|
66
|
-
if (index !== -1) {
|
|
67
|
-
this.middlewares.splice(index, 1);
|
|
68
|
-
this.logger.info('Removed middleware', { name });
|
|
69
|
-
return true;
|
|
70
|
-
}
|
|
71
|
-
return false;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* 执行中间件链
|
|
76
|
-
*/
|
|
77
|
-
async execute(context: MiddlewareContext): Promise<MiddlewareResult> {
|
|
78
|
-
let currentContext = context;
|
|
79
|
-
|
|
80
|
-
for (const middleware of this.middlewares) {
|
|
81
|
-
try {
|
|
82
|
-
const result = await middleware.process(currentContext);
|
|
83
|
-
|
|
84
|
-
if (result.action === 'drop') {
|
|
85
|
-
this.logger.info('Message dropped by middleware', {
|
|
86
|
-
middleware: middleware.name,
|
|
87
|
-
reason: result.reason
|
|
88
|
-
});
|
|
89
|
-
return result;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (result.action === 'modify') {
|
|
93
|
-
currentContext = result.context;
|
|
94
|
-
}
|
|
95
|
-
} catch (error) {
|
|
96
|
-
this.logger.error('Middleware error', {
|
|
97
|
-
middleware: middleware.name,
|
|
98
|
-
error,
|
|
99
|
-
type: middleware.type || 'optional'
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
// 根据中间件类型决定是否中断链
|
|
103
|
-
// essential: 核心中间件,异常时中断链,返回 drop
|
|
104
|
-
// optional: 可选中间件,异常时继续处理(原有行为)
|
|
105
|
-
const middlewareType = middleware.type || 'optional';
|
|
106
|
-
|
|
107
|
-
if (middlewareType === 'essential') {
|
|
108
|
-
this.logger.warn('Essential middleware failed, aborting chain', {
|
|
109
|
-
middleware: middleware.name
|
|
110
|
-
});
|
|
111
|
-
return {
|
|
112
|
-
action: 'drop',
|
|
113
|
-
reason: `Essential middleware ${middleware.name} failed: ${error instanceof Error ? error.message : String(error)}`
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// optional 中间件出错时继续处理,不阻塞消息
|
|
118
|
-
this.logger.info('Optional middleware failed, continuing chain', {
|
|
119
|
-
middleware: middleware.name
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return { action: 'continue', context: currentContext };
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* 获取已注册的中间件列表
|
|
129
|
-
*/
|
|
130
|
-
list(): string[] {
|
|
131
|
-
return this.middlewares.map(m => m.name);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* 清空所有中间件
|
|
136
|
-
*/
|
|
137
|
-
clear(): void {
|
|
138
|
-
this.middlewares = [];
|
|
139
|
-
this.logger.info('Cleared all middlewares');
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// ============================================================================
|
|
144
|
-
// 内置中间件
|
|
145
|
-
// ============================================================================
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* 消息大小限制中间件
|
|
149
|
-
*/
|
|
150
|
-
export function createMessageSizeLimitMiddleware(maxSize: number): Middleware {
|
|
151
|
-
return {
|
|
152
|
-
name: 'MessageSizeLimit',
|
|
153
|
-
priority: 100, // 高优先级,尽早检查
|
|
154
|
-
process(context: MiddlewareContext): MiddlewareResult {
|
|
155
|
-
const messageSize = JSON.stringify(context.message).length;
|
|
156
|
-
if (messageSize > maxSize) {
|
|
157
|
-
return {
|
|
158
|
-
action: 'drop',
|
|
159
|
-
reason: `Message size ${messageSize} exceeds limit ${maxSize}`
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
return { action: 'continue', context };
|
|
163
|
-
}
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* 消息类型过滤中间件
|
|
169
|
-
*/
|
|
170
|
-
export function createMessageTypeFilterMiddleware(
|
|
171
|
-
allowedTypes: string[]
|
|
172
|
-
): Middleware {
|
|
173
|
-
return {
|
|
174
|
-
name: 'MessageTypeFilter',
|
|
175
|
-
priority: 90,
|
|
176
|
-
process(context: MiddlewareContext): MiddlewareResult {
|
|
177
|
-
if (!allowedTypes.includes(context.message.type)) {
|
|
178
|
-
return {
|
|
179
|
-
action: 'drop',
|
|
180
|
-
reason: `Message type ${context.message.type} not allowed`
|
|
181
|
-
};
|
|
182
|
-
}
|
|
183
|
-
return { action: 'continue', context };
|
|
184
|
-
}
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* 消息日志中间件
|
|
190
|
-
*/
|
|
191
|
-
export function createMessageLoggingMiddleware(
|
|
192
|
-
logger?: Logger
|
|
193
|
-
): Middleware {
|
|
194
|
-
const log = logger || new Logger({ component: 'MessageLogger' });
|
|
195
|
-
return {
|
|
196
|
-
name: 'MessageLogger',
|
|
197
|
-
priority: 50, // 中等优先级
|
|
198
|
-
process(context: MiddlewareContext): MiddlewareResult {
|
|
199
|
-
log.debug('Processing message', {
|
|
200
|
-
type: context.message.type,
|
|
201
|
-
from: context.peerId.slice(0, 16),
|
|
202
|
-
id: context.message.id
|
|
203
|
-
});
|
|
204
|
-
return { action: 'continue', context };
|
|
205
|
-
}
|
|
206
|
-
};
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* 消息转换中间件示例
|
|
211
|
-
*/
|
|
212
|
-
export function createMessageTransformMiddleware(
|
|
213
|
-
transform: (msg: F2AMessage) => F2AMessage
|
|
214
|
-
): Middleware {
|
|
215
|
-
return {
|
|
216
|
-
name: 'MessageTransform',
|
|
217
|
-
priority: 10, // 低优先级,最后执行
|
|
218
|
-
process(context: MiddlewareContext): MiddlewareResult {
|
|
219
|
-
const transformedMessage = transform(context.message);
|
|
220
|
-
return {
|
|
221
|
-
action: 'modify',
|
|
222
|
-
context: {
|
|
223
|
-
...context,
|
|
224
|
-
message: transformedMessage
|
|
225
|
-
}
|
|
226
|
-
};
|
|
227
|
-
}
|
|
228
|
-
};
|
|
229
|
-
}
|
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 速率限制中间件
|
|
3
|
-
* 基于 Token Bucket 算法实现,支持突发流量
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { Logger } from './logger.js';
|
|
7
|
-
|
|
8
|
-
export interface RateLimitConfig {
|
|
9
|
-
/** 最大请求数 */
|
|
10
|
-
maxRequests: number;
|
|
11
|
-
/** 时间窗口(毫秒) */
|
|
12
|
-
windowMs: number;
|
|
13
|
-
/** 是否跳过成功请求 */
|
|
14
|
-
skipSuccessfulRequests?: boolean;
|
|
15
|
-
/** 突发容量倍数(默认 1.5,允许短暂的请求爆发) */
|
|
16
|
-
burstMultiplier?: number;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface RateLimitEntry {
|
|
20
|
-
tokens: number;
|
|
21
|
-
lastRefill: number;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* 速率限制器
|
|
26
|
-
* 实现 Disposable 接口,确保资源正确释放
|
|
27
|
-
*/
|
|
28
|
-
export class RateLimiter implements Disposable {
|
|
29
|
-
private config: Required<RateLimitConfig>;
|
|
30
|
-
private burstCapacity: number;
|
|
31
|
-
private store: Map<string, RateLimitEntry> = new Map();
|
|
32
|
-
private logger: Logger;
|
|
33
|
-
private cleanupTimer?: NodeJS.Timeout;
|
|
34
|
-
private disposed: boolean = false;
|
|
35
|
-
|
|
36
|
-
constructor(config: RateLimitConfig) {
|
|
37
|
-
this.config = {
|
|
38
|
-
skipSuccessfulRequests: false,
|
|
39
|
-
burstMultiplier: 1.5,
|
|
40
|
-
...config
|
|
41
|
-
};
|
|
42
|
-
// 计算突发容量
|
|
43
|
-
this.burstCapacity = Math.floor(this.config.maxRequests * this.config.burstMultiplier);
|
|
44
|
-
this.logger = new Logger({ component: 'RateLimiter' });
|
|
45
|
-
// 自动启动清理定时器
|
|
46
|
-
this.cleanupTimer = setInterval(() => this.cleanup(), config.windowMs);
|
|
47
|
-
|
|
48
|
-
// 注册析构回调,确保即使 stop() 未调用也能清理资源
|
|
49
|
-
if (typeof Symbol.dispose !== 'undefined') {
|
|
50
|
-
// 支持 using 语法的自动清理
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* 实现 Disposable 接口
|
|
56
|
-
* 确保资源被正确释放
|
|
57
|
-
*/
|
|
58
|
-
[Symbol.dispose](): void {
|
|
59
|
-
this.stop();
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* 检查是否已释放
|
|
64
|
-
*/
|
|
65
|
-
isDisposed(): boolean {
|
|
66
|
-
return this.disposed;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* 停止速率限制器,清理资源
|
|
71
|
-
* 幂等操作,可多次调用
|
|
72
|
-
*/
|
|
73
|
-
stop(): void {
|
|
74
|
-
if (this.disposed) {
|
|
75
|
-
return; // 幂等:已释放则跳过
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
this.disposed = true;
|
|
79
|
-
|
|
80
|
-
if (this.cleanupTimer) {
|
|
81
|
-
clearInterval(this.cleanupTimer);
|
|
82
|
-
this.cleanupTimer = undefined;
|
|
83
|
-
}
|
|
84
|
-
this.store.clear();
|
|
85
|
-
this.logger.info('Rate limiter stopped');
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* 检查是否允许请求
|
|
90
|
-
* @param key 标识符(如 IP 地址、Peer ID)
|
|
91
|
-
* @returns 是否允许请求
|
|
92
|
-
*/
|
|
93
|
-
allowRequest(key: string): boolean {
|
|
94
|
-
const now = Date.now();
|
|
95
|
-
const entry = this.store.get(key);
|
|
96
|
-
|
|
97
|
-
if (!entry) {
|
|
98
|
-
// 首次请求,初始化令牌桶
|
|
99
|
-
// 初始令牌数为 maxRequests - 1(本次请求消耗 1 个)
|
|
100
|
-
this.store.set(key, {
|
|
101
|
-
tokens: this.config.maxRequests - 1,
|
|
102
|
-
lastRefill: now
|
|
103
|
-
});
|
|
104
|
-
return true;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// 计算需要补充的令牌数
|
|
108
|
-
const timePassed = now - entry.lastRefill;
|
|
109
|
-
const tokensToAdd = Math.floor(
|
|
110
|
-
(timePassed / this.config.windowMs) * this.config.maxRequests
|
|
111
|
-
);
|
|
112
|
-
|
|
113
|
-
if (tokensToAdd > 0) {
|
|
114
|
-
// 令牌补充后不能超过突发容量
|
|
115
|
-
entry.tokens = Math.min(
|
|
116
|
-
this.burstCapacity,
|
|
117
|
-
entry.tokens + tokensToAdd
|
|
118
|
-
);
|
|
119
|
-
entry.lastRefill = now;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// 检查是否有可用令牌
|
|
123
|
-
if (entry.tokens > 0) {
|
|
124
|
-
entry.tokens--;
|
|
125
|
-
return true;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
this.logger.warn('Rate limit exceeded', {
|
|
129
|
-
key,
|
|
130
|
-
remaining: entry.tokens,
|
|
131
|
-
maxRequests: this.config.maxRequests,
|
|
132
|
-
burstCapacity: this.burstCapacity
|
|
133
|
-
});
|
|
134
|
-
return false;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* 获取剩余令牌数
|
|
139
|
-
*/
|
|
140
|
-
getRemainingTokens(key: string): number {
|
|
141
|
-
const entry = this.store.get(key);
|
|
142
|
-
if (!entry) return this.config.maxRequests;
|
|
143
|
-
|
|
144
|
-
const now = Date.now();
|
|
145
|
-
const timePassed = now - entry.lastRefill;
|
|
146
|
-
const tokensToAdd = Math.floor(
|
|
147
|
-
(timePassed / this.config.windowMs) * this.config.maxRequests
|
|
148
|
-
);
|
|
149
|
-
|
|
150
|
-
return Math.min(this.burstCapacity, entry.tokens + tokensToAdd);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* 重置限制
|
|
155
|
-
*/
|
|
156
|
-
reset(key?: string): void {
|
|
157
|
-
if (key) {
|
|
158
|
-
this.store.delete(key);
|
|
159
|
-
} else {
|
|
160
|
-
this.store.clear();
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* 清理过期的条目
|
|
166
|
-
*/
|
|
167
|
-
cleanup(): void {
|
|
168
|
-
const now = Date.now();
|
|
169
|
-
const maxAge = this.config.windowMs * 2;
|
|
170
|
-
|
|
171
|
-
for (const [key, entry] of this.store) {
|
|
172
|
-
if (now - entry.lastRefill > maxAge) {
|
|
173
|
-
this.store.delete(key);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* 创建速率限制中间件(用于 HTTP 服务器)
|
|
181
|
-
*/
|
|
182
|
-
export function createRateLimitMiddleware(config: RateLimitConfig) {
|
|
183
|
-
const limiter = new RateLimiter(config);
|
|
184
|
-
|
|
185
|
-
const middleware = (req: any, res: any, next: () => void) => {
|
|
186
|
-
const key = req.socket?.remoteAddress || 'unknown';
|
|
187
|
-
|
|
188
|
-
if (!limiter.allowRequest(key)) {
|
|
189
|
-
res.writeHead(429, { 'Content-Type': 'application/json' });
|
|
190
|
-
res.end(JSON.stringify({
|
|
191
|
-
success: false,
|
|
192
|
-
error: 'Too many requests',
|
|
193
|
-
code: 'RATE_LIMIT_EXCEEDED'
|
|
194
|
-
}));
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
next();
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
// 提供 stop 方法清理资源
|
|
202
|
-
middleware.stop = () => {
|
|
203
|
-
limiter.stop();
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
return middleware;
|
|
207
|
-
}
|
package/src/utils/signature.ts
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 请求签名验证工具
|
|
3
|
-
* 使用 HMAC-SHA256 验证消息来源真实性
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { createHmac, randomBytes, timingSafeEqual } from 'crypto';
|
|
7
|
-
import { Logger } from './logger.js';
|
|
8
|
-
|
|
9
|
-
export interface SignatureConfig {
|
|
10
|
-
/** 签名密钥 */
|
|
11
|
-
secretKey: string;
|
|
12
|
-
/** 时间戳容忍度(毫秒,默认 5 分钟) */
|
|
13
|
-
timestampTolerance?: number;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface SignedMessage {
|
|
17
|
-
/** 消息体 */
|
|
18
|
-
payload: string;
|
|
19
|
-
/** 时间戳 */
|
|
20
|
-
timestamp: number;
|
|
21
|
-
/** 签名 */
|
|
22
|
-
signature: string;
|
|
23
|
-
/** 随机 nonce */
|
|
24
|
-
nonce: string;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 请求签名验证器
|
|
29
|
-
*/
|
|
30
|
-
export class RequestSigner {
|
|
31
|
-
private secretKey: string;
|
|
32
|
-
private timestampTolerance: number;
|
|
33
|
-
private logger: Logger;
|
|
34
|
-
|
|
35
|
-
constructor(config: SignatureConfig) {
|
|
36
|
-
this.secretKey = config.secretKey;
|
|
37
|
-
this.timestampTolerance = config.timestampTolerance || 5 * 60 * 1000; // 5 分钟
|
|
38
|
-
this.logger = new Logger({ component: 'RequestSigner' });
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* 生成签名
|
|
43
|
-
* @param payload - 消息体(JSON 字符串)
|
|
44
|
-
* @returns 签名后的消息
|
|
45
|
-
*/
|
|
46
|
-
sign(payload: string): SignedMessage {
|
|
47
|
-
const timestamp = Date.now();
|
|
48
|
-
const nonce = randomBytes(16).toString('hex');
|
|
49
|
-
const signature = this.generateSignature(payload, timestamp, nonce);
|
|
50
|
-
|
|
51
|
-
return {
|
|
52
|
-
payload,
|
|
53
|
-
timestamp,
|
|
54
|
-
signature,
|
|
55
|
-
nonce
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* 验证签名
|
|
61
|
-
* @param message - 签名后的消息
|
|
62
|
-
* @returns 验证结果
|
|
63
|
-
*/
|
|
64
|
-
verify(message: SignedMessage): { valid: boolean; error?: string } {
|
|
65
|
-
// 1. 检查时间戳
|
|
66
|
-
const now = Date.now();
|
|
67
|
-
const timeDiff = Math.abs(now - message.timestamp);
|
|
68
|
-
if (timeDiff > this.timestampTolerance) {
|
|
69
|
-
this.logger.warn('Signature timestamp expired', {
|
|
70
|
-
timestamp: message.timestamp,
|
|
71
|
-
diff: timeDiff
|
|
72
|
-
});
|
|
73
|
-
return { valid: false, error: 'Timestamp expired' };
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// 2. 验证签名
|
|
77
|
-
const expectedSignature = this.generateSignature(
|
|
78
|
-
message.payload,
|
|
79
|
-
message.timestamp,
|
|
80
|
-
message.nonce
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
if (!this.constantTimeCompare(message.signature, expectedSignature)) {
|
|
84
|
-
this.logger.warn('Invalid signature');
|
|
85
|
-
return { valid: false, error: 'Invalid signature' };
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return { valid: true };
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* 生成 HMAC-SHA256 签名
|
|
93
|
-
*/
|
|
94
|
-
private generateSignature(payload: string, timestamp: number, nonce: string): string {
|
|
95
|
-
const data = `${payload}:${timestamp}:${nonce}`;
|
|
96
|
-
return createHmac('sha256', this.secretKey).update(data).digest('hex');
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* 常量时间比较(防止时序攻击)
|
|
101
|
-
*/
|
|
102
|
-
private constantTimeCompare(a: string, b: string): boolean {
|
|
103
|
-
if (a.length !== b.length) return false;
|
|
104
|
-
const bufA = Buffer.from(a);
|
|
105
|
-
const bufB = Buffer.from(b);
|
|
106
|
-
return timingSafeEqual(bufA, bufB);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* 从环境变量加载签名密钥
|
|
112
|
-
*/
|
|
113
|
-
export function loadSignatureConfig(): SignatureConfig | null {
|
|
114
|
-
const secretKey = process.env.F2A_SIGNATURE_KEY;
|
|
115
|
-
if (!secretKey) {
|
|
116
|
-
// 生产环境强制警告,开发环境仅提示
|
|
117
|
-
const isProduction = process.env.NODE_ENV === 'production';
|
|
118
|
-
const logger = new Logger({ component: 'SignatureConfig' });
|
|
119
|
-
|
|
120
|
-
if (isProduction) {
|
|
121
|
-
logger.error('F2A_SIGNATURE_KEY is not set! Signature verification is DISABLED. This is a security risk in production.');
|
|
122
|
-
} else {
|
|
123
|
-
logger.warn('F2A_SIGNATURE_KEY is not set. Signature verification is disabled. Set this environment variable for secure message verification.');
|
|
124
|
-
}
|
|
125
|
-
return null;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
const tolerance = process.env.F2A_SIGNATURE_TOLERANCE
|
|
129
|
-
? parseInt(process.env.F2A_SIGNATURE_TOLERANCE, 10)
|
|
130
|
-
: undefined;
|
|
131
|
-
|
|
132
|
-
return {
|
|
133
|
-
secretKey,
|
|
134
|
-
timestampTolerance: tolerance
|
|
135
|
-
};
|
|
136
|
-
}
|