Skip to content

Commit

Permalink
feat: Add nat-api
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Apr 13, 2023
1 parent 222e083 commit 22a954e
Show file tree
Hide file tree
Showing 9 changed files with 1,027 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/nat-api/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Alex

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
73 changes: 73 additions & 0 deletions packages/nat-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# nat-api

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

This is a fork version of [nat-api](https://npmjs.org/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

- http://miniupnp.free.fr/nat-pmp.html
- http://wikipedia.org/wiki/NAT_Port_Mapping_Protocol
- http://tools.ietf.org/html/draft-cheshire-nat-pmp-03


## License

MIT. Copyright (c) [Alex](https://github.com/alxhotel)

[nat-api-ti]: https://img.shields.io/travis/com/alxhotel/nat-api/master.svg
[nat-api-tu]: https://travis-ci.com/alxhotel/nat-api
[nat-api-ni]: https://img.shields.io/npm/v/nat-api.svg
[nat-api-nu]: https://npmjs.org/package/nat-api
[nat-api-di]: https://david-dm.org/alxhotel/nat-api/status.svg
[nat-api-du]: https://david-dm.org/alxhotel/nat-api
[standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg
[standard-url]: https://standardjs.com
6 changes: 6 additions & 0 deletions packages/nat-api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @module @xmcl/nat-api
*/

export * from './lib/upnp'
export * from './lib/pmp'
223 changes: 223 additions & 0 deletions packages/nat-api/lib/device.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
import fxparser from 'fast-xml-parser'
import { Client, request } from 'undici'
import { IncomingMessage, request as hrequest } from 'http'
import url from 'url'

export interface ServiceInfo {
serviceType: string
serviceId: string
SCPDURL: string
controlURL: string
eventSubURL: string
}

export interface DeviceInfo {
deviceType: string
friendlyName: string
manufacturer: string
manufacturerURL: string
modelDescription: string
modelName: string
modelURL: string
serialNumber: string
UDN: string
serviceList: {
service?: ServiceInfo | ServiceInfo[]
}
deviceList: {
device?: DeviceInfo | DeviceInfo[]
}
}

function getAllServicesDevices(device: DeviceInfo) {
const services: any[] = []
const devices: any[] = []

function toArray<T>(item: T | T[]): T[] {
return Array.isArray(item) ? item : [item]
}

function traverseServices(service: ServiceInfo) {
if (!service) return
services.push(service)
}

function traverseDevices(device: DeviceInfo) {
if (!device) return
devices.push(device)

if (device.deviceList && device.deviceList.device) {
toArray(device.deviceList.device).forEach(traverseDevices)
}

if (device.serviceList && device.serviceList.service) {
toArray(device.serviceList.service).forEach(traverseServices)
}
}

traverseDevices(device)

return {
services,
devices,
}
}

export class Device {
services: string[]

private device?: DeviceInfo
private lastUpdate = 0
private ttl: number = 60 * 1000
private baseUrl = ''
private client: Client

constructor(readonly url: string) {
this.client = new Client(new URL(url).origin)
this.services = [
'urn:schemas-upnp-org:service:WANIPConnection:1',
'urn:schemas-upnp-org:service:WANIPConnection:2',
'urn:schemas-upnp-org:service:WANPPPConnection:1',
]
}

async connectDevice() {
if (this.device) {
const now = Date.now()
if (now - this.lastUpdate < this.ttl) {
// not expire
return this.device
}
}

const res = await request(this.url, { method: 'GET', dispatcher: this.client })
if (res.statusCode !== 200) {
throw new Error('Request failed: ' + res.statusCode)
}

const data = await res.body.text()

const info = new fxparser.XMLParser().parse(data)

const device = info.root.device as DeviceInfo

if (!device) {
throw new Error(`Invalid router device service! ${data.toString()}`)
}

this.device = device
this.baseUrl = info.baseUrl
this.lastUpdate = Date.now()

return device
}

async run(action: string, args: Record<string, number | string | undefined>): Promise<any> {
const info = await this._getService(this.services)

const body = '<?xml version="1.0"?>' +
'<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" ' +
's:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">' +
'<s:Body>' +
'<u:' + action + ' xmlns:u=' + JSON.stringify(info.service) + '>' +
Object.entries(args).map((args) => {
return '<' + args[0] + '>' +
(args[1] !== undefined ? args[1] : '') +
'</' + args[0] + '>'
}).join('') +
'</u:' + action + '>' +
'</s:Body>' +
'</s:Envelope>'
const req = hrequest(info.controlURL, {
method: 'POST',
headers: {
'Content-Type': 'text/xml; charset="utf-8"',
'Content-Length': Buffer.byteLength(body).toString(),
connection: 'close',
SOAPAction: JSON.stringify(info.service + '#' + action),
},
})
req.write(body)
req.end()
const res = await new Promise<IncomingMessage>((resolve, reject) => {
req.once('response', resolve)
req.once('error', reject)
})
const rbody = async () => {
return new Promise<string>((resolve, reject) => {
const buf = [] as Buffer[]
res.on('data', (d) => {
buf.push(d)
})
res.on('end', () => {
resolve(Buffer.concat(buf).toString())
})
})
}
// const res = await request(info.controlURL, {
// method: 'POST',
// dispatcher: this.client,
// headers: {
// 'Content-Type': 'text/xml; charset="utf-8"',
// 'Content-Length': Buffer.byteLength(body).toString(),
// connection: 'close',
// SOAPAction: JSON.stringify(info.service + '#' + action),
// },
// body,
// })

if (res.statusCode !== 200) {
// let body = ''
const parser = new fxparser.XMLParser()
if (res.headers['content-length'] && Number(res.headers['content-length']) > 0) {
const error = parser.parse(await rbody())['s:Envelope']['s:Body']['s:Fault']
throw Object.assign(new Error(`Upnp action ${info.service}#${action} failed: ${res.statusCode}`), error)
}
// console.log(body)
throw new Error(`Upnp action ${info.service}#${action} failed: ${res.statusCode}`)
}

const data = await rbody()
const parser = new fxparser.XMLParser({ parseAttributeValue: true, ignoreAttributes: false })

const parsedData = parser.parse(data)

const parsedBody = parsedData['s:Envelope']['s:Body']

return parsedBody
}

private async _getService(types: string[]) {
const device = await this.connectDevice()
const { services, devices } = getAllServicesDevices(device)
const s = services.filter((service) => types.indexOf(service.serviceType) !== -1)

// Use the first available service
if (s.length === 0 || !s[0].controlURL || !s[0].SCPDURL) {
throw new Error('Service not found')
}

const base = new URL(this.baseUrl || this.url)
console.log(this.url)
function addPrefix(u: string) {
let uri
try {
uri = new URL(u)
} catch (err) {
// Is only the path of the URL
uri = new URL(u, base.href)
}

uri.host = uri.host || base.host
uri.protocol = uri.protocol || base.protocol

return url.format(uri)
}

return {
service: s[0].serviceType,
SCPDURL: addPrefix(s[0].SCPDURL),
controlURL: addPrefix(s[0].controlURL),
}
}
}
Loading

0 comments on commit 22a954e

Please sign in to comment.