names over addresses...

canary
Marcus 2021-04-02 10:06:40 -04:00
parent 556b940cc1
commit 7b9af3ba47
3 changed files with 23 additions and 10 deletions

View File

@ -60,6 +60,7 @@ class STPSocket extends EventEmitter {
buffer = ''; buffer = '';
externalKey; externalKey;
identity; identity;
externalName;
CONNECTING = Symbol('connecting'); CONNECTING = Symbol('connecting');
EXCHANGE = Symbol('exchange'); EXCHANGE = Symbol('exchange');
@ -77,12 +78,16 @@ class STPSocket extends EventEmitter {
return this.tcpSocket.remoteAddress; return this.tcpSocket.remoteAddress;
} }
get remoteName() {
return this.externalName;
}
get remoteIdentity() { get remoteIdentity() {
return this.externalKey.exportKey('pkcs8-public-pem'); return this.externalKey.exportKey('pkcs8-public-pem');
} }
get open() { get open() {
return this.tcpSocket.readyState === 'open' return this.tcpSocket.readyState === 'open';
} }
get secured() { get secured() {
@ -109,6 +114,7 @@ class STPSocket extends EventEmitter {
processBuffer() { processBuffer() {
const parts = this.buffer.split(/(\x02[^\x02\x03]*\x03)/g); const parts = this.buffer.split(/(\x02[^\x02\x03]*\x03)/g);
this.buffer = ''; this.buffer = '';
for(const message of parts) { for(const message of parts) {
if(message.endsWith('\x03')) { if(message.endsWith('\x03')) {
const obj = JSON.parse(message.substr(1, message.length - 2)); 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') { if(this.readyState === this.CONNECTING && obj.cmd === 'KEY') {
this.externalKey = new NodeRSA(); this.externalKey = new NodeRSA();
this.externalKey.importKey(obj.data.key, 'pkcs8-public-pem'); this.externalKey.importKey(obj.data.key, 'pkcs8-public-pem');
this.externalName = obj.meta.name;
this.tcpSocket.write(new AckPacket().toBuffer()); this.tcpSocket.write(new AckPacket().toBuffer());
this.readyState = this.EXCHANGE; this.readyState = this.EXCHANGE;
return; return;

View File

@ -30,10 +30,14 @@ function basicPacket(commandName) {
// #region === [ exotic packet classes ] === // #region === [ exotic packet classes ] ===
class KeyExchangePacket extends STPPacket { class KeyExchangePacket extends STPPacket {
constructor(key, type = 'pkcs8-pem') { constructor(key, {
type = 'pkcs8-pem',
name = 'anonymous'
} = {}) {
super(); super();
this.cmd = 'KEY'; this.cmd = 'KEY';
this.data.key = key; this.data.key = key;
this.meta.name = name;
this.meta.type = type; this.meta.type = type;
} }
} }
@ -83,7 +87,7 @@ function reconstructPacket(packet) {
const obj = JSON.parse(packet); const obj = JSON.parse(packet);
switch(obj.cmd) { 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 'NODES': return new ClientsPacket(obj.data.clients);
case 'QNODES': return new GetClientsPacket(); case 'QNODES': return new GetClientsPacket();
case 'ACK': return new AckPacket(); case 'ACK': return new AckPacket();

View File

@ -18,6 +18,7 @@ class Node extends EventEmitter {
multicastAd = null; multicastAd = null;
multicastBrowser = null; multicastBrowser = null;
connected = false; connected = false;
multicastDevices = [];
constructor(identity) { constructor(identity) {
super(); super();
@ -46,17 +47,18 @@ class Node extends EventEmitter {
identity: this.identity, identity: this.identity,
port: this.port port: this.port
}, (connection) => { }, (connection) => {
log.info('incomming connection from ' + connection.remoteAddress); log.info('incomming connection from ' + connection.remoteName);
}); });
log.info('advertising node on multicast...') log.info('advertising node on multicast...')
this.multicastAd = bonjour.publish({ this.multicastAd = bonjour.publish({
name: this.name, name: this.name,
type: 'STP', type: 'stp',
port: this.port 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('up', this.serviceUp.bind(this));
this.multicastBrowser.on('down', this.serviceDown.bind(this)); this.multicastBrowser.on('down', this.serviceDown.bind(this));
@ -65,11 +67,11 @@ class Node extends EventEmitter {
} }
async serviceUp({name, address, port, protocol}) { 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) { async serviceDown({name, address, port, protocol}) {
log.debug('down', service); log.debug(`Lost ${name} @ ${address}:${port} using ${protocol}`);
} }
async negotiatePort() { async negotiatePort() {