This repository has been archived on 2023-11-14. You can view files and clone it, but cannot push or open issues/pull-requests.
valnet/lib/upnp.js

67 lines
1.4 KiB
JavaScript

const natUpnp = require('nat-upnp');
const client = natUpnp.createClient();
module.exports.map = function(port, ttl = 10, name = 'upnp application') {
return new Promise((res, rej) => {
client.portMapping({
private: port,
public: port,
ttl,
description: name
}, (err) => {
if(err) rej(err);
res();
});
});
};
module.exports.mapIndefinite = function(port, name = 'upnp application') {
return new Promise((res, rej) => {
client.portMapping({
private: port,
public: port,
ttl: 0,
description: name
}, (err) => {
if(err) rej(err);
res();
});
});
};
module.exports.unmap = function(port) {
return new Promise((res, rej) => {
client.portUnmapping({
private: port,
public: port
}, (err) => {
if(err) rej(err);
res();
});
});
};
module.exports.mappings = function() {
return new Promise((res, rej) => {
client.getMappings((err, mappings) => {
if(err) rej(err);
res(mappings);
});
});
};
/*
(async () => {
try {
log.debug('first upnp mapping attempt...');
await client.map(this.port);
this.openServer();
} catch (e) {
log.warn(`Could not open upnp port ${this.port}`);
log.warn('Check your router is configured to allow upnp.');
log.warn('Valnet will continue to operate, but incomming')
log.warn('peer connections will not be possible.')
}
})();
*/