mirror of https://github.com/calzoneman/sync.git
Rate-limit socket.io connections per IP
This commit is contained in:
parent
b70526db4d
commit
9193423923
|
@ -1,3 +1,7 @@
|
||||||
|
Mon Oct 14 16:30 2013 CDT
|
||||||
|
* lib/server.js: Rate-limit socket.io connections
|
||||||
|
* lib/bgtask.js: Periodically clear out old rate limiters
|
||||||
|
|
||||||
Sat Oct 12 19:43 2013 CDT
|
Sat Oct 12 19:43 2013 CDT
|
||||||
* lib/user.js: Fix jumpTo kick bug (and delete)
|
* lib/user.js: Fix jumpTo kick bug (and delete)
|
||||||
* lib/api.js: Fix unloaded channel API listing bug
|
* lib/api.js: Fix unloaded channel API listing bug
|
||||||
|
|
|
@ -56,12 +56,25 @@ function initAliasCleanup(Server) {
|
||||||
}, CLEAN_INTERVAL);
|
}, CLEAN_INTERVAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Clean out old rate limiters */
|
||||||
|
function initIpThrottleCleanup(Server) {
|
||||||
|
setInterval(function () {
|
||||||
|
for (var ip in Server.ipThrottle) {
|
||||||
|
if (Server.ipThrottle[ip].lastTime < Date.now() - 60 * 1000) {
|
||||||
|
delete Server.ipThrottle[ip];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, 5 * 60 * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = function (Server) {
|
module.exports = function (Server) {
|
||||||
if (init === Server) {
|
if (init === Server) {
|
||||||
Logger.errlog.log("WARNING: Attempted to re-init background tasks");
|
Logger.errlog.log("WARNING: Attempted to re-init background tasks");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
init = Server;
|
||||||
initStats(Server);
|
initStats(Server);
|
||||||
initAliasCleanup(Server);
|
initAliasCleanup(Server);
|
||||||
|
initIpThrottleCleanup(Server);
|
||||||
};
|
};
|
||||||
|
|
|
@ -56,6 +56,7 @@ var Server = function (cfg) {
|
||||||
self.ioWeb = null;
|
self.ioWeb = null;
|
||||||
self.ioSecure = null;
|
self.ioSecure = null;
|
||||||
self.ipCount = {};
|
self.ipCount = {};
|
||||||
|
self.ipThrottle = {};
|
||||||
self.db = null;
|
self.db = null;
|
||||||
self.api = null;
|
self.api = null;
|
||||||
self.announcement = null;
|
self.announcement = null;
|
||||||
|
@ -257,11 +258,29 @@ Server.prototype.logHTTP = function (req, status) {
|
||||||
].join(" "));
|
].join(" "));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const IP_THROTTLE = {
|
||||||
|
burst: 5,
|
||||||
|
sustained: 0.1
|
||||||
|
};
|
||||||
|
|
||||||
Server.prototype.handleSocketConnection = function (socket) {
|
Server.prototype.handleSocketConnection = function (socket) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var ip = self.getSocketIP(socket);
|
var ip = self.getSocketIP(socket);
|
||||||
socket._ip = ip;
|
socket._ip = ip;
|
||||||
|
|
||||||
|
if (!(ip in self.ipThrottle)) {
|
||||||
|
self.ipThrottle[ip] = $util.newRateLimiter();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self.ipThrottle[ip].throttle(IP_THROTTLE)) {
|
||||||
|
Logger.syslog.log("WARN: IP throttled: " + ip);
|
||||||
|
socket.emit("kick", {
|
||||||
|
reason: "Your IP address is connecting too quickly. Please "+
|
||||||
|
"wait 10 seconds before joining again."
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Check for global ban on the IP
|
// Check for global ban on the IP
|
||||||
self.db.isGlobalIPBanned(ip, function (err, banned) {
|
self.db.isGlobalIPBanned(ip, function (err, banned) {
|
||||||
if (banned) {
|
if (banned) {
|
||||||
|
|
Loading…
Reference in New Issue