[addon] updates to use https proxy agent

This commit is contained in:
TheBeastLT
2020-09-09 20:36:18 +02:00
parent 7ff2a3e208
commit 1b7dda53d5
6 changed files with 65 additions and 8 deletions

View File

@@ -1,6 +1,8 @@
const UserAgent = require('user-agents');
const HttpsProxyAgent = require('https-proxy-agent');
const PROXY_HOSTS = process.env.PROXY_HOSTS && process.env.PROXY_HOSTS.split(',');
const PROXY_PORT = 89;
const PROXY_USERNAME = process.env.PROXY_USERNAME;
const PROXY_PASSWORD = process.env.PROXY_PASSWORD;
const userAgent = new UserAgent();
@@ -13,12 +15,16 @@ function getRandomProxy() {
if (PROXY_HOSTS && PROXY_HOSTS.length && PROXY_USERNAME && PROXY_PASSWORD) {
const proxyHost = PROXY_HOSTS[Math.floor(Math.random() * PROXY_HOSTS.length)];
console.log(`Using ${proxyHost} proxy`);
return `http://${PROXY_USERNAME}:${PROXY_PASSWORD}@${proxyHost}`;
return `https://${PROXY_USERNAME}:${PROXY_PASSWORD}@${proxyHost}:${PROXY_PORT}`;
}
console.warn('No proxy configured!');
return undefined;
}
function getProxyAgent(proxy) {
return new HttpsProxyAgent(proxy);
}
function blacklistProxy(proxy) {
const proxyHost = proxy.replace(/.*@/, '');
console.warn(`Blacklisting ${proxyHost}`);
@@ -27,4 +33,4 @@ function blacklistProxy(proxy) {
}
}
module.exports = { getRandomUserAgent, getRandomProxy, blacklistProxy };
module.exports = { getRandomUserAgent, getRandomProxy, getProxyAgent, blacklistProxy };