mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-25 22:57:44 -06:00
Merge pull request #374 from judsonjuniorr/proxy-improvements
Proxy improvements
This commit is contained in:
@@ -1,19 +1,25 @@
|
||||
import axios from 'axios';
|
||||
|
||||
import { Logger } from '../../config/logger.config';
|
||||
import { BadRequestException } from '../../exceptions';
|
||||
import { BadRequestException, NotFoundException } from '../../exceptions';
|
||||
import { makeProxyAgent } from '../../utils/makeProxyAgent';
|
||||
import { InstanceDto } from '../dto/instance.dto';
|
||||
import { ProxyDto } from '../dto/proxy.dto';
|
||||
import { WAMonitoringService } from '../services/monitor.service';
|
||||
import { ProxyService } from '../services/proxy.service';
|
||||
|
||||
const logger = new Logger('ProxyController');
|
||||
|
||||
export class ProxyController {
|
||||
constructor(private readonly proxyService: ProxyService) {}
|
||||
constructor(private readonly proxyService: ProxyService, private readonly waMonitor: WAMonitoringService) {}
|
||||
|
||||
public async createProxy(instance: InstanceDto, data: ProxyDto) {
|
||||
logger.verbose('requested createProxy from ' + instance.instanceName + ' instance');
|
||||
|
||||
if (!this.waMonitor.waInstances[instance.instanceName]) {
|
||||
throw new NotFoundException(`The "${instance.instanceName}" instance does not exist`);
|
||||
}
|
||||
|
||||
if (!data.enabled) {
|
||||
logger.verbose('proxy disabled');
|
||||
data.proxy = null;
|
||||
@@ -21,8 +27,7 @@ export class ProxyController {
|
||||
|
||||
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);
|
||||
const testProxy = await this.testProxy(data.proxy);
|
||||
if (!testProxy) {
|
||||
throw new BadRequestException('Invalid proxy');
|
||||
}
|
||||
@@ -33,37 +38,30 @@ export class ProxyController {
|
||||
|
||||
public async findProxy(instance: InstanceDto) {
|
||||
logger.verbose('requested findProxy from ' + instance.instanceName + ' instance');
|
||||
|
||||
if (!this.waMonitor.waInstances[instance.instanceName]) {
|
||||
throw new NotFoundException(`The "${instance.instanceName}" instance does not exist`);
|
||||
}
|
||||
|
||||
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');
|
||||
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,
|
||||
const serverIp = await axios.get('https://icanhazip.com/');
|
||||
const response = await axios.get('https://icanhazip.com/', {
|
||||
httpsAgent: makeProxyAgent(proxy),
|
||||
});
|
||||
|
||||
logger.verbose('testProxy response: ' + response.data);
|
||||
return response.data !== serverIp.data;
|
||||
} catch (error) {
|
||||
logger.error('testProxy error: ' + error);
|
||||
let errorMessage = error;
|
||||
if (axios.isAxiosError(error) && error.response.data) {
|
||||
errorMessage = error.response.data;
|
||||
}
|
||||
logger.error('testProxy error: ' + errorMessage);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user