From 9f2554056a4ee25882ab8dc010d9dd9661289b31 Mon Sep 17 00:00:00 2001 From: Bronwen Date: Wed, 18 Nov 2020 11:18:39 -0500 Subject: [PATCH] display connected Identities --- lib/STP.js | 6 +++++- relay/index.js | 29 ++++++++++++++++++----------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/STP.js b/lib/STP.js index 03284a2..c27dd96 100644 --- a/lib/STP.js +++ b/lib/STP.js @@ -51,11 +51,15 @@ class STPSocket extends EventEmitter { return this.externalIdentity.exportKey('pkcs8-public-pem'); } + get open() { + return this.tcpSocket.readyState === 'open' + } + constructor(tcpSocket, identity) { super(); this.tcpSocket = tcpSocket; this.identity = identity; - if(tcpSocket.readyState === 'open') this.handshake(); + if(this.open) this.handshake(); else this.tcpSocket.on('connect', this.handshake.bind(this)); this.tcpSocket.on('data', this.data.bind(this)); diff --git a/relay/index.js b/relay/index.js index 64aea1c..09f4c76 100644 --- a/relay/index.js +++ b/relay/index.js @@ -6,9 +6,9 @@ const { config } = require('../package.json'); const { Identity } = require('../lib/Identity'); const stp = require('../lib/STP'); title('relay', false); - const identity = await new Identity('relay', 'default'); +const clients = []; // ==================================== [STP CLIENT] let client = null; @@ -26,31 +26,38 @@ let client = null; log.debug(data.toString()); }) })(); -// stp.connect(config.ports.relay, config.addresses.relay); - - // ==================================== [STP SERVER] const server = stp.createServer(identity, (client) => { log.info(`incomming connection from ${client.remoteAddress} ${client.remoteIdentity}`); -}); + clients.push(client); + + client.on('close', pruneClients); +}); server.listen(config.ports.relay); log.success(`STP server listening on ${config.ports.relay}`); - - - - - +function pruneClients() { + clients = clients.filter(client => client.open); +} // ==================================== [EXPRESS] const express = require('express'); const app = express(); app.get('/', (req, res) => { - res.end(Date.now().toString()); + res.end(` + + ${clients.map(client => ` + + + + + `)} +
${client.remoteAddress}${client.remoteIdentity}
+ `); }) app.listen(9999);