diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f7f016d..d8a694c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# 1.6.2 (develop) + +### Fixed + +* Proxy configuration improvements +* Correction in sending lists + # 1.6.1 (2023-12-22 11:43) ### Fixed diff --git a/src/validate/validate.schema.ts b/src/validate/validate.schema.ts index 4b803873..1ccdf125 100644 --- a/src/validate/validate.schema.ts +++ b/src/validate/validate.schema.ts @@ -1086,7 +1086,18 @@ export const proxySchema: JSONSchema7 = { type: 'object', properties: { enabled: { type: 'boolean', enum: [true, false] }, - proxy: { type: 'string' }, + proxy: { + type: 'object', + properties: { + host: { type: 'string' }, + port: { type: 'string' }, + protocol: { type: 'string' }, + username: { type: 'string' }, + password: { type: 'string' }, + }, + required: ['host', 'port', 'protocol'], + ...isNotEmpty('host', 'port', 'protocol'), + }, }, required: ['enabled', 'proxy'], ...isNotEmpty('enabled', 'proxy'), diff --git a/src/whatsapp/controllers/instance.controller.ts b/src/whatsapp/controllers/instance.controller.ts index 0f06895e..0701742c 100644 --- a/src/whatsapp/controllers/instance.controller.ts +++ b/src/whatsapp/controllers/instance.controller.ts @@ -12,7 +12,6 @@ import { RepositoryBroker } from '../repository/repository.manager'; import { AuthService, OldToken } from '../services/auth.service'; import { ChatwootService } from '../services/chatwoot.service'; import { WAMonitoringService } from '../services/monitor.service'; -import { ProxyService } from '../services/proxy.service'; import { RabbitmqService } from '../services/rabbitmq.service'; import { SettingsService } from '../services/settings.service'; import { SqsService } from '../services/sqs.service'; @@ -34,7 +33,6 @@ export class InstanceController { private readonly settingsService: SettingsService, private readonly websocketService: WebsocketService, private readonly rabbitmqService: RabbitmqService, - private readonly proxyService: ProxyService, private readonly sqsService: SqsService, private readonly typebotService: TypebotService, private readonly cache: RedisCache, @@ -76,7 +74,6 @@ export class InstanceController { typebot_delay_message, typebot_unknown_message, typebot_listening_from_me, - proxy, }: InstanceDto) { try { this.logger.verbose('requested createInstance from ' + instanceName + ' instance'); @@ -259,22 +256,6 @@ export class InstanceController { } } - if (proxy) { - this.logger.verbose('creating proxy'); - try { - this.proxyService.create( - instance, - { - enabled: true, - proxy, - }, - false, - ); - } catch (error) { - this.logger.log(error); - } - } - let sqsEvents: string[]; if (sqs_enabled) { @@ -406,7 +387,6 @@ export class InstanceController { }, settings, qrcode: getQrcode, - proxy, }; this.logger.verbose('instance created'); @@ -510,7 +490,6 @@ export class InstanceController { name_inbox: instance.instanceName, webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(instance.instanceName)}`, }, - proxy, }; } catch (error) { this.logger.error(error.message[0]); diff --git a/src/whatsapp/controllers/proxy.controller.ts b/src/whatsapp/controllers/proxy.controller.ts index 1656d830..555c5975 100644 --- a/src/whatsapp/controllers/proxy.controller.ts +++ b/src/whatsapp/controllers/proxy.controller.ts @@ -1,4 +1,7 @@ +import axios from 'axios'; + import { Logger } from '../../config/logger.config'; +import { BadRequestException } from '../../exceptions'; import { InstanceDto } from '../dto/instance.dto'; import { ProxyDto } from '../dto/proxy.dto'; import { ProxyService } from '../services/proxy.service'; @@ -13,7 +16,16 @@ export class ProxyController { if (!data.enabled) { logger.verbose('proxy disabled'); - data.proxy = ''; + data.proxy = null; + } + + if (data.proxy) { + logger.verbose('proxy enabled'); + const { host, port, protocol, username, password } = data.proxy; + const testProxy = await this.testProxy(host, port, protocol, username, password); + if (!testProxy) { + throw new BadRequestException('Invalid proxy'); + } } return this.proxyService.create(instance, data); @@ -23,4 +35,36 @@ export class ProxyController { logger.verbose('requested findProxy from ' + instance.instanceName + ' instance'); return this.proxyService.find(instance); } + + private async testProxy(host: string, port: string, protocol: string, username?: string, password?: string) { + logger.verbose('requested testProxy'); + try { + let proxyConfig: any = { + host: host, + port: parseInt(port), + protocol: protocol, + }; + + if (username && password) { + proxyConfig = { + ...proxyConfig, + auth: { + username: username, + password: password, + }, + }; + } + const serverIp = await axios.get('http://meuip.com/api/meuip.php'); + + const response = await axios.get('http://meuip.com/api/meuip.php', { + proxy: proxyConfig, + }); + + logger.verbose('testProxy response: ' + response.data); + return response.data !== serverIp.data; + } catch (error) { + logger.error('testProxy error: ' + error); + return false; + } + } } diff --git a/src/whatsapp/dto/proxy.dto.ts b/src/whatsapp/dto/proxy.dto.ts index 0b6b2e70..7f3e7c06 100644 --- a/src/whatsapp/dto/proxy.dto.ts +++ b/src/whatsapp/dto/proxy.dto.ts @@ -1,4 +1,12 @@ +class Proxy { + host: string; + port: string; + protocol: string; + username?: string; + password?: string; +} + export class ProxyDto { enabled: boolean; - proxy: string; + proxy: Proxy; } diff --git a/src/whatsapp/models/proxy.model.ts b/src/whatsapp/models/proxy.model.ts index 3dea4f0c..4096f58f 100644 --- a/src/whatsapp/models/proxy.model.ts +++ b/src/whatsapp/models/proxy.model.ts @@ -2,16 +2,30 @@ import { Schema } from 'mongoose'; import { dbserver } from '../../libs/db.connect'; +class Proxy { + host?: string; + port?: string; + protocol?: string; + username?: string; + password?: string; +} + export class ProxyRaw { _id?: string; enabled?: boolean; - proxy?: string; + proxy?: Proxy; } const proxySchema = new Schema({ _id: { type: String, _id: true }, enabled: { type: Boolean, required: true }, - proxy: { type: String, required: true }, + proxy: { + host: { type: String, required: true }, + port: { type: String, required: true }, + protocol: { type: String, required: true }, + username: { type: String, required: false }, + password: { type: String, required: false }, + }, }); export const ProxyModel = dbserver?.model(ProxyRaw.name, proxySchema, 'proxy'); diff --git a/src/whatsapp/services/proxy.service.ts b/src/whatsapp/services/proxy.service.ts index 1039fd5c..66dc5342 100644 --- a/src/whatsapp/services/proxy.service.ts +++ b/src/whatsapp/services/proxy.service.ts @@ -27,7 +27,7 @@ export class ProxyService { return result; } catch (error) { - return { enabled: false, proxy: '' }; + return { enabled: false, proxy: null }; } } } diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index f222d5b7..98a6b067 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -1369,8 +1369,8 @@ export class WAStartupService { if (this.localProxy.enabled) { this.logger.info('Proxy enabled: ' + this.localProxy.proxy); - if (this.localProxy.proxy.includes('proxyscrape')) { - const response = await axios.get(this.localProxy.proxy); + if (this.localProxy.proxy.host.includes('proxyscrape')) { + const response = await axios.get(this.localProxy.proxy.host); const text = response.data; const proxyUrls = text.split('\r\n'); const rand = Math.floor(Math.random() * Math.floor(proxyUrls.length)); @@ -1379,8 +1379,15 @@ export class WAStartupService { agent: new ProxyAgent(proxyUrl as any), }; } else { + let proxyUri = + this.localProxy.proxy.protocol + '://' + this.localProxy.proxy.host + ':' + this.localProxy.proxy.port; + + if (this.localProxy.proxy.username && this.localProxy.proxy.password) { + proxyUri = `${this.localProxy.proxy.username}:${this.localProxy.proxy.password}@${proxyUri}`; + } + options = { - agent: new ProxyAgent(this.localProxy.proxy as any), + agent: new ProxyAgent(proxyUri as any), }; } } @@ -1903,7 +1910,8 @@ export class WAStartupService { this.logger.verbose('group ignored'); return; } - if (key.remoteJid !== 'status@broadcast' && !key?.remoteJid?.match(/(:\d+)/)) { + // if (key.remoteJid !== 'status@broadcast' && !key?.remoteJid?.match(/(:\d+)/)) { + if (key.remoteJid !== 'status@broadcast') { this.logger.verbose('Message update is valid'); let pollUpdates: any; diff --git a/src/whatsapp/types/wa.types.ts b/src/whatsapp/types/wa.types.ts index 5adf9ca2..9f3cb890 100644 --- a/src/whatsapp/types/wa.types.ts +++ b/src/whatsapp/types/wa.types.ts @@ -109,9 +109,17 @@ export declare namespace wa { sessions?: Session[]; }; + type Proxy = { + host?: string; + port?: string; + protocol?: string; + username?: string; + password?: string; + }; + export type LocalProxy = { enabled?: boolean; - proxy?: string; + proxy?: Proxy; }; export type LocalChamaai = { diff --git a/src/whatsapp/whatsapp.module.ts b/src/whatsapp/whatsapp.module.ts index e89d4f56..d459bf6a 100644 --- a/src/whatsapp/whatsapp.module.ts +++ b/src/whatsapp/whatsapp.module.ts @@ -148,7 +148,6 @@ export const instanceController = new InstanceController( settingsService, websocketService, rabbitmqService, - proxyService, sqsService, typebotService, cache,