npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nat-puncher

v0.1.8

Published

Opens ports through a NAT with NAT-PMP, PCP, and UPnP

Downloads

48

Readme

nat-puncher

Opens ports through a NAT with NAT-PMP, PCP, and UPnP

Usage

This module will allow you to control port mappings in a NAT and probe it for various settings.

Probing methods

To run all the NAT probing tests,

portControl.probeProtocolSupport()

This method resolves to an object of the form {"natPmp": true, "pcp": false, "upnp": true}.

You can also probe for a specific protocol:

portControl.probePmpSupport()
portControl.probePcpSupport()
portControl.probeUpnpSupport()

All of these methods return a promise that will resolve to a boolean value.

Add a port mapping

To add a NAT port mapping with any protocol available,

// Map internal port 50000 to external port 50000 with a 2 hr lifetime
portControl.addMapping(50000, 50000, 7200)

Passing in a lifetime of 0 seconds will keep the port mapping open indefinitely. If the actual lifetime of the mapping is less than the requested lifetime, the module will automatically handle refreshing the mapping to meet the requested lifetime.

This method returns a promise that will resolve to a Mapping object of the form,

{
  "internalIp": "192.168.1.50",
  "internalPort": 50000,
  "externalIp": "104.132.34.50",
  "externalPort": 50000,
  "lifetime": 120,
  "protocol": "natPmp",
  ...
}

An important optimization note: by default, addMapping() will try all the protocols sequentially (in order of NAT-PMP, PCP, UPnP). If we're waiting for timeouts, then this method can take up to ~10 seconds to run. This may be too slow for practical purposes. Instead, run probeProtocolSupport() at some point before (also can take up to ~10 seconds), which will cache the results, so that addMapping() will only try one protocol that has worked before (this will take <2 seconds).

You can also create a port mapping with a specific protocol:

portControl.addMappingPmp(55555, 55555, 7200)
portControl.addMappingPcp(55556, 55556, 7200)
portControl.addMappingUpnp(55557, 55557, 7200)

All of these methods return the same promise as addMapping() and refresh similarly.

Delete port mapping

To delete a NAT port mapping,

portControl.deleteMapping(55555)  // 55555 is the external port of the mapping

This will delete the module's record of this mapping and also attempt to delete it from the NAT's routing tables. The method will resolve to a boolean, which is true if it succeeded and false otherwise.

There are also methods for specific protocols,

portControl.deleteMappingPmp(55555)
portControl.deleteMappingPcp(55556)
portControl.deleteMappingUpnp(55557)

Note: all the deletion methods only work if we're tracking the port mapping in PortControl.activeMappings (see below).

Get active port mappings

To get the module's local record of the active port mappings,

portControl.getActiveMappings()

This method will return a promise that resolves to an object containing Mapping objects, where the keys are the external ports of each mapping. Mapping objects are removed from this list when they expire or when they are explicitly deleted.

IP address

The module can also determine the user's private IP addresses (more than one if there are multiple active network interfaces),

portControl.getPrivateIps()

This returns a promise that will resolve to an array of IP address strings, or reject with an error.