- server: Node.js TCP中继服务器,支持多节点集群 - web: React管理面板(仪表盘、房间管理、节点管理) - client: Electron桌面客户端(连接、创建/加入房间、本地代理) - deploy: Ubuntu一键部署脚本
78 lines
1.8 KiB
TypeScript
78 lines
1.8 KiB
TypeScript
import * as net from 'net';
|
|
import { RelayClient } from './relay-client';
|
|
|
|
export class LocalProxy {
|
|
private server: net.Server | null = null;
|
|
private connections: Set<net.Socket> = new Set();
|
|
|
|
constructor(
|
|
private port: number,
|
|
private relayClient: RelayClient
|
|
) {}
|
|
|
|
start(): Promise<void> {
|
|
return new Promise((resolve, reject) => {
|
|
this.server = net.createServer((clientSocket) => {
|
|
this.connections.add(clientSocket);
|
|
|
|
// Connect to relay for this MC client
|
|
const relaySocket = this.relayClient.getSocket();
|
|
if (!relaySocket || !this.relayClient.isConnected()) {
|
|
clientSocket.destroy();
|
|
return;
|
|
}
|
|
|
|
// Forward MC client data to relay
|
|
clientSocket.on('data', (data) => {
|
|
if (this.relayClient.isConnected()) {
|
|
this.relayClient.write(data);
|
|
}
|
|
});
|
|
|
|
// Forward relay data back to MC client
|
|
this.relayClient.onData((data) => {
|
|
if (!clientSocket.destroyed) {
|
|
clientSocket.write(data);
|
|
}
|
|
});
|
|
|
|
clientSocket.on('close', () => {
|
|
this.connections.delete(clientSocket);
|
|
});
|
|
|
|
clientSocket.on('error', () => {
|
|
this.connections.delete(clientSocket);
|
|
});
|
|
});
|
|
|
|
this.server.on('error', (err) => {
|
|
reject(err);
|
|
});
|
|
|
|
this.server.listen(this.port, '127.0.0.1', () => {
|
|
console.log(`[LocalProxy] Listening on 127.0.0.1:${this.port}`);
|
|
resolve();
|
|
});
|
|
});
|
|
}
|
|
|
|
stop(): void {
|
|
for (const conn of this.connections) {
|
|
conn.destroy();
|
|
}
|
|
this.connections.clear();
|
|
if (this.server) {
|
|
this.server.close();
|
|
this.server = null;
|
|
}
|
|
}
|
|
|
|
getPort(): number {
|
|
return this.port;
|
|
}
|
|
|
|
getConnectionCount(): number {
|
|
return this.connections.size;
|
|
}
|
|
}
|