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;
|
2021-04-01 01:19:00 -04:00
|
|
|
port = null;
|
|
|
|
|
identity;
|
2021-04-01 01:04:17 -04:00
|
|
|
|
|
|
|
|
constructor(identity) {
|
|
|
|
|
super();
|
2021-04-01 01:19:00 -04:00
|
|
|
this.identity = identity;
|
2021-04-01 01:04:17 -04:00
|
|
|
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);
|
|
|
|
|
// });
|
2021-04-01 01:22:48 -04:00
|
|
|
this.readyPromise = this.negotiatePort().then(this.startServer.bind(this));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
startServer() {
|
|
|
|
|
|
|
|
|
|
console.log('creating server on port ' + this.port);
|
|
|
|
|
|
|
|
|
|
stp.createServer({
|
|
|
|
|
identity: this.identity,
|
|
|
|
|
port: this.port
|
|
|
|
|
}, (connection) => {
|
|
|
|
|
console.log('incomming connection...');
|
|
|
|
|
console.dir(connection);
|
|
|
|
|
})
|
2021-04-01 01:04:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2021-04-01 01:13:07 -04:00
|
|
|
await upnp.mapIndefinite(port, this.name);
|
2021-04-01 01:19:00 -04:00
|
|
|
this.port = port;
|
2021-04-01 01:04:17 -04:00
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
console.log('port ' + port + ' is taken...');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-01 01:20:23 -04:00
|
|
|
|
2021-04-01 01:04:17 -04:00
|
|
|
|
|
|
|
|
// 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;
|