forked from smogon/pokemon-showdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.js
304 lines (273 loc) · 7.24 KB
/
monitor.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
/**
* Monitor
* Pokemon Showdown - http://pokemonshowdown.com/
*
* Various utility functions to make sure PS is running healthily.
*
* @license MIT license
*/
'use strict';
const FS = require('./fs');
const MONITOR_CLEAN_TIMEOUT = 2 * 60 * 60 * 1000;
/**
* This counts the number of times an action has been committed, and tracks the
* delta of time since the last time it was committed. Actions include
* connecting to the server, starting a battle, validating a team, and
* sending/receiving data over a connection's socket.
* @augments {Map<string, [number, number]>}
*/
// @ts-ignore TypeScript bug
class TimedCounter extends Map {
/**
* Increments the number of times an action has been committed by one, and
* updates the delta of time since it was last committed.
*
* @param {string} key
* @param {number} timeLimit
* @return {[number, number]} - [action count, time delta]
*/
increment(key, timeLimit) {
let val = this.get(key);
let now = Date.now();
if (!val || now > val[1] + timeLimit) {
this.set(key, [1, Date.now()]);
return [1, 0];
} else {
val[0]++;
return [val[0], now - val[1]];
}
}
}
// Config.loglevel is:
// 0 = everything
// 1 = debug (same as 0 for now)
// 2 = notice (default)
// 3 = warning
// (4 is currently unused)
// 5 = supposedly completely silent, but for now a lot of PS output doesn't respect loglevel
if (('Config' in global) &&
(typeof Config.loglevel !== 'number' || Config.loglevel < 0 || Config.loglevel > 5)) {
Config.loglevel = 2;
}
// @ts-ignore
const Monitor = module.exports = {
/*********************************************************
* Logging
*********************************************************/
/**
* @param {string} text
*/
log(text) {
this.notice(text);
if (Rooms('staff')) {
Rooms('staff').add(`|c|~|${text}`).update();
}
},
/**
* @param {string} text
*/
adminlog(text) {
this.notice(text);
if (Rooms('upperstaff')) {
Rooms('upperstaff').add(`|c|~|${text}`).update();
}
},
/**
* @param {string} text
*/
logHTML(text) {
this.notice(text);
if (Rooms('staff')) {
Rooms('staff').add(`|html|${text}`).update();
}
},
/**
* @param {string} text
*/
debug(text) {
if (Config.loglevel <= 1) console.log(text);
},
/**
* @param {string} text
*/
warn(text) {
if (Config.loglevel <= 3) console.log(text);
},
/**
* @param {string} text
*/
notice(text) {
if (Config.loglevel <= 2) console.log(text);
},
/*********************************************************
* Resource Monitor
*********************************************************/
clean() {
this.clearNetworkUse();
this.battlePreps.clear();
this.battles.clear();
this.connections.clear();
Dnsbl.cache.clear();
},
connections: new TimedCounter(),
battles: new TimedCounter(),
battlePreps: new TimedCounter(),
groupChats: new TimedCounter(),
/** @type {string | null} */
activeIp: null,
networkUse: {},
networkCount: {},
hotpatchLock: {},
/**
* Counts a connection. Returns true if the connection should be terminated for abuse.
*
* @param {string} ip
* @param {string} [name = '']
* @return {boolean}
*/
countConnection(ip, name = '') {
let [count, duration] = this.connections.increment(ip, 30 * 60 * 1000);
if (count === 500) {
this.adminlog(`[ResourceMonitor] IP ${ip} banned for cflooding (${count} times in ${Chat.toDurationString(duration)}${name ? ': ' + name : ''})`);
return true;
}
if (count > 500) {
if (count % 500 === 0) {
let c = count / 500;
if (c === 2 || c === 4 || c === 10 || c === 20 || c % 40 === 0) {
this.adminlog(`[ResourceMonitor] IP ${ip} still cflooding (${count} times in ${Chat.toDurationString(duration)}${name ? ': ' + name : ''})`);
}
}
return true;
}
return false;
},
/**
* Counts battles created. Returns true if the connection should be
* terminated for abuse.
*
* @param {string} ip
* @param {string} [name = '']
* @return {boolean}
*/
countBattle(ip, name = '') {
let [count, duration] = this.battles.increment(ip, 30 * 60 * 1000);
if (duration < 5 * 60 * 1000 && count % 30 === 0) {
this.adminlog(`[ResourceMonitor] IP ${ip} has battled ${count} times in the last ${Chat.toDurationString(duration)}${name ? ': ' + name : ''})`);
return true;
}
if (count % 150 === 0) {
this.adminlog(`[ResourceMonitor] IP ${ip} has battled ${count} times in the last ${Chat.toDurationString(duration)}${name ? ': ' + name : ''}`);
return true;
}
return false;
},
/**
* Counts team validations. Returns true if too many.
*
* @param {string} ip
* @param {Connection} connection
* @return {boolean}
*/
countPrepBattle(ip, connection) {
let count = this.battlePreps.increment(ip, 3 * 60 * 1000)[0];
if (count <= 12) return false;
if (count < 120 && Punishments.sharedIps.has(ip)) return false;
connection.popup('Due to high load, you are limited to 12 battles and team validations every 3 minutes.');
return true;
},
/**
* Counts concurrent battles. Returns true if too many.
*
* @param {number} count
* @param {Connection} connection
* @return {boolean}
*/
countConcurrentBattle(count, connection) {
if (count <= 5) return false;
connection.popup(`Due to high load, you are limited to 5 games at the same time.`);
return true;
},
/**
* Counts group chat creation. Returns true if too much.
*
* @param {string} ip
* @return {boolean}
*/
countGroupChat(ip) {
let count = this.groupChats.increment(ip, 60 * 60 * 1000)[0];
return count > 4;
},
/**
* Counts the data length received by the last connection to send a
* message, as well as the data length in the server's response.
*
* @param {number} size
*/
countNetworkUse(size) {
if (!Config.emergency || typeof this.activeIp !== 'string') return;
// @ts-ignore
if (this.activeIp in this.networkUse) {
// @ts-ignore
this.networkUse[this.activeIp] += size;
// @ts-ignore
this.networkCount[this.activeIp]++;
} else {
// @ts-ignore
this.networkUse[this.activeIp] = size;
// @ts-ignore
this.networkCount[this.activeIp] = 1;
}
},
writeNetworkUse() {
let buf = '';
for (let i in this.networkUse) {
buf += `${this.networkUse[i]}\t${this.networkCount[i]}\t${i}\n`;
}
FS('logs/networkuse.tsv').write(buf);
},
clearNetworkUse() {
if (Config.emergency) {
this.networkUse = {};
this.networkCount = {};
}
},
/**
* Counts roughly the size of an object to have an idea of the server load.
*
* @param {any} object
* @return {number}
*/
sizeOfObject(object) {
/** @type {Set<(Array | Object)>} */
let objectCache = new Set();
let stack = [object];
let bytes = 0;
while (stack.length) {
let value = stack.pop();
switch (typeof value) {
case 'boolean':
bytes += 4;
break;
case 'string':
bytes += value.length * 2;
break;
case 'number':
bytes += 8;
break;
case 'object':
if (!objectCache.has(value)) objectCache.add(value);
if (Array.isArray(value)) {
for (let el of value) stack.push(el);
} else {
for (let i in value) stack.push(value[i]);
}
break;
}
}
return bytes;
},
/** @type {{new(entries: [any, [number, number]]): TimedCounter}} */
TimedCounter,
};
Monitor.cleanInterval = setInterval(() => Monitor.clean(), MONITOR_CLEAN_TIMEOUT);