mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-10 18:39:38 -06:00
Durante o processo de logout de uma instância, as chaves associadas ao estado criptográfico não estavam sendo removidas corretamente do Redis.
Dessa forma, quando uma nova conexão era estabelecida reutilizando o mesmo instanceName, o Baileys carregava chaves antigas e inválidas, incompatíveis com o novo conjunto de credenciais (creds) gerado na reconexão. Essa inconsistência gerava o seguinte sintoma prático: A instância autenticava com sucesso; Contudo, ao tentar enviar mensagens, entrava em estado de bloqueio, exibindo o status “aguardando mensagem” indefinidamente.
This commit is contained in:
parent
3454bec79f
commit
be5760905e
@ -266,6 +266,28 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
|
|
||||||
this.client?.ws?.close();
|
this.client?.ws?.close();
|
||||||
|
|
||||||
|
const db = this.configService.get<Database>('DATABASE');
|
||||||
|
const cache = this.configService.get<CacheConf>('CACHE');
|
||||||
|
const provider = this.configService.get<ProviderSession>('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 } });
|
const sessionExists = await this.prismaRepository.session.findFirst({ where: { sessionId: this.instanceId } });
|
||||||
if (sessionExists) {
|
if (sessionExists) {
|
||||||
await this.prismaRepository.session.delete({ where: { sessionId: this.instanceId } });
|
await this.prismaRepository.session.delete({ where: { sessionId: this.instanceId } });
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import { INSTANCE_DIR } from '@config/path.config';
|
|||||||
import { AuthenticationState, BufferJSON, initAuthCreds, WAProto as proto } from 'baileys';
|
import { AuthenticationState, BufferJSON, initAuthCreds, WAProto as proto } from 'baileys';
|
||||||
import fs from 'fs/promises';
|
import fs from 'fs/promises';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import {Logger} from "@config/logger.config";
|
||||||
|
|
||||||
const fixFileName = (file: string): string | undefined => {
|
const fixFileName = (file: string): string | undefined => {
|
||||||
if (!file) {
|
if (!file) {
|
||||||
@ -73,12 +74,15 @@ async function fileExists(file: string): Promise<any> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const logger = new Logger('useMultiFileAuthStatePrisma');
|
||||||
|
|
||||||
export default async function useMultiFileAuthStatePrisma(
|
export default async function useMultiFileAuthStatePrisma(
|
||||||
sessionId: string,
|
sessionId: string,
|
||||||
cache: CacheService,
|
cache: CacheService,
|
||||||
): Promise<{
|
): Promise<{
|
||||||
state: AuthenticationState;
|
state: AuthenticationState;
|
||||||
saveCreds: () => Promise<void>;
|
saveCreds: () => Promise<void>;
|
||||||
|
removeCreds: () => Promise<void>;
|
||||||
}> {
|
}> {
|
||||||
const localFolder = path.join(INSTANCE_DIR, sessionId);
|
const localFolder = path.join(INSTANCE_DIR, sessionId);
|
||||||
const localFile = (key: string) => path.join(localFolder, fixFileName(key) + '.json');
|
const localFile = (key: string) => path.join(localFolder, fixFileName(key) + '.json');
|
||||||
@ -142,6 +146,27 @@ export default async function useMultiFileAuthStatePrisma(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function removeCreds(): Promise<any> {
|
||||||
|
|
||||||
|
const cacheConfig = configService.get<CacheConf>('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');
|
let creds = await readData('creds');
|
||||||
if (!creds) {
|
if (!creds) {
|
||||||
creds = initAuthCreds();
|
creds = initAuthCreds();
|
||||||
@ -183,5 +208,7 @@ export default async function useMultiFileAuthStatePrisma(
|
|||||||
saveCreds: () => {
|
saveCreds: () => {
|
||||||
return writeData(creds, 'creds');
|
return writeData(creds, 'creds');
|
||||||
},
|
},
|
||||||
|
|
||||||
|
removeCreds
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -39,7 +39,11 @@ import { Logger } from '@config/logger.config';
|
|||||||
import { AuthenticationCreds, AuthenticationState, BufferJSON, initAuthCreds, proto, SignalDataTypeMap } from 'baileys';
|
import { AuthenticationCreds, AuthenticationState, BufferJSON, initAuthCreds, proto, SignalDataTypeMap } from 'baileys';
|
||||||
import { isNotEmpty } from 'class-validator';
|
import { isNotEmpty } from 'class-validator';
|
||||||
|
|
||||||
export type AuthState = { state: AuthenticationState; saveCreds: () => Promise<void> };
|
export type AuthState = {
|
||||||
|
state: AuthenticationState;
|
||||||
|
saveCreds: () => Promise<void>
|
||||||
|
removeCreds: () => Promise<void>;
|
||||||
|
};
|
||||||
|
|
||||||
export class AuthStateProvider {
|
export class AuthStateProvider {
|
||||||
constructor(private readonly providerFiles: ProviderFiles) {}
|
constructor(private readonly providerFiles: ProviderFiles) {}
|
||||||
@ -86,6 +90,19 @@ export class AuthStateProvider {
|
|||||||
return response;
|
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 });
|
||||||
|
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
const creds: AuthenticationCreds = (await readData('creds')) || initAuthCreds();
|
const creds: AuthenticationCreds = (await readData('creds')) || initAuthCreds();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -126,6 +143,10 @@ export class AuthStateProvider {
|
|||||||
saveCreds: async () => {
|
saveCreds: async () => {
|
||||||
return await writeData(creds, 'creds');
|
return await writeData(creds, 'creds');
|
||||||
},
|
},
|
||||||
|
|
||||||
|
removeCreds
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const logger = new Logger('useMultiFileAuthStatePrisma');
|
||||||
|
|||||||
@ -8,6 +8,7 @@ export async function useMultiFileAuthStateRedisDb(
|
|||||||
): Promise<{
|
): Promise<{
|
||||||
state: AuthenticationState;
|
state: AuthenticationState;
|
||||||
saveCreds: () => Promise<void>;
|
saveCreds: () => Promise<void>;
|
||||||
|
removeCreds: () => Promise<void>;
|
||||||
}> {
|
}> {
|
||||||
const logger = new Logger('useMultiFileAuthStateRedisDb');
|
const logger = new Logger('useMultiFileAuthStateRedisDb');
|
||||||
|
|
||||||
@ -36,6 +37,17 @@ export async function useMultiFileAuthStateRedisDb(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
async function removeCreds(): Promise<any> {
|
||||||
|
try {
|
||||||
|
|
||||||
|
logger.warn({ action: 'redis.delete', instanceName });
|
||||||
|
|
||||||
|
return await cache.delete(instanceName);
|
||||||
|
} catch {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const creds: AuthenticationCreds = (await readData('creds')) || initAuthCreds();
|
const creds: AuthenticationCreds = (await readData('creds')) || initAuthCreds();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -76,5 +88,7 @@ export async function useMultiFileAuthStateRedisDb(
|
|||||||
saveCreds: async () => {
|
saveCreds: async () => {
|
||||||
return await writeData(creds, 'creds');
|
return await writeData(creds, 'creds');
|
||||||
},
|
},
|
||||||
|
|
||||||
|
removeCreds
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user