Skip to content

Instantly share code, notes, and snippets.

@aloncarmel
Created July 21, 2014 08:28
Show Gist options
  • Save aloncarmel/31a3d776fbf4aef3ac3d to your computer and use it in GitHub Desktop.
Save aloncarmel/31a3d776fbf4aef3ac3d to your computer and use it in GitHub Desktop.
Fight renegade socket.io connections that try to kill your server
/*
Very simple solution for fighting renegade connections that try to overflood your socket.io server and kill your nodejs.
This limits number of connections and sends disconnect to the rest.
You can expend it by saving IP list in memory and handle uniqueness per IP also if you wish.
*/
//Start count
var connections = 0;
var maxconnections = 1000;
io.sockets.on('connection', function (socket) {
//Add up connections
connections = connections+1;
//If we reached the limit, send disconnect to all clients.
if(connections > maxconnections) {
socket.disconnect();
}
// when the user disconnects.. perform this
socket.on('disconnect', function(){
//If client disconnects send disconnect.
socket.disconnect();
});
});
//Monitor your connected clients and see how it hardlimits everything.
app.get('/connected',function(req,res) {
res.jsonp(io.sockets.sockets.length);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment