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

111 lines
2.8 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');
const log = require('signale').scope('NODE');
2021-04-01 15:40:09 -04:00
const bonjour = require('bonjour')();
2021-04-01 21:15:41 -04:00
const Gateway = require('./Gateway');
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;
multicastAd = null;
multicastBrowser = null;
connected = false;
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-${identity.name}`;
this.readyPromise = this.negotiatePort()
.then(this.startServer.bind(this))
.catch(this.serverStartupFailed.bind(this))
.then(this.connectNetwork.bind(this))
2021-04-01 01:22:48 -04:00
}
async connectNetwork() {
2021-04-01 21:15:41 -04:00
const gateway = new Gateway(this.identity, config.endpoints);
}
async serverStartupFailed(error) {
log.warn('Failed to set up Valet server on node.')
}
async startServer() {
log.info('creating Valnet Node on port ' + this.port + '...');
2021-04-01 01:22:48 -04:00
stp.createServer({
identity: this.identity,
port: this.port
}, (connection) => {
log.info('incomming connection from ' + connection.remoteAddress);
});
log.info('advertising node on multicast...')
2021-04-01 15:40:09 -04:00
this.multicastAd = bonjour.publish({
name: this.name,
type: 'STP',
port: this.port
});
2021-04-01 15:40:09 -04:00
this.multicastBrowser = bonjour.find({});
2021-04-01 15:40:09 -04:00
this.multicastBrowser.on('up', this.serviceUp.bind(this));
this.multicastBrowser.on('down', this.serviceDown.bind(this));
// log.success('Node successfully registered!');
2021-04-01 01:04:17 -04:00
}
2021-04-01 15:40:09 -04:00
async serviceUp({name, address, port, protocol}) {
2021-04-01 21:15:41 -04:00
// log.debug(`Found ${name} @ ${address}:${port} using ${protocol}`);
}
2021-04-01 01:04:17 -04:00
async serviceDown(service) {
log.debug('down', service);
}
2021-04-01 01:04:17 -04:00
async negotiatePort() {
const mappings = await upnp.mappings();
2021-04-01 01:24:54 -04:00
const matchingMappings = mappings.filter(mapping => {
2021-04-01 01:04:17 -04:00
return mapping.description === this.name
2021-04-01 01:24:54 -04:00
});
const alreadyMapped = matchingMappings.length > 0;
const takenPorts = mappings.map(mapping => mapping.public.port);
2021-04-01 01:04:17 -04:00
if(alreadyMapped) {
2021-04-01 01:24:54 -04:00
this.port = matchingMappings[0].public.port;
log.success(`upnp port ${this.port} already registered!`);
2021-04-01 01:04:17 -04:00
return;
}
for(let port = config.ports.relay; port <= config.ports.relayEnd; port ++) {
if(takenPorts.indexOf(port) === -1) {
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;
log.success(`registered upnp port ${this.port}`);
return;
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;