feat: FunConnect v1.0.0 - Minecraft联机平台完整版
- server: Node.js TCP中继服务器,支持多节点集群 - web: React管理面板(仪表盘、房间管理、节点管理) - client: Electron桌面客户端(连接、创建/加入房间、本地代理) - deploy: Ubuntu一键部署脚本
This commit is contained in:
77
client/src/main/local-proxy.ts
Normal file
77
client/src/main/local-proxy.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user