Skip to content

Instantly share code, notes, and snippets.

@stagas
Created December 8, 2010 13:03
Show Gist options
  • Select an option

  • Save stagas/733255 to your computer and use it in GitHub Desktop.

Select an option

Save stagas/733255 to your computer and use it in GitHub Desktop.
flooder middleware (simple one)
, function(req, res, next) {
var ip = req.headers.ip
if (typeof banned[ip] !== 'undefined') {
if (Date.now() - banned[ip] > 3 * 60 * 60 * 1000) {
try {
delete banned[ip]
} catch(e) {}
log('UNBANNED:'.cyan, ip)
} else {
log('HAS BEEN BANNED:'.yellow, ip)
}
}
if (typeof users[ip] === 'undefined' && typeof banned[ip] === 'undefined') {
users[ip] = { visits: 0, timeout: null }
}
if (typeof banned[ip] === 'undefined') {
users[ip].visits++
clearTimeout(users[ip].timeout)
users[ip].timeout = setTimeout(function() {
try {
delete users[ip]
} catch(e) {}
}, 5 * 1000)
}
if (typeof banned[ip] !== 'undefined' || users[ip].visits > 30) {
if (typeof banned[ip] === 'undefined' && users[ip].visits > 60) {
banned[ip] = Date.now()
log('BANNED:'.yellow, ip)
}
log('FLOODER:'.red, ip)
res.writeHead(503, {'Content-Type': 'text/html'})
res.end('<h1>Service unavailable</h1>')
} else {
next()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment