From bdb9b6a22410f8306604103448210154e6e58740 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Fri, 18 Apr 2025 14:41:12 -0400 Subject: [PATCH] Under attack mode --- README.md | 1 + libretranslate/app.py | 6 +++++- libretranslate/default_values.py | 5 +++++ libretranslate/main.py | 6 ++++++ libretranslate/templates/index.html | 29 +++++++++++++++++++---------- 5 files changed, 36 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index d70c1c5..f0b0e8c 100644 --- a/README.md +++ b/README.md @@ -200,6 +200,7 @@ Arguments passed to the process or set via environment variables are split into | --require-api-key-origin | Require use of an API key for programmatic access to the API, unless the request origin matches this domain | `No restrictions on domain origin` | LT_REQUIRE_API_KEY_ORIGIN | | --require-api-key-secret | Require use of an API key for programmatic access to the API, unless the client also sends a secret match | `No secrets required` | LT_REQUIRE_API_KEY_SECRET | | --require-api-key-fingerprint | Require use of an API key for programmatic access to the API, unless the client also matches a fingerprint | `No fingerprinting required` | LT_REQUIRE_API_KEY_FINGERPRINT | +| --under-attack | Enable under attack mode. When enabled, requests must be made with an API key | `Disabled` | LT_UNDER_ATTACK | | --suggestions | Allow user suggestions | `Disabled` | LT_SUGGESTIONS | | --disable-files-translation | Disable files translation | `File translation allowed` | LT_DISABLE_FILES_TRANSLATION | | --disable-web-ui | Disable web ui | `Web Ui enabled` | LT_DISABLE_WEB_UI | diff --git a/libretranslate/app.py b/libretranslate/app.py index 765af32..02bc865 100644 --- a/libretranslate/app.py +++ b/libretranslate/app.py @@ -364,6 +364,9 @@ def create_app(args): if flood.fingerprint_mismatch(ip, get_fingerprint()): need_key = True + if args.under_attack and key_missing: + need_key = True + if need_key: description = _("Please contact the server operator to get an API key") if args.get_api_key_link: @@ -441,7 +444,8 @@ def create_app(args): swagger_url=swagger_url, available_locales=sorted([{'code': l['code'], 'name': _lazy(l['name'])} for l in get_available_locales(not args.debug)], key=lambda s: s['name']), current_locale=get_locale(), - alternate_locales=get_alternate_locale_links() + alternate_locales=get_alternate_locale_links(), + under_attack=args.under_attack, )) if args.require_api_key_secret: diff --git a/libretranslate/default_values.py b/libretranslate/default_values.py index 4da8e29..3d2e3a8 100644 --- a/libretranslate/default_values.py +++ b/libretranslate/default_values.py @@ -151,6 +151,11 @@ _default_options_objects = [ 'default_value': False, 'value_type': 'bool' }, + { + 'name': 'UNDER_ATTACK', + 'default_value': False, + 'value_type': 'bool' + }, { 'name': 'REQUIRE_API_KEY_FINGERPRINT', 'default_value': False, diff --git a/libretranslate/main.py b/libretranslate/main.py index 61d7585..ea6eb3d 100644 --- a/libretranslate/main.py +++ b/libretranslate/main.py @@ -153,6 +153,12 @@ def get_args(): action="store_true", help="Require use of an API key for programmatic access to the API, unless the client also matches a fingerprint", ) + parser.add_argument( + "--under-attack", + default=DEFARGS['UNDER_ATTACK'], + action="store_true", + help="Enable under attack mode. When enabled, requests must be made with an API key", + ) parser.add_argument( "--shared-storage", type=str, diff --git a/libretranslate/templates/index.html b/libretranslate/templates/index.html index 66bf936..61f1e0d 100644 --- a/libretranslate/templates/index.html +++ b/libretranslate/templates/index.html @@ -150,18 +150,18 @@
-
-
-
- warning

[[ error ]]

-
- +
+
+
+ warning

[[ error ]]

+
+ +
-
-
+
@@ -170,6 +170,15 @@
+ {% if under_attack %} +
+
+
+ warning {{ _h("Due to bot abuse, translation requests are temporarily limited to users with a valid API key. Sorry for the inconvenience!") }}

+
+
+
+ {% endif %}

{{ _h("Translation API") }}