2020-11-13 10:19:44 -05:00
|
|
|
(async () => {
|
2020-11-11 19:38:39 -05:00
|
|
|
const { title } = require('../lib/title');
|
|
|
|
|
const net = require('net');
|
2020-11-11 22:06:45 -05:00
|
|
|
const log = require('signale').scope('relay');
|
2020-11-13 10:19:44 -05:00
|
|
|
const { config } = require('../package.json');
|
2020-11-12 11:43:35 -05:00
|
|
|
const { Identity } = require('../lib/Identity');
|
|
|
|
|
const stp = require('../lib/STP');
|
2020-11-11 22:06:45 -05:00
|
|
|
title('relay', false);
|
2020-11-13 10:19:44 -05:00
|
|
|
const identity = await new Identity('relay', 'default');
|
2020-11-12 11:43:35 -05:00
|
|
|
|
2020-11-18 11:18:39 -05:00
|
|
|
const clients = [];
|
2020-11-12 11:43:35 -05:00
|
|
|
|
2020-11-13 10:19:44 -05:00
|
|
|
// ==================================== [STP CLIENT]
|
|
|
|
|
let client = null;
|
|
|
|
|
(function tryConnect() {
|
|
|
|
|
client = stp.connect(identity, config.ports.relay, config.addresses.relay);
|
|
|
|
|
client.on('ready', () => {
|
|
|
|
|
log.success(`connected to ${config.addresses.relay}`);
|
|
|
|
|
})
|
|
|
|
|
client.on('error', () => {
|
|
|
|
|
log.error(`connection error on ${config.addresses.relay}`)
|
|
|
|
|
client = null;
|
|
|
|
|
setTimeout(tryConnect, 1000);
|
|
|
|
|
});
|
|
|
|
|
client.on('data', (data) => {
|
|
|
|
|
log.debug(data.toString());
|
|
|
|
|
})
|
|
|
|
|
})();
|
2020-11-12 11:43:35 -05:00
|
|
|
|
2020-11-13 10:19:44 -05:00
|
|
|
// ==================================== [STP SERVER]
|
|
|
|
|
const server = stp.createServer(identity, (client) => {
|
|
|
|
|
log.info(`incomming connection from ${client.remoteAddress}
|
|
|
|
|
${client.remoteIdentity}`);
|
2020-11-12 11:43:35 -05:00
|
|
|
|
2020-11-18 11:18:39 -05:00
|
|
|
clients.push(client);
|
|
|
|
|
|
|
|
|
|
client.on('close', pruneClients);
|
|
|
|
|
});
|
2020-11-13 10:19:44 -05:00
|
|
|
server.listen(config.ports.relay);
|
|
|
|
|
log.success(`STP server listening on ${config.ports.relay}`);
|
2020-11-12 11:43:35 -05:00
|
|
|
|
2020-11-18 11:18:39 -05:00
|
|
|
function pruneClients() {
|
|
|
|
|
clients = clients.filter(client => client.open);
|
|
|
|
|
}
|
2020-11-13 10:19:44 -05:00
|
|
|
|
|
|
|
|
// ==================================== [EXPRESS]
|
2020-11-11 22:21:35 -05:00
|
|
|
const express = require('express');
|
|
|
|
|
const app = express();
|
2020-11-11 21:47:54 -05:00
|
|
|
|
2020-11-11 22:21:35 -05:00
|
|
|
app.get('/', (req, res) => {
|
2020-11-18 11:18:39 -05:00
|
|
|
res.end(`
|
2020-11-18 11:42:03 -05:00
|
|
|
<table style="width: 100%">
|
2020-11-18 11:18:39 -05:00
|
|
|
${clients.map(client => `
|
|
|
|
|
<tr>
|
2020-11-18 11:42:03 -05:00
|
|
|
<td><pre>${client.remoteAddress}</pre></td>
|
|
|
|
|
<td><pre>${client.remoteIdentity}</pre></td>
|
2020-11-18 11:18:39 -05:00
|
|
|
</tr>
|
|
|
|
|
`)}
|
|
|
|
|
</table>
|
|
|
|
|
`);
|
2020-11-11 22:21:35 -05:00
|
|
|
})
|
|
|
|
|
|
2020-11-13 10:19:44 -05:00
|
|
|
app.listen(9999);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
})();
|