1
0
mirror of https://github.com/LibreTranslate/LibreTranslate.git synced 2024-11-24 08:02:18 +02:00
LibreTranslate/app/flood.py

52 lines
1.1 KiB
Python

import atexit
from apscheduler.schedulers.background import BackgroundScheduler
banned = {}
active = False
threshold = -1
def clear_banned():
global banned
banned = {}
def setup(violations_threshold=100):
global active
global threshold
active = True
threshold = violations_threshold
print(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):
if active:
banned[request_ip] = banned.get(request_ip, 0)
banned[request_ip] += 1
print(banned[request_ip])
def decrease(request_ip):
if banned[request_ip] > 0:
banned[request_ip] -= 1
print('decrease', request_ip)
def has_violation(request_ip):
return request_ip in banned and banned[request_ip] > 0
def is_banned(request_ip):
# More than X offences?
return active and banned.get(request_ip, 0) >= threshold