@feng3d/ctc 0.0.6
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 +195 -0
- package/dist/admin-server.d.ts +90 -0
- package/dist/admin-server.d.ts.map +1 -0
- package/dist/admin-server.js +680 -0
- package/dist/admin-server.js.map +1 -0
- package/dist/cli.d.ts +9 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +433 -0
- package/dist/cli.js.map +1 -0
- package/dist/config.d.ts +62 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +209 -0
- package/dist/config.js.map +1 -0
- package/dist/controller.d.ts +173 -0
- package/dist/controller.d.ts.map +1 -0
- package/dist/controller.js +340 -0
- package/dist/controller.js.map +1 -0
- package/dist/handlers/unified-handler.d.ts +110 -0
- package/dist/handlers/unified-handler.d.ts.map +1 -0
- package/dist/handlers/unified-handler.js +373 -0
- package/dist/handlers/unified-handler.js.map +1 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +154 -0
- package/dist/index.js.map +1 -0
- package/dist/proxy-manager.d.ts +75 -0
- package/dist/proxy-manager.d.ts.map +1 -0
- package/dist/proxy-manager.js +124 -0
- package/dist/proxy-manager.js.map +1 -0
- package/package.json +39 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module proxy-manager
|
|
3
|
+
*
|
|
4
|
+
* 代理管理器模块。
|
|
5
|
+
*
|
|
6
|
+
* 负责管理所有代理隧道的生命周期,包括向服务器注册/注销代理、
|
|
7
|
+
* 创建统一的处理器(同时支持 HTTP 和 WebSocket)、
|
|
8
|
+
* 以及在连接断开时清理所有处理器。
|
|
9
|
+
*/
|
|
10
|
+
import { logger } from '@feng3d/chuantou-shared';
|
|
11
|
+
import { UnifiedHandler } from './handlers/unified-handler.js';
|
|
12
|
+
import { MessageType, createMessage } from '@feng3d/chuantou-shared';
|
|
13
|
+
/**
|
|
14
|
+
* 代理管理器类,负责管理所有代理隧道的注册、注销和生命周期。
|
|
15
|
+
*
|
|
16
|
+
* 创建统一的处理器实例,每个端口同时支持 HTTP 和 WebSocket 协议。
|
|
17
|
+
* 当控制器连接断开时,自动清理所有处理器。
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* const proxyManager = new ProxyManager(controller);
|
|
22
|
+
* await proxyManager.registerProxy({ remotePort: 8080, localPort: 3000 });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export class ProxyManager {
|
|
26
|
+
/**
|
|
27
|
+
* 创建代理管理器实例。
|
|
28
|
+
*
|
|
29
|
+
* @param controller - 控制器实例,用于与服务器进行通信
|
|
30
|
+
*/
|
|
31
|
+
constructor(controller) {
|
|
32
|
+
this.controller = controller;
|
|
33
|
+
this.handlers = new Map();
|
|
34
|
+
// 监听控制器事件
|
|
35
|
+
this.controller.on('disconnected', () => {
|
|
36
|
+
this.onDisconnected();
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* 向服务器注册一个代理隧道。
|
|
41
|
+
*
|
|
42
|
+
* 发送注册消息到服务器,注册成功后创建统一的处理器
|
|
43
|
+
* ({@link UnifiedHandler})并存储到处理器映射表中。
|
|
44
|
+
* 每个端口同时支持 HTTP 和 WebSocket 协议。
|
|
45
|
+
*
|
|
46
|
+
* @param config - 代理配置对象,包含远程端口、本地端口等信息
|
|
47
|
+
* @throws {Error} 注册失败时抛出错误,包含服务器返回的错误信息
|
|
48
|
+
*/
|
|
49
|
+
async registerProxy(config) {
|
|
50
|
+
logger.log(`正在注册代理: :${config.remotePort} -> ${config.localHost || 'localhost'}:${config.localPort}`);
|
|
51
|
+
// 发送注册消息
|
|
52
|
+
const registerMsg = createMessage(MessageType.REGISTER, {
|
|
53
|
+
remotePort: config.remotePort,
|
|
54
|
+
localPort: config.localPort,
|
|
55
|
+
localHost: config.localHost,
|
|
56
|
+
});
|
|
57
|
+
const response = await this.controller.sendRequest(registerMsg);
|
|
58
|
+
if (!response.payload.success) {
|
|
59
|
+
throw new Error(`注册代理失败: ${response.payload.error}`);
|
|
60
|
+
}
|
|
61
|
+
logger.log(`代理已注册: ${response.payload.remoteUrl}`);
|
|
62
|
+
// 创建统一的处理器(同时支持 HTTP 和 WebSocket)
|
|
63
|
+
const handler = new UnifiedHandler(this.controller, config);
|
|
64
|
+
// 设置处理器事件
|
|
65
|
+
handler.on('error', (error) => {
|
|
66
|
+
logger.error(`端口 ${config.remotePort} 的处理器错误:`, error);
|
|
67
|
+
});
|
|
68
|
+
this.handlers.set(config.remotePort, handler);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* 注销指定远程端口的代理隧道。
|
|
72
|
+
*
|
|
73
|
+
* 销毁对应的处理器,并向服务器发送注销请求。
|
|
74
|
+
*
|
|
75
|
+
* @param remotePort - 要注销的代理对应的远程端口号
|
|
76
|
+
*/
|
|
77
|
+
async unregisterProxy(remotePort) {
|
|
78
|
+
const handler = this.handlers.get(remotePort);
|
|
79
|
+
if (handler) {
|
|
80
|
+
handler.destroy();
|
|
81
|
+
this.handlers.delete(remotePort);
|
|
82
|
+
}
|
|
83
|
+
const unregisterMsg = createMessage(MessageType.UNREGISTER, {
|
|
84
|
+
remotePort,
|
|
85
|
+
});
|
|
86
|
+
await this.controller.sendRequest(unregisterMsg);
|
|
87
|
+
logger.log(`代理已注销: 端口 ${remotePort}`);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* 注销所有已注册的代理隧道。
|
|
91
|
+
*
|
|
92
|
+
* 并行注销所有代理,等待全部注销完成后返回。
|
|
93
|
+
*
|
|
94
|
+
* @returns 所有代理注销完成后解析的 Promise
|
|
95
|
+
*/
|
|
96
|
+
async unregisterAll() {
|
|
97
|
+
const unregisterPromises = [];
|
|
98
|
+
for (const port of this.handlers.keys()) {
|
|
99
|
+
unregisterPromises.push(this.unregisterProxy(port));
|
|
100
|
+
}
|
|
101
|
+
await Promise.all(unregisterPromises);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* 处理控制器连接断开事件。
|
|
105
|
+
*
|
|
106
|
+
* 销毁所有处理器并清空处理器映射表。
|
|
107
|
+
*/
|
|
108
|
+
onDisconnected() {
|
|
109
|
+
logger.log('连接丢失,正在停止所有处理器...');
|
|
110
|
+
for (const handler of this.handlers.values()) {
|
|
111
|
+
handler.destroy();
|
|
112
|
+
}
|
|
113
|
+
this.handlers.clear();
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* 销毁代理管理器,注销所有代理并释放资源。
|
|
117
|
+
*
|
|
118
|
+
* @returns 销毁完成后解析的 Promise
|
|
119
|
+
*/
|
|
120
|
+
async destroy() {
|
|
121
|
+
await this.unregisterAll();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=proxy-manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proxy-manager.js","sourceRoot":"","sources":["../src/proxy-manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,EAAe,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAC/D,OAAO,EAAE,WAAW,EAAE,aAAa,EAA2D,MAAM,yBAAyB,CAAC;AAE9H;;;;;;;;;;;GAWG;AACH,MAAM,OAAO,YAAY;IAOvB;;;;OAIG;IACH,YAAY,UAAsB;QAChC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,QAAQ,GAAG,IAAI,GAAG,EAAE,CAAC;QAE1B,UAAU;QACV,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC,cAAc,EAAE,GAAG,EAAE;YACtC,IAAI,CAAC,cAAc,EAAE,CAAC;QACxB,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;;;;;;OASG;IACH,KAAK,CAAC,aAAa,CAAC,M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"feng3d-ctc": "./dist/cli.js"
|
|
9
|
+
},
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc",
|
|
21
|
+
"watch": "tsc --watch",
|
|
22
|
+
"test": "vitest run",
|
|
23
|
+
"test:watch": "vitest watch"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@feng3d/chuantou-shared": "^0.0.6",
|
|
27
|
+
"chalk": "^5.6.2",
|
|
28
|
+
"commander": "^14.0.3",
|
|
29
|
+
"uuid": "^9.0.1",
|
|
30
|
+
"ws": "^8.16.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/node": "^20.10.0",
|
|
34
|
+
"@types/uuid": "^9.0.7",
|
|
35
|
+
"@types/ws": "^8.5.10",
|
|
36
|
+
"typescript": "^5.3.3",
|
|
37
|
+
"vitest": "^2.1.9"
|
|
38
|
+
}
|
|
39
|
+
}
|