const proxyLists = require('proxy-lists');
const axios = require('axios');
const config = require('../../config');


let uncheckedProxies = [];
let checkedProxies = [];

async function getUncheckedProxies() {
  const result = [];
  const gettingProxies = proxyLists.getProxies({
    protocols: ['http'],
    ipTypes: ['ipv4'],
  });

  return new Promise((resolve) => {
    gettingProxies.on('data', (proxies) => {
      result.push(...proxies);
    });

    gettingProxies.on('error', (err) => {
      console.log('Getting proxy error:', err.message);
    });

    gettingProxies.once('end', () => {
      console.log(`Done getting proxies. ${result.length} proxies found`);
      resolve(result);
    });
  });
}

async function getCheckedProxies() {
  let checkedCount = 0;
  const result = [];

  return new Promise((resolve) => {
    uncheckedProxies.forEach((proxy) => {
      axios.post(config.LASTFM_URL, null, {
        timeout: 5000,
      })
        .catch((err) => {
          if (err.response && err.response.status === 400) {
            result.push(proxy);
          }

          if (++checkedCount === uncheckedProxies.length) {
            console.log('Proxies checked. Working count:', result.length);
            resolve(result);
          }
        });
    });
  });
}

function getRandomChekedProxy() {
  if (checkedProxies.length) {
    const proxy = checkedProxies[Math.floor(Math.random() * checkedProxies.length)];
    return {
      host: proxy.ipAddress,
      port: proxy.port,
    };
  }

  return {
    host: null,
    port: null,
  };
}

getUncheckedProxies()
  .then((proxies) => {
    uncheckedProxies = proxies;
  })
  .then(() => getCheckedProxies())
  .then((result) => {
    checkedProxies = result;
  });

setInterval(async () => {
  checkedProxies = await getCheckedProxies();
}, 600000); // every 10 minutes

setInterval(async () => {
  uncheckedProxies = await getUncheckedProxies();
}, 3600000); // every 1 hour

module.exports = {
  getRandomChekedProxy,
};

@@ -709,22 +709,16 @@
  %7D);%0A%7D%0A%0A
-async 
 function
@@ -784,16 +784,213 @@
 lt = %5B%5D;
+%0A  const incrementCount = (resolve) =%3E %7B%0A    if (++checkedCount === uncheckedProxies.length) %7B%0A      console.log('Proxies checked. Working count:', result.length);%0A      resolve(result);%0A    %7D%0A  %7D;
 %0A%0A  retu
@@ -1048,16 +1048,22 @@
 forEach(
+async 
 (proxy) 
@@ -1073,16 +1073,36 @@
 %7B%0A      
+try %7B%0A        await 
 axios.po
@@ -1139,16 +1139,18 @@
         
+  
 timeout:
@@ -1163,132 +1163,88 @@
 %0A   
-   %7D)%0A
        
- .catch((err) =%3E %7B%0A          if (err.response && err.response.status === 400) %7B%0A            result.push(proxy);
+proxy: %7B%0A            host: proxy.ipAddress,%0A            port: proxy.port,
 %0A   
@@ -1251,17 +1251,17 @@
        %7D
-%0A
+,
 %0A       
@@ -1265,56 +1265,62 @@
     
-  if (++checkedCount === uncheckedProxies.length
+%7D);%0A%0A        incrementCount(resolve);%0A      %7D catch (e
 ) %7B%0A
@@ -1331,101 +1331,82 @@
     
- 
-   console.log('Proxies checked. Working count:', result.length);%0A            resolve(result
+if (e.response && e.response.status === 400) %7B%0A          result.push(proxy
 );%0A 
@@ -1412,19 +1412,18 @@
         
- 
- 
 %7D
+%0A
 %0A       
@@ -1423,19 +1423,48 @@
         
-%7D);
+incrementCount(resolve);%0A      %7D
 %0A    %7D);
@@ -1783,23 +1783,25 @@
  .then((
-proxies
+unchecked
 ) =%3E %7B%0A 
@@ -1826,35 +1826,29 @@
 s = 
-proxies;%0A  %7D)%0A  .then(() =%3E
+unchecked;%0A    return
  get
@@ -1863,16 +1863,21 @@
 roxies()
+;%0A  %7D
 )%0A  .the
@@ -1879,22 +1879,23 @@
  .then((
-result
+checked
 ) =%3E %7B%0A 
@@ -1914,22 +1914,23 @@
 oxies = 
-result
+checked
 ;%0A  %7D);%0A

Fix proxy checker