merge-requests/6/head
Matthew 2021-08-09 14:16:36 -04:00
parent 08be688daa
commit 647e3be766
No known key found for this signature in database
GPG Key ID: 210AF32ADE3B5C4B
2 changed files with 128 additions and 134 deletions

View File

@ -1,127 +1,128 @@
/* 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 } from '.'; import { Client, Collection, Context } from '.';
import type { Handler } from '.'; import type { Handler } from '.';
export default class CSCLI { export default class CSCLI {
public client: Client; public client: Client;
public servers: { public servers: {
tcp?: net.Server, tcp?: net.Server,
unix?: net.Server, unix?: net.Server,
}; };
public handlers: Collection<Handler>; public handlers: Collection<Handler>;
#hmac: string; #hmac: string;
constructor(client: Client) { constructor(client: Client) {
this.client = client; this.client = client;
this.servers = {}; this.servers = {};
this.loadKeys(); this.loadKeys();
this.servers.tcp = net.createServer((socket) => { this.servers.tcp = net.createServer((socket) => {
socket.on('data', async (data) => { socket.on('data', async (data) => {
try { try {
await this.tcpHandle(socket, data); await this.tcpHandle(socket, data);
} catch (err) { } catch (err) {
await this.client.util.handleError(err); await this.client.util.handleError(err);
socket.destroy(); socket.destroy();
} }
}); });
}); });
this.servers.unix = net.createServer((socket) => { this.servers.unix = net.createServer((socket) => {
socket.on('data', async (data) => { socket.on('data', async (data) => {
try { try {
await this.unixHandle(socket, data); await this.unixHandle(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(handlerFiles: { [s: string]: typeof Handler; } | ArrayLike<typeof Handler>) { public load(handlerFiles: { [s: string]: typeof Handler; } | ArrayLike<typeof Handler>) {
this.handlers = new Collection(); this.handlers = new Collection();
const hdFiles = Object.values<typeof Handler>(handlerFiles); const hdFiles = Object.values<typeof Handler>(handlerFiles);
for (const Handler1 of hdFiles) { for (const Handler1 of hdFiles) {
const handler = new Handler1(); const handler = new Handler1();
this.handlers.add(handler.endpoint, handler); this.handlers.add(handler.endpoint, handler);
this.client.signale.success(`Successfully loaded endpoint '${handler.endpoint}'.`); this.client.signale.success(`Successfully loaded endpoint '${handler.endpoint}'.`);
} }
} }
public async unixHandle(socket: net.Socket, data: Buffer) { public async unixHandle(socket: net.Socket, data: Buffer) {
const args = data.toString().trim().split('$'); console.log(data.toString());
const parsed: { Username: string, Type: string, Message?: string, Data?: any, HMAC: string } = JSON.parse(args[0]); const args = data.toString().trim().split('$');
// FINISH VERIFICATION CHECKS const parsed: { Username: string, Type: string, Message?: string, Data?: any, HMAC: string } = JSON.parse(args[0]);
const handler: Handler = this.handlers.get(parsed.Type); // FINISH VERIFICATION CHECKS
if (!handler) return socket.destroy(); const handler: Handler = this.handlers.get(parsed.Type);
if (!handler) return socket.destroy();
const context = new Context(socket, args[0], this.client);
await handler.handle(context); const context = new Context(socket, args[0], this.client);
if (!context.socket.destroyed) { await handler.handle(context);
socket.destroy(); if (!context.socket.destroyed) {
} socket.destroy();
} }
}
public async tcpHandle(socket: net.Socket, data: Buffer) {
const args = data.toString().trim().split('$'); public async tcpHandle(socket: net.Socket, data: Buffer) {
const verification = this.verifyConnection(args[1], args[0]); const args = data.toString().trim().split('$');
if (!verification) { const verification = this.verifyConnection(args[1], args[0]);
socket.write('UNAUTHORIZED TO EXECUTE ON THIS SERVER\n'); if (!verification) {
return socket.destroy(); 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 parsed: { Username: string, Type: string, Message?: string, Data?: any, HMAC: string } = JSON.parse(args[0]);
const handler: Handler = this.handlers.get(parsed.Type); // FINISH VERIFICATION CHECKS
if (!handler) return socket.destroy(); const handler: Handler = this.handlers.get(parsed.Type);
if (!handler) return socket.destroy();
const context = new Context(socket, args[0], this.client);
await handler.handle(context); const context = new Context(socket, args[0], this.client);
if (!context.socket.destroyed) { await handler.handle(context);
socket.destroy(); if (!context.socket.destroyed) {
} socket.destroy();
} }
}
public verifyConnection(key: string, data: any): boolean {
const hmac = crypto.createHmac('sha256', this.#hmac); public verifyConnection(key: string, data: any): boolean {
hmac.update(data); const hmac = crypto.createHmac('sha256', this.#hmac);
const computed = hmac.digest('hex'); hmac.update(data);
if (computed === key) return true; const computed = hmac.digest('hex');
return false; if (computed === key) return true;
} return false;
}
public async loadKeys() {
const key = await fs.readFile('/etc/cscli.conf', { encoding: 'utf8' }); public async loadKeys() {
this.#hmac = key.toString().trim(); const key = await fs.readFile('/etc/cscli.conf', { encoding: 'utf8' });
} this.#hmac = key.toString().trim();
}
public async init() {
try { public async init() {
await fs.unlink('/run/csd-comm.sock'); try {
} catch { await fs.unlink('/run/csd-comm.sock');
// eslint-disable-next-line no-unused-expressions } catch {
null; // eslint-disable-next-line no-unused-expressions
} null;
this.servers.tcp.on('error', (err) => { }
this.client.util.handleError(err); this.servers.tcp.on('error', (err) => {
}); this.client.util.handleError(err);
this.servers.unix.on('error', (err) => { });
this.client.util.handleError(err); this.servers.unix.on('error', (err) => {
}); this.client.util.handleError(err);
this.servers.tcp.listen(8124, () => { });
this.client.signale.success('[CSD-COMM] Listen - TCP:8124'); this.servers.tcp.listen(8124, () => {
}); this.client.signale.success('[CSD-COMM] Listen - TCP:8124');
this.servers.unix.listen('/var/run/csd-comm.sock', async () => { });
await fs.chmod('/run/csd-comm.sock', 0o770); this.servers.unix.listen('/var/run/csd-comm.sock', async () => {
await fs.chown('/run/csd-comm.sock', 0, 115); await fs.chmod('/run/csd-comm.sock', 0o770);
this.client.signale.success('[CSD-COMM] Listen - UNIX:/run/csd-comm.sock'); await fs.chown('/run/csd-comm.sock', 0, 115);
}); this.client.signale.success('[CSD-COMM] Listen - UNIX:/run/csd-comm.sock');
} });
} }
}

View File

@ -59,13 +59,6 @@ func main() {
fmt.Printf("Connected to Redis [GO]\n") fmt.Printf("Connected to Redis [GO]\n")
HandleError(err, 1) HandleError(err, 1)
/*for {
fmt.Printf("Calling handler func [GO]\n")
if status == false {
handler(*config)
time.Sleep(1000000 * time.Millisecond)
}
}*/
handler(*config) handler(*config)
} }