Node.js package built for managing a router using UPnP.
Install upnpjs Library
npm install upnpjs
Install upnp CLI
# Use `upnp -h` to show help menu
npm install upnpjs -g
Discover the Internet Gateway Device
const upnp = require('upnpjs');
const igd = await upnp.discover();
And that's all there is to it. Follow the documentation bellow to see what can be done with the newly discovered Internet Gateway Device.
InternetGatewayDevice allows access to different services potentially allowed by the device
Service action for mapping ports on the router
await igd.addPortMapping({
ip: 192.168.0.4,
internalPort: 54321,
externalPort: 54321,
protocol: 'TCP', // Defaults to TCP
description: 'Example port map from 54321 -> 54321', // Defaults to empty string
enabled: false // Defaults to true
});
Service action for removing port mappings on the router
await igd.deletePortMapping({
externalPort: 12345,
protocol: 'UDP'
});
Service action for retrieving external ip address
const ip = await igd.getExternalIPAddress();
Service action for listing all currently mapped ports on the router
const list = await igd.getPortMappingList();
Service action for remtrieving a single port map by index
const portmap = await igd.getPortMapping(2);
Documents
UPnP Architecture | Service Template | UPnP Background | Internet Gateway Device
Code Samples
miniupnp | TinyUPnP | weupnp | upnpclient | Node.js and SSDP