From a10226bea282bb3bf9cd1d889cb1371c797fe89e Mon Sep 17 00:00:00 2001 From: Alan Mosko Date: Sat, 22 Jul 2023 10:45:46 -0300 Subject: [PATCH] Start --- src/config/env.config.ts | 5 +++++ src/dev-env.yml | 4 ++++ src/whatsapp/controllers/instance.controller.ts | 10 +++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/config/env.config.ts b/src/config/env.config.ts index 88b718de..500e7f9e 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -102,6 +102,7 @@ export type Auth = { TYPE: 'jwt' | 'apikey'; }; +export type Instances = { LIMIT: number }; export type DelInstance = number | boolean; export type GlobalWebhook = { @@ -124,6 +125,7 @@ export interface Env { DATABASE: Database; REDIS: Redis; LOG: Log; + INSTANCES: Instances; DEL_INSTANCE: DelInstance; WEBHOOK: Webhook; CONFIG_SESSION_PHONE: ConfigSessionPhone; @@ -255,6 +257,9 @@ export class ConfigService { QRCODE: { LIMIT: Number.parseInt(process.env.QRCODE_LIMIT) || 30, }, + INSTANCES: { + LIMIT: Number.parseInt(process.env.INSTANCES_LIMIT) || 0, + }, AUTHENTICATION: { TYPE: process.env.AUTHENTICATION_TYPE as 'jwt', API_KEY: { diff --git a/src/dev-env.yml b/src/dev-env.yml index 41368ea4..362e857e 100644 --- a/src/dev-env.yml +++ b/src/dev-env.yml @@ -44,6 +44,10 @@ LOG: # Determine how long the instance should be deleted from memory in case of no connection. # Default time: 5 minutes # If you don't even want an expiration, enter the value false +# Instances settings +INSTANCES: + # Sets a limit of instances that can be created on the server + LIMIT: 0 DEL_INSTANCE: false # or false # Temporary data storage diff --git a/src/whatsapp/controllers/instance.controller.ts b/src/whatsapp/controllers/instance.controller.ts index 75911848..0d02b739 100644 --- a/src/whatsapp/controllers/instance.controller.ts +++ b/src/whatsapp/controllers/instance.controller.ts @@ -1,6 +1,6 @@ import { delay } from '@whiskeysockets/baileys'; import EventEmitter2 from 'eventemitter2'; -import { Auth, ConfigService, HttpServer } from '../../config/env.config'; +import { Auth, ConfigService, HttpServer, Instances } from '../../config/env.config'; import { BadRequestException, InternalServerErrorException } from '../../exceptions'; import { InstanceDto } from '../dto/instance.dto'; import { RepositoryBroker } from '../repository/repository.manager'; @@ -47,6 +47,14 @@ export class InstanceController { 'The instance name must be lowercase and without special characters', ); } + + const INSTANCES_LIMIT = this.configService.get('INSTANCES').LIMIT; + if ( + INSTANCES_LIMIT != 0 && + Object.keys(this.waMonitor.waInstances).length >= INSTANCES_LIMIT + ) { + throw new BadRequestException('Limit of instances reached'); + } this.logger.verbose('checking duplicate token'); await this.authService.checkDuplicateToken(token);