| Total Complexity | 3 |
| Total Lines | 26 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | from datetime import datetime, timedelta |
||
| 6 | class ConnectionThrottler: |
||
| 7 | """Simple Connection throttler to limit bruteforce attacks |
||
| 8 | |||
| 9 | Records the number of connection attempts from remote hosts in a specified |
||
| 10 | timespan, allowing to deny connections if there have been too many |
||
| 11 | """ |
||
| 12 | def __init__(self): |
||
| 13 | self.conn_attempts = {} |
||
| 14 | self.ts = datetime.now() |
||
| 15 | |||
| 16 | def attempt(self, ip): |
||
| 17 | """Record a connection attempt |
||
| 18 | |||
| 19 | Returns True if number of recorded attempts smaller than allowed number, |
||
| 20 | False otherwise |
||
| 21 | """ |
||
| 22 | # Check for time window rollover |
||
| 23 | now = datetime.now() |
||
| 24 | if now - self.ts > CONN_TIME: |
||
| 25 | # Reset everything if we're rolled over |
||
| 26 | self.conn_attempts = {} |
||
| 27 | self.ts = now |
||
| 28 | |||
| 29 | self.conn_attempts[ip] = self.conn_attempts.get(ip, 0) + 1 |
||
| 30 | |||
| 31 | return self.conn_attempts[ip] <= CONN_ATTEMPTS |
||
| 32 |