Files
old-proxylist/index.js

57 lines
2.0 KiB
JavaScript
Raw Normal View History

2020-08-15 04:12:42 +02:00
require('dotenv').config();
const http = require('http');
2020-08-20 15:54:39 +03:00
const { loadAllProxyServers, setTimeToFetch } = require('./proxyHelpers');
const { sortProxyServers, selectBestProxies, cleanProxyServers, convertProxyListToString } = require('./helpers');
2020-08-15 04:12:42 +02:00
let proxyServersObject = {
'https': [],
'socks5': []
};
const handleHttpRequest = (req, res) => {
const proxyType = req.url.replace('/', '');
if (proxyType === "favicon.ico"){
res.end();
return;
}
if (proxyType === 'https' || proxyType === 'socks5'){
const responseToSend = convertProxyListToString(proxyServersObject[proxyType]);
res.write(responseToSend);
}else{
console.log('[WARN](handleHttpRequest) Proxy type is not valid (https/socks5) - got : ', proxyType);
res.write('');
}
res.end();
}
const refreshProxyList = async () => {
2020-08-20 15:54:39 +03:00
console.log(new Date(), 'Refreshing proxy list--------');
2020-08-15 04:12:42 +02:00
const fullProxyList = await loadAllProxyServers();
const httpsProxyList = fullProxyList['https'];
2020-08-20 15:54:39 +03:00
const socks5ProxyList = fullProxyList['socks5'];
2020-08-15 04:12:42 +02:00
const updatedHttpsProxyList = await setTimeToFetch(httpsProxyList);
2020-08-20 15:54:39 +03:00
const updatedSocks5ProxyList = await setTimeToFetch(socks5ProxyList);
2020-08-15 04:12:42 +02:00
2020-08-20 15:54:39 +03:00
const cleanUpdatedHttpsProxyList = cleanProxyServers(updatedHttpsProxyList);
const cleanUpdatedSocks5ProxyList = cleanProxyServers(updatedSocks5ProxyList);
const sortedHttpsProxyList = sortProxyServers(cleanUpdatedHttpsProxyList);
const sortedSocks5ProxyList = sortProxyServers(cleanUpdatedSocks5ProxyList);
2020-08-15 04:12:42 +02:00
proxyServersObject = {
'https': selectBestProxies(sortedHttpsProxyList),
2020-08-20 15:54:39 +03:00
'socks5': selectBestProxies(sortedSocks5ProxyList)
2020-08-15 04:12:42 +02:00
}
2020-08-20 15:54:39 +03:00
console.log(new Date(), 'DONE----------------------', (process.memoryUsage()['rss'] / 1024 * 100) / 100, 'KiB');
2020-08-15 04:12:42 +02:00
}
(async () => {
await refreshProxyList();
})();
http.createServer(handleHttpRequest).listen(process.env.SERVER_PORT);
setInterval(refreshProxyList, parseInt(process.env.PROXY_LIST_RELOAD_INTERVAL)*60*1000);