mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-20 10:16:43 -06:00
ajustes proxy
This commit is contained in:
parent
a5477509bd
commit
589dec52a3
@ -23,6 +23,7 @@ import { WebsocketService } from '../services/websocket.service';
|
|||||||
import { BaileysStartupService } from '../services/whatsapp.baileys.service';
|
import { BaileysStartupService } from '../services/whatsapp.baileys.service';
|
||||||
import { BusinessStartupService } from '../services/whatsapp.business.service';
|
import { BusinessStartupService } from '../services/whatsapp.business.service';
|
||||||
import { Events, Integration, wa } from '../types/wa.types';
|
import { Events, Integration, wa } from '../types/wa.types';
|
||||||
|
import { ProxyController } from './proxy.controller';
|
||||||
|
|
||||||
export class InstanceController {
|
export class InstanceController {
|
||||||
constructor(
|
constructor(
|
||||||
@ -39,6 +40,7 @@ export class InstanceController {
|
|||||||
private readonly sqsService: SqsService,
|
private readonly sqsService: SqsService,
|
||||||
private readonly typebotService: TypebotService,
|
private readonly typebotService: TypebotService,
|
||||||
private readonly integrationService: IntegrationService,
|
private readonly integrationService: IntegrationService,
|
||||||
|
private readonly proxyService: ProxyController,
|
||||||
private readonly cache: RedisCache,
|
private readonly cache: RedisCache,
|
||||||
private readonly chatwootCache: CacheService,
|
private readonly chatwootCache: CacheService,
|
||||||
) {}
|
) {}
|
||||||
@ -85,6 +87,7 @@ export class InstanceController {
|
|||||||
typebot_delay_message,
|
typebot_delay_message,
|
||||||
typebot_unknown_message,
|
typebot_unknown_message,
|
||||||
typebot_listening_from_me,
|
typebot_listening_from_me,
|
||||||
|
proxy,
|
||||||
}: InstanceDto) {
|
}: InstanceDto) {
|
||||||
try {
|
try {
|
||||||
this.logger.verbose('requested createInstance from ' + instanceName + ' instance');
|
this.logger.verbose('requested createInstance from ' + instanceName + ' instance');
|
||||||
@ -346,6 +349,18 @@ export class InstanceController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (proxy) {
|
||||||
|
const testProxy = await this.proxyService.testProxy(proxy);
|
||||||
|
if (!testProxy) {
|
||||||
|
throw new BadRequestException('Invalid proxy');
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.proxyService.createProxy(instance, {
|
||||||
|
enabled: true,
|
||||||
|
proxy,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (typebot_url) {
|
if (typebot_url) {
|
||||||
try {
|
try {
|
||||||
if (!isURL(typebot_url, { require_tld: false })) {
|
if (!isURL(typebot_url, { require_tld: false })) {
|
||||||
|
@ -46,7 +46,7 @@ export class ProxyController {
|
|||||||
return this.proxyService.find(instance);
|
return this.proxyService.find(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async testProxy(proxy: ProxyDto['proxy']) {
|
public async testProxy(proxy: ProxyDto['proxy']) {
|
||||||
logger.verbose('requested testProxy');
|
logger.verbose('requested testProxy');
|
||||||
try {
|
try {
|
||||||
const serverIp = await axios.get('https://icanhazip.com/');
|
const serverIp = await axios.get('https://icanhazip.com/');
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import { WAPresence } from '@whiskeysockets/baileys';
|
import { WAPresence } from '@whiskeysockets/baileys';
|
||||||
|
|
||||||
|
import { ProxyDto } from './proxy.dto';
|
||||||
|
|
||||||
export class InstanceDto {
|
export class InstanceDto {
|
||||||
instanceName: string;
|
instanceName: string;
|
||||||
instanceId?: string;
|
instanceId?: string;
|
||||||
@ -41,7 +43,7 @@ export class InstanceDto {
|
|||||||
typebot_delay_message?: number;
|
typebot_delay_message?: number;
|
||||||
typebot_unknown_message?: string;
|
typebot_unknown_message?: string;
|
||||||
typebot_listening_from_me?: boolean;
|
typebot_listening_from_me?: boolean;
|
||||||
proxy?: string;
|
proxy?: ProxyDto['proxy'];
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SetPresenceDto {
|
export class SetPresenceDto {
|
||||||
|
@ -9,7 +9,6 @@ import {
|
|||||||
messageUpSchema,
|
messageUpSchema,
|
||||||
messageValidateSchema,
|
messageValidateSchema,
|
||||||
presenceSchema,
|
presenceSchema,
|
||||||
presenceOnlySchema,
|
|
||||||
privacySettingsSchema,
|
privacySettingsSchema,
|
||||||
profileNameSchema,
|
profileNameSchema,
|
||||||
profilePictureSchema,
|
profilePictureSchema,
|
||||||
|
@ -279,12 +279,15 @@ export class TypebotService {
|
|||||||
documentMessage: msg.documentMessage?.fileName,
|
documentMessage: msg.documentMessage?.fileName,
|
||||||
contactMessage: msg.contactMessage?.displayName,
|
contactMessage: msg.contactMessage?.displayName,
|
||||||
locationMessage: msg.locationMessage?.degreesLatitude,
|
locationMessage: msg.locationMessage?.degreesLatitude,
|
||||||
viewOnceMessageV2: msg.viewOnceMessageV2?.message?.imageMessage?.url || msg.viewOnceMessageV2?.message?.videoMessage?.url || msg.viewOnceMessageV2?.message?.audioMessage?.url,
|
viewOnceMessageV2:
|
||||||
|
msg.viewOnceMessageV2?.message?.imageMessage?.url ||
|
||||||
|
msg.viewOnceMessageV2?.message?.videoMessage?.url ||
|
||||||
|
msg.viewOnceMessageV2?.message?.audioMessage?.url,
|
||||||
listResponseMessage: msg.listResponseMessage?.singleSelectReply?.selectedRowId,
|
listResponseMessage: msg.listResponseMessage?.singleSelectReply?.selectedRowId,
|
||||||
responseRowId: msg.listResponseMessage?.singleSelectReply?.selectedRowId,
|
responseRowId: msg.listResponseMessage?.singleSelectReply?.selectedRowId,
|
||||||
};
|
};
|
||||||
|
|
||||||
const messageType = Object.keys(types).find(key => types[key] !== undefined) || 'unknown';
|
const messageType = Object.keys(types).find((key) => types[key] !== undefined) || 'unknown';
|
||||||
|
|
||||||
this.logger.verbose('Type message: ' + JSON.stringify(types));
|
this.logger.verbose('Type message: ' + JSON.stringify(types));
|
||||||
return { ...types, messageType };
|
return { ...types, messageType };
|
||||||
|
@ -12,7 +12,6 @@ import makeWASocket, {
|
|||||||
DisconnectReason,
|
DisconnectReason,
|
||||||
downloadMediaMessage,
|
downloadMediaMessage,
|
||||||
fetchLatestBaileysVersion,
|
fetchLatestBaileysVersion,
|
||||||
generateMobileNode,
|
|
||||||
generateWAMessageFromContent,
|
generateWAMessageFromContent,
|
||||||
getAggregateVotesInPollMessage,
|
getAggregateVotesInPollMessage,
|
||||||
getContentType,
|
getContentType,
|
||||||
@ -486,7 +485,7 @@ export class BaileysStartupService extends WAStartupService {
|
|||||||
let options;
|
let options;
|
||||||
|
|
||||||
if (this.localProxy.enabled) {
|
if (this.localProxy.enabled) {
|
||||||
this.logger.info('Proxy enabled: ' + this.localProxy.proxy);
|
this.logger.info('Proxy enabled: ' + this.localProxy.proxy.host);
|
||||||
|
|
||||||
if (this.localProxy?.proxy?.host?.includes('proxyscrape')) {
|
if (this.localProxy?.proxy?.host?.includes('proxyscrape')) {
|
||||||
try {
|
try {
|
||||||
@ -656,12 +655,27 @@ export class BaileysStartupService extends WAStartupService {
|
|||||||
let options;
|
let options;
|
||||||
|
|
||||||
if (this.localProxy.enabled) {
|
if (this.localProxy.enabled) {
|
||||||
this.logger.verbose('Proxy enabled');
|
this.logger.info('Proxy enabled: ' + this.localProxy.proxy.host);
|
||||||
|
|
||||||
|
if (this.localProxy?.proxy?.host?.includes('proxyscrape')) {
|
||||||
|
try {
|
||||||
|
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));
|
||||||
|
const proxyUrl = 'http://' + proxyUrls[rand];
|
||||||
|
options = {
|
||||||
|
agent: makeProxyAgent(proxyUrl),
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
this.localProxy.enabled = false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
options = {
|
options = {
|
||||||
agent: makeProxyAgent(this.localProxy.proxy),
|
agent: makeProxyAgent(this.localProxy.proxy),
|
||||||
fetchAgent: makeProxyAgent(this.localProxy.proxy),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const socketConfig: UserFacingSocketConfig = {
|
const socketConfig: UserFacingSocketConfig = {
|
||||||
...options,
|
...options,
|
||||||
@ -2147,7 +2161,18 @@ export class BaileysStartupService extends WAStartupService {
|
|||||||
mimetype = getMIMEType(mediaMessage.fileName);
|
mimetype = getMIMEType(mediaMessage.fileName);
|
||||||
|
|
||||||
if (!mimetype && isURL(mediaMessage.media)) {
|
if (!mimetype && isURL(mediaMessage.media)) {
|
||||||
const response = await axios.get(mediaMessage.media, { responseType: 'arraybuffer' });
|
let config: any = {
|
||||||
|
responseType: 'arraybuffer',
|
||||||
|
};
|
||||||
|
|
||||||
|
if (this.localProxy.enabled) {
|
||||||
|
config = {
|
||||||
|
...config,
|
||||||
|
httpsAgent: makeProxyAgent(this.localProxy.proxy),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await axios.get(mediaMessage.media, config);
|
||||||
|
|
||||||
mimetype = response.headers['content-type'];
|
mimetype = response.headers['content-type'];
|
||||||
}
|
}
|
||||||
@ -2216,7 +2241,18 @@ export class BaileysStartupService extends WAStartupService {
|
|||||||
const url = `${image}?timestamp=${timestamp}`;
|
const url = `${image}?timestamp=${timestamp}`;
|
||||||
this.logger.verbose('including timestamp in url: ' + url);
|
this.logger.verbose('including timestamp in url: ' + url);
|
||||||
|
|
||||||
const response = await axios.get(url, { responseType: 'arraybuffer' });
|
let config: any = {
|
||||||
|
responseType: 'arraybuffer',
|
||||||
|
};
|
||||||
|
|
||||||
|
if (this.localProxy.enabled) {
|
||||||
|
config = {
|
||||||
|
...config,
|
||||||
|
httpsAgent: makeProxyAgent(this.localProxy.proxy),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await axios.get(url, config);
|
||||||
this.logger.verbose('Getting image from url');
|
this.logger.verbose('Getting image from url');
|
||||||
|
|
||||||
const imageBuffer = Buffer.from(response.data, 'binary');
|
const imageBuffer = Buffer.from(response.data, 'binary');
|
||||||
@ -2286,7 +2322,18 @@ export class BaileysStartupService extends WAStartupService {
|
|||||||
|
|
||||||
this.logger.verbose('Including timestamp in url: ' + url);
|
this.logger.verbose('Including timestamp in url: ' + url);
|
||||||
|
|
||||||
const response = await axios.get(url, { responseType: 'arraybuffer' });
|
let config: any = {
|
||||||
|
responseType: 'arraybuffer',
|
||||||
|
};
|
||||||
|
|
||||||
|
if (this.localProxy.enabled) {
|
||||||
|
config = {
|
||||||
|
...config,
|
||||||
|
httpsAgent: makeProxyAgent(this.localProxy.proxy),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await axios.get(url, config);
|
||||||
this.logger.verbose('Getting audio from url');
|
this.logger.verbose('Getting audio from url');
|
||||||
|
|
||||||
fs.writeFileSync(tempAudioPath, response.data);
|
fs.writeFileSync(tempAudioPath, response.data);
|
||||||
@ -2920,7 +2967,18 @@ export class BaileysStartupService extends WAStartupService {
|
|||||||
const url = `${picture}?timestamp=${timestamp}`;
|
const url = `${picture}?timestamp=${timestamp}`;
|
||||||
this.logger.verbose('Including timestamp in url: ' + url);
|
this.logger.verbose('Including timestamp in url: ' + url);
|
||||||
|
|
||||||
pic = (await axios.get(url, { responseType: 'arraybuffer' })).data;
|
let config: any = {
|
||||||
|
responseType: 'arraybuffer',
|
||||||
|
};
|
||||||
|
|
||||||
|
if (this.localProxy.enabled) {
|
||||||
|
config = {
|
||||||
|
...config,
|
||||||
|
httpsAgent: makeProxyAgent(this.localProxy.proxy),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pic = (await axios.get(url, config)).data;
|
||||||
this.logger.verbose('Getting picture from url');
|
this.logger.verbose('Getting picture from url');
|
||||||
} else if (isBase64(picture)) {
|
} else if (isBase64(picture)) {
|
||||||
this.logger.verbose('Picture is base64');
|
this.logger.verbose('Picture is base64');
|
||||||
@ -3080,7 +3138,18 @@ export class BaileysStartupService extends WAStartupService {
|
|||||||
const url = `${picture.image}?timestamp=${timestamp}`;
|
const url = `${picture.image}?timestamp=${timestamp}`;
|
||||||
this.logger.verbose('Including timestamp in url: ' + url);
|
this.logger.verbose('Including timestamp in url: ' + url);
|
||||||
|
|
||||||
pic = (await axios.get(url, { responseType: 'arraybuffer' })).data;
|
let config: any = {
|
||||||
|
responseType: 'arraybuffer',
|
||||||
|
};
|
||||||
|
|
||||||
|
if (this.localProxy.enabled) {
|
||||||
|
config = {
|
||||||
|
...config,
|
||||||
|
httpsAgent: makeProxyAgent(this.localProxy.proxy),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pic = (await axios.get(url, config)).data;
|
||||||
this.logger.verbose('Getting picture from url');
|
this.logger.verbose('Getting picture from url');
|
||||||
} else if (isBase64(picture.image)) {
|
} else if (isBase64(picture.image)) {
|
||||||
this.logger.verbose('Picture is base64');
|
this.logger.verbose('Picture is base64');
|
||||||
|
@ -583,7 +583,7 @@ export class WAStartupService {
|
|||||||
this.logger.verbose(`Proxy enabled: ${this.localProxy.enabled}`);
|
this.logger.verbose(`Proxy enabled: ${this.localProxy.enabled}`);
|
||||||
|
|
||||||
this.localProxy.proxy = data?.proxy;
|
this.localProxy.proxy = data?.proxy;
|
||||||
this.logger.verbose(`Proxy proxy: ${this.localProxy.proxy}`);
|
this.logger.verbose(`Proxy proxy: ${this.localProxy.proxy.host}`);
|
||||||
|
|
||||||
this.logger.verbose('Proxy loaded');
|
this.logger.verbose('Proxy loaded');
|
||||||
}
|
}
|
||||||
|
@ -167,6 +167,7 @@ export const instanceController = new InstanceController(
|
|||||||
sqsService,
|
sqsService,
|
||||||
typebotService,
|
typebotService,
|
||||||
integrationService,
|
integrationService,
|
||||||
|
proxyController,
|
||||||
cache,
|
cache,
|
||||||
chatwootCache,
|
chatwootCache,
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user