Currently my code is:
var ProxyVerifier = require('proxy-verifier');
const fs = require('fs');
var request = require('request');
const { doesNotMatch } = require('assert');
let config = {
file: 'socks4_proxies.txt',
destFile: 'result.txt'
}
function countFileLines(filePath) {
return new Promise((resolve, reject) => {
let lineCount = 0;
fs.createReadStream(filePath)
.on("data", (buffer) => {
let idx = -1;
lineCount--; // Because the loop will run once for idx=-1
do {
idx = buffer.indexOf(10, idx + 1);
lineCount++;
} while (idx !== -1);
}).on("end", () => {
resolve(lineCount);
}).on("error", reject);
});
};
function getProxies() {
return new Promise(function (resolve, reject) {
request.get('https://api.proxyscrape.com/?request=displayproxies&proxytype=socks4&timeout=2500', function (error, response, body) {
if (!error && response.statusCode == 200) {
resolve(body);
console.log('Proxies pulled!')
} else {
console.log(`Oops, ProxyScrape is down! Please try again later / contact me`)
}
});
});
}
async function proxyTestLogic(proxy_list, index) {
var countLines = await countFileLines('file.txt')
console.log(index)
if (index > 150) {
console.log('Ok good done you can stop now')
return 'stop';
} else {
var splitProxy = proxy_list.split(':');
var ipPortArr = [splitProxy[0], Number(splitProxy[1])]
var proxy = {
ipAddress: ipPortArr[0],
port: ipPortArr[1],
protocol: 'socks4'
};
ProxyVerifier.testAll(proxy, function (error, result) {
if (error) {
} else {
if (result["protocols"]["socks4"]["ok"] == true) {
fs.appendFile('file.txt', ipPortArr[0] + ':' + ipPortArr[1] + '\n', (err) => {
});
} else {
}
}
});
}
}
function testProxies(proxies) {
return new Promise(function (resolve, reject) {
let newLined = proxies.replace(/\r|\"/gi, '').split("\n")
newLined.forEach((item, index) => {
proxyTestLogic(item, index)
})
})
}
async function main() {
let proxies = await getProxies();
testProxies(proxies)
}
main();
As you can see, it's a proxy verifier. It uses the forEach function to compute each proxy in an array. I'm trying to get it to stop at x amount of lines written to socks4_proxies.txt, when I enable the console.log function on it, it just outputs the same line 1200 times (the amount of proxies in the list). How can I fix this?
(I should also mention that I'm coding in node.js)
Thanks, JIBSIL
.forEach
, that never works like you expect - use a regular for looptetProxies
asyncawait proxytestLogic
new Promise
you never resolve anywayso - you end up with:
async function testProxies(proxies) {
let newLined = proxies.replace(/\r|\"/gi, '').split("\n")
for (let index=0; i < newLined.length; index++) {
let result = await proxyTestLogic(newLined[index], index);
if (result === 'stop')
break;
})
})
}