@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.
- package/README.md +278 -63
- package/dist/cli/commands.d.ts.map +1 -1
- package/dist/cli/commands.js +29 -2
- package/dist/cli/commands.js.map +1 -1
- package/dist/cli/config.d.ts +176 -0
- package/dist/cli/config.d.ts.map +1 -0
- package/dist/cli/config.js +386 -0
- package/dist/cli/config.js.map +1 -0
- package/dist/cli/daemon.d.ts +54 -0
- package/dist/cli/daemon.d.ts.map +1 -0
- package/dist/cli/daemon.js +572 -0
- package/dist/cli/daemon.js.map +1 -0
- package/dist/cli/index.js +90 -16
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/init.d.ts +13 -0
- package/dist/cli/init.d.ts.map +1 -0
- package/dist/cli/init.js +352 -0
- package/dist/cli/init.js.map +1 -0
- package/dist/core/e2ee-crypto.d.ts +127 -1
- package/dist/core/e2ee-crypto.d.ts.map +1 -1
- package/dist/core/e2ee-crypto.js +446 -12
- package/dist/core/e2ee-crypto.js.map +1 -1
- package/dist/core/f2a.d.ts +2 -1
- package/dist/core/f2a.d.ts.map +1 -1
- package/dist/core/f2a.js +6 -2
- package/dist/core/f2a.js.map +1 -1
- package/dist/core/identity/encrypted-key-store.d.ts +19 -0
- package/dist/core/identity/encrypted-key-store.d.ts.map +1 -0
- package/dist/core/identity/encrypted-key-store.js +72 -0
- package/dist/core/identity/encrypted-key-store.js.map +1 -0
- package/dist/core/identity/identity-manager.d.ts +133 -0
- package/dist/core/identity/identity-manager.d.ts.map +1 -0
- package/dist/core/identity/identity-manager.js +454 -0
- package/dist/core/identity/identity-manager.js.map +1 -0
- package/dist/core/identity/index.d.ts +8 -0
- package/dist/core/identity/index.d.ts.map +1 -0
- package/dist/core/identity/index.js +7 -0
- package/dist/core/identity/index.js.map +1 -0
- package/dist/core/identity/types.d.ts +70 -0
- package/dist/core/identity/types.d.ts.map +1 -0
- package/dist/core/identity/types.js +17 -0
- package/dist/core/identity/types.js.map +1 -0
- package/dist/core/p2p-network.d.ts +26 -0
- package/dist/core/p2p-network.d.ts.map +1 -1
- package/dist/core/p2p-network.js +434 -105
- package/dist/core/p2p-network.js.map +1 -1
- package/dist/core/reputation-security.d.ts +15 -0
- package/dist/core/reputation-security.d.ts.map +1 -1
- package/dist/core/reputation-security.js +73 -3
- package/dist/core/reputation-security.js.map +1 -1
- package/dist/core/reputation.d.ts +129 -4
- package/dist/core/reputation.d.ts.map +1 -1
- package/dist/core/reputation.js +294 -1
- package/dist/core/reputation.js.map +1 -1
- package/dist/core/review-committee.d.ts +2 -2
- package/dist/core/review-committee.d.ts.map +1 -1
- package/dist/core/review-committee.js +17 -0
- package/dist/core/review-committee.js.map +1 -1
- package/dist/daemon/control-server.d.ts.map +1 -1
- package/dist/daemon/control-server.js +44 -1
- package/dist/daemon/control-server.js.map +1 -1
- package/dist/daemon/webhook.d.ts +3 -0
- package/dist/daemon/webhook.d.ts.map +1 -1
- package/dist/daemon/webhook.js +318 -6
- package/dist/daemon/webhook.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -3
- package/dist/index.js.map +1 -1
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js.map +1 -1
- package/dist/types/result.d.ts +1 -1
- package/dist/types/result.d.ts.map +1 -1
- package/dist/types/result.js.map +1 -1
- package/dist/utils/crypto-utils.d.ts +17 -0
- package/dist/utils/crypto-utils.d.ts.map +1 -0
- package/dist/utils/crypto-utils.js +28 -0
- package/dist/utils/crypto-utils.js.map +1 -0
- package/dist/utils/logger.d.ts +1 -0
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/logger.js +9 -3
- package/dist/utils/logger.js.map +1 -1
- package/dist/utils/rate-limiter.d.ts.map +1 -1
- package/dist/utils/rate-limiter.js +3 -1
- package/dist/utils/rate-limiter.js.map +1 -1
- package/dist/utils/signature.d.ts +47 -1
- package/dist/utils/signature.d.ts.map +1 -1
- package/dist/utils/signature.js +166 -11
- package/dist/utils/signature.js.map +1 -1
- package/package.json +9 -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
|
@@ -1,202 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Webhook Server
|
|
3
|
-
* 接收 F2A Node 的事件通知
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { createServer, IncomingMessage, ServerResponse } from 'http';
|
|
7
|
-
import type {
|
|
8
|
-
WebhookEvent,
|
|
9
|
-
DiscoverWebhookPayload,
|
|
10
|
-
DelegateWebhookPayload,
|
|
11
|
-
AgentCapability,
|
|
12
|
-
TaskResponse
|
|
13
|
-
} from './types.js';
|
|
14
|
-
import { webhookLogger as logger } from './logger.js';
|
|
15
|
-
|
|
16
|
-
/** 默认请求体大小限制 (64KB) - 元数据交换足够,防止 DoS */
|
|
17
|
-
const DEFAULT_MAX_BODY_SIZE = 64 * 1024;
|
|
18
|
-
|
|
19
|
-
export interface WebhookHandler {
|
|
20
|
-
onDiscover(payload: DiscoverWebhookPayload): Promise<{
|
|
21
|
-
capabilities: AgentCapability[];
|
|
22
|
-
reputation?: number;
|
|
23
|
-
}>;
|
|
24
|
-
|
|
25
|
-
onDelegate(payload: DelegateWebhookPayload): Promise<{
|
|
26
|
-
accepted: boolean;
|
|
27
|
-
taskId: string;
|
|
28
|
-
reason?: string;
|
|
29
|
-
}>;
|
|
30
|
-
|
|
31
|
-
onStatus(): Promise<{
|
|
32
|
-
status: 'available' | 'busy' | 'offline';
|
|
33
|
-
load?: number;
|
|
34
|
-
}>;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export class WebhookServer {
|
|
38
|
-
private port: number;
|
|
39
|
-
private handler: WebhookHandler;
|
|
40
|
-
private server?: ReturnType<typeof createServer>;
|
|
41
|
-
private maxBodySize: number;
|
|
42
|
-
/** 允许的 CORS 来源列表 */
|
|
43
|
-
private allowedOrigins: string[];
|
|
44
|
-
|
|
45
|
-
constructor(port: number, handler: WebhookHandler, options?: {
|
|
46
|
-
maxBodySize?: number;
|
|
47
|
-
allowedOrigins?: string[];
|
|
48
|
-
}) {
|
|
49
|
-
this.port = port;
|
|
50
|
-
this.handler = handler;
|
|
51
|
-
this.maxBodySize = options?.maxBodySize || DEFAULT_MAX_BODY_SIZE;
|
|
52
|
-
// 默认只允许 localhost
|
|
53
|
-
this.allowedOrigins = options?.allowedOrigins ?? ['http://localhost'];
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* 启动 Webhook 服务器
|
|
58
|
-
*/
|
|
59
|
-
async start(): Promise<void> {
|
|
60
|
-
return new Promise((resolve, reject) => {
|
|
61
|
-
this.server = createServer(this.handleRequest.bind(this));
|
|
62
|
-
|
|
63
|
-
this.server.listen(this.port, () => {
|
|
64
|
-
logger.info('服务器启动在端口 %d', this.port);
|
|
65
|
-
resolve();
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
this.server.on('error', (err) => {
|
|
69
|
-
reject(err);
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* 停止 Webhook 服务器
|
|
76
|
-
*/
|
|
77
|
-
async stop(): Promise<void> {
|
|
78
|
-
if (this.server) {
|
|
79
|
-
return new Promise((resolve) => {
|
|
80
|
-
this.server?.close(() => {
|
|
81
|
-
logger.info('服务器已停止');
|
|
82
|
-
resolve();
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* 处理 HTTP 请求
|
|
90
|
-
*/
|
|
91
|
-
private async handleRequest(req: IncomingMessage, res: ServerResponse): Promise<void> {
|
|
92
|
-
// 设置 CORS - 使用配置的允许来源
|
|
93
|
-
const origin = req.headers.origin;
|
|
94
|
-
// 当 allowedOrigins 为空数组时,使用默认值 'http://localhost'
|
|
95
|
-
// 当 origin 不在允许列表中时,使用第一个允许的来源或默认值
|
|
96
|
-
const defaultOrigin = 'http://localhost';
|
|
97
|
-
let allowOrigin: string;
|
|
98
|
-
|
|
99
|
-
if (this.allowedOrigins.length === 0) {
|
|
100
|
-
// 没有配置允许来源,使用默认值
|
|
101
|
-
allowOrigin = defaultOrigin;
|
|
102
|
-
} else if (origin && this.allowedOrigins.includes(origin)) {
|
|
103
|
-
// origin 在允许列表中
|
|
104
|
-
allowOrigin = origin;
|
|
105
|
-
} else {
|
|
106
|
-
// origin 不在允许列表中,使用第一个允许的来源
|
|
107
|
-
allowOrigin = this.allowedOrigins[0];
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
res.setHeader('Access-Control-Allow-Origin', allowOrigin);
|
|
111
|
-
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
|
|
112
|
-
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
|
113
|
-
|
|
114
|
-
if (req.method === 'OPTIONS') {
|
|
115
|
-
res.writeHead(200);
|
|
116
|
-
res.end();
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
if (req.method !== 'POST') {
|
|
121
|
-
res.writeHead(405, { 'Content-Type': 'application/json' });
|
|
122
|
-
res.end(JSON.stringify({ error: 'Method not allowed' }));
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
try {
|
|
127
|
-
const body = await this.parseBody(req);
|
|
128
|
-
const event = body as WebhookEvent;
|
|
129
|
-
|
|
130
|
-
logger.info('收到事件: %s', event.type);
|
|
131
|
-
|
|
132
|
-
let result: unknown;
|
|
133
|
-
|
|
134
|
-
switch (event.type) {
|
|
135
|
-
case 'discover':
|
|
136
|
-
result = await this.handler.onDiscover(event.payload as DiscoverWebhookPayload);
|
|
137
|
-
break;
|
|
138
|
-
|
|
139
|
-
case 'delegate':
|
|
140
|
-
result = await this.handler.onDelegate(event.payload as DelegateWebhookPayload);
|
|
141
|
-
break;
|
|
142
|
-
|
|
143
|
-
case 'status':
|
|
144
|
-
result = await this.handler.onStatus();
|
|
145
|
-
break;
|
|
146
|
-
|
|
147
|
-
default:
|
|
148
|
-
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
149
|
-
res.end(JSON.stringify({ error: `Unknown event type: ${event.type}` }));
|
|
150
|
-
return;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
154
|
-
res.end(JSON.stringify(result));
|
|
155
|
-
|
|
156
|
-
} catch (error) {
|
|
157
|
-
logger.error('处理错误: %s', error);
|
|
158
|
-
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
159
|
-
res.end(JSON.stringify({
|
|
160
|
-
error: error instanceof Error ? error.message : 'Internal error'
|
|
161
|
-
}));
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* 解析请求体
|
|
167
|
-
* 带大小限制,防止 DoS 攻击
|
|
168
|
-
*/
|
|
169
|
-
private parseBody(req: IncomingMessage): Promise<unknown> {
|
|
170
|
-
return new Promise((resolve, reject) => {
|
|
171
|
-
let body = '';
|
|
172
|
-
let size = 0;
|
|
173
|
-
|
|
174
|
-
req.on('data', (chunk) => {
|
|
175
|
-
size += chunk.length;
|
|
176
|
-
|
|
177
|
-
// 检查请求体大小
|
|
178
|
-
if (size > this.maxBodySize) {
|
|
179
|
-
req.destroy();
|
|
180
|
-
reject(new Error(`Request body too large: ${size} bytes (max: ${this.maxBodySize})`));
|
|
181
|
-
return;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
body += chunk.toString();
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
req.on('end', () => {
|
|
188
|
-
try {
|
|
189
|
-
resolve(JSON.parse(body));
|
|
190
|
-
} catch (e) {
|
|
191
|
-
reject(new Error('Invalid JSON'));
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
req.on('error', reject);
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
getUrl(): string {
|
|
200
|
-
return `http://localhost:${this.port}/webhook`;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2022",
|
|
4
|
-
"module": "NodeNext",
|
|
5
|
-
"moduleResolution": "NodeNext",
|
|
6
|
-
"lib": ["ES2022"],
|
|
7
|
-
"outDir": "./dist",
|
|
8
|
-
"rootDir": "./src",
|
|
9
|
-
"strict": true,
|
|
10
|
-
"esModuleInterop": true,
|
|
11
|
-
"skipLibCheck": true,
|
|
12
|
-
"forceConsistentCasingInFileNames": true,
|
|
13
|
-
"declaration": true,
|
|
14
|
-
"declarationMap": true,
|
|
15
|
-
"sourceMap": true,
|
|
16
|
-
"resolveJsonModule": true
|
|
17
|
-
},
|
|
18
|
-
"include": ["src/**/*"],
|
|
19
|
-
"exclude": ["node_modules", "dist", "**/*.test.ts"]
|
|
20
|
-
}
|
package/src/cli/commands.test.ts
DELETED
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
-
import { listPending, confirm, reject } from './commands.js';
|
|
3
|
-
import { request, RequestOptions } from 'http';
|
|
4
|
-
|
|
5
|
-
// Mock http module
|
|
6
|
-
vi.mock('http', () => ({
|
|
7
|
-
request: vi.fn(),
|
|
8
|
-
}));
|
|
9
|
-
|
|
10
|
-
describe('CLI Commands', () => {
|
|
11
|
-
const mockRequest = {
|
|
12
|
-
on: vi.fn(),
|
|
13
|
-
write: vi.fn(),
|
|
14
|
-
end: vi.fn(),
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const mockResponse = {
|
|
18
|
-
statusCode: 200,
|
|
19
|
-
on: vi.fn(),
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
beforeEach(() => {
|
|
23
|
-
vi.clearAllMocks();
|
|
24
|
-
process.env.F2A_CONTROL_PORT = '9001';
|
|
25
|
-
process.env.F2A_CONTROL_TOKEN = 'test-token';
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
afterEach(() => {
|
|
29
|
-
delete process.env.F2A_CONTROL_PORT;
|
|
30
|
-
delete process.env.F2A_CONTROL_TOKEN;
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
describe('listPending', () => {
|
|
34
|
-
it('should list pending connections', async () => {
|
|
35
|
-
const pendingData = {
|
|
36
|
-
success: true,
|
|
37
|
-
pending: [
|
|
38
|
-
{ index: 1, agentIdShort: 'abc123', address: '192.168.1.1', port: 9000, remainingMinutes: 30 }
|
|
39
|
-
]
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
mockResponse.on.mockImplementation((event: string, callback: Function) => {
|
|
43
|
-
if (event === 'data') callback(Buffer.from(JSON.stringify(pendingData)));
|
|
44
|
-
if (event === 'end') callback();
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
(request as any).mockImplementation((options: RequestOptions, callback: Function) => {
|
|
48
|
-
callback(mockResponse);
|
|
49
|
-
return mockRequest;
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
53
|
-
await listPending();
|
|
54
|
-
|
|
55
|
-
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('待确认连接'));
|
|
56
|
-
consoleSpy.mockRestore();
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it('should show no pending message when empty', async () => {
|
|
60
|
-
mockResponse.on.mockImplementation((event: string, callback: Function) => {
|
|
61
|
-
if (event === 'data') callback(Buffer.from(JSON.stringify({ success: true, pending: [] })));
|
|
62
|
-
if (event === 'end') callback();
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
(request as any).mockImplementation((options: RequestOptions, callback: Function) => {
|
|
66
|
-
callback(mockResponse);
|
|
67
|
-
return mockRequest;
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
71
|
-
await listPending();
|
|
72
|
-
|
|
73
|
-
expect(consoleSpy).toHaveBeenCalledWith('没有待确认的连接请求');
|
|
74
|
-
consoleSpy.mockRestore();
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('should handle connection error', async () => {
|
|
78
|
-
(request as any).mockImplementation(() => {
|
|
79
|
-
const req = {
|
|
80
|
-
...mockRequest,
|
|
81
|
-
on: (event: string, callback: Function) => {
|
|
82
|
-
if (event === 'error') callback(new Error('Connection refused'));
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
return req;
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
89
|
-
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => { throw new Error('exit'); });
|
|
90
|
-
|
|
91
|
-
await expect(listPending()).rejects.toThrow('exit');
|
|
92
|
-
|
|
93
|
-
consoleSpy.mockRestore();
|
|
94
|
-
exitSpy.mockRestore();
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
describe('confirm', () => {
|
|
99
|
-
it('should confirm connection successfully', async () => {
|
|
100
|
-
mockResponse.on.mockImplementation((event: string, callback: Function) => {
|
|
101
|
-
if (event === 'data') callback(Buffer.from(JSON.stringify({ success: true, message: '已确认' })));
|
|
102
|
-
if (event === 'end') callback();
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
(request as any).mockImplementation((options: RequestOptions, callback: Function) => {
|
|
106
|
-
callback(mockResponse);
|
|
107
|
-
return mockRequest;
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
111
|
-
await confirm(1);
|
|
112
|
-
|
|
113
|
-
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('已确认'));
|
|
114
|
-
consoleSpy.mockRestore();
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it('should handle confirm error', async () => {
|
|
118
|
-
mockResponse.on.mockImplementation((event: string, callback: Function) => {
|
|
119
|
-
if (event === 'data') callback(Buffer.from(JSON.stringify({ success: false, error: 'Invalid ID' })));
|
|
120
|
-
if (event === 'end') callback();
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
(request as any).mockImplementation((options: RequestOptions, callback: Function) => {
|
|
124
|
-
callback(mockResponse);
|
|
125
|
-
return mockRequest;
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
129
|
-
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => { throw new Error('exit'); });
|
|
130
|
-
|
|
131
|
-
await expect(confirm(999)).rejects.toThrow('exit');
|
|
132
|
-
|
|
133
|
-
consoleSpy.mockRestore();
|
|
134
|
-
exitSpy.mockRestore();
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
describe('reject', () => {
|
|
139
|
-
it('should reject connection with reason', async () => {
|
|
140
|
-
mockResponse.on.mockImplementation((event: string, callback: Function) => {
|
|
141
|
-
if (event === 'data') callback(Buffer.from(JSON.stringify({ success: true, message: '已拒绝' })));
|
|
142
|
-
if (event === 'end') callback();
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
(request as any).mockImplementation((options: RequestOptions, callback: Function) => {
|
|
146
|
-
callback(mockResponse);
|
|
147
|
-
return mockRequest;
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
151
|
-
await reject(1, '可疑连接');
|
|
152
|
-
|
|
153
|
-
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('已拒绝'));
|
|
154
|
-
consoleSpy.mockRestore();
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
});
|
package/src/cli/commands.ts
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* F2A CLI
|
|
3
|
-
* 命令行工具
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { request, RequestOptions } from 'http';
|
|
7
|
-
|
|
8
|
-
const CONTROL_PORT = parseInt(process.env.F2A_CONTROL_PORT || '9001');
|
|
9
|
-
const CONTROL_TOKEN = process.env.F2A_CONTROL_TOKEN || 'f2a-default-token';
|
|
10
|
-
|
|
11
|
-
interface ControlResponse {
|
|
12
|
-
success: boolean;
|
|
13
|
-
message?: string;
|
|
14
|
-
pending?: Array<{
|
|
15
|
-
index: number;
|
|
16
|
-
agentIdShort: string;
|
|
17
|
-
address: string;
|
|
18
|
-
port: number;
|
|
19
|
-
remainingMinutes: number;
|
|
20
|
-
}>;
|
|
21
|
-
error?: string;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* 发送控制命令
|
|
26
|
-
*/
|
|
27
|
-
async function sendControlCommand(
|
|
28
|
-
action: string,
|
|
29
|
-
idOrIndex?: string | number,
|
|
30
|
-
reason?: string
|
|
31
|
-
): Promise<ControlResponse> {
|
|
32
|
-
return new Promise((resolve, reject) => {
|
|
33
|
-
const payload = JSON.stringify({ action, idOrIndex, reason });
|
|
34
|
-
|
|
35
|
-
const options: RequestOptions = {
|
|
36
|
-
hostname: '127.0.0.1',
|
|
37
|
-
port: CONTROL_PORT,
|
|
38
|
-
path: '/control',
|
|
39
|
-
method: 'POST',
|
|
40
|
-
headers: {
|
|
41
|
-
'Content-Type': 'application/json',
|
|
42
|
-
'X-F2A-Token': CONTROL_TOKEN,
|
|
43
|
-
'Content-Length': Buffer.byteLength(payload)
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const req = request(options, (res) => {
|
|
48
|
-
let data = '';
|
|
49
|
-
res.on('data', chunk => data += chunk);
|
|
50
|
-
res.on('end', () => {
|
|
51
|
-
try {
|
|
52
|
-
resolve(JSON.parse(data));
|
|
53
|
-
} catch {
|
|
54
|
-
resolve({ success: false, error: 'Invalid response' });
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
req.on('error', (err) => {
|
|
60
|
-
reject(err);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
req.write(payload);
|
|
64
|
-
req.end();
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* 列出待确认连接
|
|
70
|
-
*/
|
|
71
|
-
export async function listPending(): Promise<void> {
|
|
72
|
-
try {
|
|
73
|
-
const result = await sendControlCommand('list-pending');
|
|
74
|
-
|
|
75
|
-
if (result.success && result.pending && result.pending.length > 0) {
|
|
76
|
-
console.log(`待确认连接 (${result.pending.length}个):`);
|
|
77
|
-
result.pending.forEach(p => {
|
|
78
|
-
console.log(`${p.index}. ${p.agentIdShort} 来自 ${p.address}:${p.port} [剩余${p.remainingMinutes}分钟]`);
|
|
79
|
-
});
|
|
80
|
-
} else {
|
|
81
|
-
console.log('没有待确认的连接请求');
|
|
82
|
-
}
|
|
83
|
-
} catch (err) {
|
|
84
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
85
|
-
console.error(`[F2A] 无法连接到 Daemon: ${message}`);
|
|
86
|
-
console.error('[F2A] 请确保 Daemon 正在运行');
|
|
87
|
-
process.exit(1);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* 确认连接
|
|
93
|
-
*/
|
|
94
|
-
export async function confirm(idOrIndex: string | number): Promise<void> {
|
|
95
|
-
try {
|
|
96
|
-
const result = await sendControlCommand('confirm', idOrIndex);
|
|
97
|
-
|
|
98
|
-
if (result.success) {
|
|
99
|
-
console.log(`[F2A] ${result.message}`);
|
|
100
|
-
} else {
|
|
101
|
-
console.error(`[F2A] 错误: ${result.error}`);
|
|
102
|
-
process.exit(1);
|
|
103
|
-
}
|
|
104
|
-
} catch (err) {
|
|
105
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
106
|
-
console.error(`[F2A] 无法连接到 Daemon: ${message}`);
|
|
107
|
-
process.exit(1);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* 拒绝连接
|
|
113
|
-
*/
|
|
114
|
-
export async function reject(idOrIndex: string | number, reason?: string): Promise<void> {
|
|
115
|
-
try {
|
|
116
|
-
const result = await sendControlCommand('reject', idOrIndex, reason);
|
|
117
|
-
|
|
118
|
-
if (result.success) {
|
|
119
|
-
console.log(`[F2A] ${result.message}`);
|
|
120
|
-
} else {
|
|
121
|
-
console.error(`[F2A] 错误: ${result.error}`);
|
|
122
|
-
process.exit(1);
|
|
123
|
-
}
|
|
124
|
-
} catch (err) {
|
|
125
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
126
|
-
console.error(`[F2A] 无法连接到 Daemon: ${message}`);
|
|
127
|
-
process.exit(1);
|
|
128
|
-
}
|
|
129
|
-
}
|
package/src/cli/index.test.ts
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
-
|
|
3
|
-
// Mock dependencies before importing
|
|
4
|
-
const mockRequest = vi.fn();
|
|
5
|
-
const mockExistsSync = vi.fn();
|
|
6
|
-
const mockReadFileSync = vi.fn();
|
|
7
|
-
|
|
8
|
-
vi.mock('http', () => ({
|
|
9
|
-
request: (...args: any[]) => mockRequest(...args),
|
|
10
|
-
}));
|
|
11
|
-
|
|
12
|
-
vi.mock('fs', () => ({
|
|
13
|
-
existsSync: (...args: any[]) => mockExistsSync(...args),
|
|
14
|
-
readFileSync: (...args: any[]) => mockReadFileSync(...args),
|
|
15
|
-
}));
|
|
16
|
-
|
|
17
|
-
vi.mock('os', () => ({
|
|
18
|
-
homedir: vi.fn().mockReturnValue('/home/test'),
|
|
19
|
-
}));
|
|
20
|
-
|
|
21
|
-
describe('CLI Index', () => {
|
|
22
|
-
const mockReq = {
|
|
23
|
-
on: vi.fn(),
|
|
24
|
-
write: vi.fn(),
|
|
25
|
-
end: vi.fn(),
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const mockRes = {
|
|
29
|
-
statusCode: 200,
|
|
30
|
-
on: vi.fn(),
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
beforeEach(() => {
|
|
34
|
-
vi.clearAllMocks();
|
|
35
|
-
|
|
36
|
-
mockRequest.mockImplementation((options: any, callback: Function) => {
|
|
37
|
-
callback(mockRes);
|
|
38
|
-
return mockReq;
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
mockRes.on.mockImplementation((event: string, callback: Function) => {
|
|
42
|
-
if (event === 'data') callback(Buffer.from(JSON.stringify({ success: true, status: 'ok' })));
|
|
43
|
-
if (event === 'end') callback();
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
mockExistsSync.mockReturnValue(false);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
afterEach(() => {
|
|
50
|
-
vi.resetModules();
|
|
51
|
-
delete process.env.F2A_CONTROL_PORT;
|
|
52
|
-
delete process.env.F2A_CONTROL_TOKEN;
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
describe('getControlToken', () => {
|
|
56
|
-
it('should use environment variable token', async () => {
|
|
57
|
-
process.env.F2A_CONTROL_TOKEN = 'env-token';
|
|
58
|
-
|
|
59
|
-
vi.resetModules();
|
|
60
|
-
// Just verify env is set correctly
|
|
61
|
-
expect(process.env.F2A_CONTROL_TOKEN).toBe('env-token');
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it('should read token from file when env not set', async () => {
|
|
65
|
-
delete process.env.F2A_CONTROL_TOKEN;
|
|
66
|
-
|
|
67
|
-
mockExistsSync.mockReturnValue(true);
|
|
68
|
-
mockReadFileSync.mockReturnValue('file-token');
|
|
69
|
-
|
|
70
|
-
vi.resetModules();
|
|
71
|
-
// Import will trigger the token reading
|
|
72
|
-
await import('./index');
|
|
73
|
-
|
|
74
|
-
expect(mockExistsSync).toHaveBeenCalled();
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
});
|