mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-15 11:42:53 -06:00
Add https-proxy-agent dependency and handle NotFoundException in ProxyController
This commit is contained in:
parent
440ff2f3ea
commit
35520d85a2
@ -61,6 +61,7 @@
|
|||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"express-async-errors": "^3.1.1",
|
"express-async-errors": "^3.1.1",
|
||||||
"hbs": "^4.2.0",
|
"hbs": "^4.2.0",
|
||||||
|
"https-proxy-agent": "^7.0.2",
|
||||||
"jimp": "^0.16.13",
|
"jimp": "^0.16.13",
|
||||||
"join": "^3.0.0",
|
"join": "^3.0.0",
|
||||||
"js-yaml": "^4.1.0",
|
"js-yaml": "^4.1.0",
|
||||||
|
@ -1,19 +1,25 @@
|
|||||||
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 } from '../../exceptions';
|
import { BadRequestException, NotFoundException } from '../../exceptions';
|
||||||
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 { ProxyService } from '../services/proxy.service';
|
import { ProxyService } from '../services/proxy.service';
|
||||||
|
|
||||||
const logger = new Logger('ProxyController');
|
const logger = new Logger('ProxyController');
|
||||||
|
|
||||||
export class 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) {
|
public async createProxy(instance: InstanceDto, data: ProxyDto) {
|
||||||
logger.verbose('requested createProxy from ' + instance.instanceName + ' instance');
|
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) {
|
if (!data.enabled) {
|
||||||
logger.verbose('proxy disabled');
|
logger.verbose('proxy disabled');
|
||||||
data.proxy = null;
|
data.proxy = null;
|
||||||
@ -33,37 +39,36 @@ export class ProxyController {
|
|||||||
|
|
||||||
public async findProxy(instance: InstanceDto) {
|
public async findProxy(instance: InstanceDto) {
|
||||||
logger.verbose('requested findProxy from ' + instance.instanceName + ' instance');
|
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);
|
return this.proxyService.find(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async testProxy(host: string, port: string, protocol: string, username?: string, password?: string) {
|
private async testProxy(host: string, port: string, protocol: string, username?: string, password?: string) {
|
||||||
logger.verbose('requested testProxy');
|
logger.verbose('requested testProxy');
|
||||||
try {
|
try {
|
||||||
let proxyConfig: any = {
|
let proxyUrl = `${protocol}://${host}:${port}`;
|
||||||
host: host,
|
|
||||||
port: parseInt(port),
|
|
||||||
protocol: protocol,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (username && password) {
|
if (username && password) {
|
||||||
proxyConfig = {
|
proxyUrl = `${protocol}://${username}:${password}@${host}:${port}`;
|
||||||
...proxyConfig,
|
|
||||||
auth: {
|
|
||||||
username: username,
|
|
||||||
password: password,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
const serverIp = await axios.get('http://meuip.com/api/meuip.php');
|
const serverIp = await axios.get('http://meuip.com/api/meuip.php');
|
||||||
|
|
||||||
const response = await axios.get('http://meuip.com/api/meuip.php', {
|
const response = await axios.get('http://meuip.com/api/meuip.php', {
|
||||||
proxy: proxyConfig,
|
httpsAgent: new HttpsProxyAgent(proxyUrl),
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.verbose('testProxy response: ' + response.data);
|
logger.verbose('testProxy response: ' + response.data);
|
||||||
return response.data !== serverIp.data;
|
return response.data !== serverIp.data;
|
||||||
} catch (error) {
|
} 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;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -119,7 +119,7 @@ export const websocketController = new WebsocketController(websocketService);
|
|||||||
|
|
||||||
const proxyService = new ProxyService(waMonitor);
|
const proxyService = new ProxyService(waMonitor);
|
||||||
|
|
||||||
export const proxyController = new ProxyController(proxyService);
|
export const proxyController = new ProxyController(proxyService, waMonitor);
|
||||||
|
|
||||||
const chamaaiService = new ChamaaiService(waMonitor, configService);
|
const chamaaiService = new ChamaaiService(waMonitor, configService);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user