name upnp naming scheme (fs compatible)
parent
e0b837876d
commit
a105e5aa93
|
|
@ -69,6 +69,6 @@ module.exports.Identity = class Identity {
|
||||||
}
|
}
|
||||||
|
|
||||||
toString() {
|
toString() {
|
||||||
return `[Identity(${this.name})]`;
|
return `[Identity ${this.name}]`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
const net = require('net');
|
const net = require('net');
|
||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
const NodeRSA = require('node-rsa');
|
const NodeRSA = require('node-rsa');
|
||||||
const log = require('signale').scope('stp');
|
const log = require('signale').scope('_STP');
|
||||||
const {
|
const {
|
||||||
KeyExchangePacket,
|
KeyExchangePacket,
|
||||||
AckPacket
|
AckPacket
|
||||||
|
|
@ -36,7 +36,7 @@ class Server extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
openServer() {
|
openServer() {
|
||||||
log.info(`opening STP server on ${this.port}`);
|
// log.info(`opening STP server on ${this.port}`);
|
||||||
this.tcpServer = net.createServer(this.tcpConnectClient.bind(this));
|
this.tcpServer = net.createServer(this.tcpConnectClient.bind(this));
|
||||||
this.tcpServer.on('error', e => {
|
this.tcpServer.on('error', e => {
|
||||||
log.warn(e)
|
log.warn(e)
|
||||||
|
|
|
||||||
74
lib/node.js
74
lib/node.js
|
|
@ -4,6 +4,8 @@ const upnp = require('./upnp');
|
||||||
const md5 = require('md5');
|
const md5 = require('md5');
|
||||||
const pkg = require('./../package.json');
|
const pkg = require('./../package.json');
|
||||||
const { config, write } = require('./config.js');
|
const { config, write } = require('./config.js');
|
||||||
|
const log = require('signale').scope('NODE');
|
||||||
|
const { createAdvertisement, tcp, browseThemAll } = require('mdns');
|
||||||
|
|
||||||
class Node extends EventEmitter {
|
class Node extends EventEmitter {
|
||||||
clients = [];
|
clients = [];
|
||||||
|
|
@ -12,66 +14,84 @@ class Node extends EventEmitter {
|
||||||
readyPromise = null;
|
readyPromise = null;
|
||||||
port = null;
|
port = null;
|
||||||
identity;
|
identity;
|
||||||
|
multicastAd = null;
|
||||||
|
multicastBrowser = null;
|
||||||
|
connected = false;
|
||||||
|
|
||||||
constructor(identity) {
|
constructor(identity) {
|
||||||
super();
|
super();
|
||||||
this.identity = identity;
|
this.identity = identity;
|
||||||
this.hash = md5(identity.publicKey);
|
this.hash = md5(identity.publicKey);
|
||||||
this.name = `valnet node - ${this.hash}`;
|
this.name = `valnet-node-${identity.name}`;
|
||||||
// stp.createServer({
|
|
||||||
// identity,
|
this.readyPromise = this.negotiatePort()
|
||||||
// port
|
.then(this.startServer.bind(this))
|
||||||
// }, socket => {
|
.catch(this.serverStartupFailed.bind(this))
|
||||||
// log.info('secured connection from ' + socket.remoteAddress);
|
.then(this.connectNetwork.bind(this))
|
||||||
// this.clients.push(socket);
|
|
||||||
// });
|
|
||||||
this.readyPromise = this.negotiatePort().then(this.startServer.bind(this));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
startServer() {
|
async connectNetwork() {
|
||||||
|
|
||||||
console.log('creating server on port ' + this.port);
|
}
|
||||||
|
|
||||||
|
async serverStartupFailed(error) {
|
||||||
|
log.warn('Failed to set up Valet server on node.')
|
||||||
|
}
|
||||||
|
|
||||||
|
async startServer() {
|
||||||
|
log.info('creating Valnet Node on port ' + this.port + '...');
|
||||||
|
|
||||||
stp.createServer({
|
stp.createServer({
|
||||||
identity: this.identity,
|
identity: this.identity,
|
||||||
port: this.port
|
port: this.port
|
||||||
}, (connection) => {
|
}, (connection) => {
|
||||||
console.log('incomming connection...');
|
log.info('incomming connection from ' + connection.remoteAddress);
|
||||||
console.dir(connection);
|
});
|
||||||
})
|
|
||||||
|
log.info('advertising node on multicast...')
|
||||||
|
this.multicastAd = createAdvertisement(tcp('STP'), this.port, {
|
||||||
|
name: 'xyz.valnet.node',
|
||||||
|
});
|
||||||
|
|
||||||
|
this.multicastBrowser = browseThemAll();
|
||||||
|
|
||||||
|
this.multicastBrowser.on('serviceUp', this.serviceUp.bind(this));
|
||||||
|
this.multicastBrowser.on('serviceDown', this.serviceDown.bind(this));
|
||||||
|
|
||||||
|
// log.success('Node successfully registered!');
|
||||||
|
}
|
||||||
|
|
||||||
|
async serviceUp(service) {
|
||||||
|
log.debug('up', service);
|
||||||
|
}
|
||||||
|
|
||||||
|
async serviceDown(service) {
|
||||||
|
log.debug('down', service);
|
||||||
}
|
}
|
||||||
|
|
||||||
async negotiatePort() {
|
async negotiatePort() {
|
||||||
// await upnp.map(5600, 60 * 5, 'other application');
|
|
||||||
|
|
||||||
const mappings = await upnp.mappings();
|
const mappings = await upnp.mappings();
|
||||||
|
|
||||||
const matchingMappings = mappings.filter(mapping => {
|
const matchingMappings = mappings.filter(mapping => {
|
||||||
return mapping.description === this.name
|
return mapping.description === this.name
|
||||||
});
|
});
|
||||||
const alreadyMapped = matchingMappings.length > 0;
|
const alreadyMapped = matchingMappings.length > 0;
|
||||||
|
const takenPorts = mappings.map(mapping => mapping.public.port);
|
||||||
|
|
||||||
if(alreadyMapped) {
|
if(alreadyMapped) {
|
||||||
console.log('already mapped!');
|
|
||||||
this.port = matchingMappings[0].public.port;
|
this.port = matchingMappings[0].public.port;
|
||||||
|
log.success(`upnp port ${this.port} already registered!`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const takenPorts = mappings.map(mapping => mapping.public.port);
|
|
||||||
|
|
||||||
for(let port = config.ports.relay; port <= config.ports.relayEnd; port ++) {
|
for(let port = config.ports.relay; port <= config.ports.relayEnd; port ++) {
|
||||||
if(takenPorts.indexOf(port) === -1) {
|
if(takenPorts.indexOf(port) === -1) {
|
||||||
console.log('registering to port ' + port);
|
|
||||||
await upnp.mapIndefinite(port, this.name);
|
await upnp.mapIndefinite(port, this.name);
|
||||||
this.port = port;
|
this.port = port;
|
||||||
break;
|
log.success(`registered upnp port ${this.port}`);
|
||||||
} else {
|
return;
|
||||||
console.log('port ' + port + ' is taken...');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// console.log(mappings, this.hash);
|
// console.log(mappings, this.hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,7 @@
|
||||||
"keyv": "^4.0.3",
|
"keyv": "^4.0.3",
|
||||||
"keyv-file": "^0.2.0",
|
"keyv-file": "^0.2.0",
|
||||||
"md5": "^2.3.0",
|
"md5": "^2.3.0",
|
||||||
|
"mdns": "^2.7.2",
|
||||||
"nat-upnp": "^1.1.1",
|
"nat-upnp": "^1.1.1",
|
||||||
"nedb": "^1.8.0",
|
"nedb": "^1.8.0",
|
||||||
"node-rsa": "^1.1.1",
|
"node-rsa": "^1.1.1",
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
(async () => {
|
(async () => {
|
||||||
const { title } = require('../lib/title');
|
const { title } = require('../lib/title');
|
||||||
const net = require('net');
|
const net = require('net');
|
||||||
const log = require('signale').scope('relay');
|
const log = require('signale').scope('RLAY');
|
||||||
const { Identity } = require('../lib/Identity');
|
const { Identity } = require('../lib/Identity');
|
||||||
const stp = require('../lib/STP');
|
const stp = require('../lib/STP');
|
||||||
title('relay', false);
|
title('relay', false);
|
||||||
|
|
@ -22,7 +22,7 @@ function connectNetwork(t = 1000) {
|
||||||
if(t > 60000) t /= 2;
|
if(t > 60000) t /= 2;
|
||||||
|
|
||||||
const poopoo = config.endpoints[0].split(':');
|
const poopoo = config.endpoints[0].split(':');
|
||||||
console.log(poopoo);
|
// console.log(poopoo);
|
||||||
|
|
||||||
const client = stp.connect({
|
const client = stp.connect({
|
||||||
identity,
|
identity,
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
(async () => {
|
(async () => {
|
||||||
const log = require('signale').scope('service');
|
const log = require('signale').scope('SRVC');
|
||||||
const { execSync, spawn } = require('child_process');
|
const { execSync, spawn } = require('child_process');
|
||||||
const branch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
|
const branch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
|
||||||
let proc;
|
let proc;
|
||||||
|
|
|
||||||
15
yarn.lock
15
yarn.lock
|
|
@ -119,6 +119,10 @@ binary-search-tree@0.2.5:
|
||||||
dependencies:
|
dependencies:
|
||||||
underscore "~1.4.4"
|
underscore "~1.4.4"
|
||||||
|
|
||||||
|
bindings@~1.2.1:
|
||||||
|
version "1.2.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.2.1.tgz#14ad6113812d2d37d72e67b4cacb4bb726505f11"
|
||||||
|
|
||||||
body-parser@1.19.0:
|
body-parser@1.19.0:
|
||||||
version "1.19.0"
|
version "1.19.0"
|
||||||
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a"
|
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a"
|
||||||
|
|
@ -920,6 +924,13 @@ md5@^2.3.0:
|
||||||
crypt "0.0.2"
|
crypt "0.0.2"
|
||||||
is-buffer "~1.1.6"
|
is-buffer "~1.1.6"
|
||||||
|
|
||||||
|
mdns@^2.7.2:
|
||||||
|
version "2.7.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/mdns/-/mdns-2.7.2.tgz#1629077b950e2a9bdb02e370d505ff4fa2ace0ff"
|
||||||
|
dependencies:
|
||||||
|
bindings "~1.2.1"
|
||||||
|
nan "^2.14.0"
|
||||||
|
|
||||||
media-typer@0.3.0:
|
media-typer@0.3.0:
|
||||||
version "0.3.0"
|
version "0.3.0"
|
||||||
resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
|
resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
|
||||||
|
|
@ -992,6 +1003,10 @@ mute-stream@0.0.8:
|
||||||
version "0.0.8"
|
version "0.0.8"
|
||||||
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.8.tgz#1630c42b2251ff81e2a283de96a5497ea92e5e0d"
|
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.8.tgz#1630c42b2251ff81e2a283de96a5497ea92e5e0d"
|
||||||
|
|
||||||
|
nan@^2.14.0:
|
||||||
|
version "2.14.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.2.tgz#f5376400695168f4cc694ac9393d0c9585eeea19"
|
||||||
|
|
||||||
nat-upnp@^1.1.1:
|
nat-upnp@^1.1.1:
|
||||||
version "1.1.1"
|
version "1.1.1"
|
||||||
resolved "https://registry.yarnpkg.com/nat-upnp/-/nat-upnp-1.1.1.tgz#b18365e4faf44652549bb593c69e6b690df22043"
|
resolved "https://registry.yarnpkg.com/nat-upnp/-/nat-upnp-1.1.1.tgz#b18365e4faf44652549bb593c69e6b690df22043"
|
||||||
|
|
|
||||||
Reference in New Issue