add Unix domain socket listener
parent
11934dac8e
commit
f3ccfe11cd
|
@ -5,24 +5,27 @@ import net from 'net';
|
|||
import crypto from 'crypto';
|
||||
import { promises as fs } from 'fs';
|
||||
import { Client, Collection, Context } from '.';
|
||||
import type { TCPHandler } from '.';
|
||||
import type { Handler } from '.';
|
||||
|
||||
export default class CSCLI {
|
||||
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;
|
||||
|
||||
constructor(client: Client) {
|
||||
this.client = client;
|
||||
this.loadKeys();
|
||||
this.server = net.createServer((socket) => {
|
||||
this.servers.tcp = net.createServer((socket) => {
|
||||
socket.on('data', async (data) => {
|
||||
try {
|
||||
await this.handle(socket, data);
|
||||
await this.tcpHandle(socket, data);
|
||||
} catch (err) {
|
||||
await this.client.util.handleError(err);
|
||||
socket.destroy();
|
||||
|
@ -32,17 +35,31 @@ export default class CSCLI {
|
|||
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();
|
||||
const hdFiles = Object.values<typeof TCPHandler>(handlerFiles);
|
||||
for (const Handler of hdFiles) {
|
||||
const handler = new Handler();
|
||||
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 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 verification = this.verifyConnection(args[1], args[0]);
|
||||
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]);
|
||||
// FINISH VERIFICATION CHECKS
|
||||
const handler: TCPHandler = this.handlers.get(parsed.Type);
|
||||
const handler: Handler = this.handlers.get(parsed.Type);
|
||||
if (!handler) return socket.destroy();
|
||||
|
||||
const context = new Context(socket, args[0], this.client);
|
||||
|
@ -74,12 +91,24 @@ export default class CSCLI {
|
|||
this.#hmac = key.toString().trim();
|
||||
}
|
||||
|
||||
public init() {
|
||||
this.server.on('error', (err) => {
|
||||
public async init() {
|
||||
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.server.listen(8124, () => {
|
||||
this.client.signale.success('TCP socket is now listening for connections.');
|
||||
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', () => {
|
||||
this.client.signale.success('[CSD-COMM] Listen - UNIX:/var/run/csd-comm.sock');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { Context } from '.';
|
||||
|
||||
export default class TCPHandler {
|
||||
export default class Handler {
|
||||
public endpoint: string;
|
||||
|
||||
constructor(endpoint?: string) {
|
|
@ -5,11 +5,11 @@ export { default as Command } from './Command';
|
|||
export { default as Context } from './Context';
|
||||
export { default as CSCLI } from './CSCLI';
|
||||
export { default as Event } from './Event';
|
||||
export { default as Handler } from './Handler';
|
||||
export { default as LocalStorage } from './LocalStorage';
|
||||
export { default as Report } from './Report';
|
||||
export { default as RichEmbed } from './RichEmbed';
|
||||
export { default as Route } from './Route';
|
||||
export { default as Security } from './Security';
|
||||
export { default as Server } from './Server';
|
||||
export { default as TCPHandler } from './TCPHandler';
|
||||
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() {
|
||||
super();
|
||||
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() {
|
||||
super();
|
||||
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() {
|
||||
super();
|
||||
this.endpoint = 'processcount';
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { TCPHandler, Context } from '../class';
|
||||
import { Handler, Context } from '../class';
|
||||
import { dataConversion } from '../functions';
|
||||
|
||||
export default class RAM extends TCPHandler {
|
||||
export default class RAM extends Handler {
|
||||
constructor() {
|
||||
super();
|
||||
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() {
|
||||
super();
|
||||
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() {
|
||||
super();
|
||||
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() {
|
||||
super();
|
||||
this.endpoint = 'sshlogins';
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { TCPHandler, Context } from '../class';
|
||||
import { Handler, Context } from '../class';
|
||||
import { dataConversion } from '../functions';
|
||||
|
||||
export default class Storage extends TCPHandler {
|
||||
export default class Storage extends Handler {
|
||||
constructor() {
|
||||
super();
|
||||
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() {
|
||||
super();
|
||||
this.endpoint = 'userinfo';
|
||||
|
|
Loading…
Reference in New Issue