1
0
Fork 0

more fixes

refactor/models
Matthew 2020-12-18 19:04:09 -05:00
parent b987a09d21
commit dee85f87f8
No known key found for this signature in database
GPG Key ID: 210AF32ADE3B5C4B
4 changed files with 87 additions and 88 deletions

View File

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

View File

@ -4,10 +4,9 @@ import mongoose from 'mongoose';
import signale from 'signale'; import signale from 'signale';
import fs from 'fs-extra'; import fs from 'fs-extra';
import config from '../config.json'; import config from '../config.json';
import CSCLI from '../cscli/main';
import { Account, AccountInterface, Moderation, ModerationInterface, Domain, DomainInterface, Tier, TierInterface } from '../models'; import { Account, AccountInterface, Moderation, ModerationInterface, Domain, DomainInterface, Tier, TierInterface } from '../models';
import { emojis } from '../stores'; import { emojis } from '../stores';
import { Command, Util, Collection, Server, Event } from '.'; import { Command, CSCLI, Util, Collection, Server, Event } from '.';
export default class Client extends Eris.Client { export default class Client extends Eris.Client {

View File

@ -3,6 +3,7 @@ export { default as Client } from './Client';
export { default as Collection } from './Collection'; export { default as Collection } from './Collection';
export { default as Command } from './Command'; export { default as Command } from './Command';
export { default as Context } from './Context'; export { default as Context } from './Context';
export { default as CSCLI } from './CSCLI';
export { default as Event } from './Event'; export { default as Event } from './Event';
export { default as LocalStorage } from './LocalStorage'; export { default as LocalStorage } from './LocalStorage';
export { default as Report } from './Report'; export { default as Report } from './Report';

View File

@ -1 +0,0 @@
export { default as CSCLI } from './main';