mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-21 12:52:19 -06:00
378 lines
13 KiB
TypeScript
378 lines
13 KiB
TypeScript
import { opendirSync, readdirSync, rmSync } from 'fs';
|
|
import { WAStartupService } from './whatsapp.service';
|
|
import { INSTANCE_DIR, STORE_DIR } from '../../config/path.config';
|
|
import EventEmitter2 from 'eventemitter2';
|
|
import { join } from 'path';
|
|
import { Logger } from '../../config/logger.config';
|
|
import {
|
|
Auth,
|
|
ConfigService,
|
|
Database,
|
|
DelInstance,
|
|
HttpServer,
|
|
Redis,
|
|
} from '../../config/env.config';
|
|
import { RepositoryBroker } from '../repository/repository.manager';
|
|
import { NotFoundException } from '../../exceptions';
|
|
import { Db } from 'mongodb';
|
|
import { initInstance } from '../whatsapp.module';
|
|
import { RedisCache } from '../../db/redis.client';
|
|
import { execSync } from 'child_process';
|
|
|
|
export class WAMonitoringService {
|
|
constructor(
|
|
private readonly eventEmitter: EventEmitter2,
|
|
private readonly configService: ConfigService,
|
|
private readonly repository: RepositoryBroker,
|
|
private readonly cache: RedisCache,
|
|
) {
|
|
this.logger.verbose('instance created');
|
|
|
|
this.removeInstance();
|
|
this.noConnection();
|
|
this.delInstanceFiles();
|
|
|
|
Object.assign(this.db, configService.get<Database>('DATABASE'));
|
|
Object.assign(this.redis, configService.get<Redis>('REDIS'));
|
|
|
|
this.dbInstance = this.db.ENABLED
|
|
? this.repository.dbServer?.db(this.db.CONNECTION.DB_PREFIX_NAME + '-instances')
|
|
: undefined;
|
|
}
|
|
|
|
private readonly db: Partial<Database> = {};
|
|
private readonly redis: Partial<Redis> = {};
|
|
|
|
private dbInstance: Db;
|
|
|
|
private readonly logger = new Logger(WAMonitoringService.name);
|
|
public readonly waInstances: Record<string, WAStartupService> = {};
|
|
|
|
public delInstanceTime(instance: string) {
|
|
const time = this.configService.get<DelInstance>('DEL_INSTANCE');
|
|
if (typeof time === 'number' && time > 0) {
|
|
this.logger.verbose(
|
|
`Instance "${instance}" don't have connection, will be removed in ${time} minutes`,
|
|
);
|
|
|
|
setTimeout(async () => {
|
|
if (this.waInstances[instance]?.connectionStatus?.state !== 'open') {
|
|
if (this.waInstances[instance]?.connectionStatus?.state === 'connecting') {
|
|
await this.waInstances[instance]?.client?.logout(
|
|
'Log out instance: ' + instance,
|
|
);
|
|
this.waInstances[instance]?.client?.ws?.close();
|
|
this.waInstances[instance]?.client?.end(undefined);
|
|
delete this.waInstances[instance];
|
|
} else {
|
|
delete this.waInstances[instance];
|
|
this.eventEmitter.emit('remove.instance', instance, 'inner');
|
|
}
|
|
}
|
|
}, 1000 * 60 * time);
|
|
}
|
|
}
|
|
|
|
public async instanceInfo(instanceName?: string) {
|
|
this.logger.verbose('get instance info');
|
|
if (instanceName && !this.waInstances[instanceName]) {
|
|
throw new NotFoundException(`Instance "${instanceName}" not found`);
|
|
}
|
|
|
|
const instances: any[] = [];
|
|
|
|
for await (const [key, value] of Object.entries(this.waInstances)) {
|
|
if (value) {
|
|
this.logger.verbose('get instance info: ' + key);
|
|
let chatwoot: any;
|
|
|
|
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
|
|
|
const findChatwoot = await this.waInstances[key].findChatwoot();
|
|
|
|
if (findChatwoot.enabled) {
|
|
chatwoot = {
|
|
...findChatwoot,
|
|
webhook_url: `${urlServer}/chatwoot/webhook/${key}`,
|
|
};
|
|
}
|
|
|
|
if (value.connectionStatus.state === 'open') {
|
|
this.logger.verbose('instance: ' + key + ' - connectionStatus: open');
|
|
let apikey: string;
|
|
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
|
|
this.logger.verbose(
|
|
'instance: ' + key + ' - hash exposed in fetch instances',
|
|
);
|
|
const tokenStore = await this.repository.auth.find(key);
|
|
apikey = tokenStore.apikey || 'Apikey not found';
|
|
|
|
instances.push({
|
|
instance: {
|
|
instanceName: key,
|
|
owner: value.wuid,
|
|
profileName: (await value.getProfileName()) || 'not loaded',
|
|
profilePictureUrl: value.profilePictureUrl,
|
|
profileStatus: (await value.getProfileStatus()) || '',
|
|
status: value.connectionStatus.state,
|
|
apikey,
|
|
chatwoot,
|
|
},
|
|
});
|
|
} else {
|
|
this.logger.verbose(
|
|
'instance: ' + key + ' - hash not exposed in fetch instances',
|
|
);
|
|
instances.push({
|
|
instance: {
|
|
instanceName: key,
|
|
owner: value.wuid,
|
|
profileName: (await value.getProfileName()) || 'not loaded',
|
|
profilePictureUrl: value.profilePictureUrl,
|
|
profileStatus: (await value.getProfileStatus()) || '',
|
|
status: value.connectionStatus.state,
|
|
},
|
|
});
|
|
}
|
|
} else {
|
|
this.logger.verbose(
|
|
'instance: ' + key + ' - connectionStatus: ' + value.connectionStatus.state,
|
|
);
|
|
let apikey: string;
|
|
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
|
|
this.logger.verbose(
|
|
'instance: ' + key + ' - hash exposed in fetch instances',
|
|
);
|
|
const tokenStore = await this.repository.auth.find(key);
|
|
apikey = tokenStore.apikey || 'Apikey not found';
|
|
|
|
instances.push({
|
|
instance: {
|
|
instanceName: key,
|
|
status: value.connectionStatus.state,
|
|
apikey,
|
|
chatwoot,
|
|
},
|
|
});
|
|
} else {
|
|
this.logger.verbose(
|
|
'instance: ' + key + ' - hash not exposed in fetch instances',
|
|
);
|
|
instances.push({
|
|
instance: {
|
|
instanceName: key,
|
|
status: value.connectionStatus.state,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
this.logger.verbose('return instance info: ' + instances.length);
|
|
|
|
return instances.find((i) => i.instance.instanceName === instanceName) ?? instances;
|
|
}
|
|
|
|
private delInstanceFiles() {
|
|
this.logger.verbose('cron to delete instance files started');
|
|
setInterval(async () => {
|
|
if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
|
|
const collections = await this.dbInstance.collections();
|
|
collections.forEach(async (collection) => {
|
|
const name = collection.namespace.replace(/^[\w-]+./, '');
|
|
await this.dbInstance.collection(name).deleteMany({
|
|
$or: [
|
|
{ _id: { $regex: /^app.state.*/ } },
|
|
{ _id: { $regex: /^session-.*/ } },
|
|
],
|
|
});
|
|
this.logger.verbose('instance files deleted: ' + name);
|
|
});
|
|
} else if (this.redis.ENABLED) {
|
|
} else {
|
|
const dir = opendirSync(INSTANCE_DIR, { encoding: 'utf-8' });
|
|
for await (const dirent of dir) {
|
|
if (dirent.isDirectory()) {
|
|
const files = readdirSync(join(INSTANCE_DIR, dirent.name), {
|
|
encoding: 'utf-8',
|
|
});
|
|
files.forEach(async (file) => {
|
|
if (file.match(/^app.state.*/) || file.match(/^session-.*/)) {
|
|
rmSync(join(INSTANCE_DIR, dirent.name, file), {
|
|
recursive: true,
|
|
force: true,
|
|
});
|
|
}
|
|
});
|
|
this.logger.verbose('instance files deleted: ' + dirent.name);
|
|
}
|
|
}
|
|
}
|
|
}, 3600 * 1000 * 2);
|
|
}
|
|
|
|
public async cleaningUp(instanceName: string) {
|
|
this.logger.verbose('cleaning up instance: ' + instanceName);
|
|
if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
|
|
this.logger.verbose('cleaning up instance in database: ' + instanceName);
|
|
await this.repository.dbServer.connect();
|
|
const collections: any[] = await this.dbInstance.collections();
|
|
if (collections.length > 0) {
|
|
await this.dbInstance.dropCollection(instanceName);
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (this.redis.ENABLED) {
|
|
this.logger.verbose('cleaning up instance in redis: ' + instanceName);
|
|
this.cache.reference = instanceName;
|
|
await this.cache.delAll();
|
|
return;
|
|
}
|
|
|
|
this.logger.verbose('cleaning up instance in files: ' + instanceName);
|
|
rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true });
|
|
}
|
|
|
|
public async cleaningStoreFiles(instanceName: string) {
|
|
this.logger.verbose('cleaning store files instance: ' + instanceName);
|
|
|
|
if (!this.db.ENABLED) {
|
|
const instance = this.waInstances[instanceName];
|
|
|
|
rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true });
|
|
|
|
execSync(`rm -rf ${join(STORE_DIR, 'chats', instanceName)}`);
|
|
execSync(`rm -rf ${join(STORE_DIR, 'contacts', instanceName)}`);
|
|
execSync(`rm -rf ${join(STORE_DIR, 'message-up', instanceName)}`);
|
|
execSync(`rm -rf ${join(STORE_DIR, 'messages', instanceName)}`);
|
|
|
|
execSync(`rm -rf ${join(STORE_DIR, 'auth', 'apikey', instanceName + '.json')}`);
|
|
execSync(`rm -rf ${join(STORE_DIR, 'webhook', instanceName + '.json')}`);
|
|
execSync(`rm -rf ${join(STORE_DIR, 'chatwoot', instanceName + '*')}`);
|
|
}
|
|
}
|
|
|
|
public async loadInstance() {
|
|
this.logger.verbose('load instances');
|
|
const set = async (name: string) => {
|
|
const instance = new WAStartupService(
|
|
this.configService,
|
|
this.eventEmitter,
|
|
this.repository,
|
|
this.cache,
|
|
);
|
|
instance.instanceName = name;
|
|
this.logger.verbose('instance loaded: ' + name);
|
|
|
|
await instance.connectToWhatsapp();
|
|
this.logger.verbose('connectToWhatsapp: ' + name);
|
|
|
|
this.waInstances[name] = instance;
|
|
};
|
|
|
|
try {
|
|
if (this.redis.ENABLED) {
|
|
this.logger.verbose('redis enabled');
|
|
await this.cache.connect(this.redis as Redis);
|
|
const keys = await this.cache.instanceKeys();
|
|
if (keys?.length > 0) {
|
|
this.logger.verbose('reading instance keys and setting instances');
|
|
keys.forEach(async (k) => await set(k.split(':')[1]));
|
|
} else {
|
|
this.logger.verbose('no instance keys found');
|
|
initInstance();
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
|
|
this.logger.verbose('database enabled');
|
|
await this.repository.dbServer.connect();
|
|
const collections: any[] = await this.dbInstance.collections();
|
|
if (collections.length > 0) {
|
|
this.logger.verbose('reading collections and setting instances');
|
|
collections.forEach(
|
|
async (coll) => await set(coll.namespace.replace(/^[\w-]+\./, '')),
|
|
);
|
|
} else {
|
|
this.logger.verbose('no collections found');
|
|
initInstance();
|
|
}
|
|
return;
|
|
}
|
|
|
|
this.logger.verbose('store in files enabled');
|
|
const dir = opendirSync(INSTANCE_DIR, { encoding: 'utf-8' });
|
|
for await (const dirent of dir) {
|
|
if (dirent.isDirectory()) {
|
|
this.logger.verbose('reading instance files and setting instances');
|
|
const files = readdirSync(join(INSTANCE_DIR, dirent.name), {
|
|
encoding: 'utf-8',
|
|
});
|
|
if (files.length === 0) {
|
|
rmSync(join(INSTANCE_DIR, dirent.name), { recursive: true, force: true });
|
|
break;
|
|
}
|
|
|
|
await set(dirent.name);
|
|
} else {
|
|
this.logger.verbose('no instance files found');
|
|
initInstance();
|
|
}
|
|
}
|
|
} catch (error) {
|
|
this.logger.error(error);
|
|
}
|
|
}
|
|
|
|
private removeInstance() {
|
|
this.eventEmitter.on('remove.instance', async (instanceName: string) => {
|
|
this.logger.verbose('remove instance: ' + instanceName);
|
|
try {
|
|
this.logger.verbose('instance: ' + instanceName + ' - removing from memory');
|
|
this.waInstances[instanceName] = undefined;
|
|
} catch {}
|
|
|
|
try {
|
|
this.logger.verbose('request cleaning up instance: ' + instanceName);
|
|
this.cleaningUp(instanceName);
|
|
this.cleaningStoreFiles(instanceName);
|
|
} finally {
|
|
this.logger.warn(`Instance "${instanceName}" - REMOVED`);
|
|
}
|
|
});
|
|
this.eventEmitter.on('logout.instance', async (instanceName: string) => {
|
|
this.logger.verbose('logout instance: ' + instanceName);
|
|
try {
|
|
this.logger.verbose('request cleaning up instance: ' + instanceName);
|
|
this.cleaningUp(instanceName);
|
|
} finally {
|
|
this.logger.warn(`Instance "${instanceName}" - LOGOUT`);
|
|
}
|
|
});
|
|
}
|
|
|
|
private noConnection() {
|
|
this.logger.verbose('checking instances without connection');
|
|
this.eventEmitter.on('no.connection', async (instanceName) => {
|
|
try {
|
|
this.logger.verbose('instance: ' + instanceName + ' - removing from memory');
|
|
this.waInstances[instanceName] = undefined;
|
|
|
|
this.logger.verbose('request cleaning up instance: ' + instanceName);
|
|
this.cleaningUp(instanceName);
|
|
} catch (error) {
|
|
this.logger.error({
|
|
localError: 'noConnection',
|
|
warn: 'Error deleting instance from memory.',
|
|
error,
|
|
});
|
|
} finally {
|
|
this.logger.warn(`Instance "${instanceName}" - NOT CONNECTION`);
|
|
}
|
|
});
|
|
}
|
|
}
|