fix: Proxy configuration improvements

This commit is contained in:
Davidson Gomes
2023-12-28 10:35:41 -03:00
parent dfc8330035
commit 7c2a8c0abb
10 changed files with 111 additions and 33 deletions

View File

@@ -1,4 +1,7 @@
import axios from 'axios';
import { Logger } from '../../config/logger.config';
import { BadRequestException } from '../../exceptions';
import { InstanceDto } from '../dto/instance.dto';
import { ProxyDto } from '../dto/proxy.dto';
import { ProxyService } from '../services/proxy.service';
@@ -13,7 +16,16 @@ export class ProxyController {
if (!data.enabled) {
logger.verbose('proxy disabled');
data.proxy = '';
data.proxy = null;
}
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);
if (!testProxy) {
throw new BadRequestException('Invalid proxy');
}
}
return this.proxyService.create(instance, data);
@@ -23,4 +35,36 @@ export class ProxyController {
logger.verbose('requested findProxy from ' + instance.instanceName + ' instance');
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,
};
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,
});
logger.verbose('testProxy response: ' + response.data);
return response.data !== serverIp.data;
} catch (error) {
logger.error('testProxy error: ' + error);
return false;
}
}
}