1
0
mirror of https://github.com/LibreTranslate/LibreTranslate.git synced 2024-11-05 15:30:17 +01:00
LibreTranslate/app/flood.py

39 lines
793 B
Python
Raw Normal View History

2021-05-16 17:50:22 +02:00
import atexit
from apscheduler.schedulers.background import BackgroundScheduler
banned = {}
active = False
threshold = -1
2021-05-18 05:41:02 +02:00
2021-05-16 17:50:22 +02:00
def clear_banned():
global banned
banned = {}
2021-05-18 05:41:02 +02:00
def setup(violations_threshold=100):
2021-05-16 17:50:22 +02:00
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):
2021-05-16 17:52:39 +02:00
if active:
banned[request_ip] = banned.get(request_ip, 0)
banned[request_ip] += 1
2021-05-16 17:50:22 +02:00
2021-05-18 05:41:02 +02:00
2021-05-16 17:50:22 +02:00
def is_banned(request_ip):
# More than X offences?
2021-05-16 17:59:39 +02:00
return active and banned.get(request_ip, 0) >= threshold