簡體   English   中英

如何節流node.js http.get(…)

[英]how to throttle node.js http.get(…)

我正在嘗試掃描網絡並限制所使用的資源(帶寬,套接字等)。 我以為我可以將http.globalAgent.maxSockets與http.get()一起使用。

我的測試代碼是:

// node.js v6.11.3
var http = require('http');
http.globalAgent.maxSockets = 10;
console.log(http.globalAgent);

function getWebpage(ip) {
  const options = {
    hostname: ip,
    port: 80,
    path: '/',
    method: 'GET'
  };
  var req = http.get(options, (res) => {
    const statusCode = res.statusCode;

    let error;
    if ( ! (statusCode == 200 || statusCode == 401 || statusCode == 404 ) ) {
      error = new Error(`Request Failed. Status Code: ${statusCode}`);
    }

    if (error) {
      console.log(error.message);
      // consume response data to free up memory
      res.resume();
      return;
    }

    res.setEncoding('utf8');
    let rawData = '';
    res.on('data', (chunk) => rawData += chunk);
    res.on('end', () => {
      try {
        console.log(ip + ':' + rawData.length);
      } catch (e) {
        console.log(e.message);
      }
    });
  });

  req.on('error', (e) => {
    console.log(ip + ':' + `Got error: ${e.message}`);
  });

  req.on('socket', function (socket) {
    myTimeout = 3000; // millis
    socket.setTimeout(myTimeout);
    socket.on('timeout', function() {
      console.log(ip + ':' + "Timeout, aborting request")
      req.abort();
    });
  });
};


process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(data) {
  data.split(/\r?\n/).forEach(function (line) {
    if (line.length > 7) {
      getWebpage(line);
    }
  })
});

我希望腳本最多同時嘗試10個主機/插槽,但是網絡捕獲顯示該腳本使用了更多的插槽。

0.000000000 10.10.10.5 -> 10.10.0.0   TCP 74 56780 > http [SYN] Seq=0 ....
0.002956490 10.10.10.5 -> 10.10.0.1   TCP 74 47944 > http [SYN] Seq=0 ....
0.006717145 10.10.10.5 -> 10.10.0.2   TCP 74 53730 > http [SYN] Seq=0 ....
0.006887912 10.10.10.5 -> 10.10.0.3   TCP 74 32808 > http [SYN] Seq=0 ....
0.007037631 10.10.10.5 -> 10.10.0.4   TCP 74 47710 > http [SYN] Seq=0 ....
0.007196762 10.10.10.5 -> 10.10.0.5   TCP 74 34412 > http [SYN] Seq=0 ....
0.007336858 10.10.10.5 -> 10.10.0.6   TCP 74 58658 > http [SYN] Seq=0 ....
0.007479043 10.10.10.5 -> 10.10.0.7   TCP 74 56266 > http [SYN] Seq=0 ....
0.007641515 10.10.10.5 -> 10.10.0.8   TCP 74 46192 > http [SYN] Seq=0 ....
0.007781481 10.10.10.5 -> 10.10.0.9   TCP 74 51736 > http [SYN] Seq=0 ....
0.007919173 10.10.10.5 -> 10.10.0.10  TCP 74 49874 > http [SYN] Seq=0 ....
0.008054775 10.10.10.5 -> 10.10.0.11  TCP 74 47524 > http [SYN] Seq=0 ....
0.008204060 10.10.10.5 -> 10.10.0.12  TCP 74 57618 > http [SYN] Seq=0 ....
0.008344422 10.10.10.5 -> 10.10.0.13  TCP 74 54228 > http [SYN] Seq=0 ....
0.008486984 10.10.10.5 -> 10.10.0.14  TCP 74 50942 > http [SYN] Seq=0 ....
0.008639808 10.10.10.5 -> 10.10.0.15  TCP 74 41750 > http [SYN] Seq=0 ....
0.008776874 10.10.10.5 -> 10.10.0.16  TCP 74 35480 > http [SYN] Seq=0 ....
0.008915166 10.10.10.5 -> 10.10.0.17  TCP 74 48540 > http [SYN] Seq=0 ....
0.009058030 10.10.10.5 -> 10.10.0.18  TCP 74 40784 > http [SYN] Seq=0 ....
0.009206112 10.10.10.5 -> 10.10.0.19  TCP 74 36716 > http [SYN] Seq=0 ....
0.009355106 10.10.10.5 -> 10.10.0.20  TCP 74 51430 > http [SYN] Seq=0 ....
....

通過以下方式運行代碼:

$ nmap -sL 10.10.0.0/24 | grep "Nmap scan report" | awk '{print $NF}' | node maxsockettest.njs

我是否需要創建http.agent並通過選項傳遞?

maxSockets控制http.get()到同一目標的並發連接數。

我實現了一個工作隊列,以限制並發http.get()請求的數量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM