mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-14 01:41:24 -06:00
66 lines
2.1 KiB
TypeScript
66 lines
2.1 KiB
TypeScript
import { InstanceDto } from '@api/dto/instance.dto';
|
|
import { ProxyDto } from '@api/dto/proxy.dto';
|
|
import { WAMonitoringService } from '@api/services/monitor.service';
|
|
import { ProxyService } from '@api/services/proxy.service';
|
|
import { Logger } from '@config/logger.config';
|
|
import { BadRequestException, NotFoundException } from '@exceptions';
|
|
import { makeProxyAgent } from '@utils/makeProxyAgent';
|
|
import axios from 'axios';
|
|
|
|
const logger = new Logger('ProxyController');
|
|
|
|
export class ProxyController {
|
|
constructor(private readonly proxyService: ProxyService, private readonly waMonitor: WAMonitoringService) {}
|
|
|
|
public async createProxy(instance: InstanceDto, data: ProxyDto) {
|
|
if (!this.waMonitor.waInstances[instance.instanceName]) {
|
|
throw new NotFoundException(`The "${instance.instanceName}" instance does not exist`);
|
|
}
|
|
|
|
if (!data.enabled) {
|
|
data.host = '';
|
|
data.port = '';
|
|
data.protocol = '';
|
|
data.username = '';
|
|
data.password = '';
|
|
}
|
|
|
|
if (data.host) {
|
|
const testProxy = await this.testProxy(data);
|
|
if (!testProxy) {
|
|
throw new BadRequestException('Invalid proxy');
|
|
}
|
|
}
|
|
|
|
return this.proxyService.create(instance, data);
|
|
}
|
|
|
|
public async findProxy(instance: InstanceDto) {
|
|
if (!this.waMonitor.waInstances[instance.instanceName]) {
|
|
throw new NotFoundException(`The "${instance.instanceName}" instance does not exist`);
|
|
}
|
|
|
|
return this.proxyService.find(instance);
|
|
}
|
|
|
|
public async testProxy(proxy: ProxyDto) {
|
|
try {
|
|
const serverIp = await axios.get('https://icanhazip.com/');
|
|
const response = await axios.get('https://icanhazip.com/', {
|
|
httpsAgent: makeProxyAgent(proxy),
|
|
});
|
|
|
|
return response?.data !== serverIp?.data;
|
|
} catch (error) {
|
|
if (axios.isAxiosError(error) && error.response?.data) {
|
|
logger.error('testProxy error: ' + error.response.data);
|
|
} else if (axios.isAxiosError(error)) {
|
|
logger.error('testProxy error: ');
|
|
} else {
|
|
logger.error('testProxy error: ');
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|