names over addresses...
parent
556b940cc1
commit
7b9af3ba47
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
16
lib/node.js
16
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() {
|
||||
|
|
|
|||
Reference in New Issue