cvmts: rip out raw tcp transport
it's really just a tech debt that nothing uses, these is pretty much no point keeping it
This commit is contained in:
@@ -16,11 +16,6 @@ export default interface IConfig {
|
||||
accountID: string;
|
||||
licenseKey: string;
|
||||
};
|
||||
tcp: {
|
||||
enabled: boolean;
|
||||
host: string;
|
||||
port: number;
|
||||
};
|
||||
auth: {
|
||||
enabled: boolean;
|
||||
apiEndpoint: string;
|
||||
|
||||
@@ -8,7 +8,6 @@ import { QemuVmDefinition } from '@computernewb/superqemu';
|
||||
import AuthManager from './AuthManager.js';
|
||||
import WSServer from './net/ws/WSServer.js';
|
||||
import { User } from './User.js';
|
||||
import TCPServer from './net/tcp/TCPServer.js';
|
||||
import VM from './vm/interface.js';
|
||||
import VNCVM from './vm/vnc/VNCVM.js';
|
||||
import GeoIPDownloader from './GeoIPDownloader.js';
|
||||
@@ -111,11 +110,5 @@ async function start() {
|
||||
var WS = new WSServer(Config, banmgr);
|
||||
WS.on('connect', (client: User) => CVM.connectionOpened(client));
|
||||
WS.start();
|
||||
|
||||
if (Config.tcp.enabled) {
|
||||
var TCP = new TCPServer(Config, banmgr);
|
||||
TCP.on('connect', (client: User) => CVM.connectionOpened(client));
|
||||
TCP.start();
|
||||
}
|
||||
}
|
||||
start();
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
import EventEmitter from 'events';
|
||||
import { NetworkClient } from '../NetworkClient.js';
|
||||
import { Socket } from 'net';
|
||||
|
||||
const TextHeader = 0;
|
||||
const BinaryHeader = 1;
|
||||
|
||||
export default class TCPClient extends EventEmitter implements NetworkClient {
|
||||
private socket: Socket;
|
||||
private cache: string;
|
||||
|
||||
constructor(socket: Socket) {
|
||||
super();
|
||||
this.socket = socket;
|
||||
this.cache = '';
|
||||
this.socket.on('end', () => {
|
||||
this.emit('disconnect');
|
||||
});
|
||||
this.socket.on('data', (data) => {
|
||||
var msg = data.toString('utf-8');
|
||||
if (msg[msg.length - 1] === '\n') msg = msg.slice(0, -1);
|
||||
this.cache += msg;
|
||||
this.readCache();
|
||||
});
|
||||
}
|
||||
|
||||
private readCache() {
|
||||
for (var index = this.cache.indexOf(';'); index !== -1; index = this.cache.indexOf(';')) {
|
||||
this.emit('msg', this.cache.slice(0, index + 1));
|
||||
this.cache = this.cache.slice(index + 1);
|
||||
}
|
||||
}
|
||||
|
||||
getIP(): string {
|
||||
return this.socket.remoteAddress!;
|
||||
}
|
||||
|
||||
send(msg: string): Promise<void> {
|
||||
return new Promise((res, rej) => {
|
||||
let _msg = new Uint32Array([TextHeader, ...Buffer.from(msg, 'utf-8')]);
|
||||
this.socket.write(Buffer.from(_msg), (err) => {
|
||||
if (err) {
|
||||
rej(err);
|
||||
return;
|
||||
}
|
||||
res();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
sendBinary(msg: Uint8Array): Promise<void> {
|
||||
return new Promise((res, rej) => {
|
||||
let _msg = new Uint32Array([BinaryHeader, msg.length, ...msg]);
|
||||
this.socket.write(Buffer.from(_msg), (err) => {
|
||||
if (err) {
|
||||
rej(err);
|
||||
return;
|
||||
}
|
||||
res();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
close(): void {
|
||||
this.emit('disconnect');
|
||||
this.socket.end();
|
||||
}
|
||||
|
||||
isOpen(): boolean {
|
||||
return this.socket.writable;
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
// TODO: replace tcp protocol with smth like
|
||||
// struct msg { beu32 len; char data[len] }
|
||||
// (along with a length cap obviously)
|
||||
import EventEmitter from 'events';
|
||||
import { NetworkServer } from '../NetworkServer.js';
|
||||
import { Server, Socket } from 'net';
|
||||
import IConfig from '../../IConfig.js';
|
||||
import TCPClient from './TCPClient.js';
|
||||
import { IPDataManager } from '../../IPData.js';
|
||||
import { User } from '../../User.js';
|
||||
import pino from 'pino';
|
||||
import { BanManager } from '../../BanManager.js';
|
||||
|
||||
export default class TCPServer extends EventEmitter implements NetworkServer {
|
||||
listener: Server;
|
||||
Config: IConfig;
|
||||
logger = pino({ name: 'CVMTS.TCPServer' });
|
||||
clients: TCPClient[];
|
||||
private banmgr: BanManager;
|
||||
|
||||
constructor(config: IConfig, banmgr: BanManager) {
|
||||
super();
|
||||
this.Config = config;
|
||||
this.listener = new Server();
|
||||
this.clients = [];
|
||||
this.listener.on('connection', (socket) => this.onConnection(socket));
|
||||
this.banmgr = banmgr;
|
||||
}
|
||||
|
||||
private async onConnection(socket: Socket) {
|
||||
this.logger.info(`New TCP connection from ${socket.remoteAddress}`);
|
||||
if (await this.banmgr.isIPBanned(socket.remoteAddress!)) {
|
||||
socket.write('6.banned;');
|
||||
socket.destroy();
|
||||
return;
|
||||
}
|
||||
var client = new TCPClient(socket);
|
||||
this.clients.push(client);
|
||||
this.emit('connect', new User(client, IPDataManager.GetIPData(client.getIP()), this.Config));
|
||||
}
|
||||
|
||||
start(): void {
|
||||
this.listener.listen(this.Config.tcp.port, this.Config.tcp.host, () => {
|
||||
this.logger.info(`TCP server listening on ${this.Config.tcp.host}:${this.Config.tcp.port}`);
|
||||
});
|
||||
}
|
||||
stop(): void {
|
||||
this.listener.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user