@globalart/nestcord
Version:
A module for creating Discord bots using NestJS, based on Discord.js
70 lines (69 loc) • 2.75 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var ProxyService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProxyService = void 0;
const common_1 = require("@nestjs/common");
let ProxyService = ProxyService_1 = class ProxyService {
constructor() {
this.logger = new common_1.Logger(ProxyService_1.name);
}
configure(options) {
if (options.proxyPath) {
this.setupProxyPath(options.proxyPath);
}
else if (options.proxy) {
this.setupProxyObject(options.proxy);
}
}
getConfig() {
return this.proxyConfig;
}
setupProxyPath(proxyPath) {
this.proxyConfig = proxyPath;
const maskedPath = this.maskProxyUrl(proxyPath);
this.logger.log(`Proxy path configured: ${maskedPath}`);
this.setEnvVariables(proxyPath);
}
setupProxyObject(proxy) {
if (!proxy)
return;
const { host, port, auth, protocol = 'http' } = proxy;
const authString = auth ? `${auth.username}:${auth.password}@` : '';
const proxyUrl = `${protocol}://${authString}${host}:${port}`;
this.proxyConfig = proxy;
const maskedHost = this.maskString(host);
this.logger.log(`Proxy configured: ${protocol}://${maskedHost}:${port}`);
this.setEnvVariables(proxyUrl);
}
setEnvVariables(proxyUrl) {
process.env.HTTPS_PROXY = proxyUrl;
process.env.HTTP_PROXY = proxyUrl;
}
maskProxyUrl(url) {
try {
const proxyUrl = new URL(url);
const maskedHost = this.maskString(proxyUrl.hostname);
return `${proxyUrl.protocol}//${proxyUrl.username ? '****:****@' : ''}${maskedHost}:${proxyUrl.port}`;
}
catch (_a) {
return '****';
}
}
maskString(str) {
if (!str)
return '';
const visibleChars = 2;
const start = str.slice(0, visibleChars);
return start + '*'.repeat(Math.max(str.length - visibleChars, 3));
}
};
exports.ProxyService = ProxyService;
exports.ProxyService = ProxyService = ProxyService_1 = __decorate([
(0, common_1.Injectable)()
], ProxyService);