-
Notifications
You must be signed in to change notification settings - Fork 84
/
punishments.js
511 lines (458 loc) · 14.6 KB
/
punishments.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
'use strict';
/**
* Punishments
* Pokemon Showdown - http://pokemonshowdown.com/
*
* Handles the punishing of users on PS.
*
* There are four types of punishments on PS. Locks, bans, namelocks and rangelocks.
* This file contains the lists of users that have been punished (both IPs and usernames),
* as well as the functions that handle the execution of said punishments.
*
* @license MIT license
*/
let Punishments = module.exports;
let fs = require('fs');
let path = require('path');
const PUNISHMENT_FILE = path.resolve(__dirname, 'config/punishments.tsv');
const RANGELOCK_DURATION = 60 * 60 * 1000; // 1 hour
const LOCK_DURATION = 37 * 60 * 60 * 1000; // 37 hours
const BAN_DURATION = 7 * 24 * 60 * 60 * 1000; // 1 week
// a punishment is an array: [punishType, userid, expireTime, reason]
// ips is an ip:punishment Map
Punishments.ips = new Map();
// userids is a userid:punishment Map
Punishments.userids = new Map();
/*********************************************************
* Persistence
*********************************************************/
// punishType is an allcaps string, one of:
// 'LOCK'
// 'BAN'
// 'NAMELOCK'
// punishments.tsv is in the format:
// punishType, userid, ips/usernames, expiration time
Punishments.loadPunishments = function () {
fs.readFile(PUNISHMENT_FILE, (err, data) => {
if (err) return;
data = ('' + data).split("\n");
for (let i = 0; i < data.length; i++) {
if (!data[i] || data[i] === '\r') continue;
let row = data[i].trim().split("\t");
if (row[0] === "Punishment") continue;
let keys = row[2].split(',').concat(row[1]);
let punishment = [row[0], row[1], Number(row[3])].concat(row.slice(4));
if (Date.now() >= punishment[2]) {
continue;
}
for (let j = 0; j < keys.length; j++) {
let key = keys[j];
if (key.includes('.')) {
Punishments.ips.set(key, punishment);
} else {
Punishments.userids.set(key, punishment);
}
}
}
});
};
Punishments.savePunishments = function () {
let saveTable = new Map();
Punishments.ips.forEach((punishment, ip) => {
if (Date.now() >= punishment[2]) {
Punishments.ips.delete(ip);
return;
}
let id = punishment[1];
if (id.charAt(0) === '#') return;
let entry = saveTable.get(id);
if (entry) {
entry.keys.push(ip);
return;
}
entry = {
keys: [ip],
punishType: punishment[0],
rest: punishment.slice(2),
};
saveTable.set(id, entry);
});
Punishments.userids.forEach((punishment, userid) => {
if (Date.now() >= punishment[2]) {
Punishments.userids.delete(userid);
return;
}
let id = punishment[1];
let entry = saveTable.get(id);
if (!entry) {
entry = {
keys: [],
punishType: punishment[0],
rest: punishment.slice(2),
};
saveTable.set(id, entry);
}
if (userid !== id) entry.keys.push(userid);
});
let buf = 'Punishment\tUser ID\tIPs and alts\tExpires\r\n';
saveTable.forEach((entry, id) => {
buf += Punishments.renderEntry(entry, id);
});
fs.writeFile(PUNISHMENT_FILE, buf, () => {});
};
Punishments.appendPunishment = function (entry, id) {
if (id.charAt(0) === '#') return;
let buf = Punishments.renderEntry(entry, id);
fs.appendFile(PUNISHMENT_FILE, buf, () => {});
};
Punishments.renderEntry = function (entry, id) {
let row = [entry.punishType, id, entry.keys.join(',')].concat(entry.rest);
return row.join('\t') + '\r\n';
};
Punishments.loadBanlist = function () {
return new Promise(function (resolve, reject) {
fs.readFile(path.resolve(__dirname, 'config/ipbans.txt'), (err, data) => {
if (err) return reject(err);
data = ('' + data).split("\n");
let rangebans = [];
for (let i = 0; i < data.length; i++) {
data[i] = data[i].split('#')[0].trim();
if (!data[i]) continue;
if (data[i].includes('/')) {
rangebans.push(data[i]);
} else if (!Punishments.ips.has(data[i])) {
Punishments.ips.set(data[i], ['BAN', '#ipban', Infinity]);
}
}
Punishments.checkRangeBanned = Cidr.checker(rangebans);
resolve();
});
});
};
setImmediate(() => {
Punishments.loadPunishments();
Punishments.loadBanlist().catch(err => {
if (err.code === 'ENOENT') return;
throw err;
});
});
/*********************************************************
* Adding and removing
*********************************************************/
Punishments.punish = function (user, punishment, noRecurse) {
let keys = noRecurse;
if (!keys) {
keys = new Set();
}
if (!noRecurse) {
Users.users.forEach(curUser => {
if (user === curUser || curUser.confirmed) return;
for (let myIp in curUser.ips) {
if (myIp in user.ips) {
this.punish(curUser, punishment, keys);
return;
}
}
});
Punishments.userids.set(user.userid, punishment);
}
for (let ip in user.ips) {
Punishments.ips.set(ip, punishment);
keys.add(ip);
}
if (user.autoconfirmed) {
Punishments.userids.set(user.autoconfirmed, punishment);
keys.add(user.autoconfirmed);
}
if (user.confirmed) {
Punishments.userids.set(user.confirmed, punishment);
keys.add(user.confirmed);
}
if (!noRecurse) {
keys.delete(punishment[1]);
Punishments.appendPunishment({
keys: Array.from(keys),
punishType: punishment[0],
rest: punishment.slice(2),
}, punishment[1]);
}
};
Punishments.unpunish = function (id, punishType, noRecurse) {
id = toId(id);
let punishment = Punishments.useridSearch(id);
if (punishment) {
id = punishment[1];
}
// in theory we can stop here if punishment doesn't exist, but
// in case of inconsistent state, we'll try anyway
let success = false;
Punishments.ips.forEach((punishment, key) => {
if (punishment[1] === id && punishment[0] === punishType) {
Punishments.ips.delete(key);
success = id;
}
});
Punishments.userids.forEach((punishment, key) => {
if (punishment[1] === id && punishment[0] === punishType) {
Punishments.userids.delete(key);
success = id;
}
});
if (success) {
Punishments.savePunishments();
}
return success;
};
Punishments.ban = function (user, expireTime, id, reason) {
if (!id) id = user.getLastId();
if (!expireTime) expireTime = Date.now() + BAN_DURATION;
let punishment = ['BAN', id, expireTime].concat(Array.prototype.slice.call(arguments, 3));
Punishments.punish(user, punishment);
let affected = user.getAltUsers(false, true);
for (let curUser of affected) {
curUser.locked = id;
curUser.disconnectAll();
}
};
Punishments.unban = function (name) {
let success = Punishments.unpunish(name, 'BAN');
return success;
};
Punishments.lock = function (user, expireTime, id, reason) {
if (!id) id = user.getLastId();
if (!expireTime) expireTime = Date.now() + LOCK_DURATION;
let punishment = ['LOCK', id, expireTime].concat(Array.prototype.slice.call(arguments, 3));
Punishments.punish(user, punishment);
let affected = user.getAltUsers(false, true);
for (let curUser of affected) {
curUser.locked = id;
curUser.updateIdentity();
}
};
Punishments.unlock = function (name) {
let user = Users(name);
let id = toId(name);
let success = [];
if (user && user.locked) {
id = user.locked;
user.locked = false;
user.updateIdentity();
success.push(user.getLastName());
if (id.charAt(0) !== '#') {
Users.users.forEach(curUser => {
if (curUser.locked === id) {
curUser.locked = false;
curUser.updateIdentity();
success.push(curUser.getLastName());
}
});
}
}
if (Punishments.unpunish(name, 'LOCK')) {
if (!success.length) success.push(name);
}
if (!success.length) return false;
if (!success.some(v => toId(v) === id)) {
success.push(id);
}
return success;
};
Punishments.namelock = function (user, expireTime, id, reason) {
if (!id) id = user.getLastId();
if (!expireTime) expireTime = Date.now() + LOCK_DURATION;
let punishment = ['NAMELOCK', id, expireTime].concat(Array.prototype.slice.call(arguments, 3));
Punishments.punish(user, punishment);
let affected = user.getAltUsers(false, true);
for (let curUser of affected) {
curUser.locked = id;
curUser.namelocked = id;
curUser.resetName();
curUser.updateIdentity();
}
};
Punishments.unnamelock = function (name) {
let user = Users(name);
let id = toId(name);
let success = [];
if (user && user.locked) {
id = user.locked;
user.locked = false;
user.namelocked = false;
user.resetName();
success.push(user.getLastName());
if (id.charAt(0) !== '#') {
Users.users.forEach(curUser => {
if (curUser.locked === id) {
curUser.locked = false;
curUser.namelocked = false;
curUser.resetName();
success.push(curUser.getLastName());
}
});
}
}
if (Punishments.unpunish(name, 'NAMELOCK')) {
if (!success.length) success.push(name);
}
if (!success.length) return false;
if (!success.some(v => toId(v) === id)) {
success.push(id);
}
return success;
};
Punishments.lockRange = function (range, reason) {
let punishment = ['LOCK', '#rangelock', Date.now() + RANGELOCK_DURATION, reason];
Punishments.ips.set(range, punishment);
};
Punishments.banRange = function (range, reason) {
let punishment = ['BAN', '#rangelock', Date.now() + RANGELOCK_DURATION, reason];
Punishments.ips.set(range, punishment);
};
/*********************************************************
* Checking
*********************************************************/
Punishments.getPunishType = function (name) {
let punishment = Punishments.useridSearch(toId(name));
if (punishment) return punishment[0];
let user = Users.get(name);
if (!user) return;
punishment = Punishments.ipSearch(user.latestIp);
if (punishment) return punishment[0];
return '';
};
/**
* Searches for IP in Punishments.ips
*
* For instance, if IP is '1.2.3.4', will return the value corresponding
* to any of the keys in table match '1.2.3.4', '1.2.3.*', '1.2.*', or '1.*'
*/
Punishments.ipSearch = function (ip) {
let punishment = Punishments.ips.get(ip);
if (punishment) {
if (Date.now() < punishment[2]) return punishment;
Punishments.ips.delete(ip);
}
let dotIndex = ip.lastIndexOf('.');
for (let i = 0; i < 4 && dotIndex > 0; i++) {
ip = ip.substr(0, dotIndex);
punishment = Punishments.ips.get(ip + '.*');
if (punishment) {
if (Date.now() < punishment[2]) return punishment;
Punishments.ips.delete(ip + '.*');
}
dotIndex = ip.lastIndexOf('.');
}
return false;
};
Punishments.useridSearch = function (userid) {
let punishment = Punishments.userids.get(userid);
if (punishment) {
if (Date.now() < punishment[2]) return punishment;
Punishments.userids.delete(userid);
}
return false;
};
Punishments.shortenHost = function (host) {
if (host.slice(-7) === '-nohost') return host;
let dotLoc = host.lastIndexOf('.');
let tld = host.substr(dotLoc);
if (tld === '.uk' || tld === '.au' || tld === '.br') dotLoc = host.lastIndexOf('.', dotLoc - 1);
dotLoc = host.lastIndexOf('.', dotLoc - 1);
return host.substr(dotLoc + 1);
};
// Defined in Punishments.loadBanlist
Punishments.checkRangeBanned = function () {};
Punishments.checkName = function (user, registered) {
let userid = user.userid;
let punishment = Punishments.useridSearch(userid);
if (!punishment && user.namelocked) {
punishment = Punishments.useridSearch(user.namelocked);
if (!punishment) punishment = ['NAMELOCK', user.namelocked, 0];
}
if (!punishment) return;
let id = punishment[0];
let punishUserid = punishment[1];
if (registered && id === 'BAN') {
let bannedUnder = '';
if (punishUserid !== userid) bannedUnder = ' because it has the same IP as banned user: ' + punishUserid;
user.send("|popup|Your username (" + user.name + ") is banned" + bannedUnder + "'. Your ban will expire in a few days." + (Config.appealurl ? " Or you can appeal at:\n" + Config.appealurl : ""));
Punishments.punish(user, punishment);
user.disconnectAll();
return;
}
if (id === 'NAMELOCK' || user.namelocked) {
let bannedUnder = '';
if (punishUserid !== userid) bannedUnder = ' because it has the same IP as banned user: ' + punishUserid;
user.send("|popup|You are namelocked" + bannedUnder + "'. Your namelock will expire in a few days.");
if (punishment[2]) Punishments.punish(user, punishment);
user.locked = punishUserid;
user.namelocked = punishUserid;
user.resetName();
user.updateIdentity();
} else {
let bannedUnder = '';
if (punishUserid !== userid) bannedUnder = ' because it has the same IP as banned user: ' + punishUserid;
user.send("|popup|Your username (" + user.name + ") is locked" + bannedUnder + "'. Your lock will expire in a few days." + (Config.appealurl ? " Or you can appeal at:\n" + Config.appealurl : ""));
Punishments.punish(user, punishment);
user.locked = punishUserid;
user.updateIdentity();
}
if (user.namelocked) {
user.popup("You can't change your name because you're namelocked.");
user.resetName();
user.updateIdentity();
}
};
Punishments.checkIp = function (user, connection) {
let ip = connection.ip;
let punishment = Punishments.ipSearch(ip);
if (punishment) {
user.locked = punishment[1];
if (punishment[0] === 'NAMELOCK') {
user.namelocked = punishment[1];
}
}
Dnsbl.reverse(ip, (err, hosts) => {
if (hosts && hosts[0]) {
user.latestHost = hosts[0];
if (Config.hostfilter) Config.hostfilter(hosts[0], user, connection);
} else {
if (Config.hostfilter) Config.hostfilter('', user, connection);
}
});
Dnsbl.query(connection.ip, isBlocked => {
if (isBlocked) {
if (connection.user && !connection.user.locked && !connection.user.autoconfirmed) {
connection.user.semilocked = '#dnsbl';
}
}
});
};
// Connection flood table. Separate table from IP bans.
let cfloods = new Set();
/**
* IP bans need to be checked separately since we don't even want to
* make a User object if an IP is banned.
*/
Punishments.checkIpBanned = function (connection) {
let ip = connection.ip;
if (cfloods.has(ip) || (Monitor.countConnection(ip) && cfloods.add(ip))) {
connection.send("|popup||modal|PS is under heavy load and cannot accommodate your connection right now.");
return '#cflood';
}
let banned = false;
let punishment = Punishments.ipSearch(ip);
if (punishment && punishment[0] === 'BAN') {
banned = punishment[1];
} else if (Punishments.checkRangeBanned(ip)) {
banned = '#ipban';
}
if (!banned) return false;
if (banned === '#ipban') {
connection.send("|popup||modal|Your IP (" + ip + ") is not allowed to connect to PS, because it has been used to spam, hack, or otherwise attack our server.||Make sure you are not using any proxies to connect to PS.");
} else {
connection.send("|popup||modal|You are banned because you have the same IP (" + ip + ") as banned user '" + banned + "'. Your ban will expire in a few days.||" + (Config.appealurl ? " Or you can appeal at:\n" + Config.appealurl : ""));
}
if (!Config.quietconsole) console.log('CONNECT BLOCKED - IP BANNED: ' + ip + ' (' + banned + ')');
return banned;
};