Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

freedom.js Port Control

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


npm install
grunt build

This will build the module file at build/port-control.js and a demo Chrome app in build/demo_chrome_app/.


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,


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

You can also probe for a specific protocol:


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": "", 
  "internalPort": 50000, 
  "externalIp": "", 
  "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,


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,


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),


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


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






No releases published


No packages published