Skip to content
Open

v2 #6

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules/*
node_modules
test.*
*.csv
.DS_Store
Expand Down
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict'

module.exports = {
server : require('./lib/server'),
client : require('./lib/client'),
udpClient : require('./lib/udp_client'),
udpServer : require('./lib/udp_server'),
portscan : require('./lib/portscan')
};
server : require('./lib/server'),
client : require('./lib/client'),
udpClient : require('./lib/udp_client'),
udpServer : require('./lib/udp_server'),
portscan : require('./lib/portscan'),
NetcatError : require('./lib/error')
}
3 changes: 1 addition & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ inherits(Client, EventEmitter);

Client.init = function(port, host, options) {
if (!parseInt(port)) {
throw new Error('in node-nc server the port is mandatory!');
throw new Error('in node-nc server the port is mandatory!');
}

// check args
Expand Down Expand Up @@ -120,5 +120,4 @@ Client.prototype.send = function(msg, end, cb) {

Client.prototype.close = function() {
this._client.end();
return this;
};
19 changes: 19 additions & 0 deletions lib/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

var util = require('util')

module.exports = NetcatError

function NetcatError(error) {
Error.call(this)
Error.captureStackTrace(this, this.constructor)

if (error instanceof Error) {
error = util.format('%s: %s', error.name, error.message)
}

this.name = this.constructor.name
this.message = error
}

util.inherits(NetcatError, Error)
165 changes: 82 additions & 83 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,162 +1,161 @@
'use strict';
'use strict'

var net = require('net');
var inherits = require('util').inherits;
var EventEmitter = require('events').EventEmitter;
var _ = require('underscore');
var ut = require('./util');
var net = require('net')
var inherits = require('util').inherits
var EE = require('events').EventEmitter
var _ = require('underscore')
var isJsObject = require('is-js-object')
var ut = require('./util')
var NetcatError = require('./error')

module.exports = Server;
module.exports = Server

function Server(port, host, options) {
function Server (port, host, options) {
if (!(this instanceof Server)) {
return new Server(port, host, options);
return new Server(port, host, options)
}

EventEmitter.call(this);
Server.init.call(this, port, host, options);
EE.call(this)
Server.init.call(this, port, host, options)
}

inherits(Server, EventEmitter);
inherits(Server, EE)

Server.init = function(port, host, options) {
var self = this;
Server.init = function init (port, host, options) {
var self = this

if (!parseInt(port)) {
throw new Error('in node-nc server the port is mandatory!');
throw new NetcatError('the port is required')
}

// check args
if (_.isObject(host)) {
options = host;
host = 'localhost';
if (isJsObject(host)) {
options = host
host = 'localhost'
}

this._clients = {};
this._port = port;
this._host = host || 'localhost';
this.timeout = (options && options.timeout) || 3600000;
this._clients = {}
this._port = port
this._host = host || 'localhost'
this._timeout = (options && options.timeout) || 3600000

function handler(socket) {
var client = socket.remoteAddress + ':' + socket.remotePort;
function handler (socket) {
var client = socket.remoteAddress + ':' + socket.remotePort

// socket configurations
socket.setKeepAlive(true, 60000);
socket.setTimeout(this._timeout);
socket.setKeepAlive(true, 60000)
socket.setTimeout(this._timeout)

if (options && options.readEncoding) {
socket.setEncoding(options.readEncoding);
socket.setEncoding(options.readEncoding)
}

//
// client events handlers
//

function data(chunk) {
self.emit('data', client, chunk);
function data (chunk) {
self.emit('data', client, chunk)
}

function timeout() {
socket.destroy();
function timeout () {
socket.destroy()
}

function error(err, cl, chunk) {
self.emit('client_error', err, cl, chunk);
function error (err, cl, chunk) {
self.emit('client_error', err, cl, chunk)
}

function close() {
delete self._clients[client];
self.emit('client_off', client);
function close () {
self.emit('client_off', client)
delete self._clients[client]
}

//
// register events
// register the client events
//

process.nextTick(function register() {
socket.on('data', data);
socket.on('timeout', timeout);
socket.on('error', error);
socket.on('close', close);
});
socket.on('data', data)
socket.on('timeout', timeout)
socket.on('error', error)
socket.on('close', close)
}

this._server = net.createServer(handler);
this._server = net.createServer(handler)

//
// server events handlers
//

function listening() {
self.emit('ready');
function listening () {
self.emit('ready')
}

function error(err) {
self.emit('error', err);
function error (err) {
self.emit('error', err)
}

function close() {
self.emit('close');
function close () {
self.emit('close')
}

function conn(socket) {
self._clients[socket.remoteAddress + ':' + socket.remotePort] = socket;
self.emit('client_on', socket.remoteAddress + ':' + socket.remotePort);
function conn (socket) {
self._clients[socket.remoteAddress + ':' + socket.remotePort] = socket
self.emit('client_on', socket.remoteAddress + ':' + socket.remotePort)
}

//
// register events
// register the server events
//

process.nextTick(function register() {
this._server.on('listening', listening);
this._server.on('connection', conn);
this._server.on('close', close);
this._server.on('error', error);
}, this);
};
this._server.on('listening', listening)
this._server.on('connection', conn)
this._server.on('close', close)
this._server.on('error', error)
}

Server.prototype.listen = function() {
this._server.listen(this._port, this._host);
};
Server.prototype.listen = function listen () {
this._server.listen(this._port, this._host)
}

Server.prototype.close = function(cb) {
cb = cb || ut.noop;
this._server.close(cb);
};
Server.prototype.close = function close (cb) {
cb = cb || ut.noop
this._server.close(cb)
}

Server.prototype.send = function(client, msg, end, cb) {
Server.prototype.send = function send (client, msg, end, cb) {
if (typeof end === 'function') {
cb = end;
end = false;
cb = end
end = false
} else {
cb = cb || ut.noop
}

cb = cb || ut.noop;
msg = msg || new Buffer([0x00]);
msg = msg || new Buffer([0x00])

if (!Buffer.isBuffer(msg)) {
msg = new Buffer(msg.toString());
msg = new Buffer(msg.toString())
}

// check client exists
if (_.contains(_.keys(this._clients), client)) {
// client exists
// send and close connection
if (end) {
this._clients[client].end(msg);
cb();
this._clients[client].end(msg)
cb()
} else {
this._clients[client].write(msg, cb);
this._clients[client].write(msg, cb)
}
} else {
this._clients[client].emit('error',
new Error(),
client,
msg);
cb();
msg)
cb()
}
};
}

Server.prototype.getClients = function() {
return _.keys(this._clients);
};
Server.prototype.getClients = function getClients () {
return _.keys(this._clients)
}
6 changes: 3 additions & 3 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';
'use strict'

module.exports = {
noop: noop
};
}

function noop() {
return function() {};
return function() {}
}
21 changes: 12 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "node-netcat",
"version": "1.5.8",
"version": "2.0.0-1",
"description": "nc/netcat - arbitrary TCP and UDP connections and listens in node.js",
"main": "index.js",
"scripts": {
"test": "istanbul cover tape test/*.js",
"test": "lab -v -c",
"jshint": "jshint -c .jshintrc lib/*.js test/*.js",
"code-style": "jscs -p google lib/*.js test/*.js",
"check-coverage": "istanbul check-coverage --statements 100 --functions 100 --lines 100 --branches 100",
"open-coverage": "open coverage/lcov-report/index.html"
"check-coverage": "lab -t 100 -r html -o coverage.html",
"open-coverage": "open coverage.html"
},
"files": [
"LICENSE",
Expand All @@ -30,13 +30,16 @@
"author": "@joaquimserafim",
"license": "ISC",
"devDependencies": {
"istanbul": "^0.3.5",
"jscs": "^1.10.0",
"jshint": "^2.6.0",
"pre-commit": "^1.0.1",
"tape": "^3.5.0"
"code": "^1.4.0",
"istanbul": "^0.3.13",
"jscs": "^1.12.0",
"jshint": "^2.7.0",
"lab": "^5.7.0",
"pre-commit": "^1.0.6",
"tape": "^4.0.0"
},
"dependencies": {
"is-js-object": "^1.0.0",
"lasync": "^1.1.0",
"underscore": "^1.7.0"
},
Expand Down
Loading