diff --git a/src/class/CSCLI.ts b/src/class/CSCLI.ts index c58e70b..b043a6d 100644 --- a/src/class/CSCLI.ts +++ b/src/class/CSCLI.ts @@ -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; + public handlers: Collection; #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) { + public load(handlerFiles: { [s: string]: typeof Handler; } | ArrayLike) { this.handlers = new Collection(); - const hdFiles = Object.values(handlerFiles); - for (const Handler of hdFiles) { - const handler = new Handler(); + const hdFiles = Object.values(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'); }); } } diff --git a/src/class/TCPHandler.ts b/src/class/Handler.ts similarity index 85% rename from src/class/TCPHandler.ts rename to src/class/Handler.ts index 9ef1c88..ad261c3 100644 --- a/src/class/TCPHandler.ts +++ b/src/class/Handler.ts @@ -1,6 +1,6 @@ import { Context } from '.'; -export default class TCPHandler { +export default class Handler { public endpoint: string; constructor(endpoint?: string) { diff --git a/src/class/index.ts b/src/class/index.ts index 1fe357c..d8dcf1b 100644 --- a/src/class/index.ts +++ b/src/class/index.ts @@ -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'; diff --git a/src/cscli/domains.ts b/src/cscli/domains.ts new file mode 100644 index 0000000..92a7639 --- /dev/null +++ b/src/cscli/domains.ts @@ -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(); + } +} diff --git a/src/cscli/killpid.ts b/src/cscli/killpid.ts index f3cad50..1b74e98 100644 --- a/src/cscli/killpid.ts +++ b/src/cscli/killpid.ts @@ -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'; diff --git a/src/cscli/lock.ts b/src/cscli/lock.ts index 1f782d9..b173a5e 100644 --- a/src/cscli/lock.ts +++ b/src/cscli/lock.ts @@ -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'; diff --git a/src/cscli/processCount.ts b/src/cscli/processCount.ts index 94ec396..893e2e0 100644 --- a/src/cscli/processCount.ts +++ b/src/cscli/processCount.ts @@ -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'; diff --git a/src/cscli/ram.ts b/src/cscli/ram.ts index 9abcdea..0a2c955 100644 --- a/src/cscli/ram.ts +++ b/src/cscli/ram.ts @@ -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'; diff --git a/src/cscli/ramLimit.ts b/src/cscli/ramLimit.ts index 69d6c30..bbaf401 100644 --- a/src/cscli/ramLimit.ts +++ b/src/cscli/ramLimit.ts @@ -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'; diff --git a/src/cscli/score.ts b/src/cscli/score.ts index 61fabc9..37ba7d0 100644 --- a/src/cscli/score.ts +++ b/src/cscli/score.ts @@ -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'; diff --git a/src/cscli/sshLogins.ts b/src/cscli/sshLogins.ts index 18bd943..34f48a6 100644 --- a/src/cscli/sshLogins.ts +++ b/src/cscli/sshLogins.ts @@ -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'; diff --git a/src/cscli/storage.ts b/src/cscli/storage.ts index 1c948d5..8c06c7d 100644 --- a/src/cscli/storage.ts +++ b/src/cscli/storage.ts @@ -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'; diff --git a/src/cscli/userInfo.ts b/src/cscli/userInfo.ts index 6d7e6ae..d066b76 100644 --- a/src/cscli/userInfo.ts +++ b/src/cscli/userInfo.ts @@ -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';