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