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/node.js

69 lines
1.5 KiB
JavaScript
Raw Normal View History

2021-02-13 23:56:20 -05:00
const EventEmitter = require('events')
2021-04-01 01:04:17 -04:00
const stp = require('./STP');
const upnp = require('./upnp');
const md5 = require('md5');
const pkg = require('./../package.json');
const { config, write } = require('./config.js');
2021-02-13 23:56:20 -05:00
class Node extends EventEmitter {
2021-04-01 01:04:17 -04:00
clients = [];
hash = null;
name = null;
readyPromise = null;
constructor(identity) {
super();
this.hash = md5(identity.publicKey);
this.name = `valnet node - ${this.hash}`;
// stp.createServer({
// identity,
// port
// }, socket => {
// log.info('secured connection from ' + socket.remoteAddress);
// this.clients.push(socket);
// });
this.readyPromise = this.negotiatePort();
}
async negotiatePort() {
2021-04-01 01:10:23 -04:00
// await upnp.map(5600, 60 * 5, 'other application');
2021-04-01 01:04:17 -04:00
const mappings = await upnp.mappings();
const alreadyMapped = mappings.filter(mapping => {
return mapping.description === this.name
}).length > 0;
if(alreadyMapped) {
console.log('already mapped!');
return;
}
const takenPorts = mappings.map(mapping => mapping.public.port);
for(let port = config.ports.relay; port <= config.ports.relayEnd; port ++) {
if(takenPorts.indexOf(port) === -1) {
console.log('registering to port ' + port);
await upnp.map(port, 10, this.name);
break;
} else {
console.log('port ' + port + ' is taken...');
}
}
// console.log(mappings, this.hash);
2021-02-13 23:56:20 -05:00
}
2021-03-20 10:36:40 -04:00
static get Node() {
return Node;
}
2021-04-01 01:04:17 -04:00
get ready() {
return this.readyPromise;
}
2021-02-13 23:56:20 -05:00
}
2021-03-20 10:36:40 -04:00
module.exports = Node;