From f66d5185ad69c033fc98719f815b8d37d864428a Mon Sep 17 00:00:00 2001 From: Girish Ramakrishnan Date: Thu, 30 Mar 2017 16:13:11 -0700 Subject: [PATCH] add tcpbomb.js --- tcpbomb.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tcpbomb.js diff --git a/tcpbomb.js b/tcpbomb.js new file mode 100644 index 0000000..ad50142 --- /dev/null +++ b/tcpbomb.js @@ -0,0 +1,37 @@ +'use strict'; + +exports = module.exports = tcpBomb; + +var async = require('async'), + net = require('net'); + +function tcpBomb(ip, port, times, timeout, callback) { + async.times(times, function (n, done) { + var client = new net.Socket(); + client.setTimeout(timeout); + + client.connect(port, ip, function() { + client.destroy(); + done(null, 'Connected'); + }); + + client.on('timeout', function () { + client.destroy(); + done(null, 'Timeout'); + }); + + client.on('error', function (error) { + done(null, 'Error: ' + error.message); + }); + }, function (error, result) { + var x = result.reduce(function (ac, val) { + if (val === 'Connected') ++ac.connected; + else if (val === 'Timeout') ++ac.timeout; + else ++ac.error; + + return ac; + }, { connected: 0, error: 0, timeout: 0 }); + return callback(null, x); + }); +} +