Created
December 8, 2010 13:03
-
-
Save stagas/733255 to your computer and use it in GitHub Desktop.
flooder middleware (simple one)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| , 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