New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@achingbrain/nat-port-mapper

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@achingbrain/nat-port-mapper

Port mapping with UPnP and NAT-PMP

  • 4.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7.1K
decreased by-12.07%
Maintainers
1
Weekly downloads
 
Created
Source

@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.

Keywords

FAQs

Package last updated on 04 Feb 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc