UNPKG

@silentbot1/nat-api

Version:

Port mapping with UPnP and NAT-PMP

75 lines (68 loc) 2.13 kB
import NatAPI from '../index.js' // For NAT-UPNP only, use: // const client = new NatAPI() const client = new NatAPI({ enablePMP: false, enableUPNP: true, upnpPermanentFallback: false }) // Map public port 1000 to private port 1000 with UDP and TCP // client.map(1000).then((res) => { // console.log('Port mapped!', res) // // Unmap port public and private port 1000 with UDP and TCP // try { // client.unmap(1000).then((res) => { // console.log('Port unmapped!', res) // // client.destroy() // }).catch((err) => { // return console.log('Error', err) // // client.destroy() // }) // } catch (e) {} // }).catch((err) => { // return console.log('Error', err) // }) // // client.map(1005).then((res) => { // console.log('Port mapped!', res) // }).catch((err) => { // return console.log('Error', err) // }) // // client.map(1010).then((res) => { // console.log('Port mapped!', res) // }).catch((err) => { // return console.log('Error', err) // }) // // // Map public port 2000 to private port 3000 with UDP and TCP // client.map(2000, 3000).then((res) => { // console.log('Port mapped!', res) // // client.destroy() // }).catch((err) => { // return console.log('Error', err) // }) // // // Map public port 4000 to private port 5000 with only UDP // client.map({ publicPort: 4000, privatePort: 5000, ttl: 1800, protocol: 'UDP' }).then((res) => { // console.log('Port mapped!', res) // // client.destroy() // }).catch((err) => { // return console.log('Error', err) // }) // Get external IP client.externalIp().then((ip) => { console.log('External IP:', ip) // client.destroy() }).catch((err) => { return console.log('Error', err) }) setTimeout(() => { client.destroy().then((res) => { console.log('Client has been destroyed!', res) }).catch((err) => { return console.log('Error', err) }) }, 15000) // Destroy object // client.destroy().then((res) => { // console.log('Client has been destroyed!', res) // }).catch((err) => { // return console.log('Error', err) // })