1
0
Fork 0
master
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 no-case-declarations */
/* eslint-disable consistent-return */
import net from 'net';
import crypto from 'crypto';
import { promises as fs } from 'fs';
import { Client, Collection, Context } from '.';
import type { Handler } from '.';
export default class CSCLI {
public client: Client;
public servers: {
tcp?: net.Server,
unix?: net.Server,
};
public handlers: Collection<Handler>;
#hmac: string;
constructor(client: Client) {
this.client = client;
this.servers = {};
this.loadKeys();
this.servers.tcp = net.createServer((socket) => {
socket.on('data', async (data) => {
try {
await this.tcpHandle(socket, data);
} catch (err) {
await this.client.util.handleError(err);
socket.destroy();
}
});
});
this.servers.unix = net.createServer((socket) => {
socket.on('data', async (data) => {
try {
await this.unixHandle(socket, data);
} catch (err) {
await this.client.util.handleError(err);
socket.destroy();
}
});
});
this.init();
}
public load(handlerFiles: { [s: string]: typeof Handler; } | ArrayLike<typeof Handler>) {
this.handlers = new Collection();
const hdFiles = Object.values<typeof Handler>(handlerFiles);
for (const Handler1 of hdFiles) {
const handler = new Handler1();
this.handlers.add(handler.endpoint, handler);
this.client.signale.success(`Successfully loaded endpoint '${handler.endpoint}'.`);
}
}
public async unixHandle(socket: net.Socket, data: Buffer) {
const args = data.toString().trim().split('$');
const parsed: { Username: string, Type: string, Message?: string, Data?: any, HMAC: string } = JSON.parse(args[0]);
// FINISH VERIFICATION CHECKS
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);
if (!context.socket.destroyed) {
socket.destroy();
}
}
public async tcpHandle(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: Handler = 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 async init() {
try {
await fs.unlink('/run/csd-comm.sock');
} catch {
// eslint-disable-next-line no-unused-expressions
null;
}
this.servers.tcp.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.unix.listen('/var/run/csd-comm.sock', async () => {
await fs.chmod('/run/csd-comm.sock', 0o770);
await fs.chown('/run/csd-comm.sock', 0, 115);
this.client.signale.success('[CSD-COMM] Listen - UNIX:/run/csd-comm.sock');
});
}
}
/* 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, Context } from '.';
import type { Handler } from '.';
export default class CSCLI {
public client: Client;
public servers: {
tcp?: net.Server,
unix?: net.Server,
};
public handlers: Collection<Handler>;
#hmac: string;
constructor(client: Client) {
this.client = client;
this.servers = {};
this.loadKeys();
this.servers.tcp = net.createServer((socket) => {
socket.on('data', async (data) => {
try {
await this.tcpHandle(socket, data);
} catch (err) {
await this.client.util.handleError(err);
socket.destroy();
}
});
});
this.servers.unix = net.createServer((socket) => {
socket.on('data', async (data) => {
try {
await this.unixHandle(socket, data);
} catch (err) {
await this.client.util.handleError(err);
socket.destroy();
}
});
});
this.init();
}
public load(handlerFiles: { [s: string]: typeof Handler; } | ArrayLike<typeof Handler>) {
this.handlers = new Collection();
const hdFiles = Object.values<typeof Handler>(handlerFiles);
for (const Handler1 of hdFiles) {
const handler = new Handler1();
this.handlers.add(handler.endpoint, handler);
this.client.signale.success(`Successfully loaded endpoint '${handler.endpoint}'.`);
}
}
public async unixHandle(socket: net.Socket, data: Buffer) {
console.log(data.toString());
const args = data.toString().trim().split('$');
const parsed: { Username: string, Type: string, Message?: string, Data?: any, HMAC: string } = JSON.parse(args[0]);
// FINISH VERIFICATION CHECKS
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);
if (!context.socket.destroyed) {
socket.destroy();
}
}
public async tcpHandle(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: Handler = 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 async init() {
try {
await fs.unlink('/run/csd-comm.sock');
} catch {
// eslint-disable-next-line no-unused-expressions
null;
}
this.servers.tcp.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.unix.listen('/var/run/csd-comm.sock', async () => {
await fs.chmod('/run/csd-comm.sock', 0o770);
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")
HandleError(err, 1)
/*for {
fmt.Printf("Calling handler func [GO]\n")
if status == false {
handler(*config)
time.Sleep(1000000 * time.Millisecond)
}
}*/
handler(*config)
}