Merge pull request #87 from pierotofy/flood

Flood protection
This commit is contained in:
Piero Toffanin 2021-05-16 10:59:14 -04:00 committed by GitHub
commit d576cfb344
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 4 deletions

View file

@ -22,7 +22,7 @@ def get_remote_address():
return ip
def get_routes_limits(default_req_limit, api_keys_db):
def get_routes_limits(default_req_limit, daily_req_limit, api_keys_db):
if default_req_limit == -1:
# TODO: better way?
default_req_limit = 9999999999999
@ -44,7 +44,12 @@ def get_routes_limits(default_req_limit, api_keys_db):
return "%s per minute" % req_limit
return [limits]
res = [limits]
if daily_req_limit > 0:
res.append("%s per day" % daily_req_limit)
return res
def create_app(args):
from app.init import boot
@ -73,12 +78,12 @@ def create_app(args):
if frontend_argos_language_target is None:
raise AttributeError(f"{args.frontend_language_target} as frontend target language is not supported.")
if args.req_limit > 0 or args.api_keys:
if args.req_limit > 0 or args.api_keys or args.daily_req_limit > 0:
from flask_limiter import Limiter
limiter = Limiter(
app,
key_func=get_remote_address,
default_limits=get_routes_limits(args.req_limit, Database() if args.api_keys else None)
default_limits=get_routes_limits(args.req_limit, args.daily_req_limit, Database() if args.api_keys else None)
)
else:
from .no_limiter import Limiter

View file

@ -12,6 +12,8 @@ def main():
help='Set character limit (%(default)s)')
parser.add_argument('--req-limit', default=-1, type=int, metavar="<number>",
help='Set the default maximum number of requests per minute per client (%(default)s)')
parser.add_argument('--daily-req-limit', default=-1, type=int, metavar="<number>",
help='Set the default maximum number of requests per day per client, in addition to req-limit. (%(default)s)')
parser.add_argument('--batch-limit', default=-1, type=int, metavar="<number of texts>",
help='Set maximum number of texts to translate in a batch request (%(default)s)')
parser.add_argument('--ga-id', type=str, default=None, metavar="<GA ID>",