IMPORTANT: This commit contains new config options. Make sure your local config file is up to date with config.example.toml to avoid issues. CHANGELOG: - Fix crash exploit caused by abnormally long WebSocket messages - Added config option for limiting the maximum amount of active connections from the same IP - Made per-IP turn limiter configurable - Fixed an issue where vote updates were not sent to clients in some cases - Added warning about using qmpSockDir on Windows hosts - Updated dependencies - Removed unused code/imports
36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import { EventEmitter } from "events";
|
|
|
|
// Class to ratelimit a resource (chatting, logging in, etc)
|
|
export default class RateLimiter extends EventEmitter {
|
|
private limit : number;
|
|
private interval : number;
|
|
private requestCount : number;
|
|
private limiter? : NodeJS.Timeout;
|
|
private limiterSet : boolean;
|
|
constructor(limit : number, interval : number) {
|
|
super();
|
|
this.limit = limit;
|
|
this.interval = interval;
|
|
this.requestCount = 0;
|
|
this.limiterSet = false;
|
|
}
|
|
// Return value is whether or not the action should be continued
|
|
request() : boolean {
|
|
this.requestCount++;
|
|
if (this.requestCount === this.limit) {
|
|
this.emit('limit');
|
|
clearTimeout(this.limiter);
|
|
this.limiterSet = false;
|
|
this.requestCount = 0;
|
|
return false;
|
|
}
|
|
if (!this.limiterSet) {
|
|
this.limiter = setTimeout(() => {
|
|
this.limiterSet = false;
|
|
this.requestCount = 0;
|
|
}, this.interval * 1000);
|
|
this.limiterSet = true;
|
|
}
|
|
return true;
|
|
}
|
|
} |