mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-14 09:51:24 -06:00
Remove unused dependencies and refactor proxy handling
In some tests made on the testProxy function, the `new ProxyAgent()` was not working, even adding it the IP result was the same. I've change it to the `new HttpsProxyAgent()` and it worked. Refactored the proxy agent to center the same function/creation the same where it is used
This commit is contained in:
parent
82e111f1be
commit
5292e569d9
@ -74,7 +74,6 @@
|
|||||||
"node-mime-types": "^1.1.0",
|
"node-mime-types": "^1.1.0",
|
||||||
"node-windows": "^1.0.0-beta.8",
|
"node-windows": "^1.0.0-beta.8",
|
||||||
"pino": "^8.11.0",
|
"pino": "^8.11.0",
|
||||||
"proxy-agent": "^6.3.0",
|
|
||||||
"qrcode": "^1.5.1",
|
"qrcode": "^1.5.1",
|
||||||
"qrcode-terminal": "^0.12.0",
|
"qrcode-terminal": "^0.12.0",
|
||||||
"redis": "^4.6.5",
|
"redis": "^4.6.5",
|
||||||
|
17
src/utils/makeProxyAgent.ts
Normal file
17
src/utils/makeProxyAgent.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import { HttpsProxyAgent } from 'https-proxy-agent';
|
||||||
|
|
||||||
|
import { wa } from '../whatsapp/types/wa.types';
|
||||||
|
|
||||||
|
export function makeProxyAgent(proxy: wa.Proxy | string) {
|
||||||
|
if (typeof proxy === 'string') {
|
||||||
|
return new HttpsProxyAgent(proxy);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { host, password, port, protocol, username } = proxy;
|
||||||
|
let proxyUrl = `${protocol}://${host}:${port}`;
|
||||||
|
|
||||||
|
if (username && password) {
|
||||||
|
proxyUrl = `${protocol}://${username}:${password}@${host}:${port}`;
|
||||||
|
}
|
||||||
|
return new HttpsProxyAgent(proxyUrl);
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { HttpsProxyAgent } from 'https-proxy-agent';
|
|
||||||
|
|
||||||
import { Logger } from '../../config/logger.config';
|
import { Logger } from '../../config/logger.config';
|
||||||
import { BadRequestException, NotFoundException } from '../../exceptions';
|
import { BadRequestException, NotFoundException } from '../../exceptions';
|
||||||
|
import { makeProxyAgent } from '../../utils/makeProxyAgent';
|
||||||
import { InstanceDto } from '../dto/instance.dto';
|
import { InstanceDto } from '../dto/instance.dto';
|
||||||
import { ProxyDto } from '../dto/proxy.dto';
|
import { ProxyDto } from '../dto/proxy.dto';
|
||||||
import { WAMonitoringService } from '../services/monitor.service';
|
import { WAMonitoringService } from '../services/monitor.service';
|
||||||
@ -27,8 +27,7 @@ export class ProxyController {
|
|||||||
|
|
||||||
if (data.proxy) {
|
if (data.proxy) {
|
||||||
logger.verbose('proxy enabled');
|
logger.verbose('proxy enabled');
|
||||||
const { host, port, protocol, username, password } = data.proxy;
|
const testProxy = await this.testProxy(data.proxy);
|
||||||
const testProxy = await this.testProxy(host, port, protocol, username, password);
|
|
||||||
if (!testProxy) {
|
if (!testProxy) {
|
||||||
throw new BadRequestException('Invalid proxy');
|
throw new BadRequestException('Invalid proxy');
|
||||||
}
|
}
|
||||||
@ -47,18 +46,12 @@ export class ProxyController {
|
|||||||
return this.proxyService.find(instance);
|
return this.proxyService.find(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async testProxy(host: string, port: string, protocol: string, username?: string, password?: string) {
|
private async testProxy(proxy: ProxyDto['proxy']) {
|
||||||
logger.verbose('requested testProxy');
|
logger.verbose('requested testProxy');
|
||||||
try {
|
try {
|
||||||
let proxyUrl = `${protocol}://${host}:${port}`;
|
|
||||||
|
|
||||||
if (username && password) {
|
|
||||||
proxyUrl = `${protocol}://${username}:${password}@${host}:${port}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const serverIp = await axios.get('https://icanhazip.com/');
|
const serverIp = await axios.get('https://icanhazip.com/');
|
||||||
const response = await axios.get('https://icanhazip.com/', {
|
const response = await axios.get('https://icanhazip.com/', {
|
||||||
httpsAgent: new HttpsProxyAgent(proxyUrl),
|
httpsAgent: makeProxyAgent(proxy),
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.verbose('testProxy response: ' + response.data);
|
logger.verbose('testProxy response: ' + response.data);
|
||||||
|
@ -45,7 +45,6 @@ import { getMIMEType } from 'node-mime-types';
|
|||||||
import { release } from 'os';
|
import { release } from 'os';
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import P from 'pino';
|
import P from 'pino';
|
||||||
import { ProxyAgent } from 'proxy-agent';
|
|
||||||
import qrcode, { QRCodeToDataURLOptions } from 'qrcode';
|
import qrcode, { QRCodeToDataURLOptions } from 'qrcode';
|
||||||
import qrcodeTerminal from 'qrcode-terminal';
|
import qrcodeTerminal from 'qrcode-terminal';
|
||||||
import sharp from 'sharp';
|
import sharp from 'sharp';
|
||||||
@ -73,6 +72,7 @@ import { dbserver } from '../../libs/db.connect';
|
|||||||
import { RedisCache } from '../../libs/redis.client';
|
import { RedisCache } from '../../libs/redis.client';
|
||||||
import { getIO } from '../../libs/socket.server';
|
import { getIO } from '../../libs/socket.server';
|
||||||
import { getSQS, removeQueues as removeQueuesSQS } from '../../libs/sqs.server';
|
import { getSQS, removeQueues as removeQueuesSQS } from '../../libs/sqs.server';
|
||||||
|
import { makeProxyAgent } from '../../utils/makeProxyAgent';
|
||||||
import { useMultiFileAuthStateDb } from '../../utils/use-multi-file-auth-state-db';
|
import { useMultiFileAuthStateDb } from '../../utils/use-multi-file-auth-state-db';
|
||||||
import { useMultiFileAuthStateRedisDb } from '../../utils/use-multi-file-auth-state-redis-db';
|
import { useMultiFileAuthStateRedisDb } from '../../utils/use-multi-file-auth-state-redis-db';
|
||||||
import {
|
import {
|
||||||
@ -1391,21 +1391,14 @@ export class WAStartupService {
|
|||||||
const rand = Math.floor(Math.random() * Math.floor(proxyUrls.length));
|
const rand = Math.floor(Math.random() * Math.floor(proxyUrls.length));
|
||||||
const proxyUrl = 'http://' + proxyUrls[rand];
|
const proxyUrl = 'http://' + proxyUrls[rand];
|
||||||
options = {
|
options = {
|
||||||
agent: new ProxyAgent(proxyUrl as any),
|
agent: makeProxyAgent(proxyUrl),
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.localProxy.enabled = false;
|
this.localProxy.enabled = false;
|
||||||
}
|
}
|
||||||
} else {
|
} 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 = {
|
options = {
|
||||||
agent: new ProxyAgent(proxyUri as any),
|
agent: makeProxyAgent(this.localProxy.proxy),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1492,8 +1485,8 @@ export class WAStartupService {
|
|||||||
if (this.localProxy.enabled) {
|
if (this.localProxy.enabled) {
|
||||||
this.logger.verbose('Proxy enabled');
|
this.logger.verbose('Proxy enabled');
|
||||||
options = {
|
options = {
|
||||||
agent: new ProxyAgent(this.localProxy.proxy as any),
|
agent: makeProxyAgent(this.localProxy.proxy),
|
||||||
fetchAgent: new ProxyAgent(this.localProxy.proxy as any),
|
fetchAgent: makeProxyAgent(this.localProxy.proxy),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user