Skip to content

nat-api

Fast port mapping with UPnP and NAT-PMP in NodeJS.

This is a fork version of nat-api.

The major differences are:

  • Use typescript, so has type definition.
  • Split PMP and upnp API, so you can use them individually.
  • Support ESM.
  • Optimize the dependencies. (only 2 up-to-dated deps)

Install

Required: NodeJS >= 16

sh
npm install @xmcl/nat-api

Usage

js
const { createUpnpClient } = require('nat-api')

const client = await createUpnpClient()

// map 25565 to 25565 for 1 min:
await client.map({
  description: "Mapped by @xmcl/nat-api",
  protocol: 'tcp',
  public: 25565,
  private: 25565,
  ttl: 60 * 1000,
})

// Unmap port public and private port 25565 with TCP by default
await client.unmap({ port: 25565 })

// Get external IP
const mappings = await client.getMappings()

// see existed mappings
console.log(mappings)

// Destroy client
client.destroy()

API

See type definition in typescript.

Additional Information

License

MIT. Copyright (c) Alex

🧾 Classes

🤝 Interfaces

🏭 Functions

createPmpClient

ts
createPmpClient(gateway: string): Promise<PmpClient>

Parameters

  • gateway: string

Return Type

  • Promise<PmpClient>

Defined in: packages/nat-api/lib/pmp.ts:26

createSsdp

ts
createSsdp(options: Object= {}): Promise<Ssdp>

Parameters

  • options: Object

Return Type

  • Promise<Ssdp>

Defined in: packages/nat-api/lib/ssdp.ts:14

createUpnpClient

ts
createUpnpClient(): Promise<UpnpClient>

Return Type

  • Promise<UpnpClient>

Defined in: packages/nat-api/lib/upnp.ts:7

🏷️ Variables

ERROR_GATEWAY_NOTFOUND const

ts
ERROR_GATEWAY_NOTFOUND: "GatewayNotFound" = 'GatewayNotFound'

Defined in: packages/nat-api/lib/upnp.ts:13