-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
101 lines (82 loc) · 2.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
import 'console.table';
import argv from './lib/argv.js';
import net from 'node:net';
import packageInfo from './lib/packageInfo.js';
import UdpServer from './lib/UdpServer.js';
import WsServer from './lib/WsServer.js';
import WwwServer from './lib/WwwServer.js';
function formatAddressPort(address, port) {
if (net.isIPv6(address)) {
address = `[${address}]`;
}
return `${address}:${port}`;
}
// Console output loop
let outputTimeout;
function output() {
clearTimeout(outputTimeout);
console.clear();
// Header
console.log(`${packageInfo.name} v${packageInfo.version} -- ${packageInfo.copyright}`);
console.log(`Target X32: ${formatAddressPort(argv.target, argv.targetPort)}`);
for (const server of servers) {
if (server.listening) {
console.log(`${server.type} Listening: ${formatAddressPort(server.address, server.port)}`);
}
}
console.log(); // Blank line
// Array of Objects containing string-based client data, for display
const clientTableData = [...clients.values()].map((client) => {
return {
type: client.type,
address: client.downstreamAddress,
port: client.downstreamPort,
'packets-tx': client.upstreamPacketCount.toLocaleString(),
'packets-rx': client.downstreamPacketCount.toLocaleString()
}
});
// If no clients, say so
if (!clientTableData.length) {
console.log('No connected clients');
}
// Output table of clients
console.table('Clients', clientTableData);
outputTimeout = setTimeout(output, 1000);
}
// List of active clients, WS or UDP
const clients = new Set();
// List of servers
const servers = new Set();
for (const [type, constructor, defaultPort] of [
['udp', UdpServer, argv.targetPort],
['ws', WsServer, 8080],
['www', WwwServer, 80]
]) {
for (const host of argv[type] || []) {
const url = new URL(`fake-protocol://${host || '127.0.0.1'}`);
servers.add(
new constructor({
target: argv.target,
targetPort: argv.targetPort,
port: url.port || defaultPort,
address: url.hostname
})
);
}
}
for (const server of servers) {
server.addEventListener('connection', (e) => {
clients.add(e.detail);
e.detail.addEventListener('destroy', () => {
clients.delete(e.detail);
output();
}, {once: true});
output();
});
server.addEventListener('listening', output);
}
if (!servers.size) {
throw new Error('No servers configured. You must set at least one of --udp, --ws, or --www. See --help for details.');
}
output();