Git Product home page Git Product logo

node-nat-pmp's Introduction

node-nat-pmp

Node.js implementation of the NAT Port Mapping Protocol

This module offers an implementation of the NAT-PMP written in pure JavaScript. You can use this module to dynamically open and close arbitrary TCP and UDP ports against the network's internet gateway device.

Installation

Install with npm:

$ npm install nat-pmp

Examples

var natpmp = require('nat-pmp');

// create a "client" instance connecting to your local gateway
var client = natpmp.connect('10.0.1.1');


// explicitly ask for the current external IP address
client.externalIp(function (err, info) {
  if (err) throw err;
  console.log('Current external IP address: %s', info.ip.join('.'));
});


// setup a new port mapping
client.portMapping({ private: 22, public: 2222, ttl: 3600 }, function (err, info) {
  if (err) throw err;
  console.log(info);
  // {
  //   type: 'tcp',
  //   epoch: 8922109,
  //   private: 22,
  //   public: 2222,
  //   ...
  // }
});

API

License

(The MIT License)

Copyright (c) 2012 Nathan Rajlich <[email protected]>

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.

node-nat-pmp's People

Contributors

indutny avatar tootallnate avatar unrob 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  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  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

node-nat-pmp's Issues

Implement PCP (Port Control Protocol)

From RFC 6886:

1.1. Transition to Port Control Protocol

NAT-PMP enjoyed almost a decade of useful service, and operational
experience with NAT-PMP informed the design of its IETF Standards
Track successor, Port Control Protocol (PCP) [RFC6887]. PCP builds
on NAT-PMP, using the same UDP ports 5350 and 5351, and a compatible
packet format. PCP also adds significant enhancements, including
IPv6 support, management of outbound mappings, management of firewall
rules, full compatibility with large-scale NATs with a pool of
external addresses, error lifetimes, and an extension mechanism to
enable future enhancements.

Because of the significant enhancements in PCP, all existing NAT-PMP
implementations are encouraged to migrate to PCP. The version number
in the packet header is 0 for NAT-PMP and 2 for PCP, so the packets
are easily distinguished. (Version number 1 was used by a vendor
that shipped products that use a protocol that is incompatible with
the IETF Standard. PCP implementations MUST NOT use version
number 1.)

PCP is RFC 6887.

implement "timeout"

The protocol describes this incremental retry and then a final timeout logic. Need to implement that.

TypeError: Cannot read property 'IPv4' of undefined

Hello,

I'm trying to run nat-pmp in armv7.
and when I do the following.

var gateway = netroute.getGateway();
var client = new natpmp.Client(gateway);

I get errors below in node-netroute

def = info.IPv4.filter(function(route) {
^
TypeError: Cannot read property 'IPv4' of undefined

like this one:
indutny/node-netroute#6

When I don't know gateway address, Is there a way not using netroute?

I npm installed like this below

export npm_config_arch=arm
export npm_config_nodedir=/home/build/node-v0.10.29
npm install --production

p.s.
I used network instead of netroute
https://www.npmjs.com/package/network

Unbreak vock

/// Vock - VoIP on node.js ///

events.js:66
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: bind EADDRINUSE
    at errnoException (dgram.js:356:11)
    at Socket.bind (dgram.js:123:15)
    at lookup (dgram.js:49:12)
    at UDP.lookup4 [as lookup] (dgram.js:59:10)
    at Socket.bind (dgram.js:120:16)
    at new Client (/private/tmp/node_modules/vock/node_modules/nat-pmp/index.js:78:15)
    at Object.exports.connect (/private/tmp/node_modules/vock/node_modules/nat-pmp/index.js:55:10)
    at Socket.init (/private/tmp/node_modules/vock/lib/vock/socket.js:93:24)
    at new Socket (/private/tmp/node_modules/vock/lib/vock/socket.js:32:8)
    at Object.create (/private/tmp/node_modules/vock/lib/vock/socket.js:42:10)

/cc @drudge

Assertion error in onmessage

assert.js:104
  throw new assert.AssertionError({
        ^
AssertionError: 16 == 8
    at Client.onmessage (/home/josh/dev/vock/node_modules/nat-pmp/index.js:304:12)
    at Socket.<anonymous> (/home/josh/dev/vock/node_modules/nat-pmp/index.js:337:32)
    at Socket.EventEmitter.emit (events.js:91:17)
    at UDP.onMessage (dgram.js:350:8)

Example code results in error

Code:

var natpmp = require('nat-pmp');

// create a "client" instance connecting to your local gateway
var client = natpmp.connect('10.0.1.1');

// explicitly ask for the current external IP address
client.externalIp(function (err, info) {
  if (err) throw err;
  console.log('Current external IP address: %s', info.ip.join('.'));
});


// setup a new port mapping
client.portMapping({ private: 8080, public: 8080, ttl: 3600 }, function (err, info) {
  if (err) throw err;
  console.log(info);
  // {
  //   type: 'tcp',
  //   epoch: 8922109,
  //   private: 22,
  //   public: 2222,
  //   ...
  // }
});

Result:

events.js:71
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: bind EADDRINUSE
    at errnoException (dgram.js:356:11)
    at Socket.bind (dgram.js:123:15)
    at lookup (dgram.js:49:12)
    at UDP.lookup4 [as lookup] (dgram.js:59:10)
    at Socket.bind (dgram.js:120:16)
    at Client.connect (/Users/jake/Dropbox/Development/nat-pmp test/node_modules/nat-pmp/index.js:93:15)
    at Client._next (/Users/jake/Dropbox/Development/nat-pmp test/node_modules/nat-pmp/index.js:205:12)
    at Client.request (/Users/jake/Dropbox/Development/nat-pmp test/node_modules/nat-pmp/index.js:152:8)
    at Client.externalIp (/Users/jake/Dropbox/Development/nat-pmp test/node_modules/nat-pmp/index.js:160:8)
    at Object.<anonymous> (/Users/jake/Dropbox/Development/nat-pmp test/server.js:16:8)

Error: Socket is already bound

I'm using nat-pmp for my server.

var natpmp = require('nat-pmp');
...


var client = natpmp.connect("192.168.1.1");

client.portMapping({ private: 8070, public: 8070, ttl: 0 }, function (err, info) {
  if (err) throw err;
      console.log(info);

      // server code
      ...

      client.close();
});

On Windows I get this error:

$ node main.js
dgram.js:159
    throw new Error('Socket is already bound');
          ^
Error: Socket is already bound
    at Socket.bind (dgram.js:159:11)
    at Client.connect (C:\Users\jczimm\uSocketConsole\node_modules\nat-pmp\index.js:93:15)
    at C:\Users\jczimm\uSocketConsole\node_modules\nat-pmp\index.js:57:12
    at process._tickCallback (node.js:343:11)
    at Function.Module.runMain (module.js:492:11)
    at startup (node.js:124:16)
    at node.js:807:3

Does nat-pmp simply not work on Windows? Thanks!

externalIp not working

As pointed out in issue #8, I am using index.js of master branch but still, it isn't showing external IP address even with sample code provided in README.md

Here is the debug output, after sending request, it takes forever.

nat-pmp creating new Client instance for gateway +0ms 192.168.1.1
nat-pmp Client#request() +7ms [ 0, null, [Function] ]
nat-pmp Client#_next() +3ms
nat-pmp _next: not "listening" yet, cannot send out request yet +0ms
nat-pmp Client#connect() +0ms
nat-pmp Client#connect() +2ms
nat-pmp on: socket event "listening" +5ms
nat-pmp Client#onlistening() +0ms
nat-pmp Client#_next() +0ms
nat-pmp _next: sending request +0ms <Buffer 00 00> 192.168.1.1

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.