我正在尝试扫描网络并限制正在使用的资源(带宽、套接字等)。我以为我可以将 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 并通过选项传递吗?