Advertisement
Arbitrator

Untitled

Jan 2nd, 2020
8,093
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. var fs = require('fs');
  2. var readline = require('readline');
  3. var stream = require('stream');
  4. var request = require('request');
  5.  
  6. process.on('uncaughtException', (err) => {
  7. console.warn(err)
  8. });
  9. process.on('unhandledRejection', (err) => {
  10. console.warn(err)
  11. });
  12. var readProxiesFromFile = function(file, callback) {
  13. var instream = fs.createReadStream(file);
  14. var outstream = new stream;
  15. var rl = readline.createInterface(instream, outstream);
  16.  
  17. rl.on('line', function(line) {
  18. if( !/^#/.exec(line) ) {
  19. var elts = line.split(':');
  20. var host = elts[0];
  21. var port = elts[1];
  22. if( host && port )
  23. callback(host, port);
  24. }
  25. });
  26. }
  27.  
  28. /**
  29. * Checks if a proxy is accessible.
  30. *
  31. * @param host Host of the proxy
  32. * @param port Port of the proxy
  33. * @param options Options to check if the proxy is accessible. It's an object {
  34. * url: the complete URL to check the proxy,
  35. * regex: an optional regex to check for the presence of some text on the page
  36. * }.
  37. * @param callback Callback function to be called after the check. Parameters:
  38. * host: host of the proxy
  39. * port: port of the proxy,
  40. * ok: true if the proxy is accessible, false otherwise,
  41. * statusCode: HTTP code returned by the request (usually 200 if no error),
  42. * err: error if there was one.
  43. */
  44. var checkProxy = function(host, port, options, callback) {
  45. var proxyRequest = request.defaults({
  46. proxy: 'http://' + host + ':' + port
  47. });
  48. proxyRequest(options.url, function(err, res) {
  49. var testText = 'content="Yelp"';
  50. if( err ) {
  51. callback(host, port, false, -1, err);
  52. } else if( res.statusCode != 200 ) {
  53. callback(host, port, false, res.statusCode, err);
  54. } else if( !res.body || (options.regex && !options.regex.exec(res.body)) ) {
  55. callback(host, port, false, res.statusCode, "Body doesn't match the regex " + options.regex + ".");
  56. } else {
  57. callback(host, port, true, res.statusCode);
  58. }
  59. });
  60. }
  61.  
  62. /**
  63. * Checks if a proxy is accessible.
  64. *
  65. * @param file File containing the proxy list, one per line formatted as "host:port".
  66. * @param options Options to check if the proxy is accessible. It's an object {
  67. * url: the complete URL to check the proxy,
  68. * regex: an optional regex to check for the presence of some text on the page
  69. * }.
  70. * @param callback Callback function to be called after each proxy check. Parameters:
  71. * host: host of the proxy
  72. * port: port of the proxy,
  73. * ok: true if the proxy is accessible, false otherwise,
  74. * statusCode: HTTP code returned by the request (usually 200 if no error),
  75. * err: error if there was one.
  76. */
  77. var checkProxiesFromFile = function(file, options, callback) {
  78. readProxiesFromFile(file, function(host, port) {
  79. checkProxy(host, port, options, callback);
  80. });
  81. }
  82.  
  83. module.exports = {
  84. checkProxiesFromFile: checkProxiesFromFile,
  85. checkProxy: checkProxy
  86. };
  87.  
  88. fs.truncate('checked.txt', 0, function() {checkProxiesFromFile(
  89. // The path to the file containing proxies
  90. process.argv[2],
  91. {
  92. // the complete URL to check the proxy
  93. url: 'http://www.example.com',
  94. // an optional regex to check for the presence of some text on the page
  95. regex: /Example Domain/
  96. },
  97. // Callback function to be called after the check
  98. function(host, port, ok, statusCode, err) {
  99. console.log(host + ':' + port + ' => '
  100. + ok + ' (status: ' + statusCode + ', err: ' + err + ')');
  101. if (ok) {
  102. fs.appendFileSync('checked.txt', "http://" + host + ':' + port + '\r\n');
  103. }
  104. }
  105. );});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement