From 3c4ed0de1a166be47cc02723ab0fa4b2a1af5007 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Sun, 25 Feb 2024 06:59:25 +0100 Subject: [PATCH] Added ratelimiterbackends --- cps/__init__.py | 14 ++++++++++++-- cps/admin.py | 4 +++- cps/config_sql.py | 2 ++ cps/constants.py | 2 +- cps/kobo_auth.py | 3 +++ cps/templates/config_edit.html | 10 ++++++++++ cps/web.py | 10 +++++++++- 7 files changed, 40 insertions(+), 5 deletions(-) mode change 100644 => 100755 cps/__init__.py mode change 100644 => 100755 cps/admin.py mode change 100644 => 100755 cps/templates/config_edit.html diff --git a/cps/__init__.py b/cps/__init__.py old mode 100644 new mode 100755 index f4f8dbf2..536e1bb4 --- a/cps/__init__.py +++ b/cps/__init__.py @@ -103,7 +103,7 @@ web_server = WebServer() updater_thread = Updater() if limiter_present: - limiter = Limiter(key_func=True, headers_enabled=True, auto_check=False, swallow_errors=True) + limiter = Limiter(key_func=True, headers_enabled=True, auto_check=False, swallow_errors=False) else: limiter = None @@ -196,8 +196,18 @@ def create_app(): config.config_use_goodreads) config.store_calibre_uuid(calibre_db, db.Library_Id) # Configure rate limiter + # https://limits.readthedocs.io/en/stable/storage.html app.config.update(RATELIMIT_ENABLED=config.config_ratelimiter) - limiter.init_app(app) + if config.config_limiter_uri != "": + app.config.update(RATELIMIT_STORAGE_URI=config.config_limiter_uri) + if config.config_limiter_options != "": + app.config.update(RATELIMIT_STORAGE_OPTIONS=config.config_limiter_options) + try: + limiter.init_app(app) + except Exception as e: + log.error('Wrong Flask Limiter configuration, falling back to default: {}'.format(e)) + app.config.update(RATELIMIT_STORAGE_URI=None) + limiter.init_app(app) # Register scheduled tasks from .schedule import register_scheduled_tasks, register_startup_tasks diff --git a/cps/admin.py b/cps/admin.py old mode 100644 new mode 100755 index 9b504094..9f3d46a0 --- a/cps/admin.py +++ b/cps/admin.py @@ -1706,7 +1706,7 @@ def _db_configuration_update_helper(): return _db_configuration_result('{}'.format(ex), gdrive_error) if db_change or not db_valid or not config.db_configured \ - or config.config_calibre_dir != to_save["config_calibre_dir"]: + or config.config_calibre_dir != to_save["config_calibre_dir"]: if not os.path.exists(metadata_db) or not to_save['config_calibre_dir']: return _db_configuration_result(_('DB Location is not Valid, Please Enter Correct Path'), gdrive_error) else: @@ -1830,6 +1830,8 @@ def _configuration_update_helper(): return _configuration_result(_('Password length has to be between 1 and 40')) reboot_required |= _config_int(to_save, "config_session") reboot_required |= _config_checkbox(to_save, "config_ratelimiter") + reboot_required |= _config_string(to_save, "config_limiter_uri") + reboot_required |= _config_string(to_save, "config_limiter_options") # Rarfile Content configuration _config_string(to_save, "config_rarfile_location") diff --git a/cps/config_sql.py b/cps/config_sql.py index dabf54f0..d95d5956 100644 --- a/cps/config_sql.py +++ b/cps/config_sql.py @@ -168,6 +168,8 @@ class _Settings(_Base): config_password_special = Column(Boolean, default=True) config_session = Column(Integer, default=1) config_ratelimiter = Column(Boolean, default=True) + config_limiter_uri = Column(String, default="") + config_limiter_options = Column(String, default="") def __repr__(self): return self.__class__.__name__ diff --git a/cps/constants.py b/cps/constants.py index c31c8425..ef207e02 100644 --- a/cps/constants.py +++ b/cps/constants.py @@ -159,7 +159,7 @@ EXTENSIONS_UPLOAD = {'txt', 'pdf', 'epub', 'kepub', 'mobi', 'azw', 'azw3', 'cbr' _extension = "" if sys.platform == "win32": _extension = ".exe" -SUPPORTED_CALIBRE_BINARIES = {binary:binary + _extension for binary in ["ebook-convert", "calibredb", "ebook-meta"]} +SUPPORTED_CALIBRE_BINARIES = {binary:binary + _extension for binary in ["ebook-convert", "calibredb"]} def has_flag(value, bit_flag): diff --git a/cps/kobo_auth.py b/cps/kobo_auth.py index 3736e4e1..49b7e475 100644 --- a/cps/kobo_auth.py +++ b/cps/kobo_auth.py @@ -156,6 +156,9 @@ def requires_kobo_auth(f): limiter.check() except RateLimitExceeded: return abort(429) + except (ConnectionError, Exception) as e: + log.error("Connection error to limiter backend: %s", e) + return abort(429) user = ( ub.session.query(ub.User) .join(ub.RemoteAuthToken) diff --git a/cps/templates/config_edit.html b/cps/templates/config_edit.html old mode 100644 new mode 100755 index 89d4abcb..695cd823 --- a/cps/templates/config_edit.html +++ b/cps/templates/config_edit.html @@ -372,6 +372,16 @@ +
+
+ + +
+
+ + +
+