1
0
Fork 0
cloudservices/src/cscli/main.ts

129 lines
3.5 KiB
TypeScript

/* eslint-disable max-classes-per-file */
/* eslint-disable no-case-declarations */
/* eslint-disable consistent-return */
import net from 'net';
import crypto from 'crypto';
import { promises as fs } from 'fs';
import { Client, Collection } from '../class';
import * as handlerFiles from './handlers';
export class TCPHandler {
public endpoint: string;
constructor(endpoint?: string) {
this.endpoint = endpoint;
}
public handle(ctx: Context): Promise<any> { return Promise.resolve(); }
}
export class Context {
public socket: net.Socket;
public client: Client;
public data: {
username: string,
endpoint: string,
message?: string,
additionalData?: object,
HMAC: string,
}
constructor(socket: net.Socket, data: string, client: Client) {
const parsed: { Username: string, Type: string, Message?: string, Data?: object, HMAC: string } = JSON.parse(data);
this.socket = socket;
this.client = client;
this.data = {
username: parsed.Username,
endpoint: parsed.Type,
message: parsed.Message,
additionalData: parsed.Data,
HMAC: parsed.HMAC,
};
}
public send(v: string) {
this.socket.write(`${v.toString()}\n`);
this.socket.destroy();
}
}
export default class CSCLI {
public client: Client;
public server: net.Server;
public handlers: Collection<TCPHandler>;
#hmac: string;
constructor(client: Client) {
this.client = client;
this.loadKeys();
this.server = net.createServer((socket) => {
socket.on('data', async (data) => {
try {
await this.handle(socket, data);
} catch (err) {
await this.client.util.handleError(err);
socket.destroy();
}
});
});
this.init();
}
public load() {
const hdFiles = Object.values<typeof TCPHandler>(handlerFiles);
for (const Handler of hdFiles) {
const handler = new Handler();
this.handlers.add(handler.endpoint, handler);
this.client.signale.success(`Successfully loaded TCP endpoint '${handler.endpoint}'.`);
}
}
public async handle(socket: net.Socket, data: Buffer) {
const args = data.toString().trim().split('$');
const verification = this.verifyConnection(args[1], args[0]);
if (!verification) {
socket.write('UNAUTHORIZED TO EXECUTE ON THIS SERVER\n');
return socket.destroy();
}
const parsed: { Username: string, Type: string, Message?: string, Data?: any, HMAC: string } = JSON.parse(args[0]);
// FINISH VERIFICATION CHECKS
const handler: TCPHandler = this.handlers.get(parsed.Type);
if (!handler) return socket.destroy();
const context = new Context(socket, args[0], this.client);
await handler.handle(context);
if (!context.socket.destroyed) {
socket.destroy();
}
}
public verifyConnection(key: string, data: any): boolean {
const hmac = crypto.createHmac('sha256', this.#hmac);
hmac.update(data);
const computed = hmac.digest('hex');
if (computed === key) return true;
return false;
}
public async loadKeys() {
const key = await fs.readFile('/etc/cscli.conf', { encoding: 'utf8' });
this.#hmac = key.toString().trim();
}
public init() {
this.server.on('error', (err) => {
this.client.util.handleError(err);
});
this.server.listen(8124, () => {
this.client.signale.success('TCP socket is now listening for connections.');
});
}
}