@achingbrain/nat-port-mapper
Port mapping with UPnP and NAT-PMP
Last updated 6 months ago by achingbrain .
Apache-2.0 OR MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @achingbrain/nat-port-mapper 
SYNC missed versions from official npm registry.

@achingbrain/nat-port-mapper

codecov CI

Port mapping with UPnP and NAT-PMP

About

Enable NAT traversal by mapping public ports to ports on your computer using either UPnP or NAT-PMP.

Example - UPnP NAT

import { upnpNat } from '@achingbrain/nat-port-mapper'

const client = upnpNat()

for await (const gateway of client.findGateways({ signal: AbortSignal.timeout(10000) })) {
  // Map public port 1000 to private port 1000 with TCP
  await gateway.map(1000, '192.168.1.123', {
    protocol: 'tcp'
  })

  // Map port 3000 to any available host name
  for await (const mapping of gateway.mapAll(3000, {
    protocol: 'udp'
  })) {
    console.info(`mapped ${mapping.internalHost}:${mapping.internalPort} to ${mapping.externalHost}:${mapping.externalPort}`)
  }

  // Unmap previously mapped private port 1000
  await gateway.unmap(1000)

  // Get external IP
  const externalIp = await gateway.externalIp()

  console.log('External IP:', externalIp)

  // Unmap all mapped ports and cancel any in-flight network operations
  await gateway.stop()
}

Example - NAT-PMP

import { pmpNat } from '@achingbrain/nat-port-mapper'
import { gateway4sync } from 'default-gateway'

const gateway = pmpNat(gateway4sync().gateway)

// Map public port 1000 to private port 1000 with TCP
await gateway.map(1000, '192.168.1.123', {
  protocol: 'tcp'
})

// Map public port 2000 to private port 3000 with UDP
await gateway.map(3000, '192.168.1.123', {
  externalPort: 2000,
  protocol: 'udp'
})

// Unmap previously mapped private port 1000
await gateway.unmap(1000)

// Get external IP
const externalIp = await gateway.externalIp()

console.log('External IP:', externalIp)

// Unmap all mapped ports and cancel any in-flight network operations
await gateway.stop()

Credits

Based on alxhotel/nat-api

Additional Information

Install

$ npm i @achingbrain/nat-port-mapper

API Docs

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Current Tags

  • 4.0.5                                ...           latest (6 months ago)

40 Versions

  • 4.0.5                                ...           6 months ago
  • 4.0.4                                ...           10 months ago
  • 4.0.3                                ...           10 months ago
  • 4.0.2                                ...           a year ago
  • 4.0.1                                ...           a year ago
  • 4.0.0                                ...           a year ago
  • 3.0.2                                ...           a year ago
  • 3.0.1                                ...           a year ago
  • 3.0.0                                ...           a year ago
  • 2.0.10                                ...           a year ago
  • 2.0.9                                ...           a year ago
  • 2.0.8                                ...           a year ago
  • 2.0.7                                ...           a year ago
  • 2.0.6                                ...           a year ago
  • 2.0.5                                ...           a year ago
  • 2.0.4                                ...           a year ago
  • 2.0.3                                ...           a year ago
  • 2.0.2                                ...           a year ago
  • 2.0.1                                ...           a year ago
  • 2.0.0                                ...           a year ago
  • 1.0.18                                ...           a year ago
  • 1.0.17                                ...           a year ago
  • 1.0.16                                ...           a year ago
  • 1.0.15                                ...           2 years ago
  • 1.0.14                                ...           2 years ago
  • 1.0.13                                ...           2 years ago
  • 1.0.12                                ...           2 years ago
  • 1.0.11                                ...           3 years ago
  • 1.0.10                                ...           3 years ago
  • 1.0.9                                ...           3 years ago
  • 1.0.8                                ...           3 years ago
  • 1.0.7                                ...           4 years ago
  • 1.0.6                                ...           4 years ago
  • 1.0.5                                ...           4 years ago
  • 1.0.4                                ...           4 years ago
  • 1.0.3                                ...           4 years ago
  • 1.0.2                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
  • 0.0.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (9)
Dev Dependencies (5)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |