From 7b9af3ba4772f6ea8ce0ae51404a3630d12db121 Mon Sep 17 00:00:00 2001 From: Marcus Date: Fri, 2 Apr 2021 10:06:40 -0400 Subject: [PATCH] names over addresses... --- lib/STP/index.js | 9 ++++++++- lib/STP/packets.js | 8 ++++++-- lib/node.js | 16 +++++++++------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/STP/index.js b/lib/STP/index.js index 64bd6f2..0c3c8ad 100644 --- a/lib/STP/index.js +++ b/lib/STP/index.js @@ -60,6 +60,7 @@ class STPSocket extends EventEmitter { buffer = ''; externalKey; identity; + externalName; CONNECTING = Symbol('connecting'); EXCHANGE = Symbol('exchange'); @@ -77,12 +78,16 @@ class STPSocket extends EventEmitter { return this.tcpSocket.remoteAddress; } + get remoteName() { + return this.externalName; + } + get remoteIdentity() { return this.externalKey.exportKey('pkcs8-public-pem'); } get open() { - return this.tcpSocket.readyState === 'open' + return this.tcpSocket.readyState === 'open'; } get secured() { @@ -109,6 +114,7 @@ class STPSocket extends EventEmitter { processBuffer() { const parts = this.buffer.split(/(\x02[^\x02\x03]*\x03)/g); this.buffer = ''; + for(const message of parts) { if(message.endsWith('\x03')) { const obj = JSON.parse(message.substr(1, message.length - 2)); @@ -124,6 +130,7 @@ class STPSocket extends EventEmitter { if(this.readyState === this.CONNECTING && obj.cmd === 'KEY') { this.externalKey = new NodeRSA(); this.externalKey.importKey(obj.data.key, 'pkcs8-public-pem'); + this.externalName = obj.meta.name; this.tcpSocket.write(new AckPacket().toBuffer()); this.readyState = this.EXCHANGE; return; diff --git a/lib/STP/packets.js b/lib/STP/packets.js index 58b1832..36729c3 100644 --- a/lib/STP/packets.js +++ b/lib/STP/packets.js @@ -30,10 +30,14 @@ function basicPacket(commandName) { // #region === [ exotic packet classes ] === class KeyExchangePacket extends STPPacket { - constructor(key, type = 'pkcs8-pem') { + constructor(key, { + type = 'pkcs8-pem', + name = 'anonymous' + } = {}) { super(); this.cmd = 'KEY'; this.data.key = key; + this.meta.name = name; this.meta.type = type; } } @@ -83,7 +87,7 @@ function reconstructPacket(packet) { const obj = JSON.parse(packet); switch(obj.cmd) { - case 'KEY': return new KeyExchangePacket(obj.data.key, obj.meta.type); + case 'KEY': return new KeyExchangePacket(obj.data.key, obj.meta); case 'NODES': return new ClientsPacket(obj.data.clients); case 'QNODES': return new GetClientsPacket(); case 'ACK': return new AckPacket(); diff --git a/lib/node.js b/lib/node.js index 233e1d7..ea05dc2 100644 --- a/lib/node.js +++ b/lib/node.js @@ -18,6 +18,7 @@ class Node extends EventEmitter { multicastAd = null; multicastBrowser = null; connected = false; + multicastDevices = []; constructor(identity) { super(); @@ -46,17 +47,18 @@ class Node extends EventEmitter { identity: this.identity, port: this.port }, (connection) => { - log.info('incomming connection from ' + connection.remoteAddress); + log.info('incomming connection from ' + connection.remoteName); }); log.info('advertising node on multicast...') this.multicastAd = bonjour.publish({ name: this.name, - type: 'STP', - port: this.port + type: 'stp', + port: this.port, + protocol: 'tcp' }); - this.multicastBrowser = bonjour.find({}); + this.multicastBrowser = bonjour.find({type: 'stp'}); this.multicastBrowser.on('up', this.serviceUp.bind(this)); this.multicastBrowser.on('down', this.serviceDown.bind(this)); @@ -65,11 +67,11 @@ class Node extends EventEmitter { } async serviceUp({name, address, port, protocol}) { - // log.debug(`Found ${name} @ ${address}:${port} using ${protocol}`); + log.debug(`Found ${name} @ ${address}:${port} using ${protocol}`); } - async serviceDown(service) { - log.debug('down', service); + async serviceDown({name, address, port, protocol}) { + log.debug(`Lost ${name} @ ${address}:${port} using ${protocol}`); } async negotiatePort() {