diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 2636adbda..947b81903 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -82,7 +82,7 @@ import { createId as cuid } from '@paralleldrive/cuid2'; import { Instance, Message } from '@prisma/client'; import { createJid } from '@utils/createJid'; import { fetchLatestWaWebVersion } from '@utils/fetchLatestWaWebVersion'; -import {makeProxyAgent, makeProxyAgentUndici} from '@utils/makeProxyAgent'; +import { makeProxyAgent, makeProxyAgentUndici } from '@utils/makeProxyAgent'; import { getOnWhatsappCache, saveOnWhatsappCache } from '@utils/onWhatsappCache'; import { status } from '@utils/renderStatus'; import { sendTelemetry } from '@utils/sendTelemetry'; @@ -266,6 +266,28 @@ export class BaileysStartupService extends ChannelStartupService { this.client?.ws?.close(); + const db = this.configService.get('DATABASE'); + const cache = this.configService.get('CACHE'); + const provider = this.configService.get('PROVIDER'); + + if (provider?.ENABLED) { + const authState = await this.authStateProvider.authStateProvider(this.instance.id); + + await authState.removeCreds(); + } + + if (cache?.REDIS.ENABLED && cache?.REDIS.SAVE_INSTANCES) { + const authState = await useMultiFileAuthStateRedisDb(this.instance.id, this.cache); + + await authState.removeCreds(); + } + + if (db.SAVE_DATA.INSTANCE) { + const authState = await useMultiFileAuthStatePrisma(this.instance.id, this.cache); + + await authState.removeCreds(); + } + const sessionExists = await this.prismaRepository.session.findFirst({ where: { sessionId: this.instanceId } }); if (sessionExists) { await this.prismaRepository.session.delete({ where: { sessionId: this.instanceId } }); diff --git a/src/utils/makeProxyAgent.ts b/src/utils/makeProxyAgent.ts index e882876ef..ac64b9dec 100644 --- a/src/utils/makeProxyAgent.ts +++ b/src/utils/makeProxyAgent.ts @@ -1,7 +1,6 @@ import { HttpsProxyAgent } from 'https-proxy-agent'; import { SocksProxyAgent } from 'socks-proxy-agent'; - -import { ProxyAgent } from 'undici' +import { ProxyAgent } from 'undici'; type Proxy = { host: string; @@ -46,38 +45,38 @@ export function makeProxyAgent(proxy: Proxy | string): HttpsProxyAgent | } export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent { - let proxyUrl: string - let protocol: string + let proxyUrl: string; + let protocol: string; if (typeof proxy === 'string') { - const url = new URL(proxy) - protocol = url.protocol.replace(':', '') - proxyUrl = proxy + const url = new URL(proxy); + protocol = url.protocol.replace(':', ''); + proxyUrl = proxy; } else { - const { host, password, port, protocol: proto, username } = proxy - protocol = (proto || 'http').replace(':', '') + const { host, password, port, protocol: proto, username } = proxy; + protocol = (proto || 'http').replace(':', ''); if (protocol === 'socks') { - protocol = 'socks5' + protocol = 'socks5'; } - const auth = username && password ? `${username}:${password}@` : '' - proxyUrl = `${protocol}://${auth}${host}:${port}` + const auth = username && password ? `${username}:${password}@` : ''; + proxyUrl = `${protocol}://${auth}${host}:${port}`; } - const PROXY_HTTP_PROTOCOL = 'http' - const PROXY_HTTPS_PROTOCOL = 'https' - const PROXY_SOCKS4_PROTOCOL = 'socks4' - const PROXY_SOCKS5_PROTOCOL = 'socks5' + const PROXY_HTTP_PROTOCOL = 'http'; + const PROXY_HTTPS_PROTOCOL = 'https'; + const PROXY_SOCKS4_PROTOCOL = 'socks4'; + const PROXY_SOCKS5_PROTOCOL = 'socks5'; switch (protocol) { case PROXY_HTTP_PROTOCOL: case PROXY_HTTPS_PROTOCOL: case PROXY_SOCKS4_PROTOCOL: case PROXY_SOCKS5_PROTOCOL: - return new ProxyAgent(proxyUrl) + return new ProxyAgent(proxyUrl); default: - throw new Error(`Unsupported proxy protocol: ${protocol}`) + throw new Error(`Unsupported proxy protocol: ${protocol}`); } } diff --git a/src/utils/use-multi-file-auth-state-prisma.ts b/src/utils/use-multi-file-auth-state-prisma.ts index 0f7faa076..d09078023 100644 --- a/src/utils/use-multi-file-auth-state-prisma.ts +++ b/src/utils/use-multi-file-auth-state-prisma.ts @@ -1,6 +1,7 @@ import { prismaRepository } from '@api/server.module'; import { CacheService } from '@api/services/cache.service'; import { CacheConf, configService } from '@config/env.config'; +import { Logger } from '@config/logger.config'; import { INSTANCE_DIR } from '@config/path.config'; import { AuthenticationState, BufferJSON, initAuthCreds, WAProto as proto } from 'baileys'; import fs from 'fs/promises'; @@ -73,12 +74,15 @@ async function fileExists(file: string): Promise { } } +const logger = new Logger('useMultiFileAuthStatePrisma'); + export default async function useMultiFileAuthStatePrisma( sessionId: string, cache: CacheService, ): Promise<{ state: AuthenticationState; saveCreds: () => Promise; + removeCreds: () => Promise; }> { const localFolder = path.join(INSTANCE_DIR, sessionId); const localFile = (key: string) => path.join(localFolder, fixFileName(key) + '.json'); @@ -142,6 +146,26 @@ export default async function useMultiFileAuthStatePrisma( } } + async function removeCreds(): Promise { + const cacheConfig = configService.get('CACHE'); + + // Redis + try { + if (cacheConfig.REDIS.ENABLED) { + await cache.delete(sessionId); + logger.info({ action: 'redis.delete', sessionId }); + + return; + } + } catch (err) { + logger.warn({ action: 'redis.delete', sessionId, err }); + } + + logger.info({ action: 'auth.key.delete', sessionId }); + + await deleteAuthKey(sessionId); + } + let creds = await readData('creds'); if (!creds) { creds = initAuthCreds(); @@ -183,5 +207,7 @@ export default async function useMultiFileAuthStatePrisma( saveCreds: () => { return writeData(creds, 'creds'); }, + + removeCreds, }; } diff --git a/src/utils/use-multi-file-auth-state-provider-files.ts b/src/utils/use-multi-file-auth-state-provider-files.ts index 6a15d6541..eecc3100e 100644 --- a/src/utils/use-multi-file-auth-state-provider-files.ts +++ b/src/utils/use-multi-file-auth-state-provider-files.ts @@ -39,7 +39,11 @@ import { Logger } from '@config/logger.config'; import { AuthenticationCreds, AuthenticationState, BufferJSON, initAuthCreds, proto, SignalDataTypeMap } from 'baileys'; import { isNotEmpty } from 'class-validator'; -export type AuthState = { state: AuthenticationState; saveCreds: () => Promise }; +export type AuthState = { + state: AuthenticationState; + saveCreds: () => Promise; + removeCreds: () => Promise; +}; export class AuthStateProvider { constructor(private readonly providerFiles: ProviderFiles) {} @@ -86,6 +90,18 @@ export class AuthStateProvider { return response; }; + const removeCreds = async () => { + const [response, error] = await this.providerFiles.removeSession(instance); + if (error) { + // this.logger.error(['removeData', error?.message, error?.stack]); + return; + } + + logger.info({ action: 'remove.session', instance, response }); + + return; + }; + const creds: AuthenticationCreds = (await readData('creds')) || initAuthCreds(); return { @@ -126,6 +142,10 @@ export class AuthStateProvider { saveCreds: async () => { return await writeData(creds, 'creds'); }, + + removeCreds, }; } } + +const logger = new Logger('useMultiFileAuthStatePrisma'); diff --git a/src/utils/use-multi-file-auth-state-redis-db.ts b/src/utils/use-multi-file-auth-state-redis-db.ts index 2f10c6718..e0981c700 100644 --- a/src/utils/use-multi-file-auth-state-redis-db.ts +++ b/src/utils/use-multi-file-auth-state-redis-db.ts @@ -8,6 +8,7 @@ export async function useMultiFileAuthStateRedisDb( ): Promise<{ state: AuthenticationState; saveCreds: () => Promise; + removeCreds: () => Promise; }> { const logger = new Logger('useMultiFileAuthStateRedisDb'); @@ -36,6 +37,16 @@ export async function useMultiFileAuthStateRedisDb( } }; + async function removeCreds(): Promise { + try { + logger.warn({ action: 'redis.delete', instanceName }); + + return await cache.delete(instanceName); + } catch { + return; + } + } + const creds: AuthenticationCreds = (await readData('creds')) || initAuthCreds(); return { @@ -76,5 +87,7 @@ export async function useMultiFileAuthStateRedisDb( saveCreds: async () => { return await writeData(creds, 'creds'); }, + + removeCreds, }; }