add Unix domain socket listener
parent
11934dac8e
commit
f3ccfe11cd
|
@ -5,24 +5,27 @@ 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 { TCPHandler } from '.';
|
import type { Handler } from '.';
|
||||||
|
|
||||||
export default class CSCLI {
|
export default class CSCLI {
|
||||||
public client: Client;
|
public client: Client;
|
||||||
|
|
||||||
public server: net.Server;
|
public servers: {
|
||||||
|
tcp: net.Server,
|
||||||
|
unix: net.Server,
|
||||||
|
};
|
||||||
|
|
||||||
public handlers: Collection<TCPHandler>;
|
public handlers: Collection<Handler>;
|
||||||
|
|
||||||
#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.servers.tcp = net.createServer((socket) => {
|
||||||
socket.on('data', async (data) => {
|
socket.on('data', async (data) => {
|
||||||
try {
|
try {
|
||||||
await this.handle(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();
|
||||||
|
@ -32,17 +35,31 @@ export default class CSCLI {
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
public load(handlerFiles: { [s: string]: typeof TCPHandler; } | ArrayLike<typeof TCPHandler>) {
|
public load(handlerFiles: { [s: string]: typeof Handler; } | ArrayLike<typeof Handler>) {
|
||||||
this.handlers = new Collection();
|
this.handlers = new Collection();
|
||||||
const hdFiles = Object.values<typeof TCPHandler>(handlerFiles);
|
const hdFiles = Object.values<typeof Handler>(handlerFiles);
|
||||||
for (const Handler of hdFiles) {
|
for (const Handler1 of hdFiles) {
|
||||||
const handler = new Handler();
|
const handler = new Handler1();
|
||||||
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 endpoint '${handler.endpoint}'.`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async handle(socket: net.Socket, data: Buffer) {
|
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 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) {
|
||||||
|
@ -51,7 +68,7 @@ export default class CSCLI {
|
||||||
}
|
}
|
||||||
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: Handler = 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);
|
||||||
|
@ -74,12 +91,24 @@ export default class CSCLI {
|
||||||
this.#hmac = key.toString().trim();
|
this.#hmac = key.toString().trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
public init() {
|
public async init() {
|
||||||
this.server.on('error', (err) => {
|
try {
|
||||||
|
await fs.unlink('/var/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.client.util.handleError(err);
|
||||||
});
|
});
|
||||||
this.server.listen(8124, () => {
|
this.servers.unix.on('error', (err) => {
|
||||||
this.client.signale.success('TCP socket is now listening for connections.');
|
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', () => {
|
||||||
|
this.client.signale.success('[CSD-COMM] Listen - UNIX:/var/run/csd-comm.sock');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { Context } from '.';
|
import { Context } from '.';
|
||||||
|
|
||||||
export default class TCPHandler {
|
export default class Handler {
|
||||||
public endpoint: string;
|
public endpoint: string;
|
||||||
|
|
||||||
constructor(endpoint?: string) {
|
constructor(endpoint?: string) {
|
|
@ -5,11 +5,11 @@ 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 CSCLI } from './CSCLI';
|
||||||
export { default as Event } from './Event';
|
export { default as Event } from './Event';
|
||||||
|
export { default as Handler } from './Handler';
|
||||||
export { default as LocalStorage } from './LocalStorage';
|
export { default as LocalStorage } from './LocalStorage';
|
||||||
export { default as Report } from './Report';
|
export { default as Report } from './Report';
|
||||||
export { default as RichEmbed } from './RichEmbed';
|
export { default as RichEmbed } from './RichEmbed';
|
||||||
export { default as Route } from './Route';
|
export { default as Route } from './Route';
|
||||||
export { default as Security } from './Security';
|
export { default as Security } from './Security';
|
||||||
export { default as Server } from './Server';
|
export { default as Server } from './Server';
|
||||||
export { default as TCPHandler } from './TCPHandler';
|
|
||||||
export { default as Util } from './Util';
|
export { default as Util } from './Util';
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
import { Handler, Context } from '../class';
|
||||||
|
|
||||||
|
export default class Domains extends Handler {
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
this.endpoint = 'domains';
|
||||||
|
}
|
||||||
|
|
||||||
|
public async handle(ctx: Context) {
|
||||||
|
const domains = await ctx.client.db.Domain.find({ 'account.username': ctx.data.username }).lean().exec();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
import { TCPHandler, Context } from '../class';
|
import { Handler, Context } from '../class';
|
||||||
|
|
||||||
export default class KillPID extends TCPHandler {
|
export default class KillPID extends Handler {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.endpoint = 'killpid';
|
this.endpoint = 'killpid';
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { TCPHandler, Context } from '../class';
|
import { Handler, Context } from '../class';
|
||||||
|
|
||||||
export default class Lock extends TCPHandler {
|
export default class Lock extends Handler {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.endpoint = 'lock';
|
this.endpoint = 'lock';
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { TCPHandler, Context } from '../class';
|
import { Handler, Context } from '../class';
|
||||||
|
|
||||||
export default class ProcessCount extends TCPHandler {
|
export default class ProcessCount extends Handler {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.endpoint = 'processcount';
|
this.endpoint = 'processcount';
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { TCPHandler, Context } from '../class';
|
import { Handler, Context } from '../class';
|
||||||
import { dataConversion } from '../functions';
|
import { dataConversion } from '../functions';
|
||||||
|
|
||||||
export default class RAM extends TCPHandler {
|
export default class RAM extends Handler {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.endpoint = 'ram';
|
this.endpoint = 'ram';
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { TCPHandler, Context } from '../class';
|
import { Handler, Context } from '../class';
|
||||||
|
|
||||||
export default class RAMLimits extends TCPHandler {
|
export default class RAMLimits extends Handler {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.endpoint = 'ramlimit';
|
this.endpoint = 'ramlimit';
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { TCPHandler, Context, Report } from '../class';
|
import { Handler, Context, Report } from '../class';
|
||||||
|
|
||||||
export default class Score extends TCPHandler {
|
export default class Score extends Handler {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.endpoint = 'score';
|
this.endpoint = 'score';
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { TCPHandler, Context } from '../class';
|
import { Handler, Context } from '../class';
|
||||||
|
|
||||||
export default class SSHLogins extends TCPHandler {
|
export default class SSHLogins extends Handler {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.endpoint = 'sshlogins';
|
this.endpoint = 'sshlogins';
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { TCPHandler, Context } from '../class';
|
import { Handler, Context } from '../class';
|
||||||
import { dataConversion } from '../functions';
|
import { dataConversion } from '../functions';
|
||||||
|
|
||||||
export default class Storage extends TCPHandler {
|
export default class Storage extends Handler {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.endpoint = 'storage';
|
this.endpoint = 'storage';
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { TCPHandler, Context } from '../class';
|
import { Handler, Context } from '../class';
|
||||||
|
|
||||||
export default class UserInfo extends TCPHandler {
|
export default class UserInfo extends Handler {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.endpoint = 'userinfo';
|
this.endpoint = 'userinfo';
|
||||||
|
|
Loading…
Reference in New Issue