-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
139 lines (108 loc) · 2.87 KB
/
server.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const os = require("os");
const ws = require("ws");
const net = require("net");
const path = require("path");
const axios = require("axios");
const zero = require("dev-zero-stream");
const bodyParser = require("body-parser");
const interfaces = os.networkInterfaces();
const ips = Object.values(interfaces)
.flat()
.filter((inf) => inf.family === "IPv4")
.map((inf) => {
return inf.address;
});
const wss = new ws.WebSocketServer({
port: 9075,
});
let clients = {};
let downloadMbps = 0;
let uploadMbps = 0;
let ping = 0;
wss.on("connection", (ws) => {
ws.on("message", (message) => {
const str = message.toString();
const [_, d, p] = str.split(" ");
if (d !== undefined && p !== undefined) {
downloadMbps = parseFloat(d);
ping = parseInt(p, 10);
}
if (str[0] === "I") {
ws.send("O");
} else if (str[0] === "P") {
} else if (str[0] === "U") {
}
});
ws.on("close", () => {
ping = 0;
downloadMbps = 0;
});
});
let server = net.createServer();
let last = Date.now();
let sinceLast = 0;
server.on("connection", (connection) => {
connection.on("error", () => console.log("Connection error"));
const id = Date.now();
const z = zero();
z.pipe(connection);
connection.on("data", (data) => {
sinceLast += data.length;
});
setInterval(() => {
const sinceLastMB = (sinceLast * 8) / (1024 * 1024);
const diff = (Date.now() - last) / 1000;
const mbps = Math.round((sinceLastMB / diff) * 100) / 100;
if (uploadMbps !== undefined) {
uploadMbps = mbps;
}
clients[id] = {
id: id,
ping: ping,
uploadRate: uploadMbps,
downloadRate: downloadMbps,
address: connection.remoteAddress,
};
axios
.post("http://localhost:9073/data", {
clients: clients,
})
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
console.log(clients);
// console.log(`Upload rate: ${mbps} Mb/s`);
sinceLast = 0;
last = Date.now();
}, 1000);
});
server.listen(9074, "0.0.0.0");
const fs = require("fs/promises");
const app = require("express")();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.get("/", function (req, res) {
res.sendFile(path.join(__dirname + "/index.html"));
});
app.get("/table", function (req, res) {
res.sendFile(path.join(__dirname + "/table.html"));
});
app.get("/termux", async (req, res) => {
const content = (await fs.readFile("./termux.sh")).toString("utf-8");
res.end(
content.replace("999.999.999.999", req.headers.host.split(":").shift())
);
});
app.post("/data", (req, res) => {
clients = req.body.clients;
res.status(200);
});
app.get("/data", (req, res) => {
res.send({
clients: clients,
});
});
app.listen(9073, "0.0.0.0", () => console.log("Server started"));