-
Notifications
You must be signed in to change notification settings - Fork 7
/
registry.js
309 lines (266 loc) · 7.12 KB
/
registry.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import net from "net";
import { createClient } from "redis";
import analyze from "./app/analyzepacket.js";
import limiter from "limiter";
import fs from "fs";
import config from "./app/config.js";
const redisClient = createClient();
var announcement = "";
var bannedIps = [];
redisClient.connect();
redisClient.on("error", function (err) {
console.log("Redis Error: " + err);
});
var servers = {}; //assoc id/servers
var names = {}; //assoc name/servers
var antidos = {};
var ipcount = {};
function getIp(c) {
var ip = c.remoteAddress;
if (!ip) {
return null;
}
if (ip.indexOf("ffff:") != -1) {
ip = ip.substring(ip.indexOf("ffff:") + 5);
}
return ip;
}
function initIp(ip) {
if (!(ip in antidos)) {
// new client / server! Limit to 40 messages/logins per minute
// only cache per day
antidos[ip] = {
logins: new limiter.RateLimiter(config.antispam.logins, "minute", true),
requests: new limiter.RateLimiter(config.antispam.requests, "minute", true),
byterate: new limiter.RateLimiter(config.antispam.byterate, "minute", true),
};
setTimeout(function () {
delete antidos[ip];
}, 24 * 60 * 60 * 1000);
}
}
function canLogin(c) {
var ip = getIp(c);
if (!ip) {
return false;
}
initIp(ip);
return antidos[ip].logins.tryRemoveTokens(1);
}
function canReceive(c) {
var ip = getIp(c);
initIp(ip);
return antidos[ip].requests.tryRemoveTokens(1);
}
function canReceiveBytes(c, bytes) {
initIp(getIp(c));
return antidos[getIp(c)].byterate.tryRemoveTokens(bytes);
}
function incIp(ip) {
ipcount[ip] = (ipcount[ip] || 0) + 1;
}
function decIp(ip) {
ipcount[ip] = (ipcount[ip] || 0) - 1;
if (ipcount[ip] <= 0) {
delete ipcount[ip];
}
}
function clientListener(c) {
c.on("error", function () {});
try {
var ip = getIp(c);
if (!canLogin(c)) {
c.destroy();
return;
}
if (bannedIps.indexOf(ip) != -1) {
console.log("Banned client attempting to log in: " + ip);
c.destroy();
return;
}
if (ipcount[ip] > 5) {
c.destroy();
return;
}
incIp(ip);
console.log("client connected");
c.on("close", function () {
decIp(ip);
console.log("client disconnected");
});
if (announcement) {
analyze.write(c, "announcement", announcement);
}
//todo: cache the binary data?
for (const server of Object.values(names)) {
analyze.write(c, "server", server.podata);
}
analyze.write(c, "serverend");
c.end();
} catch (err) {
console.error(err);
fs.promises.appendFile("reg-errors.txt", err + "\n" + err.stack + "\n").catch(console.error);
}
}
function serverListener(s) {
s.on("error", function () {});
if (!canLogin(s)) {
s.destroy();
return;
}
var id = getIp(s);
if (!id || bannedIps.indexOf(id) != -1) {
console.log("Banned server attempting to log on: " + id);
s.destroy();
return;
}
if (ipcount[id] > 5) {
s.destroy();
return;
}
incIp(id);
console.log("server " + id + " connected");
var disconnected = false;
s.dc = function () {
disconnected = true;
s.end();
};
if (id in servers) {
//Todo: send error message to server
console.log("IP already in use, disconnecting");
s.dc();
return;
}
servers[id] = s;
s.podata = { name: "", ip: getIp(s) };
s.setKeepAlive(true);
s.on("close", function () {
console.log("server " + id + " disconnected");
delete servers[id];
decIp(id);
if (s.podata.name in names && names[s.podata.name] == s) {
delete names[s.podata.name];
}
analyze.disconnect(s);
});
s.on("data", function (data) {
//Ignore data from connections we closed
if (disconnected) {
return;
}
if (!canReceiveBytes(s, data.length)) {
console.log("Server " + id + " sent too many bytes, disconnecting");
s.dc();
return;
}
//console.log("Server " + id + " sent data of length " + data.length);
analyze.addData(s, data, function (s, command) {
if (!canReceive(s)) {
console.log("Server " + id + " sent too many packets, disconnecting");
s.dc();
return;
}
console.log("Server " + id + " sent command " + JSON.stringify(command));
if ("name" in command && command.name.length > 20) {
command.name = command.name.substring(0, 20);
}
if ("desc" in command && command.desc.length > 500) {
command.desc = command.desc.substring(0, 500);
}
const oldName = s.podata.name;
Object.assign(s.podata, command);
delete s.podata["type"];
if (s.podata.name !== oldName) {
delete names[oldName];
if (s.podata.name in names) {
console.log("Name already in use, disconnecting");
//Todo: send error message to server
s.dc();
return;
}
if (s.podata.name.length > 0) {
names[s.podata.name] = s;
}
}
if (s.podata.name.length == 0) {
console.log("Empty name, disconnecting");
//Todo: send error message to server
s.dc();
return;
}
});
});
}
const clientserv = net.createServer(clientListener);
clientserv.potag = { name: "Client serv 1", port: 5090 };
const clientserv2 = net.createServer(clientListener);
clientserv2.potag = { name: "Client serv 2", port: 8080 };
const serverserv = net.createServer(serverListener);
serverserv.potag = { name: "Server serv", port: 8081 };
var servs = [clientserv, clientserv2, serverserv];
servs.forEach(function (serv) {
serv.on("error", function (e) {
if (e.code == "EADDRINUSE") {
console.log(serv.potag.name + ": Port " + serv.potag.port + " in use, failure... Retrying in 10 seconds");
setTimeout(function () {
//serv.close();
serv.listen(serv.potag.port);
}, 10000);
}
});
});
clientserv.listen(clientserv.potag.port, function () {
//'listening' listener
console.log("Client listener 1 working on port 5090");
});
clientserv2.listen(clientserv2.potag.port, function () {
//'listening' listener
console.log("Client listener 2 working on port 8080");
});
serverserv.listen(serverserv.potag.port, function () {
console.log("Server listener working on port 8081");
});
/* Update the announcement every 10 seconds */
function updateAnnouncement() {
redisClient.get("po-registry:announcement", function (err, ann) {
if (err) {
console.log("Redis error when getting announcement");
} else if (announcement != ann) {
console.log("Announcement updated: " + ann);
} else {
//console.log("no update");
}
announcement = ann;
});
}
setInterval(updateAnnouncement, 10_000);
/* Update the banned IPs every 10 seconds */
function updateBannedIPs() {
redisClient.sMembers("po-registry:banned-ips", function (err, banned) {
if (err) {
console.log("Redis error when getting banned ips");
} else {
bannedIps = banned;
bannedIps.forEach(function (ip) {
if (ip in servers) {
servers[ip].dc();
}
});
}
});
}
setInterval(updateBannedIPs, 5_000);
/* Update the list of servers in the database every 10 seconds */
function updateServers() {
const dataToStore = Object.values(servers).map((server) => server.podata);
dataToStore.sort(function (a, b) {
return b.players - a.players;
});
redisClient.set("po-registry:servers", JSON.stringify(dataToStore));
setTimeout(updateServers, 10000);
}
updateServers();
process.on("unhandledRejection", (error) => {
// Will print "unhandledRejection err is not defined"
console.log("unhandledRejection", error.message);
});