1.0.0 • Published 8 years ago

noderelay v1.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
8 years ago

js-standard-style Build Status Coverage Status Dependency Status devDependency Status NPM pkg NPM dm Gratipay

Modules

NodeRelay

A forwarder/redirector server inspired in WinRelay.

Author: Ivan Garavito ivangaravito@gmail.com

Introduction

This is an app that forwards/redirects data as-is to the servers defined. This helps you to:

  • Hide your real server address for security reasons.
  • Separate networks between clients and services.
  • Serve data from an isolated network by redirecting only the services you need.

Installation

All you need to get it with your is:

  1. Install Node.js. Optionally, you can use node-install
  2. Install NodeRelay with NPM:
ૐ » ~ λ npm install -g NodeRelay

or if you need root permissions:

ૐ » ~ λ sudo npm install -g NodeRelay

Quick Start

Let's say you have a fixed HTTP service at 127.0.0.1:3000, and you want to access using port 80. All you need to do is create the file config/local.json5 and write the following lines:

{
  localHost: '0.0.0.0',
  pool: [
    {
      localPort: 80
      rdirHost: '127.0.0.1',
      rdirPort: 3000
    }
  ]
}

After NodeRelay configuration is ready, simply run the app:

  $ cd /path/to/NodeRelay
  $ node .

Terminology

First of all, let's define the terminology used into this app.

  • service, is the server where you want to redirect.
  • local, is here, where NodeRelay is installed and redirecting.
  • client, is the host connecting to local and redirected to service.
  • localserver or server, is the server needed at local to get client connection and redirect it to service.

Configuration

Well documented, the default options are stored inside config/default.json5 file, and the user defined configuration is inside the config/local.json5 file, which can be reduced to something like the following:

{
  localHost: '127.0.0.1',
  pool: [
    {
      localPort: <localPort>,
      serverHost: '<serverHost>',
      serverPort: <serverPort>
    }
  ]
}

localHost defines the IP address where NodeRelay is going to be listening to.

pool is the port list where is defined for each local port to which server to redirect to:

  • localPort is the port at the local host to listen to new connections.
  • serverHost is the server's address to redirect to.
  • serverPort is the port at the server to redirect to.

Dynamic Forwarding/Redirection

Let's say you have two servers within a LAN network isolated from your Internet connection. These servers have a HTTP service for UI purposes, then the UI connects to a fixed port 3000 (and you cannot change it) for polling data. How you can make NodeRelay connect dynamically to that fixed port at the proper server?

Well, it's as easy as defining a local port with no server connection params at the pool. Your local.json5 file should look like this:

{
  localHost: 'my_isp_assing_ip',
  pool: [
    {	//DEVICE 1
      localPort: 81,
      serverHost: '192.168.1.101',
      serverPort: 80
    },
    {	//DEVICE 2
      localPort: 82,
      serverHost: '192.168.1.102',
      serverPort: 80
    },
    {	//FIXED PORT FOR DYNAMIC FORWARDING/REDIRECTION
      localPort: 3000
    }
  ]
}

API

NodeRelay~LocalServer ⇐ external:EventEmitter

Abstracts the local server that redirect connections from a client to a service

Kind: inner class of NodeRelay
Extends: external:EventEmitter
Emits: event:close, event:error, event:listening, event:client-close, event:client-connection, event:service-close, event:service-error, event:service-redirection, event:service-redirection-dynamic, event:service-redirection-fixed

new LocalServer(options)

ParamTypeDescription
optionsObjectlocal and service options

localServer.close()

Stop local server from accepting new connections

Kind: instance method of LocalServer

localServer._connectService(clientSocket, options)

Connect to service and make client-service redirection

Kind: instance method of LocalServer

ParamTypeDescription
clientSocketSocketClient socket
optionsObjectConnection options to service
options.hoststringAddress where is the service
options.portstringTCP port at options.host where is the service

localServer.getDynamicServiceHost(Client) ⇒ string

Initializes the LocalServer instance

Kind: instance method of LocalServer
Returns: string - Service address when this is a local server for a dynamic redirection
Throws:

  • UserError The app instancing this class must override this method
ParamTypeDescription
ClientstringIP address

localServer.init(options)

Initializes the LocalServer instance

Kind: instance method of LocalServer

ParamTypeDefaultDescription
optionsObjectlocal and service options
options.localHoststring"localhost"IP address to listen to at local
options.localPortstringTCP port to listen to at localHost
options.serviceHoststring"options.localHost"Address where is the service
options.servicePortstring"options.localPort"TCP port at serviceHost where is the service
options.listenRetryTimesnumber1Retry times to get the local server up
options.listenRetryTimeoutnumber500Time in milliseconds to wait before next listen try
options.connRetryTimesnumber1Retry times to connect to service
options.connRetryTimeoutnumber500Time in milliseconds to wait before next connection try

localServer._onConnection(client)

Client connection handler

Kind: instance method of LocalServer

ParamTypeDescription
clientSocketClient connection socket

localServer.start()

Start local server listening

Kind: instance method of LocalServer

"event:close"

Server close event

Kind: event emitted by LocalServer

"event:error" (err)

Server error event

Kind: event emitted by LocalServer

ParamTypeDescription
errErrorThe error from local server

"event:listening" (server)

Server listening event

Kind: event emitted by LocalServer

ParamTypeDescription
serverLocalServerServer listening

"event:client-close" (had_error, client)

Client close event

Kind: event emitted by LocalServer

ParamTypeDescription
had_errorboolean
clientObjectDetails of client connection
client.portnumberClient port
client.familystringClient socket family
client.addressstringClient address

"event:client-connection" (had_error, localPort, clientAddress)

Client connection event

Kind: event emitted by LocalServer

ParamTypeDescription
had_errorboolean
localPortnumberServer port at local
clientAddressstringClient IP address

"event:service-close" (had_error, service)

Service connection closed event

Kind: event emitted by LocalServer

ParamTypeDescription
had_errorboolean
serviceObjectDetails of service connection
service.portnumberService port
service.familystringService socket family
service.addressstringService address

"event:service-error" (err)

Connection to service failed event

Kind: event emitted by LocalServer

ParamType
errError

"event:service-redirection" (localPort, client, service)

Client redirected to service event

Kind: event emitted by LocalServer

ParamTypeDescription
localPortnumberServer port at local
clientSocketClient connection socket
serviceSocketService connection socket

"event:service-redirection-dynamic" (localPort, client, service)

Client redirected to a dynamic service event

Kind: event emitted by LocalServer

ParamTypeDescription
localPortnumberServer port at local
clientSocketClient connection socket
serviceSocketService connection socket

"event:service-redirection-fixed" (localPort, client, service)

Client redirected to a fixed service event

Kind: event emitted by LocalServer

ParamTypeDescription
localPortnumberServer port at local
clientSocketClient connection socket
serviceSocketService connection socket

LocalServer.errors : enum

LocalServer error definitions

Kind: static enum property of LocalServer
Read only: true
Properties

NameTypeDefaultDescription
ENOLOCALPORTUserErrorNo local port defined
ENOSERVICEPORTUserErrorNo service port defined
ESAMESERVICEANDLOCALUserErrorService and local are the same
EUNTRACKEDCLIENTUserErrorUntracked client
EDYNAMICSERVICEUserErrorgetDynamicServiceHost must be implemented by the app

LocalServer.getSocketRemoteParams(socket) ⇒ Object

Returns and object with remote port, family and address

Kind: static method of LocalServer
Returns: Object - Object with remote port, family and address

ParamType
socketSocket

log

log.levels : enum

Log level definitions

Kind: static enum property of log
Read only: true
Properties

NameTypeDefaultDescription
Infonumber1Info messages
Errornumber2Error messages
Debugnumber4Debug messages
Connectionnumber16Connection messages
DataEventnumber32DataEvent messages.
IncomingDatanumber64IncomingData messages
OutcomingDatanumber128OutcomingData messages

log~info(...args)

Kind: inner method of log

ParamTypeDescription
...args*The arguments to log

log~error(...args)

Kind: inner method of log

ParamTypeDescription
...args*The arguments to log

log~debug(...args)

Kind: inner method of log

ParamTypeDescription
...args*The arguments to log

log~connection(...args)

Kind: inner method of log

ParamTypeDescription
...args*The arguments to log

tracker

tracker~getDynamicServiceHost(clientAddress) ⇒ string | undefined

Kind: inner method of tracker
Returns: string | undefined - Service address to redirect to or undefined when it's an untracked client

ParamTypeDescription
clientAddressstringThe client IP address

tracker~getRedirections() ⇒ Object

Return the redirection collection

Kind: inner method of tracker
Returns: Object - The redirection collection

tracker~getServers() ⇒ Object

Return the server collection

Kind: inner method of tracker
Returns: Object - The server collection

tracker~trackClient(localPort, clientAddress)

Tracks a client when connected to a non-dynamic service

Kind: inner method of tracker

ParamTypeDescription
localPortnumberThe local server port
clientAddressstringThe client IP address

tracker~trackRedirection(localPort, clientSocket, serviceSocket)

Kind: inner method of tracker

ParamTypeDescription
localPortnumberThe local server port
clientSocketSocketThe client socket connection
serviceSocketSocketThe service socket connection

tracker~trackServer(server) ⇒ LocalServer

Keep track of a server

Kind: inner method of tracker
Returns: LocalServer - The server param

ParamTypeDescription
serverLocalServerThe local server

tracker~untrackRedirection(id)

Kind: inner method of tracker

ParamTypeDescription
idstringThe redirection id to remove

License

(The MIT License)

Copyright (c) 2016 Ivan Garavito <ivangaravito@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.