From a18ac0355c2ebccb7a7d96459807b97538100ca6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Majdand=C5=BEi=C4=87?= Date: Wed, 7 Jun 2023 13:49:39 +0200 Subject: [PATCH] Add source and desitnation set processors --- package.json | 1 + pnpm-lock.yaml | 3 + .../Client/DestinationSetPreprocessor.ts | 40 ++++ .../Client/SourceSetPreprocessor.ts | 40 ++++ src/PDUProcessor/ProcessorManager.ts | 188 +++++++++--------- src/main.ts | 8 +- 6 files changed, 185 insertions(+), 95 deletions(-) create mode 100644 src/PDUProcessor/Preprocessor/Client/DestinationSetPreprocessor.ts create mode 100644 src/PDUProcessor/Preprocessor/Client/SourceSetPreprocessor.ts diff --git a/package.json b/package.json index c0017bb..f4ee78a 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "nanotimer": "^0.3.15", "smpp": "0.6.0-rc.4", "ts-node": "^10.9.1", + "typescript": "^5.1.3", "ws": "^8.13.0", "zlib": "^1.0.5" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 17b0b7c..a91c5e4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -26,6 +26,9 @@ dependencies: ts-node: specifier: ^10.9.1 version: 10.9.1(@types/node@20.2.5)(typescript@5.1.3) + typescript: + specifier: ^5.1.3 + version: 5.1.3 ws: specifier: ^8.13.0 version: 8.13.0 diff --git a/src/PDUProcessor/Preprocessor/Client/DestinationSetPreprocessor.ts b/src/PDUProcessor/Preprocessor/Client/DestinationSetPreprocessor.ts new file mode 100644 index 0000000..e96a950 --- /dev/null +++ b/src/PDUProcessor/Preprocessor/Client/DestinationSetPreprocessor.ts @@ -0,0 +1,40 @@ +import SmppSession from "../../../SmppSession"; +import Preprocessor from "../Preprocessor"; +import {PDU} from "../../../CommonObjects"; + +export default class DestinationSetPreprocessor extends Preprocessor { + applicableCommands: string[] = ['submit_sm', 'deliver_sm']; + private sourceSet: string[] = []; + + constructor(type: string) { + super(type); + while (this.sourceSet.length < 100) { + this.sourceSet.push(this.getRandomInt(100000, 999999).toString()); + } + } + + protected doProcess(session: any, pdu: PDU, entity?: SmppSession | undefined): Promise { + return new Promise((resolve, reject) => { + if (pdu.short_message) { + if (pdu.short_message.includes("arg:")) { + let temp: string = pdu.short_message.split(";"); + let arg: number = Number(temp[0].split(":")[1]); + while (this.sourceSet.length < arg) { + this.sourceSet.push(this.getRandomInt(100000, 999999).toString()); + } + while (this.sourceSet.length > arg) { + this.sourceSet.pop(); + } + pdu.short_message = temp[1]; + } + } + pdu.destination_addr = pdu.destination_addr + this.sourceSet[this.getRandomInt(0, this.sourceSet.length)]; + }); + } + + private getRandomInt(min: number, max: number): number { + min = Math.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min) + min); + } +} diff --git a/src/PDUProcessor/Preprocessor/Client/SourceSetPreprocessor.ts b/src/PDUProcessor/Preprocessor/Client/SourceSetPreprocessor.ts new file mode 100644 index 0000000..3539916 --- /dev/null +++ b/src/PDUProcessor/Preprocessor/Client/SourceSetPreprocessor.ts @@ -0,0 +1,40 @@ +import SmppSession from "../../../SmppSession"; +import Preprocessor from "../Preprocessor"; +import {PDU} from "../../../CommonObjects"; + +export default class SourceSetPreprocessor extends Preprocessor { + applicableCommands: string[] = ['submit_sm', 'deliver_sm']; + private sourceSet: string[] = []; + + constructor(type: string) { + super(type); + while (this.sourceSet.length < 100) { + this.sourceSet.push(this.getRandomInt(100000, 999999).toString()); + } + } + + protected doProcess(session: any, pdu: PDU, entity?: SmppSession | undefined): Promise { + return new Promise((resolve, reject) => { + if (pdu.short_message) { + if (pdu.short_message.includes("arg:")) { + let temp: string = pdu.short_message.split(";"); + let arg: number = Number(temp[0].split(":")[1]); + while (this.sourceSet.length < arg) { + this.sourceSet.push(this.getRandomInt(100000, 999999).toString()); + } + while (this.sourceSet.length > arg) { + this.sourceSet.pop(); + } + pdu.short_message = temp[1]; + } + } + pdu.source_addr = pdu.source_addr + this.sourceSet[this.getRandomInt(0, this.sourceSet.length)]; + }); + } + + private getRandomInt(min: number, max: number): number { + min = Math.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min) + min); + } +} diff --git a/src/PDUProcessor/ProcessorManager.ts b/src/PDUProcessor/ProcessorManager.ts index 81362ca..6fb13f1 100644 --- a/src/PDUProcessor/ProcessorManager.ts +++ b/src/PDUProcessor/ProcessorManager.ts @@ -20,106 +20,112 @@ import UCS2Preprocessor from "./Preprocessor/Client/UCS2Preprocessor"; import ProtocolId2DigitProcessor from "./Preprocessor/Client/ProtocolId-2Digit-Processor"; import ProtocolId3DigitProcessor from "./Preprocessor/Client/ProtocolId-3Digit-Processor"; import ProtocolId4DigitProcessor from "./Preprocessor/Client/ProtocolId-4Digit-Processor"; +import SourceSetPreprocessor from "./Preprocessor/Client/SourceSetPreprocessor"; +import DestinationSetPreprocessor from "./Preprocessor/Client/DestinationSetPreprocessor"; export default class ProcessorManager { - static preprocessors: PduProcessor[]; - static postprocessors: PduProcessor[]; - private static readonly logger: Logger = new Logger(this.name); + static preprocessors: PduProcessor[]; + static postprocessors: PduProcessor[]; + private static readonly logger: Logger = new Logger(this.name); - constructor() { - // This is an IDIOTIC solution, but it works - // Try running eb22a43 to find out what's wrong with the previous approach - ProcessorManager.postprocessors = [ - new EnquireLinkReplyProcessor(Center.name), - new DeliverSmReplyProcessor(Client.name), - new SubmitSmReplyProcessor(Center.name), - new BindTranscieverReplyProcessor(Center.name), - new EchoPduProcessor(Center.name), - new DeliveryReceiptProcessor(Center.name) - ]; - ProcessorManager.preprocessors = [ - new DestinationEnumeratorProcessor(Client.name), - new SourceEnumeratorProcessor(Client.name), - new DestinationEnumeratorProcessor(Center.name), - new SourceEnumeratorProcessor(Center.name), - new DeliveryReceiptRequestProcessor(Client.name), - new LongSmsProcessor(Client.name), - new LongSmsProcessor(Center.name), - new ProtocolIdProcessor(Client.name), - new ProtocolIdProcessor(Center.name), - new UCS2Preprocessor(Client.name), - new UCS2Preprocessor(Center.name), - new ProtocolId2DigitProcessor(Client.name), - new ProtocolId2DigitProcessor(Center.name), - new ProtocolId3DigitProcessor(Client.name), - new ProtocolId3DigitProcessor(Center.name), - new ProtocolId4DigitProcessor(Client.name), - new ProtocolId4DigitProcessor(Center.name), - ]; - } + constructor() { + // This is an IDIOTIC solution, but it works + // Try running eb22a43 to find out what's wrong with the previous approach + ProcessorManager.postprocessors = [ + new EnquireLinkReplyProcessor(Center.name), + new DeliverSmReplyProcessor(Client.name), + new SubmitSmReplyProcessor(Center.name), + new BindTranscieverReplyProcessor(Center.name), + new EchoPduProcessor(Center.name), + new DeliveryReceiptProcessor(Center.name) + ]; + ProcessorManager.preprocessors = [ + new DestinationEnumeratorProcessor(Client.name), + new SourceEnumeratorProcessor(Client.name), + new DestinationEnumeratorProcessor(Center.name), + new SourceEnumeratorProcessor(Center.name), + new DeliveryReceiptRequestProcessor(Client.name), + new LongSmsProcessor(Client.name), + new LongSmsProcessor(Center.name), + new ProtocolIdProcessor(Client.name), + new ProtocolIdProcessor(Center.name), + new UCS2Preprocessor(Client.name), + new UCS2Preprocessor(Center.name), + new ProtocolId2DigitProcessor(Client.name), + new ProtocolId2DigitProcessor(Center.name), + new ProtocolId3DigitProcessor(Client.name), + new ProtocolId3DigitProcessor(Center.name), + new ProtocolId4DigitProcessor(Client.name), + new ProtocolId4DigitProcessor(Center.name), + new SourceSetPreprocessor(Client.name), + new SourceSetPreprocessor(Center.name), + new DestinationSetPreprocessor(Client.name), + new DestinationSetPreprocessor(Center.name) + ]; + } - static get processors(): PduProcessor[] { - return this.preprocessors.concat(this.postprocessors); - } + static get processors(): PduProcessor[] { + return this.preprocessors.concat(this.postprocessors); + } - static getProcessors(name: string): PduProcessor[] { - this.logger.log1(`Looking for processor with name ${name}...`); - let pduProcessors: PduProcessor[] = this.processors.filter((processor: PduProcessor) => processor.name === name); - this.logger.log1(`Found ${pduProcessors.length} processor(s) with name ${name}`); - return pduProcessors; - } + static getProcessors(name: string): PduProcessor[] { + this.logger.log1(`Looking for processor with name ${name}...`); + let pduProcessors: PduProcessor[] = this.processors.filter((processor: PduProcessor) => processor.name === name); + this.logger.log1(`Found ${pduProcessors.length} processor(s) with name ${name}`); + return pduProcessors; + } - static attachProcessors(session: SmppSession, processors: PduProcessor[]): void { - this.logger.log1(`Trying to attach processor ${processors.toString()} to session ${session.constructor.name}-${session.id}`); - for (const processor of processors) { - if (this.areCompatible(session, processor)) { - // This could be done a little better but this is OK for now - switch (processor.type) { - case Preprocessor.name: - session.attachPreprocessor(processor); - break; - case Postprocessor.name: - session.attachPostprocessor(processor); - break; - default: - this.logger.log1(`Processor ${processor.name} is not a preprocessor or a postprocessor`); - break; - } - } - } - } + static attachProcessors(session: SmppSession, processors: PduProcessor[]): void { + this.logger.log1(`Trying to attach processor ${processors.toString()} to session ${session.constructor.name}-${session.id}`); + for (const processor of processors) { + if (this.areCompatible(session, processor)) { + // This could be done a little better but this is OK for now + switch (processor.type) { + case Preprocessor.name: + session.attachPreprocessor(processor); + break; + case Postprocessor.name: + session.attachPostprocessor(processor); + break; + default: + this.logger.log1(`Processor ${processor.name} is not a preprocessor or a postprocessor`); + break; + } + } + } + } - static detachProcessors(session: SmppSession, processors: PduProcessor[]): void { - this.logger.log1(`Trying to detach processors ${processors.toString()} from session ${session.constructor.name}-${session.id}`); - for (const processor of processors) { - switch (processor.type) { - case Preprocessor.name: - session.detachPreprocessor(processor); - break; - case Postprocessor.name: - session.detachPostprocessor(processor); - break; - default: - this.logger.log1(`Processor ${processor.name} is not a preprocessor or a postprocessor`); - break; - } - } - } + static detachProcessors(session: SmppSession, processors: PduProcessor[]): void { + this.logger.log1(`Trying to detach processors ${processors.toString()} from session ${session.constructor.name}-${session.id}`); + for (const processor of processors) { + switch (processor.type) { + case Preprocessor.name: + session.detachPreprocessor(processor); + break; + case Postprocessor.name: + session.detachPostprocessor(processor); + break; + default: + this.logger.log1(`Processor ${processor.name} is not a preprocessor or a postprocessor`); + break; + } + } + } - static areCompatible(session: SmppSession, processor: PduProcessor): boolean { - this.logger.log1(`Checking compatibility between session ${session.constructor.name}-${session.id} and processor ${processor.name}`); - return session.constructor.name === processor.sessionType; - } + static areCompatible(session: SmppSession, processor: PduProcessor): boolean { + this.logger.log1(`Checking compatibility between session ${session.constructor.name}-${session.id} and processor ${processor.name}`); + return session.constructor.name === processor.sessionType; + } - static getProcessorsForType(type: string): PduProcessor[] { - return this.processors.filter((processor: PduProcessor) => processor.sessionType === type); - } + static getProcessorsForType(type: string): PduProcessor[] { + return this.processors.filter((processor: PduProcessor) => processor.sessionType === type); + } - static getPreprocessorsForType(type: string): PduProcessor[] { - return this.preprocessors.filter((processor: PduProcessor) => processor.sessionType === type); - } + static getPreprocessorsForType(type: string): PduProcessor[] { + return this.preprocessors.filter((processor: PduProcessor) => processor.sessionType === type); + } - static getPostprocessorsForType(type: string): PduProcessor[] { - return this.postprocessors.filter((processor: PduProcessor) => processor.sessionType === type); - } + static getPostprocessorsForType(type: string): PduProcessor[] { + return this.postprocessors.filter((processor: PduProcessor) => processor.sessionType === type); + } } diff --git a/src/main.ts b/src/main.ts index 5ff3c0c..72d274f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -23,7 +23,7 @@ function cleanup(): void { process.exit(0); } -process.on('exit', cleanup); -process.on('SIGINT', cleanup); -process.on('SIGUSR1', cleanup); -process.on('SIGUSR2', cleanup); \ No newline at end of file +// process.on('exit', cleanup); +// process.on('SIGINT', cleanup); +// process.on('SIGUSR1', cleanup); +// process.on('SIGUSR2', cleanup);