require('dotenv').config(); const http = require('http'); const { loadAllProxyServers, setTimeToFetch } = require('./proxyHelpers'); const { sortProxyServers, selectBestProxies, cleanProxyServers, convertProxyListToString } = require('./helpers'); 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 () => { console.log(new Date(), 'Refreshing proxy list--------'); const fullProxyList = await loadAllProxyServers(); const httpsProxyList = fullProxyList['https']; const socks5ProxyList = fullProxyList['socks5']; const updatedHttpsProxyList = await setTimeToFetch(httpsProxyList); const updatedSocks5ProxyList = await setTimeToFetch(socks5ProxyList); const cleanUpdatedHttpsProxyList = cleanProxyServers(updatedHttpsProxyList); const cleanUpdatedSocks5ProxyList = cleanProxyServers(updatedSocks5ProxyList); const sortedHttpsProxyList = sortProxyServers(cleanUpdatedHttpsProxyList); const sortedSocks5ProxyList = sortProxyServers(cleanUpdatedSocks5ProxyList); proxyServersObject = { 'https': selectBestProxies(sortedHttpsProxyList), 'socks5': selectBestProxies(sortedSocks5ProxyList) } console.log(new Date(), 'DONE----------------------', (process.memoryUsage()['rss'] / 1024 * 100) / 100, 'KiB'); } (async () => { await refreshProxyList(); })(); http.createServer(handleHttpRequest).listen(process.env.SERVER_PORT); setInterval(refreshProxyList, parseInt(process.env.PROXY_LIST_RELOAD_INTERVAL)*60*1000);