2023-03-09 16:09:04 -05:00
|
|
|
from libretranslate.storage import get_storage
|
2023-03-09 13:59:25 -05:00
|
|
|
|
2021-05-16 11:50:22 -04:00
|
|
|
active = False
|
|
|
|
threshold = -1
|
2021-05-18 09:11:02 +05:30
|
|
|
|
2021-11-24 12:41:12 -05:00
|
|
|
def forgive_banned():
|
2021-11-24 12:49:07 -05:00
|
|
|
global threshold
|
2021-05-16 11:50:22 -04:00
|
|
|
|
2021-11-24 12:41:12 -05:00
|
|
|
clear_list = []
|
2023-03-09 16:09:04 -05:00
|
|
|
s = get_storage()
|
|
|
|
banned = s.get_all_hash_int("banned")
|
2021-11-24 12:41:12 -05:00
|
|
|
|
|
|
|
for ip in banned:
|
|
|
|
if banned[ip] <= 0:
|
|
|
|
clear_list.append(ip)
|
|
|
|
else:
|
2023-03-09 23:07:12 -05:00
|
|
|
s.set_hash_int("banned", ip, min(threshold, banned[ip]) - 1)
|
2021-11-24 12:41:12 -05:00
|
|
|
|
|
|
|
for ip in clear_list:
|
2023-03-09 16:09:04 -05:00
|
|
|
s.del_hash("banned", ip)
|
2022-02-20 13:39:02 +05:30
|
|
|
|
2023-03-09 23:07:12 -05:00
|
|
|
def setup(args):
|
2021-05-16 11:50:22 -04:00
|
|
|
global active
|
|
|
|
global threshold
|
|
|
|
|
2023-03-09 23:07:12 -05:00
|
|
|
if args.req_flood_threshold > 0:
|
|
|
|
active = True
|
|
|
|
threshold = args.req_flood_threshold
|
2021-05-16 11:50:22 -04:00
|
|
|
|
|
|
|
def report(request_ip):
|
2021-05-16 11:52:39 -04:00
|
|
|
if active:
|
2023-03-09 16:09:04 -05:00
|
|
|
get_storage().inc_hash_int("banned", request_ip)
|
2021-11-05 14:55:45 +01:00
|
|
|
|
|
|
|
def decrease(request_ip):
|
2023-03-09 16:09:04 -05:00
|
|
|
s = get_storage()
|
|
|
|
if s.get_hash_int("banned", request_ip) > 0:
|
|
|
|
s.dec_hash_int("banned", request_ip)
|
2021-11-05 14:55:45 +01:00
|
|
|
|
|
|
|
def has_violation(request_ip):
|
2023-03-09 16:09:04 -05:00
|
|
|
s = get_storage()
|
|
|
|
return s.get_hash_int("banned", request_ip) > 0
|
2021-05-18 09:11:02 +05:30
|
|
|
|
2021-05-16 11:50:22 -04:00
|
|
|
def is_banned(request_ip):
|
2023-03-09 16:09:04 -05:00
|
|
|
s = get_storage()
|
|
|
|
|
2021-05-16 11:50:22 -04:00
|
|
|
# More than X offences?
|
2023-03-09 16:09:04 -05:00
|
|
|
return active and s.get_hash_int("banned", request_ip) >= threshold
|