require('dotenv').config(); const http = require('http'); const { convertProxyListToString } = require('./helpers'); const { loadAllProxyServers, setTimeToFetch, sortProxyServers, selectBestProxies } = require('./proxyHelpers'); 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 () => { const fullProxyList = await loadAllProxyServers(); const httpsProxyList = fullProxyList['https']; // const socks5ProxyList = fullProxyList['socks5']; const updatedHttpsProxyList = await setTimeToFetch(httpsProxyList); // const updatedSocks5ProxyList = await setTimeToFetch(socks5ProxyList); const sortedHttpsProxyList = sortProxyServers(updatedHttpsProxyList); // const sortedSocks5ProxyList = sortProxyServers(updatedSocks5ProxyList); proxyServersObject = { 'https': selectBestProxies(sortedHttpsProxyList), // 'socks5': selectBestProxies(sortedSocks5ProxyList) } } (async () => { await refreshProxyList(); })(); http.createServer(handleHttpRequest).listen(process.env.SERVER_PORT); setInterval(refreshProxyList, parseInt(process.env.PROXY_LIST_RELOAD_INTERVAL)*60*1000);