Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. var request = require('request-promise');
  2. var promise = require('bluebird'); // promises lib used by request-promise
  3. var lookup = promise.promisify(require('dns').lookup);
  4. var http = require('http');
  5. var username = 'lum-customer-hl_02d72fa0-zone-static';
  6. var password = 'nwbd652o7gfl';
  7. var port = 22225;
  8. var user_agent = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36';
  9. var at_req = 0;
  10. var n_total_req = 1000;
  11. var n_parallel_exit_nodes = 100;
  12. var switch_ip_every_n_req = 50;
  13. var max_failures = 3;
  14. var req_timeout = 60*1000;
  15.  
  16. function main(){
  17. http.Agent.defaultMaxSockets = Infinity;
  18. for (var i=0; i < n_parallel_exit_nodes; i++)
  19. new Session(i).start();
  20. }
  21.  
  22. function Session(id){
  23. this.id = id;
  24. this.n_req_for_exit_node = 0;
  25. this.fail_count = 0;
  26. this.switch_session_id();
  27. }
  28.  
  29. var proto = Session.prototype;
  30.  
  31. proto.start = proto.next = function(){
  32. if (at_req >= n_total_req)
  33. return this.cleanup(); // all done
  34. at_req++;
  35. var _this = this;
  36. promise.try(function(){
  37. if (!_this.have_good_super_proxy())
  38. return _this.switch_super_proxy();
  39. }).then(function(){
  40. if (_this.n_req_for_exit_node==switch_ip_every_n_req)
  41. _this.switch_session_id();
  42. var options = {
  43. url: 'http://lumtest.com/myip.json',
  44. timeout: req_timeout,
  45. pool: _this.pool,
  46. forever: true,
  47. proxy: _this.super_proxy_url,
  48. headers: {'User-Agent': user_agent},
  49. };
  50. return request(options);
  51. }).then(function success(res){
  52. console.log(res);
  53. _this.fail_count = 0;
  54. _this.n_req_for_exit_node++;
  55. }, function error(err){
  56. if (err.statusCode
  57. && !status_code_requires_exit_node_switch(err.statusCode))
  58. {
  59. // this could be 404 or other website error
  60. _this.n_req_for_exit_node++;
  61. return;
  62. }
  63. _this.switch_session_id();
  64. _this.fail_count++;
  65. }).finally(function(){
  66. _this.next();
  67. });
  68. };
  69.  
  70. proto.have_good_super_proxy = function(){
  71. return this.super_proxy_host && this.fail_count < max_failures;
  72. };
  73.  
  74. proto.update_super_proxy_url = function(){
  75. this.super_proxy_url = 'http://'+username+
  76. '-session-'+
  77. this.session_id+':'+password+'@'+this.super_proxy_host+':'+port;
  78. };
  79.  
  80. proto.switch_session_id = function(){
  81. connection_pool_cleanup(this.pool);
  82. this.pool = {};
  83. this.session_id = (1000000 * Math.random())|0;
  84. this.n_req_for_exit_node = 0;
  85. this.update_super_proxy_url();
  86. };
  87.  
  88. proto.switch_super_proxy = function(){
  89. var _this = this;
  90. this.switch_session_id();
  91. return promise.try(function(){
  92. return lookup('session-'+_this.session_id+
  93. '.'+
  94. 'zproxy.lum-superproxy.io');
  95. }).then(function success(res){
  96. _this.super_proxy_host = res;
  97. _this.update_super_proxy_url();
  98. });
  99. };
  100.  
  101. proto.cleanup = function(){
  102. connection_pool_cleanup(this.pool);
  103. };
  104.  
  105. function connection_pool_cleanup(pool){
  106. if (!pool)
  107. return;
  108. for (let key in pool)
  109. {
  110. var sockets = pool[key].sockets;
  111. for (let name in sockets)
  112. sockets[name].forEach(s=>s.destroy());
  113. }
  114. }
  115.  
  116. function status_code_requires_exit_node_switch(status_code){
  117. return [403, 429, 502, 503].indexOf(status_code)>=0;
  118. }
  119.  
  120. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement