implement socks5 proxy measurement

This commit is contained in:
Bilal
2020-08-20 15:54:39 +03:00
parent ad42e25a25
commit 7adcbe3102
4 changed files with 57 additions and 27 deletions

View File

@@ -5,7 +5,9 @@ const axios = require('axios').default;
const HttpsProxyAgent = require('https-proxy-agent');
const SocksProxyAgent = require('socks-proxy-agent');
// setup axios interceptors required to measure request times
const { timeoutPromise } = require('./helpers');
// setup axios interceptors required to measure request times and timeout
axios.interceptors.request.use( x => {
// to avoid overwriting if another interceptor
@@ -49,27 +51,32 @@ const loadProxyServersOfType = async (proxyType) => {
const setTimeToFetch = async (proxyList) => {
const urlToFetch = process.env.URL_TO_FETCH.trim();
const timeout = parseInt(process.env.SINGLE_PROXY_TIMEOUT)*1000;
return await Promise.map(proxyList, async ({ address, type }, i) => {
console.log('Analyzing ', type, i, '/', proxyList.length);
try{
if (address.length === 0) {
throw new Error('Malformed Proxy URL');
}
const proxyAgent = (type === 'https') ? new HttpsProxyAgent(`https://${address}`) : new SocksProxyAgent(`socks5://${address}`);
let responseObject = null;
let responsePromise;
if (type === 'https'){
// when using HTTPS, set 'httpAgent' instead of 'httpsAgent'
responseObject = await axios({ httpAgent: proxyAgent, url: urlToFetch });
responsePromise = axios({ httpAgent: proxyAgent, url: urlToFetch, timeout });
}else{
responseObject = await axios({ httpsAgent: proxyAgent, url: urlToFetch });
responsePromise = axios({ httpsAgent: proxyAgent, url: urlToFetch, timeout });
}
// setting timeout in axios object is not enough for some requests
const responseObject = await Promise.race([responsePromise, timeoutPromise(timeout)]);
return {
address,
type,
timeToFetch: responseObject.responseTime
}
}catch (e) {
console.log('[ERROR](setTimeToFetch)', type, address, e);
console.log('[ERROR](setTimeToFetch)', type, address);
return {
address,
type,
@@ -79,15 +86,5 @@ const setTimeToFetch = async (proxyList) => {
}, { concurrency: parseInt(process.env.MAX_PROXY_CONNECTIONS) || 5});
}
const sortProxyServers = (proxyList) => {
return proxyList;
}
const selectBestProxies = (proxyList) => {
return proxyList;
}
exports.loadAllProxyServers = loadAllProxyServers;
exports.setTimeToFetch = setTimeToFetch;
exports.sortProxyServers = sortProxyServers;
exports.selectBestProxies = selectBestProxies;
exports.setTimeToFetch = setTimeToFetch;