diff --git a/src/class/Context.ts b/src/class/Context.ts new file mode 100644 index 0000000..d219af6 --- /dev/null +++ b/src/class/Context.ts @@ -0,0 +1,35 @@ +import net from 'net'; +import { Client } from '.'; + +export default class Context { + public socket: net.Socket; + + public client: Client; + + public data: { + username: string, + endpoint: string, + message?: string, + additionalData?: object, + HMAC: string, + } + + constructor(socket: net.Socket, data: string, client: Client) { + const parsed: { Username: string, Type: string, Message?: string, Data?: object, HMAC: string } = JSON.parse(data); + + this.socket = socket; + this.client = client; + this.data = { + username: parsed.Username, + endpoint: parsed.Type, + message: parsed.Message, + additionalData: parsed.Data, + HMAC: parsed.HMAC, + }; + } + + public send(v: string) { + this.socket.write(`${v.toString()}\n`); + this.socket.destroy(); + } +} diff --git a/src/class/TCPHandler.ts b/src/class/TCPHandler.ts new file mode 100644 index 0000000..9ef1c88 --- /dev/null +++ b/src/class/TCPHandler.ts @@ -0,0 +1,11 @@ +import { Context } from '.'; + +export default class TCPHandler { + public endpoint: string; + + constructor(endpoint?: string) { + this.endpoint = endpoint; + } + + public handle(ctx: Context): Promise { return Promise.resolve(); } +} diff --git a/src/class/index.ts b/src/class/index.ts index fa2579b..c1d02e5 100644 --- a/src/class/index.ts +++ b/src/class/index.ts @@ -2,6 +2,7 @@ export { default as AccountUtil } from './AccountUtil'; export { default as Client } from './Client'; export { default as Collection } from './Collection'; export { default as Command } from './Command'; +export { default as Context } from './Context'; export { default as Event } from './Event'; export { default as LocalStorage } from './LocalStorage'; export { default as Report } from './Report'; @@ -9,4 +10,5 @@ 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/handlers/killpid.ts b/src/cscli/handlers/killpid.ts index 97a53f0..67d4408 100644 --- a/src/cscli/handlers/killpid.ts +++ b/src/cscli/handlers/killpid.ts @@ -1,4 +1,4 @@ -import { TCPHandler, Context } from '..'; +import { TCPHandler, Context } from '../../class'; export default class KillPID extends TCPHandler { constructor() { diff --git a/src/cscli/handlers/lock.ts b/src/cscli/handlers/lock.ts index 66987f4..f610141 100644 --- a/src/cscli/handlers/lock.ts +++ b/src/cscli/handlers/lock.ts @@ -1,4 +1,4 @@ -import { TCPHandler, Context } from '..'; +import { TCPHandler, Context } from '../../class'; export default class Lock extends TCPHandler { constructor() { diff --git a/src/cscli/handlers/processCount.ts b/src/cscli/handlers/processCount.ts index d41c140..da1b79f 100644 --- a/src/cscli/handlers/processCount.ts +++ b/src/cscli/handlers/processCount.ts @@ -1,4 +1,4 @@ -import { TCPHandler, Context } from '..'; +import { TCPHandler, Context } from '../../class'; export default class ProcessCount extends TCPHandler { constructor() { diff --git a/src/cscli/handlers/ram.ts b/src/cscli/handlers/ram.ts index 114d41b..6e943f8 100644 --- a/src/cscli/handlers/ram.ts +++ b/src/cscli/handlers/ram.ts @@ -1,4 +1,4 @@ -import { TCPHandler, Context } from '..'; +import { TCPHandler, Context } from '../../class'; import { dataConversion } from '../../functions'; export default class RAM extends TCPHandler { diff --git a/src/cscli/handlers/ramLimit.ts b/src/cscli/handlers/ramLimit.ts index 442c39d..37300ed 100644 --- a/src/cscli/handlers/ramLimit.ts +++ b/src/cscli/handlers/ramLimit.ts @@ -1,4 +1,4 @@ -import { TCPHandler, Context } from '..'; +import { TCPHandler, Context } from '../../class'; export default class RAMLimits extends TCPHandler { constructor() { diff --git a/src/cscli/handlers/score.ts b/src/cscli/handlers/score.ts index 9c04dbe..ca8c94e 100644 --- a/src/cscli/handlers/score.ts +++ b/src/cscli/handlers/score.ts @@ -1,5 +1,4 @@ -import { TCPHandler, Context } from '..'; -import { Report } from '../../class'; +import { TCPHandler, Context, Report } from '../../class'; export default class Score extends TCPHandler { constructor() { diff --git a/src/cscli/handlers/sshLogins.ts b/src/cscli/handlers/sshLogins.ts index 2b29fb8..5600649 100644 --- a/src/cscli/handlers/sshLogins.ts +++ b/src/cscli/handlers/sshLogins.ts @@ -1,4 +1,4 @@ -import { TCPHandler, Context } from '..'; +import { TCPHandler, Context } from '../../class'; export default class SSHLogins extends TCPHandler { constructor() { diff --git a/src/cscli/handlers/storage.ts b/src/cscli/handlers/storage.ts index 9c6f529..fb8e0bf 100644 --- a/src/cscli/handlers/storage.ts +++ b/src/cscli/handlers/storage.ts @@ -1,4 +1,4 @@ -import { TCPHandler, Context } from '..'; +import { TCPHandler, Context } from '../../class'; import { dataConversion } from '../../functions'; export default class Storage extends TCPHandler { diff --git a/src/cscli/handlers/userInfo.ts b/src/cscli/handlers/userInfo.ts index fbdf788..3e293af 100644 --- a/src/cscli/handlers/userInfo.ts +++ b/src/cscli/handlers/userInfo.ts @@ -1,4 +1,4 @@ -import { TCPHandler, Context } from '..'; +import { TCPHandler, Context } from '../../class'; export default class UserInfo extends TCPHandler { constructor() { diff --git a/src/cscli/index.ts b/src/cscli/index.ts index 12fdc49..ab883d0 100644 --- a/src/cscli/index.ts +++ b/src/cscli/index.ts @@ -1 +1 @@ -export { default as CSCLI, TCPHandler, Context } from './main'; +export { default as CSCLI } from './main'; diff --git a/src/cscli/main.ts b/src/cscli/main.ts index 510f1e3..a5bc05b 100644 --- a/src/cscli/main.ts +++ b/src/cscli/main.ts @@ -4,53 +4,10 @@ import net from 'net'; import crypto from 'crypto'; import { promises as fs } from 'fs'; -import { Client, Collection } from '../class'; +import { Client, Collection, Context, TCPHandler } from '../class'; import * as handlerFiles from './handlers'; -export class TCPHandler { - public endpoint: string; - - constructor(endpoint?: string) { - this.endpoint = endpoint; - } - - public handle(ctx: Context): Promise { return Promise.resolve(); } -} - -export class Context { - public socket: net.Socket; - - public client: Client; - - public data: { - username: string, - endpoint: string, - message?: string, - additionalData?: object, - HMAC: string, - } - - constructor(socket: net.Socket, data: string, client: Client) { - const parsed: { Username: string, Type: string, Message?: string, Data?: object, HMAC: string } = JSON.parse(data); - - this.socket = socket; - this.client = client; - this.data = { - username: parsed.Username, - endpoint: parsed.Type, - message: parsed.Message, - additionalData: parsed.Data, - HMAC: parsed.HMAC, - }; - } - - public send(v: string) { - this.socket.write(`${v.toString()}\n`); - this.socket.destroy(); - } -} - export default class CSCLI { public client: Client;