@f2a/network 0.1.2 → 0.2.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.
Files changed (226) hide show
  1. package/README.md +278 -63
  2. package/dist/cli/commands.d.ts.map +1 -1
  3. package/dist/cli/commands.js +29 -2
  4. package/dist/cli/commands.js.map +1 -1
  5. package/dist/cli/config.d.ts +176 -0
  6. package/dist/cli/config.d.ts.map +1 -0
  7. package/dist/cli/config.js +386 -0
  8. package/dist/cli/config.js.map +1 -0
  9. package/dist/cli/daemon.d.ts +54 -0
  10. package/dist/cli/daemon.d.ts.map +1 -0
  11. package/dist/cli/daemon.js +572 -0
  12. package/dist/cli/daemon.js.map +1 -0
  13. package/dist/cli/index.js +90 -16
  14. package/dist/cli/index.js.map +1 -1
  15. package/dist/cli/init.d.ts +13 -0
  16. package/dist/cli/init.d.ts.map +1 -0
  17. package/dist/cli/init.js +352 -0
  18. package/dist/cli/init.js.map +1 -0
  19. package/dist/core/e2ee-crypto.d.ts +127 -1
  20. package/dist/core/e2ee-crypto.d.ts.map +1 -1
  21. package/dist/core/e2ee-crypto.js +446 -12
  22. package/dist/core/e2ee-crypto.js.map +1 -1
  23. package/dist/core/f2a.d.ts +2 -1
  24. package/dist/core/f2a.d.ts.map +1 -1
  25. package/dist/core/f2a.js +6 -2
  26. package/dist/core/f2a.js.map +1 -1
  27. package/dist/core/identity/encrypted-key-store.d.ts +19 -0
  28. package/dist/core/identity/encrypted-key-store.d.ts.map +1 -0
  29. package/dist/core/identity/encrypted-key-store.js +72 -0
  30. package/dist/core/identity/encrypted-key-store.js.map +1 -0
  31. package/dist/core/identity/identity-manager.d.ts +133 -0
  32. package/dist/core/identity/identity-manager.d.ts.map +1 -0
  33. package/dist/core/identity/identity-manager.js +454 -0
  34. package/dist/core/identity/identity-manager.js.map +1 -0
  35. package/dist/core/identity/index.d.ts +8 -0
  36. package/dist/core/identity/index.d.ts.map +1 -0
  37. package/dist/core/identity/index.js +7 -0
  38. package/dist/core/identity/index.js.map +1 -0
  39. package/dist/core/identity/types.d.ts +70 -0
  40. package/dist/core/identity/types.d.ts.map +1 -0
  41. package/dist/core/identity/types.js +17 -0
  42. package/dist/core/identity/types.js.map +1 -0
  43. package/dist/core/p2p-network.d.ts +26 -0
  44. package/dist/core/p2p-network.d.ts.map +1 -1
  45. package/dist/core/p2p-network.js +434 -105
  46. package/dist/core/p2p-network.js.map +1 -1
  47. package/dist/core/reputation-security.d.ts +15 -0
  48. package/dist/core/reputation-security.d.ts.map +1 -1
  49. package/dist/core/reputation-security.js +73 -3
  50. package/dist/core/reputation-security.js.map +1 -1
  51. package/dist/core/reputation.d.ts +129 -4
  52. package/dist/core/reputation.d.ts.map +1 -1
  53. package/dist/core/reputation.js +294 -1
  54. package/dist/core/reputation.js.map +1 -1
  55. package/dist/core/review-committee.d.ts +2 -2
  56. package/dist/core/review-committee.d.ts.map +1 -1
  57. package/dist/core/review-committee.js +17 -0
  58. package/dist/core/review-committee.js.map +1 -1
  59. package/dist/daemon/control-server.d.ts.map +1 -1
  60. package/dist/daemon/control-server.js +44 -1
  61. package/dist/daemon/control-server.js.map +1 -1
  62. package/dist/daemon/webhook.d.ts +3 -0
  63. package/dist/daemon/webhook.d.ts.map +1 -1
  64. package/dist/daemon/webhook.js +318 -6
  65. package/dist/daemon/webhook.js.map +1 -1
  66. package/dist/index.d.ts +3 -3
  67. package/dist/index.d.ts.map +1 -1
  68. package/dist/index.js +7 -3
  69. package/dist/index.js.map +1 -1
  70. package/dist/types/index.d.ts +4 -0
  71. package/dist/types/index.d.ts.map +1 -1
  72. package/dist/types/index.js.map +1 -1
  73. package/dist/types/result.d.ts +1 -1
  74. package/dist/types/result.d.ts.map +1 -1
  75. package/dist/types/result.js.map +1 -1
  76. package/dist/utils/crypto-utils.d.ts +17 -0
  77. package/dist/utils/crypto-utils.d.ts.map +1 -0
  78. package/dist/utils/crypto-utils.js +28 -0
  79. package/dist/utils/crypto-utils.js.map +1 -0
  80. package/dist/utils/logger.d.ts +1 -0
  81. package/dist/utils/logger.d.ts.map +1 -1
  82. package/dist/utils/logger.js +9 -3
  83. package/dist/utils/logger.js.map +1 -1
  84. package/dist/utils/rate-limiter.d.ts.map +1 -1
  85. package/dist/utils/rate-limiter.js +3 -1
  86. package/dist/utils/rate-limiter.js.map +1 -1
  87. package/dist/utils/signature.d.ts +47 -1
  88. package/dist/utils/signature.d.ts.map +1 -1
  89. package/dist/utils/signature.js +166 -11
  90. package/dist/utils/signature.js.map +1 -1
  91. package/package.json +9 -1
  92. package/.github/workflows/ci.yml +0 -113
  93. package/.github/workflows/publish.yml +0 -60
  94. package/MONOREPO.md +0 -58
  95. package/SKILL.md +0 -137
  96. package/dist/adapters/openclaw.d.ts +0 -103
  97. package/dist/adapters/openclaw.d.ts.map +0 -1
  98. package/dist/adapters/openclaw.js +0 -297
  99. package/dist/adapters/openclaw.js.map +0 -1
  100. package/dist/core/connection-manager.d.ts +0 -80
  101. package/dist/core/connection-manager.d.ts.map +0 -1
  102. package/dist/core/connection-manager.js +0 -235
  103. package/dist/core/connection-manager.js.map +0 -1
  104. package/dist/core/connection-manager.test.d.ts +0 -2
  105. package/dist/core/connection-manager.test.d.ts.map +0 -1
  106. package/dist/core/connection-manager.test.js +0 -52
  107. package/dist/core/connection-manager.test.js.map +0 -1
  108. package/dist/core/identity.d.ts +0 -47
  109. package/dist/core/identity.d.ts.map +0 -1
  110. package/dist/core/identity.js +0 -130
  111. package/dist/core/identity.js.map +0 -1
  112. package/dist/core/identity.test.d.ts +0 -2
  113. package/dist/core/identity.test.d.ts.map +0 -1
  114. package/dist/core/identity.test.js +0 -43
  115. package/dist/core/identity.test.js.map +0 -1
  116. package/dist/core/serverless.d.ts +0 -155
  117. package/dist/core/serverless.d.ts.map +0 -1
  118. package/dist/core/serverless.js +0 -615
  119. package/dist/core/serverless.js.map +0 -1
  120. package/dist/daemon/webhook.test.d.ts +0 -2
  121. package/dist/daemon/webhook.test.d.ts.map +0 -1
  122. package/dist/daemon/webhook.test.js +0 -24
  123. package/dist/daemon/webhook.test.js.map +0 -1
  124. package/dist/protocol/messages.d.ts +0 -739
  125. package/dist/protocol/messages.d.ts.map +0 -1
  126. package/dist/protocol/messages.js +0 -188
  127. package/dist/protocol/messages.js.map +0 -1
  128. package/dist/protocol/messages.test.d.ts +0 -2
  129. package/dist/protocol/messages.test.d.ts.map +0 -1
  130. package/dist/protocol/messages.test.js +0 -55
  131. package/dist/protocol/messages.test.js.map +0 -1
  132. package/docs/F2A-PROTOCOL.md +0 -61
  133. package/docs/MOBILE_BOOTSTRAP_DESIGN.md +0 -126
  134. package/docs/a2a-lessons.md +0 -316
  135. package/docs/middleware-guide.md +0 -448
  136. package/docs/readme-update-checklist.md +0 -90
  137. package/docs/reputation-guide.md +0 -396
  138. package/docs/rfcs/001-reputation-system.md +0 -712
  139. package/docs/security-design.md +0 -247
  140. package/install.sh +0 -231
  141. package/packages/openclaw-adapter/README.md +0 -510
  142. package/packages/openclaw-adapter/openclaw.plugin.json +0 -106
  143. package/packages/openclaw-adapter/package.json +0 -40
  144. package/packages/openclaw-adapter/src/announcement-queue.test.ts +0 -449
  145. package/packages/openclaw-adapter/src/announcement-queue.ts +0 -403
  146. package/packages/openclaw-adapter/src/capability-detector.test.ts +0 -99
  147. package/packages/openclaw-adapter/src/capability-detector.ts +0 -183
  148. package/packages/openclaw-adapter/src/claim-handlers.test.ts +0 -974
  149. package/packages/openclaw-adapter/src/claim-handlers.ts +0 -482
  150. package/packages/openclaw-adapter/src/connector.business.test.ts +0 -583
  151. package/packages/openclaw-adapter/src/connector.ts +0 -795
  152. package/packages/openclaw-adapter/src/index.test.ts +0 -82
  153. package/packages/openclaw-adapter/src/index.ts +0 -18
  154. package/packages/openclaw-adapter/src/integration.e2e.test.ts +0 -829
  155. package/packages/openclaw-adapter/src/logger.ts +0 -51
  156. package/packages/openclaw-adapter/src/network-client.test.ts +0 -266
  157. package/packages/openclaw-adapter/src/network-client.ts +0 -251
  158. package/packages/openclaw-adapter/src/network-recovery.test.ts +0 -465
  159. package/packages/openclaw-adapter/src/node-manager.test.ts +0 -136
  160. package/packages/openclaw-adapter/src/node-manager.ts +0 -429
  161. package/packages/openclaw-adapter/src/plugin.test.ts +0 -439
  162. package/packages/openclaw-adapter/src/plugin.ts +0 -104
  163. package/packages/openclaw-adapter/src/reputation.test.ts +0 -221
  164. package/packages/openclaw-adapter/src/reputation.ts +0 -368
  165. package/packages/openclaw-adapter/src/task-guard.test.ts +0 -502
  166. package/packages/openclaw-adapter/src/task-guard.ts +0 -860
  167. package/packages/openclaw-adapter/src/task-queue.concurrency.test.ts +0 -462
  168. package/packages/openclaw-adapter/src/task-queue.edge-cases.test.ts +0 -284
  169. package/packages/openclaw-adapter/src/task-queue.persistence.test.ts +0 -408
  170. package/packages/openclaw-adapter/src/task-queue.ts +0 -668
  171. package/packages/openclaw-adapter/src/tool-handlers.test.ts +0 -906
  172. package/packages/openclaw-adapter/src/tool-handlers.ts +0 -574
  173. package/packages/openclaw-adapter/src/types.ts +0 -361
  174. package/packages/openclaw-adapter/src/webhook-pusher.test.ts +0 -188
  175. package/packages/openclaw-adapter/src/webhook-pusher.ts +0 -220
  176. package/packages/openclaw-adapter/src/webhook-server.test.ts +0 -580
  177. package/packages/openclaw-adapter/src/webhook-server.ts +0 -202
  178. package/packages/openclaw-adapter/tsconfig.json +0 -20
  179. package/src/cli/commands.test.ts +0 -157
  180. package/src/cli/commands.ts +0 -129
  181. package/src/cli/index.test.ts +0 -77
  182. package/src/cli/index.ts +0 -234
  183. package/src/core/autonomous-economy.test.ts +0 -291
  184. package/src/core/autonomous-economy.ts +0 -428
  185. package/src/core/e2ee-crypto.test.ts +0 -125
  186. package/src/core/e2ee-crypto.ts +0 -246
  187. package/src/core/f2a.test.ts +0 -269
  188. package/src/core/f2a.ts +0 -618
  189. package/src/core/p2p-network.test.ts +0 -199
  190. package/src/core/p2p-network.ts +0 -1432
  191. package/src/core/reputation-security.test.ts +0 -403
  192. package/src/core/reputation-security.ts +0 -562
  193. package/src/core/reputation.test.ts +0 -260
  194. package/src/core/reputation.ts +0 -576
  195. package/src/core/review-committee.test.ts +0 -380
  196. package/src/core/review-committee.ts +0 -401
  197. package/src/core/token-manager.test.ts +0 -133
  198. package/src/core/token-manager.ts +0 -140
  199. package/src/daemon/control-server.test.ts +0 -216
  200. package/src/daemon/control-server.ts +0 -292
  201. package/src/daemon/index.test.ts +0 -85
  202. package/src/daemon/index.ts +0 -89
  203. package/src/daemon/main.ts +0 -44
  204. package/src/daemon/start.ts +0 -29
  205. package/src/daemon/webhook.test.ts +0 -68
  206. package/src/daemon/webhook.ts +0 -105
  207. package/src/index.test.ts +0 -436
  208. package/src/index.ts +0 -72
  209. package/src/types/index.test.ts +0 -87
  210. package/src/types/index.ts +0 -341
  211. package/src/types/result.ts +0 -68
  212. package/src/utils/benchmark.ts +0 -237
  213. package/src/utils/logger.ts +0 -331
  214. package/src/utils/middleware.ts +0 -229
  215. package/src/utils/rate-limiter.ts +0 -207
  216. package/src/utils/signature.ts +0 -136
  217. package/src/utils/validation.ts +0 -186
  218. package/tests/docker/Dockerfile.node +0 -23
  219. package/tests/docker/Dockerfile.runner +0 -18
  220. package/tests/docker/docker-compose.test.yml +0 -73
  221. package/tests/integration/message-passing.test.ts +0 -109
  222. package/tests/integration/multi-node.test.ts +0 -92
  223. package/tests/integration/p2p-connection.test.ts +0 -83
  224. package/tests/integration/test-config.ts +0 -32
  225. package/tsconfig.json +0 -21
  226. package/vitest.config.ts +0 -26
@@ -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
- }
@@ -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
- }