This repository has been archived on 2023-11-14. You can view files and clone it, but cannot push or open issues/pull-requests.
valnet/relay/index.js

119 lines
2.2 KiB
JavaScript
Raw Normal View History

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');
2021-02-14 01:11:58 -05:00
const upnp = require('../lib/upnp');
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
// const client = stp.connect(identity, config.ports.relay, '127.0.0.1');
2020-11-12 11:43:35 -05:00
2021-02-14 01:11:58 -05:00
// upnp.mapIndefinite(5600);
2020-11-13 10:19:44 -05:00
// ==================================== [STP SERVER]
2021-02-13 23:56:20 -05:00
stp.createServer({
identity: identity,
port: config.ports.relay
}, socket => {
log.debug('loopback ' + socket.loopback)
clients.push(socket);
});
2021-02-14 01:11:58 -05:00
function connectNetwork(t = 1000) {
2021-02-14 02:40:21 -05:00
if(t > 60000) t /= 2;
2021-02-14 01:11:58 -05:00
const client = stp.connect({
identity,
port: config.ports.relay,
2021-02-14 03:06:34 -05:00
ip: config.addresses.relay
2021-02-14 01:11:58 -05:00
});
client.on('ready', () => {
log.success('connectd!');
t = 500;
})
client.on('error', e => {
});
client.on('close', e => {
2021-02-14 02:03:26 -05:00
t *= 2;
setTimeout(connectNetwork.bind(global, t), t);
2021-02-14 01:11:58 -05:00
log.debug('retrying connection... ' + (t/1000) + 's')
});
}
connectNetwork();
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(`
2021-02-13 23:56:20 -05:00
<style>
2021-02-14 02:03:26 -05:00
html {
background: black;
color: white;
}
2021-02-13 23:56:20 -05:00
td:not(:last-child), th:not(:last-child) {
2021-02-14 02:03:26 -05:00
border-right: 1px solid white;
2021-02-13 23:56:20 -05:00
}
td, th {
padding-left: 8px;
}
th {
2021-02-14 02:03:26 -05:00
border-bottom: 3px solid white;
2021-02-13 23:56:20 -05:00
}
table {
border-spacing: 0px;
font-family: sans-serif;
font-size: 13px;
}
tr:nth-child(2n) {
2021-02-14 02:03:26 -05:00
background: #111;
2021-02-13 23:56:20 -05:00
}
</style>
<table style="min-width: 300px">
2020-11-18 11:18:39 -05:00
<tr>
2021-02-13 23:56:20 -05:00
<th>Id</th>
<th>Address</th>
<th>loopback</th>
2020-11-18 11:18:39 -05:00
</tr>
2021-02-13 23:56:20 -05:00
${clients.map((client, index) => `
<tr>
<td><pre>${index}</pre></td>
<td><pre>${client.remoteAddress}</pre></td>
<td><pre>${client.loopback}</pre></td>
</tr>
`).join('')}
2020-11-18 11:18:39 -05:00
</table>
`);
2020-11-18 12:02:58 -05:00
});
// app.post
2020-11-11 22:21:35 -05:00
2021-02-14 02:21:35 -05:00
app.listen(config.ports.http).on('error', e => {
2021-02-14 02:03:26 -05:00
log.warn(e);
setTimeout(_ => {
2021-02-14 02:21:35 -05:00
app.listen(config.ports.http).on('error', e => {
2021-02-14 02:03:26 -05:00
log.error(e);
});
2021-02-14 02:21:35 -05:00
}, config.ports.http);
2021-02-14 02:03:26 -05:00
});
2020-11-13 10:19:44 -05:00
})();