diff --git a/docker-compose.yml b/docker-compose.yml index 71c1dc16..c455d0d2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,3 @@ -version: '3.8' - networks: public-network: driver: bridge @@ -7,67 +5,15 @@ networks: services: api: container_name: codechat_api - image: codechat/api:v1.3.4 + build: + context: . + dockerfile: Dockerfile restart: unless-stopped ports: - 8084:8084 volumes: - - instances_connecteds:/codechat/instances - environment: - - SERVER_PORT=8084 - - SESSION_SECRET=W0NvZGVDaGF0XTpbU2Vzc - - - LOG_LEVEL=ERROR|WARN|INFO|DEBUG|LOG|VERBOSE|DARK - - LOG_COLOR=true - - - DATABASE_ENABLED=true - - DATABASE_URL=postgres://[USER]:[PASS]@[HOST]:[PORT]/[DATABASE_NAME]?schema=public - - DATABASE_SYNC_MESSAGES=true - - DATABASE_SAVE_DATA_NEW_MESSAGE=true - - DATABASE_SAVE_MESSAGE_UPDATE=true - - DATABASE_SAVE_DATA_CONTACTS=true - - DATABASE_SAVE_DATA_CHATS=true - - - GLOBAL_WEBHOOK_URL=url - - GLOBAL_WEBHOOK_ENABLED=false - - - INSTANCE_EXPIRATION_TIME=false - - - CONFIG_SESSION_PHONE_CLIENT=CodeChat_V1 - - CONFIG_SESSION_PHONE_NAME=Edge - - - WA_VERSION=[ 2, 3000, 1023235404 ] - - - QRCODE_LIMIT=10 - - QRCODE_EXPIRATION_TIME=60 - - QRCODE_LIGHT_COLOR='#ffffff' - - QRCODE_DARK_COLOR='#198754' - - - CONNECTION_TIMEOUT=300 - - - AUTHENTICATION_GLOBAL_AUTH_TOKEN=zYzP7ocstxh3SJ23D4FZTCu4ehnM8v4hu - - AUTHENTICATION_JWT_EXPIRES_IN=0 - - AUTHENTICATION_JWT_SECRET=3RFYiiRmvNiokSBrLZzx - - - PROVIDER_ENABLED=false - - PROVIDER_HOST=127.0.0.1 - - PROVIDER_PORT=5656 - - PROVIDER_PREFIX=codechat - - - BASE_URL= - - # - S3_ENABLED=false - # - S3_ACCESS_KEY=[ACCESS_KEY] - # - S3_SECRET_KEY=[SECRET_KEY] - # - S3_BUCKET=[BUCKET] - # - S3_PORT=9000 - # - S3_ENDPOINT=[ENDPOINT] - # - S3_USE_SSL=false + - ./instances:/codechat/instances + env_file: + - .env networks: - public-network - -volumes: - # In terminal run: docker volume create --name=instances_connecteds - instances_connecteds: - external: - name: instances_connecteds diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index 822fc901..3c5e2ba3 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -83,7 +83,7 @@ import { import { Logger } from '../../config/logger.config'; import { INSTANCE_DIR, ROOT_DIR } from '../../config/path.config'; import { join, normalize } from 'path'; -import axios, { AxiosError } from 'axios'; +import axios, { AxiosError, AxiosInstance } from 'axios'; import qrcode, { QRCodeToDataURLOptions } from 'qrcode'; import qrcodeTerminal from 'qrcode-terminal'; import { Boom } from '@hapi/boom'; @@ -156,6 +156,7 @@ import { import { createProxyAgents } from '../../utils/proxy'; import { fetchLatestBaileysVersionV2 } from '../../utils/wa-version'; import { getJidUser, getUserGroup } from '../../utils/extract-id'; +import http from 'http'; type InstanceQrCode = { count: number; @@ -180,6 +181,18 @@ export class WAStartupService { this.configService, this.providerFiles, ); + + const httpAgent = new http.Agent({ + keepAlive: true, + maxSockets: 100, + maxFreeSockets: 10, + timeout: 60000, + }); + + this.axiosInstance = axios.create({ + httpAgent, + timeout: 60000, + }); } private readonly logger = new Logger(this.configService, WAStartupService.name); @@ -191,6 +204,7 @@ export class WAStartupService { private readonly stateConnection: InstanceStateConnection = { state: 'close' }; private readonly databaseOptions: Database = this.configService.get('DATABASE'); + private readonly axiosInstance: AxiosInstance; private endSession = false; public client: WASocket; @@ -297,7 +311,7 @@ export class WAStartupService { try { if (this.webhook?.enabled) { if (this.webhook?.events && this.webhook?.events[event]) { - await axios.post( + await this.axiosInstance.post( this.webhook.url, { event: eventDesc, @@ -308,7 +322,7 @@ export class WAStartupService { ); } if (!this.webhook?.events) { - await axios.post( + await this.axiosInstance.post( this.webhook.url, { event: eventDesc, @@ -342,7 +356,7 @@ export class WAStartupService { try { const globalWebhook = this.configService.get('GLOBAL_WEBHOOK'); if (globalWebhook?.ENABLED && isURL(globalWebhook.URL)) { - await axios.post( + await this.axiosInstance.post( globalWebhook.URL, { event: eventDesc, @@ -1654,7 +1668,7 @@ export class WAStartupService { const isURL = /http(s?):\/\//.test(mediaMessage.media as string); if (isURL) { - const response = await axios.get(mediaMessage.media as string, { + const response = await this.axiosInstance.get(mediaMessage.media as string, { responseType: 'arraybuffer', }); @@ -1927,7 +1941,7 @@ export class WAStartupService { jpegThumbnail: await (async () => { if (data.linkMessage?.thumbnailUrl) { try { - const response = await axios.get(data.linkMessage.thumbnailUrl, { + const response = await this.axiosInstance.get(data.linkMessage.thumbnailUrl, { responseType: 'arraybuffer', }); return new Uint8Array(response.data); @@ -2417,7 +2431,7 @@ export class WAStartupService { try { let pic: WAMediaUpload; if (isURL(picture.image)) { - pic = (await axios.get(picture.image, { responseType: 'arraybuffer' })).data; + pic = (await this.axiosInstance.get(picture.image, { responseType: 'arraybuffer' })).data; } else if (isBase64(picture.image)) { pic = Buffer.from(picture.image, 'base64'); } else {