display connected Identities
parent
4aa755f9ac
commit
9f2554056a
|
|
@ -51,11 +51,15 @@ class STPSocket extends EventEmitter {
|
||||||
return this.externalIdentity.exportKey('pkcs8-public-pem');
|
return this.externalIdentity.exportKey('pkcs8-public-pem');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get open() {
|
||||||
|
return this.tcpSocket.readyState === 'open'
|
||||||
|
}
|
||||||
|
|
||||||
constructor(tcpSocket, identity) {
|
constructor(tcpSocket, identity) {
|
||||||
super();
|
super();
|
||||||
this.tcpSocket = tcpSocket;
|
this.tcpSocket = tcpSocket;
|
||||||
this.identity = identity;
|
this.identity = identity;
|
||||||
if(tcpSocket.readyState === 'open') this.handshake();
|
if(this.open) this.handshake();
|
||||||
else this.tcpSocket.on('connect', this.handshake.bind(this));
|
else this.tcpSocket.on('connect', this.handshake.bind(this));
|
||||||
|
|
||||||
this.tcpSocket.on('data', this.data.bind(this));
|
this.tcpSocket.on('data', this.data.bind(this));
|
||||||
|
|
|
||||||
|
|
@ -6,9 +6,9 @@ const { config } = require('../package.json');
|
||||||
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);
|
||||||
|
|
||||||
const identity = await new Identity('relay', 'default');
|
const identity = await new Identity('relay', 'default');
|
||||||
|
|
||||||
|
const clients = [];
|
||||||
|
|
||||||
// ==================================== [STP CLIENT]
|
// ==================================== [STP CLIENT]
|
||||||
let client = null;
|
let client = null;
|
||||||
|
|
@ -26,31 +26,38 @@ let client = null;
|
||||||
log.debug(data.toString());
|
log.debug(data.toString());
|
||||||
})
|
})
|
||||||
})();
|
})();
|
||||||
// stp.connect(config.ports.relay, config.addresses.relay);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ==================================== [STP SERVER]
|
// ==================================== [STP SERVER]
|
||||||
const server = stp.createServer(identity, (client) => {
|
const server = stp.createServer(identity, (client) => {
|
||||||
log.info(`incomming connection from ${client.remoteAddress}
|
log.info(`incomming connection from ${client.remoteAddress}
|
||||||
${client.remoteIdentity}`);
|
${client.remoteIdentity}`);
|
||||||
});
|
|
||||||
|
|
||||||
|
clients.push(client);
|
||||||
|
|
||||||
|
client.on('close', pruneClients);
|
||||||
|
});
|
||||||
server.listen(config.ports.relay);
|
server.listen(config.ports.relay);
|
||||||
log.success(`STP server listening on ${config.ports.relay}`);
|
log.success(`STP server listening on ${config.ports.relay}`);
|
||||||
|
|
||||||
|
function pruneClients() {
|
||||||
|
clients = clients.filter(client => client.open);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ==================================== [EXPRESS]
|
// ==================================== [EXPRESS]
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
app.get('/', (req, res) => {
|
app.get('/', (req, res) => {
|
||||||
res.end(Date.now().toString());
|
res.end(`
|
||||||
|
<table>
|
||||||
|
${clients.map(client => `
|
||||||
|
<tr>
|
||||||
|
<td>${client.remoteAddress}</td>
|
||||||
|
<td>${client.remoteIdentity}</td>
|
||||||
|
</tr>
|
||||||
|
`)}
|
||||||
|
</table>
|
||||||
|
`);
|
||||||
})
|
})
|
||||||
|
|
||||||
app.listen(9999);
|
app.listen(9999);
|
||||||
|
|
|
||||||
Reference in New Issue