2021-12-19 10:01:50 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
# lint: pylint
|
|
|
|
# pyright: basic
|
2023-05-23 16:16:37 +00:00
|
|
|
"""see :ref:`limiter src`"""
|
2021-12-19 10:01:50 +00:00
|
|
|
|
2023-05-23 16:16:37 +00:00
|
|
|
import flask
|
2021-12-19 10:01:50 +00:00
|
|
|
|
2022-11-11 20:58:32 +00:00
|
|
|
from searx import redisdb
|
2023-02-12 11:14:15 +00:00
|
|
|
from searx.plugins import logger
|
2023-05-23 16:16:37 +00:00
|
|
|
from searx.botdetection import limiter
|
2021-12-19 10:01:50 +00:00
|
|
|
|
|
|
|
name = "Request limiter"
|
|
|
|
description = "Limit the number of request"
|
|
|
|
default_on = False
|
|
|
|
preference_section = 'service'
|
2023-04-03 17:36:28 +00:00
|
|
|
|
2023-05-23 16:16:37 +00:00
|
|
|
logger = logger.getChild('limiter')
|
2023-02-12 11:14:15 +00:00
|
|
|
|
|
|
|
|
2023-05-23 16:16:37 +00:00
|
|
|
def pre_request():
|
|
|
|
"""See :ref:`flask.Flask.before_request`"""
|
2023-06-01 13:41:48 +00:00
|
|
|
return limiter.filter_request(flask.request)
|
2021-12-19 10:01:50 +00:00
|
|
|
|
|
|
|
|
2023-05-23 16:16:37 +00:00
|
|
|
def init(app: flask.Flask, settings) -> bool:
|
2021-12-19 10:01:50 +00:00
|
|
|
if not settings['server']['limiter']:
|
|
|
|
return False
|
2022-07-15 16:38:32 +00:00
|
|
|
if not redisdb.client():
|
2023-05-23 16:16:37 +00:00
|
|
|
logger.error("The limiter requires Redis")
|
2021-12-19 10:01:50 +00:00
|
|
|
return False
|
2022-04-06 21:46:11 +00:00
|
|
|
app.before_request(pre_request)
|
2021-12-19 10:01:50 +00:00
|
|
|
return True
|