Giter Club home page Giter Club logo

node-netcat's Introduction

node-netcat

Build StatusCode Coverage 100%ISC License

Description

Arbitrary TCP and UDP connections and listens to be used in Node.js

This module try to implement all that "nc" allows to be used in Node.js, this is a good module to implement simple server/client testing stuff or even to create simple tcp servers and clients.

Current features:

  • open TCP/UDP connections and sending messages (client)
  • listen on arbitary TCP/UDP ports and response to the received messages (server)
  • PortScan (portscan)
  • TCP only deal with IPV4

var Netcat = require('node-netcat')

nc node-netcat
nc listener (-k -l cmdline) Necat.server
nc host port Netcat.client
nc -z host port_start[-port_end] Netcat.portscan

Installation

npm i --save node-netcat

Netcat -> API

Client

Netcat.client(port, host, [options])

  • port {int} required
  • host {string} required
  • options
    • timeout {int} define a connection timeout in miliseconds, default to 60000,
    • read_encoding {string} the read encoding, default to 'buffer', others values ascii, hex,utf8, base64

start()

client starts the connection

close()

close the connection

send(message, [close_connection], [callback])

send messages and can close the connection after send the message

  • message {string} don't need to be a Buffer
  • close_connection {boolean} default to false
  • callback - {function} ?

events

  • open callback()
  • data callback(data)
  • error callback(err)
  • close callback()

Server (-k -l)

new Netcat.server(port, [host], [options])

  • port {int} required
  • host {string} required
  • options
    • timeout {int} define a connection timeout in miliseconds, default to 60000,
    • read_encoding {string} the read encoding, default to 'buffer', others values ascii, hex,utf8, base64

listen()

initialize the server

close()

close the server but must not exists active clients

send(client, message, [close_connection], [callback])

send messages to a particular client and can close the connection after send the message

  • message {string} don't need to be a Buffer
  • close_connection {boolean} default to false
  • callback - {function} ? parameter will be executed when the data is finally written out, this may not be immediately

getClients()

return an array with all active clients

events

  • ready callback() - server it's ready
  • data callback(data)
  • client_on callback(client)
  • client_off callback(client)
  • error callback(err)
  • close callback()

UDP Client (-u)

Netcat.udpClient(port, host, [options])

  • port {int} required
  • host {string} required
  • options
    • timeout {int} define a connection timeout in miliseconds, default to 60000,
    • read_encoding {string} the read encoding, default to 'buffer', others values ascii, hex,utf8, base64

events

  • open callback()
  • message callback(message, {port, address}, protocol_family*[ipv4 | ipv6]*)
  • error callback(err)
  • close callback()

start()

init the client

close()

close the client

send(message)

send a message and the message should not be a Buffer

A Note about UDP datagram size

The maximum size of an IPv4/v6 datagram depends on the MTU (Maximum Transmission Unit) and on the Payload Length field size.

The Payload Length field is 16 bits wide, which means that a normal payload cannot be larger than 64K octets including internet header and data (65,507 bytes = 65,535 โˆ’ 8 bytes UDP header โˆ’ 20 bytes IP header); this is generally true for loopback interfaces, but such long datagrams are impractical for most hosts and networks.

The MTU is the largest size a given link layer technology can support for datagrams. For any link, IPv4 mandates a minimum MTU of 68 octets, while the recommended MTU for IPv4 is 576 (typically recommended as the MTU for dial-up type applications), whether they arrive whole or in fragments.

For IPv6, the minimum MTU is 1280 octets, however, the mandatory minimum fragment reassembly buffer size is 1500 octets. The value of 68 octets is very small, since most current link layer technologies have a minimum MTU of 1500 (like Ethernet).

Note that it's impossible to know in advance the MTU of each link through which a packet might travel, and that generally sending a datagram greater than the (receiver) MTU won't work (the packet gets silently dropped, without informing the source that the data did not reach its intended recipient).

UDP Server (-u -k -l)

Netcat.udpServer(port, host, [options])

  • port {int} required
  • host {string} required
  • options
    • timeout {int} define a connection timeout in miliseconds, default to 60000,
    • read_encoding {string} the read encoding, default to 'buffer', others values ascii, hex,utf8, base64

bind()

binding to a port

close()

events

  • ready callback() - server it's ready
  • data callback(data)
  • error callback(err)
  • close callback()

PortScan (-z [port_start-port_end])

scan.run(host, ports*, callback)

  • host {string}
  • ports {int | expression} a single port 80 or between various ports for example: 22-80
  • callback {function}

Examples

Client

var NetcatClient = require('node-netcat').client;
var client = NetcatClient(5000, 'localhost');
	
client.on('open', function () {
	console.log('connect');
	client.send('this is a test' + '\n');
});

client.on('data', function (data) {
  console.log(data.toString('ascii'));
  client.send('Goodbye!!!', true);
});

client.on('error', function (err) {
  console.log(err);
});

client.on('close', function () {
  console.log('close');
});

client.start();

Server

var NetcatServer = require('node-netcat').server;
var server = NetcatServer(5000);

server.on('ready', function() {
	console.log('server ready');
});

server.on('data', function(client, data) {
	console.log('server rx: ' + data + ' from ' + client);
});

server.on('client_on', function(client) {
	console.log('client on ', client);
});

server.on('client_of', function(client) {
	console.log('client off ', client);
});

server.on('error', function(err) {
	console.log(err);
});

server.on('close', function() {
	console.log('server closed');
});

server.listen();// start to listening
	
// get active clients
var clients = server.getClients();

// send messages to clients	 and close the connection
Object.keys(clients).forEach(function(client) {
	server.send(clients[client], 'received ' + data, true);
});

// or a normal message	
server.send(client, 'message');

UDP Client

var NetcatUdpClient = require('node-netcat').udpClient;
var client = NetcatUdpClient(5000, '127.0.0.1');

client.on('open', function() { 
	console.log('open');
});

client.once('error', function(err) {
	console.error('err');
});

client.once('close', function() {
	console.log('client, closed');
});

clien.send('Hello World');

UDP Server

var NetcatUdpServer = require('node-netcat').udpServer;
var server = NetcatUdpServer(5000, '127.0.0.1');

server.on('data', function(msg, client, protocol) {
  console.log('rx: ' + msg + ', from ' + client);
});

server.on('ready', function() {
	console.log('ready');
});

server.once('error', function(err) {
	console.log(err);
});

server.once('close', function() {
	console.log('close');
});

server.bind();

setTimeout(function () {
  server.close();
}, 30000);

PortScan

var scan = require('node-netcat').portscan();

scan.run('google.com', '80-81', function(err, res) {
	if (err) {
		// ERR
	} else {
		// RES
	}
});

Development

this projet has been set up with a precommit that forces you to follow a code style, no jshint issues and 100% of code coverage before commit

to run test

npm test

to run jshint

npm run jshint

to run code style

npm run code-style

to run check code coverage

npm run check-coverage

to open the code coverage report

npm run open-coverage

node-netcat's People

Contributors

joaquimserafim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

node-netcat's Issues

timeout error

I used the code below in server as app.js:

var NetcatServer = require('node-netcat').server;
var server = NetcatServer(81);

server.on('ready', function() {
    console.log('server ready');
});

server.on('data', function(client, data) {
    console.log('server rx: ' + data + ' from ' + client);
});

server.on('client_on', function(client) {
    console.log('client on ', client);
});

server.on('client_of', function(client) {
    console.log('client off ', client);
});

server.on('error', function(err) {
    console.log(err);
});

server.on('close', function() {
    console.log('server closed');
});

server.listen();// start to listening

and connect using command from bash:

nc localhost 81

app.js throw below error:

timers.js:156
    throw new TypeError('msecs must be a number');
          ^
TypeError: msecs must be a number
    at Object.exports.enroll (timers.js:156:11)
    at Socket.setTimeout (net.js:337:12)
    at Server.handler (/home/1111hui/public_html/pdf/node_modules/node-netcat/lib/server.js:38:12)
    at Server.emit (events.js:129:20)
    at TCP.onconnection (net.js:1320:8)

seems there's bug in server.js

Please provide an example with client.send using callback

Hello,
I'm new to node.js. I am able to connect and send netcat client commands using node-netcat.

What I've been struggling with is - I need to send a series of commands.
client.send (password)
(handle authentication)
client.send (command)
(handle result)

It would be incredibly helpful to see an example chaining a series of client.send to each other using callbacks.
I've tried a variety of ways, and I'm getting undefined in the callback, like it's not waiting for the result before proceeding.

I suspect it'd be a common usecase to chain client commands together, so it would be a huge help to provide an example.

Thank you!

Doesn't work in the browser

Is it possible to use it in the browser (using browserify)?
I am getting this error:

this._client = net.connect({port: self._port, host: self._host}, connect);
Uncaught TypeError: undefined is not a function

Data is not received after client.send

this is my code, when i send data to the client , client.on('data') should be invoked again but it doesn't happen.

var Netcat = require('node-netcat')
var rpn = require('rpn');
var BigNumber = require('bignumber.js');

options = {
 // define a connection timeout
	timeout: 60000,
 // buffer(default, to receive the original Buffer objects), ascii, hex,utf8, base64
  read_encoding: 'ascii'
 }

var client = new Netcat.client("10119", "35.158.25.165",options)

client.on('data', function(data){
  s = data.toString()

    try{
        res = rpn(s)
        if (res != null){
            client.send(res, false);
        }

    }
    catch(e){
    }


})


client.on('error', function (err){
    console.log(err)
})


client.start()

ECONNRESET

I recently started getting the following error:

{ Error: read ECONNRESET
at exports._errnoException (util.js:1007:11)
at TCP.onread (net.js:563:26) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }

I've had this process running for a couple of months, and it just randomly started throwing these errors. Any idea why?

Undefined error during portscan

Hi,

I have been using node-netcat's portscan facility to test the status of a port tunneled to a VNC server over an SSH tunnel. This works well to determine whether the tunnel has finished opening, and the port is available on the local system, however there's something about the VNC server's response (possibly?) that causes an error in your respMsg function. I don't see it when I scan a web server for example.

Essentially what happens is the function fires, but the desc object is undefined, therefore the library raises an error when attempting to construct the return string like so:

/opt/quetzal-web/app/programs/server/npm/npm/main/node_modules/node-netcat/lib/portscan.js:32
        ' [' + desc.prot + 
                   ^
TypeError: Cannot read property 'prot' of undefined
    at respMsg (/opt/quetzal-web/app/programs/server/npm/npm/main/node_modules/node-netcat/lib/portscan.js:32:20)
    at cb_call (/opt/quetzal-web/app/programs/server/npm/npm/main/node_modules/node-netcat/lib/portscan.js:77:27)
    at Client.send (/opt/quetzal-web/app/programs/server/npm/npm/main/node_modules/node-netcat/lib/client.js:80:5)
    at Client.cb_open (/opt/quetzal-web/app/programs/server/npm/npm/main/node_modules/node-netcat/lib/portscan.js:75:13)
    at Client.emit (events.js:92:17)
    at Socket.connect (/opt/quetzal-web/app/programs/server/npm/npm/main/node_modules/node-netcat/lib/client.js:37:30)
    at Socket.g (events.js:180:16)
    at Socket.emit (events.js:117:20)
    at Object.afterConnect [as oncomplete] (net.js:886:10)
error: Forever detected script exited with code: 8
error: Script restart attempt #1

Commenting out the return string allows the program to continue as expected.

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.