mirror of
https://github.com/LibreTranslate/LibreTranslate.git
synced 2025-06-23 01:20:57 +00:00
Flood protection
This commit is contained in:
parent
4c5d828300
commit
1ac27aeb82
4 changed files with 73 additions and 3 deletions
36
app/flood.py
Normal file
36
app/flood.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
import time
|
||||
import atexit
|
||||
|
||||
from apscheduler.schedulers.background import BackgroundScheduler
|
||||
|
||||
banned = {}
|
||||
active = False
|
||||
threshold = -1
|
||||
|
||||
def clear_banned():
|
||||
global banned
|
||||
print(banned)
|
||||
banned = {}
|
||||
|
||||
def setup(violations_threshold = 100):
|
||||
global active
|
||||
global threshold
|
||||
|
||||
active = True
|
||||
threshold = violations_threshold
|
||||
|
||||
scheduler = BackgroundScheduler()
|
||||
scheduler.add_job(func=clear_banned, trigger="interval", weeks=4)
|
||||
scheduler.start()
|
||||
|
||||
# Shut down the scheduler when exiting the app
|
||||
atexit.register(lambda: scheduler.shutdown())
|
||||
|
||||
|
||||
def report(request_ip):
|
||||
banned[request_ip] = banned.get(request_ip, 0)
|
||||
banned[request_ip] += 1
|
||||
|
||||
def is_banned(request_ip):
|
||||
# More than X offences?
|
||||
return active and banned.get(request_ip, 0) >= threshold
|
Loading…
Add table
Add a link
Reference in a new issue