mirror of
https://github.com/LibreTranslate/LibreTranslate.git
synced 2024-12-18 08:27:03 +02:00
Merge pull request #169 from dingedi/main
decrease counter if the user wait after the slowdown notice
This commit is contained in:
commit
8b0e04c8d1
10
app/app.py
10
app/app.py
@ -25,6 +25,7 @@ def get_version():
|
|||||||
except:
|
except:
|
||||||
return "?"
|
return "?"
|
||||||
|
|
||||||
|
|
||||||
def get_upload_dir():
|
def get_upload_dir():
|
||||||
upload_dir = os.path.join(tempfile.gettempdir(), "libretranslate-files-translate")
|
upload_dir = os.path.join(tempfile.gettempdir(), "libretranslate-files-translate")
|
||||||
|
|
||||||
@ -33,6 +34,7 @@ def get_upload_dir():
|
|||||||
|
|
||||||
return upload_dir
|
return upload_dir
|
||||||
|
|
||||||
|
|
||||||
def get_req_api_key():
|
def get_req_api_key():
|
||||||
if request.is_json:
|
if request.is_json:
|
||||||
json = get_json_dict(request)
|
json = get_json_dict(request)
|
||||||
@ -42,6 +44,7 @@ def get_req_api_key():
|
|||||||
|
|
||||||
return ak
|
return ak
|
||||||
|
|
||||||
|
|
||||||
def get_json_dict(request):
|
def get_json_dict(request):
|
||||||
d = request.get_json()
|
d = request.get_json()
|
||||||
if not isinstance(d, dict):
|
if not isinstance(d, dict):
|
||||||
@ -162,8 +165,13 @@ def create_app(args):
|
|||||||
def access_check(f):
|
def access_check(f):
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
def func(*a, **kw):
|
def func(*a, **kw):
|
||||||
if flood.is_banned(get_remote_address()):
|
ip = get_remote_address()
|
||||||
|
|
||||||
|
if flood.is_banned(ip):
|
||||||
abort(403, description="Too many request limits violations")
|
abort(403, description="Too many request limits violations")
|
||||||
|
else:
|
||||||
|
if flood.has_violation(ip):
|
||||||
|
flood.decrease(ip)
|
||||||
|
|
||||||
if args.api_keys and args.require_api_key_origin:
|
if args.api_keys and args.require_api_key_origin:
|
||||||
ak = get_req_api_key()
|
ak = get_req_api_key()
|
||||||
|
@ -33,6 +33,15 @@ def report(request_ip):
|
|||||||
banned[request_ip] += 1
|
banned[request_ip] += 1
|
||||||
|
|
||||||
|
|
||||||
|
def decrease(request_ip):
|
||||||
|
if banned[request_ip] > 0:
|
||||||
|
banned[request_ip] -= 1
|
||||||
|
|
||||||
|
|
||||||
|
def has_violation(request_ip):
|
||||||
|
return request_ip in banned and banned[request_ip] > 0
|
||||||
|
|
||||||
|
|
||||||
def is_banned(request_ip):
|
def is_banned(request_ip):
|
||||||
# More than X offences?
|
# More than X offences?
|
||||||
return active and banned.get(request_ip, 0) >= threshold
|
return active and banned.get(request_ip, 0) >= threshold
|
||||||
|
Loading…
Reference in New Issue
Block a user