Add https-proxy-agent dependency and handle NotFoundException in ProxyController

This commit is contained in:
Judson Junior 2024-01-21 13:00:23 -03:00
parent 440ff2f3ea
commit 35520d85a2
3 changed files with 23 additions and 17 deletions

View File

@ -61,6 +61,7 @@
"express": "^4.18.2",
"express-async-errors": "^3.1.1",
"hbs": "^4.2.0",
"https-proxy-agent": "^7.0.2",
"jimp": "^0.16.13",
"join": "^3.0.0",
"js-yaml": "^4.1.0",

View File

@ -1,19 +1,25 @@
import axios from 'axios';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { Logger } from '../../config/logger.config';
import { BadRequestException } from '../../exceptions';
import { BadRequestException, NotFoundException } from '../../exceptions';
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;
@ -33,37 +39,36 @@ 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) {
logger.verbose('requested testProxy');
try {
let proxyConfig: any = {
host: host,
port: parseInt(port),
protocol: protocol,
};
let proxyUrl = `${protocol}://${host}:${port}`;
if (username && password) {
proxyConfig = {
...proxyConfig,
auth: {
username: username,
password: password,
},
};
proxyUrl = `${protocol}://${username}:${password}@${host}:${port}`;
}
const serverIp = 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);
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;
}
}

View File

@ -119,7 +119,7 @@ export const websocketController = new WebsocketController(websocketService);
const proxyService = new ProxyService(waMonitor);
export const proxyController = new ProxyController(proxyService);
export const proxyController = new ProxyController(proxyService, waMonitor);
const chamaaiService = new ChamaaiService(waMonitor, configService);